I'm using the circuit in the attached image to implement a finger switch, meaning a switch that is made from two metal contacts and the switch is closed when you place your finger across the contacts. In the circuit,
R1 = 470 ohm,
R2 = 5 kohm,
Q1 = Q2 = KSP2222A NPN transistors,
+5 V and Gnd are the Arduino 5V and ground connections.
When using the computer USB as a power supply, this setup works perfectly using essentially the code in the Arduino AnalogInOutSerial example. I get a very stable value of 1023 on A0 when no connection is being made, and a fairly stable 150-160 value when a connection is being made. In order to make it a switch, I set a threshold value of 350 with A0 > 350 being open and closed otherwise. To test, I'm just toggling the built-in LED.
My problem is that when I disconnect the USB power and connect a 5V external PSU I find that the switch closes if I touch just the finger switch contact that is connected to R2. That is, I don't have to close the switch at all, I just touch that one contact and this is enough for the value at A0 to cross the threshold.
I'm finding this hard to debug because if I plug the USB power back in, the circuit behaves properly again, so I can't see the values that are coming in at A0 on the Serial monitor.
Has anyone any suggestions as to why the behavior changes with external power? How can I fix this?
No matter which power you use, the circuit is wrong. You are acting as an antenna any time you touch a finger to the end of R2. Does your 5 volt PFU have a mains ground pin on the plug and is the -5 volts connected to the PSU ground?
The PC USB connection likely has a grounding pin on the mains connection cord.
Neither the computer supplying the USB power (a current model Mac Mini) nor the 5V external PSU have ground connectors. In each case the plug is 2-pin only. The PSU is connected to the Arduino Uno using the on-board barrel connector. The USB is connected using the USB port.
If it were a problem that the person is acting as an antenna which for some reason is a problem when using the PSU but not USB, how would I deal with that?
Actually, you were right, @Paul_KD7HB, the computer supplying USB power was being grounded through a peripheral despite having only a 2-pin plug itself. I disconnected the peripheral and was able to reproduce the same behavior I saw with the external PSU.
With the external PSU, I had some success connecting the lower finger switch contact (that connected to R2) to ground through a capacitor. I thought that since a capacitor acts as a high-pass filter that any signal present because of something acting as an antenna (which should be AC, I suppose) would go straight to ground. This worked insofar as it prevented the switch being closed by touching just a single contact. However it also greatly slowed the reponsiveness of the switch (it took many seconds for the voltage to go back up after removing my finger, for example).
Is a capacitor to ground a reasonable way to go? Should there be a resistor in series with it? What values might work best to perform the high-pass filtering while not reducing the responsiveness too much?
Try a resistor (10k-1M should be a good target) between the finger switch and GND. This helps switching off the transistor, as it bleeds off any leakage current and brings the base down to GND level, and stops it from floating.
Your finger now acts as voltage divider together with that resistor, when you touch the two electrodes a small current flows, and the signal on A0 should go down.
You may actually be able to reliably sense this through a digital pin as well if you pass it through a Schmitt trigger such as the 74HC14.