Home      Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science

22 January 2011
Author: Giorgos Lazaridis
PIC Frequency-Change Capacitance Touch Button

The circuit assembled on a breadboard for test

This is the first capacitance touch sensor that i present, in continue to the Resistance touch sensor and the AC Hum touch sensor.. I had to decide if i would start with a PIC-based or with an analog circuit. I decided to start with a PIC-based touch button for two reasons: First, i thought that a simpler and compact touch circuit is more practical, even if basic knowledge of PIC programming is needed. The second reason is that i had the proper PIC in stock, but i was still waiting for some parts that i purchased a few days ago, for making an analog touch button...

If you want to get some theoretical knowledge about touch sensors in general, i suggest you read the theory of touch sensors that i have.

The circuit

A microcontroller always makes things simpler. Look at the following schematic:

The operation is as follows. I use the internal comparator of the pic connected as a relaxation oscillator. The frequency is determined by the resistor R1, and the capacitance of the touch sensor. The following picture is a screenshot of the oscilloscope screen, with the probe connected at pin 6 (GP1): (click to zoom in)

I use a double sided board for electrode

The touch electrode is connected to pads E1-E2. The electrode must be designed in a way that it performs a capacitor with the ground. Either use a 2-sided PCB (as i do), with one side connected to the E1 electrode and the other to ground (E2), or use a single sided with 2 regions, the electrode and the grounding layer. I really encourage you to read the theory of touch sensors to learn more. The frequency that my setup runs at is about 94.5 KHz. You can change the frequency by changing the value of R1. The greater the value, the less the frequency. This frequency is then driven to the the Timer0 module of the PIC through the prescaller. From that point, the software takes control of the operation.

The Software

Testing the software. The oscilloscope really helps! Sometimes i wonder myself, why hadn't i got this years before... I use two digital outputs of the PIC to transmit (temporarily) the frequency count, so i know what i deal with and i can select a resistor easily.

To measure the frequency, first i reset the timer0 module, which also resets the prescaller counter. Then, i run a fixed delay, in my case this is 60mSec. When this delay is finished, i acquire the tmr0 and the prescaller value, to make a 16-bit (sort of) value, the TMR0:Prescaller. Actually, this is not 16 bits. The problem is with the prescaller. There is no immediate way to read the prescaller. To get its value, i increase the prescaller with the PIC's internal oscillator (Fosc/4), and i measure how many loops are required to increase the tmr0 value.

Let's see an example. Suppose that the delay finished when the prescaller was at count 64. I cannot read the 64, instead i can keep on increasing it until i notice an increment on tmr0, which means that the prescaller has overflow (from 255 to 0). This will happen in 192 pulses (256-64). But here comes the real problem. The routine that i use to measure the pulses and check tmr0 for overflow, is 6 instructions long, which means that i can only count once every 6 increments. Suppose now that the prescaller has stopped at 0x00. It will take 256 (255+1) instructions to overflow, but i will only be able to measure 256/6 = 42.6 (ceiling=43). So, the 16-bit pair is not exactly 16 bits. The 8 bits of the HIGH byte comes from the tmr0 which are indeed 8-bits, but the 8 bit for the LOW byte comes from the prescaller, and they can only climb up to 43. This is not a real problem if we do know this detail.

Now i have to determine if the touch-pad is touched or not. To do so, i need to check if the frequency is changed. But how do i know what the frequency was originally? Well, i do a trick for this. First, i added a pair of constants, which i named GLPressHysteresis_H and GLPressHysteresis_L. During start-up, the PIC measures the frequency of the relaxation oscillator, and from this it subtracts the 16-bit number GLPressHysteresis_H:GLPressHysteresis_L. The result is the press threshold value. When this step is completed (during start-up), then it continuously runs a loop during which it measures the frequency and compares it to the press threshold value calculated before. If the frequency is lower than this threshold value, then the PIC considers this as a key-press.

With this trick, i save a lot of testing and setup time of the electrode. That is because, even a small small change of the position or the cover or the distance of the electrode, results in different oscillating frequency. The PIC automatically detects the quiescence frequency during every start-up. But this has one drawback. If the sensor is touched during start-up, then the frequency that will be calculated will be wrong. You can change this easily in my code, by simply replacing the calculations with a fixed value, but i do not encourage you to do so, unless you know what you are doing and you are familiar with assembly sheets.

Regarding the key-release, i could have used the same set of threshold values, but that would cause a malfunction, because there are times that the electrode is barely touched, yet it is still acknowledged as a touch. Whenever this happens, the output is not stable. It turns on and off randomly. So i added another set of threshold constants, which i named GLReleaseHysteresis_H and GLReleaseHysteresis_L. They also perform a 16-bit long number, which is slightly lower than the press threshold. The difference between these two thresholds is the hysteresis. This hysteresis really saves the day regarding the touch-release recognition, and really works with no problem.

Here are the files with the firmware. First, the full assembly listing sheet to compile and upload:

 PIC Touch Button Frequency Change - Assembly listing - V1.0

And this is the HEX file if someone wants to simply upload the code and make a button:

 PIC Touch Button Frequency Change - Hex file - V1.0

What are the switches S1 and S2?

Actually, these are no switches. These are setup-pins which determines some parameters of the touch sensor. First of all, S2 which is connected to GP4. This controls the sensitivity of the sensor. If you look at the code, you will notice that i have two pairs of press and release threshold values. The first pair is for low sensitivity and the second for high sensitivity. If GP4 is connected to LOW, then the sensor will have low sensitivity. If it is connected to HIGH, i will have higher sensitivity. This is an easy setup for those who want just to upload the hex on a PIC and make a switch. Keep in mind that this change needs a reboot to become active, just as happens in every big (or small) change on windows...

The switch S1 changes the touch sensor operation between a temporary make button (touch button) and a toggle switch. If GP5 is driven LOW, then the PIC operates as a pushbutton. The output is active only as long as the electrode is touched. If it is driven HIGH, then the output toggles between ON and OFF each time that the electrode is touched (and then released of course). This is again for those who have no idea of PIC programming but still are able to upload a hex file on a PIC. Unlike the previous switch (S2), this one can be changed during run-time. No reboot is needed, just as happens in most small (or big) changes on Linux...

 Operating as pushbutton: When the electrode is released, the output is deactivated Operating as pushbutton: When the electrode is touched, the output is activated Operating as toggle switch: The output is toggled ON and OFF on each touch Operating as toggle switch: Now the output is ON

Will this work no matter what?

No, it will not. There are some things that may go wrong. The most simple issue that i can think of, is the frequency of the relaxation oscillator. My setup oscillates at around 95 KHz. But this is almost impossible for you to achieve with the first test, because this has to do with the touch pad itself. Different touch-pads will result in different oscillation frequencies. If your frequency is close to mine, then it will operate normally. But if there is a large difference, two things may happen: If your frequency is way too high, then the TMR0 will overflow. If this happens, then the results will not be as expected. It may sense a touch, or maybe not. The point is that it will not operate correct. If your frequency is too low, then the touch will not be able to reduce the (already low) count enough, and the PIC will never be able to sense it.

If you notice a strange behavior after powering up the circuit, then it is time to turn on your oscilloscope. Measure the frequency at PIN #6 (GP1). It should be around 95 KHz. If lower, reduce the value of resistor R1. If higher, increase it. Keep also in mind, that if the electrode introduces a very large capacitance to the circuit, the the body capacitance will not be able to change the overall capacitance of the circuit in a noticeable way, and so the switch will not operate.

Capacitance sensors have many tricks hidden when designing the touch pad, and for this, some people call the designing "an art". I don't see any art whatsoever. I only see different geometrical characteristics that needs to be taken into account, and lot of experience that the designer must gain through trial and error. Simple small rectangular 10x10mm or 20x20mm, or even circular touch pads are guarantee to work. Good luck.

I want larger touch pad, and i mean LARGER. Can i?

A 10pF works fine for me

Sure you can. But this requires a slight modification. Instead of a 2-regions touch pad, you will use only one region touch pad. You do not need to have a grounding layer. That is because, if the grounding layer is big like the touch pad, then the capacitance that will be created will be very big, and the body capacitance will not have any great influence. Therefore, a small 10pF capacitor must be added. The capacitor is connected at the electrode pins E1 and E2. The smaller the capacitor, the higher the sensitivity of the circuit. But smaller capacitor also means higher frequency of the relaxation oscillator. So you need to choose a capacitor that will not break the rules discussed before. A 10pf works fine for me. It should fit for you as well.

This piece of aluminum foil is now the touch pad. As a matter of fact, this is no longer a touch-sensor, but a proximity sensor. The surface of the pad is so large and the circuit sensitivity so high, that it can sense my hand from 10cm away:

 My hand is not sensed (LED=off) My hand is sensed from 10cm away

Bill Of Materials
 Resistors R1 Resistor 47 KOhm 1/4 Watt 5% Carbon Film R2 Resistor 100 Ohm 1/4 Watt 5% Carbon Film Semiconductors D1 BAT 85 Schottky barrier diode LED1 LED 3mm green Integrated Circuits IC1 PIC12F615 Microcontroller