Variable capacitance buttons!

alband

Senior Member
Variable capacitance buttons = anything nowadays that is touch sensitive.
So how do we do it with a PICAXE???
Here are a few sites:
http://www.analog.com/static/imported-files/Data_Sheets/AD7148.pdf
http://en.wikipedia.org/wiki/Capacitance
http://en.wikipedia.org/wiki/Voltage
http://physics.bu.edu/~duffy/PY106/Charge.html
http://www.eskimo.com/~billb/miscon/voltage.html

Capacitance is what we want to measure.
Capacitance is Charge/Voltage.
Charge is number of electrons relative to number of protons.
Voltage is totally relative (well explained in last link, paragraph = "Potential Energy vs. 'Potential' ".

We could use that chip in the first link but it isn't very compatible, blo... I mean very hard to understand and frankly, feals too much like giving up.

I do know this: When you put your finger on an iPod clickwheel, you are putting your fingertip near to a charged mesh. At the point near your finger, the meshes' charge increases. On the other side of the circuit there are a series of pads that measure that change in charge. However, we all know it is very hard for a device to just measure something without anything to compare it with (+Vcc). So instead the iPod measures the capacitance which is also relative to voltage (C=Q/V). This must allow the device to calculate the charge.

So if we can measure the capacitance of a pin on a PICAXE then, well...
 

boriz

Senior Member
An oscillator built around the capacitor and the COUNT command to measure frequency changes?
 

eclectic

Moderator
Alband

Following on from Boriz's suggestion:

have a read around

http://www.kpsec.freeuk.com/capacit.htm

and

http://en.wikipedia.org/wiki/LC_circuit

Then search the forum for measuring current.

1. You'll need to build an acceptor/rejector circuit. (Known inductor + Unknown Cap.)
2. Use Pwmout to provide variable frequency.
3. Measure current(s) until you find resonance.

And, if you don't succeed, you'll still find out an awful lot
about electronics.

Best of luck

e
 

Jeremy Leach

Senior Member
Something else to think about.... even with a method for measuring capacitance, the capacitance value will be a very wobbly type of variable.

You won't get repeatable, definite values when you touch or don't touch the plate. The actual value will depend on lots of factors (skin moisture, humidity, temp, width of finger on plate etc etc etc). So the solution needs to use thresholds to determine if the reading means 'on' or 'off'.
 

ylp88

Senior Member
Microchip has some resources about capacitive sensing switches. Have a search on their website for "CMTU" and take a look at some of the backgroun application notes.

ylp88
 

alband

Senior Member
That microchip one looks great, just need to get hold of the code behind it.
This also looks hopeful:
http://www.freepatentsonline.com/5329239.html
Sending a pulse out then measuring how long it takes for the input pin to rise thus the capacitance. How to wire it up though?
and this for the PIC?

main: pulsout 0,1
let b1 = 0

label_1: readadc 3,b0
serout 4,T2400,(254,128,#b0)
let b1 = b1 + 1
if b1 = 20 then main
goto label_1
 
Last edited:

hippy

Ex-Staff (retired)
Here's one primitive solution similar to alband's idea ...

Code:
.-----.
|  Ix |<---------------.
|     |    ___         |
|  Oy |---|___|----.---^----O
`-----'          __|__   .--O
                 --.--   |
                  _|_   _|_
Do
  Low Oy
  Pause 1000
  High Oy
  If Ix = 0 Then
    Gosub BigCapacitancePresent
  End If
Loop
Of course you can end up acting as an aerial and put all manner of 50Hz/60Hz signals in which can confuse things, and inject ESD which would be nasty which is why I called it primitive.

I looked at a chip which had been designed to do things like the capacitive-coupled ring in an iPod and the techniques there were quite complicated. Touch-sensitive light switch circuits are another source of information using more primitive technology which would be worth looking at.

All the techniques come down to determining that circuit operation has been influenced when touched, things like an iPod ring are more complicated because they need to know how and where they are being touched.
 

alband

Senior Member
I've taken apart many-a-ipod and know how they work.
Two layer PCB ribbon.
Back layer is a charged grid.
Front layer is eight or so sensing pads in a ring.
All of these pads go to a chip not dissimilar to the one at the top of the thread. That IC measures changes in charge by sending out pulses This gives the capacitance.
When a large conductor (finger) comes close to the charged grid, charge builds up in that area.
The IC can then measure this.
 

hippy

Ex-Staff (retired)
I think there's quite a jump from the theory to the practice, knowing what effect should happen and being able to detect that then translate into something usable. While it is all about measuring capacitance it's actually a lot more complicated than that in total.

The info from QRG site makes for interesting reading, a good starting point ...

http://www.qprox.com/technologies/qmatrix.html
 

hax

New Member
I have been a fan of the Qprox chips for quite a while now.

It is a lot harder than you might think to measure a finger press. The qprox chips have it all built in:

- automatic recalibration
- stuck button time out
- spread spectrum technology (changes frequency and PWM)
- automatic variable gain
- adjacent button decoupling

A DIY approach will possibly create an antenna that will trigger when a bird farts as it flies over your house or when John Laws hits one of his resonant low notes on the AM band.


The absolute beauty of these switches is that you can make a professional looking panel that is rid of those ugly $2 toggle switches.

I'll post some pics of my project if anyone is interested. I have a glass front panel that has been etched with buttons and labels. Looks very nice if I may say so myself, and doesnt cost the earth.
 

alband

Senior Member
Try this:
If you have a PICAXE LCD use it, if not use the debug command instead.
Have one loose wire coming from a PICAXE analogue pin. Use
Code:
readadc10 w0
for acuracy then either display the w0 on an LCD or debug it.
You'll be surprised at the readings.
 

alband

Senior Member
Basically, I managed to get mine set up so that when I touched a piece of tin foil attached to some wire an LED went on. Although for some unfair reason my mum only had to put here finger an cm or so away and it lit.:rolleyes:

Now I'm working on getting a slider working by using two inputs and measuring each.
Attached is the code for a button that should calibrate itself (28X chip).

This is the code for the first simple one I did. It is for an 08M. Have a wire connected to the analogue pin and attach it to some tin foil.
You will need to debug w0 first time to find out the normal value with no finger. Then, insert the maximum and minimum value and it should work.
If you have an LCD that is way more usefull than debug.

Code:
main:		readadc10 1,w0 'debug after this when required
		if w0 > 350 then label_1 'change these values
		if w0 < 200 then label_1 'change these values
		low 1
		goto main
		
label_1:	high 1
		goto main
 

Jeremy Leach

Senior Member
Hey, you're making good progress there. Could try defining some meaningful symbol names and label names in your code though - would make it easier to read. Maybe your mum is highly charged?? ;)
 

alband

Senior Member
Please be aware, most of it is spelt rong. I'm dyslexic an the programmer doesn't have the luxury of a syntax AND spell check:)
It may also be very patronizing - just to be sure;)
At least I remember to erm... yep, attach it!
 

Attachments

Jeremy Leach

Senior Member
You've got your head round the min and max idea well. Hope you don't mind, but I rewrote your code into a more usual format. It's using a few structures like 'IF...Then' and 'Do...Loop' and 'For...Next' that are very useful for making things clear. I haven't run my code but I've tried to copy yours exactly in the way it functions. I've also shown how you can define names using 'Symbol'. This is very helpful for making things clear, also if you want to change the pin number (you don't have to go through the whole code and edit!).

Anyway, got some good experiments going on here - how do you intend the slider to work ?

Code:
'DECLARATIONS
'============

'Constants
Symbol ADCPin		= 3
Symbol SeroutPin		= 4
Symbol LEDPin		= 7

'Variables
Symbol LoopCounter 	= b13
Symbol MaximumNormal	= w1
Symbol MinimumNormal	= w2
Symbol ADCValue		= w3		

'PROGRAM
'=======

Initialise:
	Pause 5000 'wait for the numbers to stablize
	MinimumNormal = 5000 'Set to a high value

Do	
	For LoopCounter = 0 to 254
		Readadc10 ADCPin,ADCValue 'reads the pin to be measured
		
		'Adjust maximum if necessary
		If ADCValue > MaximumNormal Then
			MaximumNormal = ADCValue
		EndIf

		'Adjust minimum if necessary
		If ADCValue < MinimumNormal Then
			MinimumNormal = ADCValue
		EndIf

		Pause 10
	Next

	Readadc10 ADCPin,ADCValue 'check the reading
	Serout SeroutPin,T2400,(254,128,#MaximumNormal," ",#MinimumNormal," ",254,192,#ADCValue,"     ") 'Im using an LCD this could be a debug
	
	If ADCValue > MaximumNormal Then 
		High LEDPin 'Turn LED On
	EndIf
	
	If ADCValue < MinimumNormal Then
		Low LEDPin 'Turn LED Off
	EndIf
Loop
 

alband

Senior Member
much nicer.
If I was starting fresh I'd use yours but I'm still working on it so I'll stick to mine to avoid confuction.
I tryed using the loop function myself but couldn't get it to work.
That variable and pin system is brilliant.
It'ld stop the variable confusion trouble.:D
 

alband

Senior Member
Made another discovery. If you attach the + of a radial capacitor to the input pin then touch the top metal bit the number goes haywaire.
 

alband

Senior Member
No.
I've been trying to refine it though.
One problem I was just about to post is how to get a pulse of 0.01s every 0.01s in time with the programing.
Currently attached is as far as I got.
Pin 6 goes to the analogue input via a diode.
I'm trying to set up a charge pump basicaly.
This was posted by RogerTango.
The problem is, is it's messing up the calibration stage. The delay between the readadc10 is different in the calibration stage than to the normal running stage.
I cant use a seperate PIC because I need the readadc10 to be right after he end of the pulse.
Any ideas?
 

Attachments

alband

Senior Member
ARG I've just deleted a long reply for the second time!:mad::eek::mad:

No they don't stablize.

I'm trying to refin it and attached is the progress.
I'm trying to set up a charge pump using pulse out.
This was posted by RogerTango.
The problem is, is I need the readadc10 to happen just after the pulse has finnished. But I need the pulse to happen at a regular interval. The problem is that the time delay between the readadc10 in the configuration stage and the main stage take different times - thus gives different readings - thus messing up the minimum and maximum's.
Any ideas?
 

Attachments

Jeremy Leach

Senior Member
I'm losing the plot here ;) What are you trying to do? You can use Pulsout command to send out a pulse. You can use PWM to send out a continuous stream of pulses. You can set a pin high and low quickly to make a pulse.
 

alband

Senior Member
I can make a pulse easy. I just need that pulse to be sent out regularly. Don't care how long the gap is as long as it's the same.

When the program is configuring the maximum and minimums, there is a different delay between each pulse, to when the program is running normally.

This is because I need the PICAXE to do two things at once (the Achillies heal of a PICAXE).

I could have a small 08M seperately sending the pulse but then the pulse wouldn't be in sync with the readadc commands.

Basically I need a way for the PICAXE to time how long it has taken to do each cycle then pause for a defined amount to make it take the same time. e.g.

Time taken to do calibration cycle = 3ms so pause 7ms
Time taken to do normal cycle = 5ms so pause 5ms

The problem is I have no idea how long any of the cycles take and also the calibrations cycles would vary in length.

I know that the latest version of PIC logicator has a timer on and off command; saving the time in a separate variable. But I've never got it to work.
I've made the following program in PIC Logicator and converted it into basic.

Code:
'BASIC converted from Logicator for PIC® micros flowsheet:
'Flowsheet1
'Converted on 23/10/2008 at 10:31:44

symbol varA = b0
symbol varB = b1
symbol varC = b2
symbol varD = b3
symbol varE = b4
symbol varF = b5
symbol varG = b6
symbol varH = b7

main:
label_1:
		settimer t1s_4		'time on 

		pause 100	'Wait command
		let varA = 139 - timer	'Expression command
		settimer off		'time off 

		goto label_1
The problem is "varA always ends up as 139.?
 

Jeremy Leach

Senior Member
Ok, well you can use the SetTimer command with the appropriate preload on a 28X1 (which I think you are using), and then read the Timer variable. See SetTimer in manual 2.

The preload will depend on your clock speed:
t1s_4 (preload value 49910 - 1 second at 4MHz)
t1s_8 (preload value 34286 - 1 second at 8MHz)
t1s_16 (preload value 3036 - 1 second at 16MHz)

So, what I'd do is use a temporary FOR...NEXT Loop and just before entering the loop use SetTimer to start the timer. And set Timer = 0. Put the code you want to time in the loop. After the loop has finished read the time value.

So, for example, you might loop 10000 times and the timer reading that you get back gives a result of 8 seconds. So you know that one pass through your code will take 8/1000 seconds = 8ms.

Once you know the timing of your code, you can then add pauses as necessary to get things to balance.
 

hippy

Ex-Staff (retired)
I have to admit that like Jeremy I've lost the plot here ...

What hardware are you actually using, how is it wired, what's the circuit ?
What is the principle of operation you are trying to achieve ?

In post #14 it seemed you were simply connecting a piece of tin foil to a wire and doing a READADC of that. That's effectively turning yourself into an aerial and picking up 'mains hum', same as one sees if you hold a scope probe by the tip, something like a 50Hz/60Hz sine wave. That we are now onto pulses and charge pumps suggests there's something added to that scheme.

I'm guessing this charge pump is used so its different characteristics can be determined when touched and when not but it's not clear to me how this works, whether you are measuring capacitance change or simply injected EMF pickup through contact. If I were working on such a thing I'd want to be clear about its theory of operation and would want to see some scope traces showing what is going on.

PS : The link for the QProx(TM) datasheet in post #24 is ...

http://academic.greensboroday.org/~regesterj/potl/Electronics/Stock/qt113.pdf
 

alband

Senior Member
Ok...

28X chip.
Normal setup 4Mhz.
leg no5 (analogue3) is conected to tin foil glued to card.
leg no27 (out6) is conected to diode then diode to same tin foil.
second piece of tin foil is grounded.
tin foil is as in attachment.
small square of plastic (dialectric) over the zigzag gap in tin foil.
The chip sends out a pulse to charge the first tin foil pad.
This creats an electric field between the two pads.
When a finger is put into the field it "soaks up" some of the field lines.
This has an affect of changing the capacitance (I think) changing the voltage at the pin.
The readadc10 reads the voltage afterwards.

My fingins are that if any pin is left flowting, it eventualy settles on a bunch of numbers. When my mum puts her finer near it (I have to touch it) the number goes crazy.

I'm trying to refine the idea by first calibrating the normal value (affected by moisture, heat etc.) and setting limits them measureing if the number goes out of these limmits.
I need the pulse to have the same time delay so that the calibration stage is valid.

I have nearly sorted the time delay by measuring the time it takes to run the two sections of code (thanks Jeremy :)).

I'm off to have lunch. :D
 

Jeremy Leach

Senior Member
The chip sends out a pulse to charge the first tin foil pad.
This creats an electric field between the two pads.
When a finger is put into the field it "soaks up" some of the field lines.
This has an affect of changing the capacitance (I think) changing the voltage at the pin.
The readadc10 reads the voltage afterwards.
Hmmm, I'm still lost on this. Why the zigzag? Also I think there are different things going on here. I'm no physicist, but I think it's a complex situation. If your finger actually touches across this gap then I'd say that is like putting a resitor across a capacitor, as well as modifying the capcitance.

You might hit on something useful here, and it sounds fun, but I think it's already showing how unpredictable the readings can be. I did a little project a while ago (I called it the picaxe CAT) which you'll find in misc projects, and this was playing with tin foil - but I realised it's a very difficult thing to get any repeatable results from.
 

alband

Senior Member
The zigzag was initialy for a slider whereby i'd have two inputs and measure the difference between them but for now it works just as an ordinary button.
The finger never touches the plates because of the piece of plastic on top.
It is acting as a dialectric.
Right now I'm still trying to perfect it. Everything is pretty consistant or at lease consistantly variable. The reading allways wanders around but alway between about 300 and 400.

I've attached my current code.

This one shouldn't be so patronizing - I just like the name :)
All the other "non-'ed" code should be the same (roughly).
 

Attachments

Jeremy Leach

Senior Member
Hmmmm...... just think you've got a lot going on here and because a 'finger' has such variable properties (charge on finger, properties as a dielectric, shape etc) and the foil capacitor is such a tiny value - then it's a very difficult thing to capture accurately. If it was easy then you wouldn't have these clever chips to try to deal with all the variables.
 

alband

Senior Member
Yeah; see what you mean.

I've just had agood read through this link and have come to the conclusion that actualy it is very possible with a PICAXE (just one downside which I'll come to).
"Capacitive sensing 101" is the section this is all referring to.
The way the system works in this example is as follows.
It uses a "Relaxation oscillator" to sense the capacitance. This can be doen with an op amp too.
http://en.wikipedia.org/wiki/Relaxation_oscillator (Read the "op amp base RO" section).

It basicaly is a way of measuring a capacitor with digital technology. The output gradual climbs depending on the capacitor and then falls quickly. This can then be turned into a pulse. If the capacitance changes then the rate of pulses changes (finger present = rate increase). This can very easily be determined as a finger touching.

I think this can all be doen by a PICAXE. :eek:
I think it is possible to make an op amp based relaxation oscillator with a PICAXE. I've just tried it but couldn't - perhaps somone else can. Anyway I'm sure thats do-able. However, because it's a PICAXE not an op amp, if it is managed, the output could be coded to be a digital pulse not a varing pulse. This rate of pulse can then easily be turned into a single on/off output by annother PICAXE or even the same PICAXE.
 

Jeremy Leach

Senior Member
Well, I think the info about the sensor pad layout is useful in that link. You'd still need an op-amp. We're talking very small capacitance and very fast charge time - no chance of detecting this change with an ADC input. The constant current source is to get a steady ramp up in the voltage. The op-amp might actually be a bit more than a simple comparator (I'm struggling to see how the output of the comparator can discharge the cap totally, because as it discharges a bit the comparator output will flip). But I can see that the oscillator frequency would vary with finger touch, and could perhaps count pulses on picaxe input.

EDIT: See here http://en.wikipedia.org/wiki/Relaxation_oscillator ...the comparator needs hysterysis. I think you could forget the constant current source and just implement the osc as per this circuit ....see what you get. But if you are wanting a multiple button solution that will take more thought !
 
Last edited:

Jeremy Leach

Senior Member
I've been thinking about this, and here's a rough idea for multiple pad solution. Not sure if it would work...

Use a very simple oscillator based on scmitt inverter...
Code:
              R1
             ___
       .----|___|-.
       |          |
       |     |\   |
       o-----| >O-o----o Osc output
       |     |/
      ---    Scmitt
  C1  ---    inverter
       |
       |
       o
      Gnd

Fig1
C1 is the foil pad. Using two Hex Scmitt inverter ICs, have 12 oscillators. One oscillator for each touch pad. 12 pads would be useful because could have digits 0 to 9 and two pads for other use.

Use two picaxe 14Ms. Use as per advanced information in manual. Each 14M to have 6 inputs, corresponding to 6 inverter outputs.

Each 14M scans it's 6 attached oscillators. Uses the Count command to determine the incoming frequency. Must use as high frequencies as possile so that don't have to run the count command for very long. Count command is useful because it automatically averages the incoming frequency. Translate frequency into 'On' or 'Off' for each pad.

Each 14M serially outputs the calculated state of the 6 pads, as soon as scan cycle is completed. One of the 14Ms outputs a synch pulse to the other 14M so that both 14Ms are synchronised and output their own serial results at predictable times, so that receiving picaxe can receive one result then immediately receive the other result.

Scan cycle for each 14M must be pretty quick for responsive system. I'd say results should ideally be transmitted at least 5 times a second.

(If you wonder why not just a 12X1 ...the Count command can only operate on input 0 to 7. Also two 14Ms are multi-tasking together, so scan cycle in theory half of what it would be if a single 28X1 !)
 
Last edited:

alband

Senior Member
Sounds very promissing.
I think it all is going to boild down to the fact that we would be able to get the code working, perhaps after lots of refining and messing with circuitry get everything done with one PICAXE, but those pads would still need to be made. They require more complexe calculations and more sophisticated tooling to build than just using card, foil and prit stick :rolleyes:.
If anyone has the facility to do this stuff (and can be bothered) it would be fantastic if they could get it working reliably and I would request that they share the code.
Until then, as annoying as it is, we'll have to use dedicated IC's which can be found all over the place now.
So, unless anyone makes any other brilliant descoveries I think I will bow out of this descution. ;)
 

hippy

Ex-Staff (retired)
@ Jeremy : That looks like a good starting point. My impression in reading the QProx datasheet is that they use some 'smartness' instead of the inverter so they can modify the fundamental oscillating frequency to better discriminate changes; I may have however misunderstood.

@ alband : You are right, it may be possible, but it does require some experimenting with different circuits and algorithms and fine tuning the hardware and software in parallel. It is quite a project in itself or QProx et al wouldn't likely have a viable product. It seems that much of their effort has been in producing a solution which is largely independent of hardware as well as getting the fundamentals working well.
 
Last edited:
Top