In this article, i will use the PIC 16F1937, a powerful microcontroller with a 16-channel capacitance module. I will only use the 4 of them, but the idea to expand it to all 16 channels is the same.
Using the capacitance module of the PIC, the circuit is very simple: (click to enlarge)
I will not add a BOM this time. The PIC is the 16F1937. All the job is done by the PIC software. The electrodes are connected to the E1 through E4 inputs. The 4 outputs drive the load. In my case, the load is an LED with a 220 Ohm resistor, but you can interface a transistor to further increase the load.
There are 3 ways to use the capacitance touch module of the PIC. The method used is the frequency change, as described in the theory of capacitance sensors. The 3 different ways mentioned before, have to do with the time base source selection. One way is to use the Timer0 module, the second way is using the Timer2 module, and the third way is using the watchdog timer. All these ways are explained in the Microchip Application Note AN1171, written from Enrique Aleman. The problem with this application note is that it assumes that the reader has plenty of experience using the timer modules and the gate module for timer1. I read it twice and 3 times, and i read the 16F1937 datasheet a couple of times before i can tell that i fully understand the settings. So, i will try to re-explain the method that i use.
For my circuit, i will use the Timer0 module as a time base. Here is how it works: The Timer0 module receives counts from the internal oscillator, through the prescaller. The Tmr0 overflow interrupt is enabled to watch every time that the Tmr0 overflows. This way, we have a fixed time base, because the internal oscillator and the prescaller remain unchanged. The Timer1 module receives counts only as long as the gate module has not detect a tmr0 overflow, and these counts come from the capacitance sensor module. So, when the tmr0 overflows, 2 things happen: First, the tmr1 stops counting, and second the ISR routine is called for further check. This is the rough explanation of this method.
For me, it is more important that you understand how this works, rather than reproducing the circuit. Human creativity has infinite starting points and no limits. So, let's take a closer look. First, the tmr0 module. Here is the portion of the code that i set it up:
movlw b'11000101' ; I use the capacitance module with TMR0 as timer.
OPTION ; So i have to set-it up through the Option Register
; The prescaler is also connected to TMR0.
; Bit 5: TMR0 Source is internal instruction clock (Fosc/4)
; Bit 3: Prescaller assigned to TMR0 module
; Bits <2:0>: Prescaller rate is 1:64
The option register is used to set up Timer0 module. Bits 7 and 6 are irrelevant to the capacitance module. In bit 5, i set Tmr0 to receive counts from the internal oscillator, which is Fosc/4. In my case, the internal oscillator oscillates at 16Mhz, so the pulses driven to the prescaller have a frequency of 4 MHz. Then, the prescaller is assigned to Tmr0 module to further divide these counts by 64. The pulses that finally are delivered to the Tmr0 module, have a frequency of 62.5 KHz. The following drawing is from the PIC16F1937 datasheet, and shows the diagram of Tmr0. With red, i have marked the route of the pulses to the Tmr0 register:
Timer0 is used as a timebase to measure the frequency. The Tmr1 (explained bellow) is used to measure the frequency (go to page 2).
[reply @ Giorgos Lazaridis],,,,,
i know the code is in assembly language but i need the code in C & i am using pic16f1939 i dont know ho to fix the timer in C so please can you send me...
please give me the embedded C code for this program.I am facing lot of problem reading assembly level code and i am using PIC16F1939 so please tell me how to fix the timer and also how to use two timers in the program
@Muhammad Awais Tahir The reason for this is probably the threshold setup. Different touch pads have different capacitance, and even if you move them to another position they change their capacitance (due to the cable). First of all, when you change anything to the touchpad (position, wire, etc) you need to restart the program so that it reads the starting capacitance. And second, you may need to change the threshold values of the touch pads (PressThreshold_H,PressThreshold_L and ReleaseThreshold_L)
I'm experiencing a strange behavior. I just kept the CapCheck4 (associated with CPS0) , and removed others just to understand how it works. I've connected an LED to the relevant output port to test the touch. Now, when I connected my touch pad with CPS0 or CPS1 the LED just remains ON whether I touch the pad or not. But, it behaves exactly how it should when I connect the touch pad with CPS2. I'm quite confused on why its working correctly with CPS2 rather than CPS0, and why the port turns ON when I plug the touch pad into CPS0 or CPS1.
@Muhammad Awais Tahir first of all, it will be extremely sensitive, so you may need to alter the software variables for sensitivity. Additionally, it is good to cover with some sort of coating the button, since PICs are sensitive to ESD, so a direct touch to an input from a heavily charged body could destroy the port, or reduce its lifespan radically.
I know that. Was mine too
Happy easter and keep looking for easter eggs .... maybe you find a rectangular shape one .... but not BOILED :)
If you did behave, for sure easter bunny will visit you :) ....
@ion I knew it :D
Changing the size to bigger will increase the capacitance radically, due the increment if copper area. That is why i state (somewhere) that it is rather difficult to make proper touch-pads. Anyway, it will not be that simple to change the capacitance channels. It will need a lot of changes in the ISR routine. I really do not remember by heart the code since it is rather old.
Thank you Giorgio.
In fact i changed the the size of the touch pads.
I put back the old ones and works OK
As a questions, if i just change the start point from cpscon1 from 00000000 to 00001100 will be suficient to let me use chanel 12 to 15 ?
@ion i think that the problem is that you radically changed the shape or size of the touch pad, which changed the quiescence capacitance and this makes the output oscillate. Before you do anything else, try to change the parameters:
PressThreshold_H equ b'00000000'
PressThreshold_L equ b'00010110'
into something bigger, for example the PressThreshold_L make it b'00110110'
I had this project working fine for a while
Then, maybe because due to my messy table, something happen.
I do not know what
Except CPS3-RB3 which works fine, all other channels flicker fast between 0 and 1.
I do not have a scope because i am in China now, but i can see on my LCD.
What do i have to do to move all the channels to CPS12 to 15
I am not good at all in assembler , so a good working example will be the best.
@Muhammad Awais I'm almost sure that this program wont work on the pic you mention. I do not have a clue though regarding the changes that have to be done, since i've never worked with the pic you mention.
As for the SR latches, do you mean the Tmr0 trigger latch? I use it for this program, and if you do not use it then this will not work at all, since the tmr stops from the latch.
I did your instructions ( click on the menu project>built options> and then appears "No Active Project". After star a new project and paste the program then follow again your instructions ( project>built options>
project> and "disable case sensitivity" ) but now not appears "Quickbuild" option.
@veroo Click on the menu Project -> Build options... -> Project.
The "Buld options" dialog appears. Go to the tab labeled "MPASM Assembler" and make sure the checkbox "Disable case sensitivity" is checked. Then, click "Ok".
I already download the MPLAB IDE sofware and found the device pic16f1937, and open the .asm file to edit only to practice, but at the moment of compile the file appears me a list of erros in each one it says Symbol not previously defined.
Manish, the change must be done in hardware to make it toggle. Regarding the LED dimmer, this is not that simple. You need to use the PWM output of the PIC and find a way to make the interface. I will make one more circuit, a capacitance touch sensor without microcontroller, and i will not work any more with touch sensors, unless i really need something. As a matter of fact, i did this research to prepare some interface for my coffee machine.
Very nice design,but with this circuit can we make the toggle switch with some external componant & switch on & off any equipment with the help of relay, If possible please upload the schemetic or videos.& also with this can we make the 1 watt LED light dimmer circuit, if possible plz upload the details. Thanking u........
geia sou file mou.
poli orees oi kataskeues sou kai mpravo.
ama itheles tha sou elega na ta anaivazeis kai sta ellinika kai ta video kai tis perigrafes...gt opos xereis edo sti ellada den eimaste sainia sta agglika..
me endiaferei poli auto me ta Capacitance Sensor eidika to allo poy eida xtes pou epiane piso apo to plastiko.
ama itheles na ta exigisis ligo sta ellinika tha me voithouse poli.
se euxaristo poli.