# Arduino - Using NPN transistor as a switch to pull-up/down analog pin

Let me start by saying that I am not an EE, and my knowledge in Electronics is basic, so please bear with me and be patient if, initially I am bending a few rules.

In Epee fencing, a touch is awarded when contact is made at the end of a weapon's tip: when A (Analog Pin A0) and B (Analog Pin A1) are in contact, both A0 and A1 read 2,5V and a touch is awarded.

In Foil fencing, I need to establish that a fencer has been touched on its Lame (a conductive jacket). Therefore, in addition to the Analog functionality, I also need to implement a Capacitive sensor on the A0 pin. The Cap sensor uses a digital Pin (D4 in Diagram) to charge the Capacitor via a large (1M) resistor and reads the Voltage/status of a Receiving Pin (here A0).

The A0 pin is therefore used both for Analog and Digital readings purposes. This is the source of my issue, and why I am here.

I have no choice but to share the analog pin A0 for both analog and digital read purposes (there is only one line A). I have established that I cannot read the Analog levels and the Cap sensor output at the same time, without exposing myself to adverse side effects (wrongfully sending 5V to the Cap during the Cap sensor operation when A&B are in contact being one of them...). I, therefore, perform these tasks simultaneously:

1. Sampling both Analog A0 and A1 Pins (Analog ON/Digital OFF),
2. Sampling Analog A0 as a Digital INPUT (Analog OFF/Digital ON),

My primary concern is to achieve some form of isolation of the Cap sensor when in operation: this is to avoid the 1K resistors getting in the way of my capacitive sensor sampling, and CRUCIALLY, the A1 pin (connected to 5V via a 1K resistor) not charging the Cap during a touch sensing operation, thus destroying the significance of the values sampled.

simulate this circuit – Schematic created using CircuitLab

I also critically need to avoid the limits of the Cap Sensing circuit extending beyond its absolute necessary boundaries, which is not the case if the tip is depressed, or if the 1K resistor is pulling the A0 to GND, preventing the Capacitor from being charged properly.

So, I have a few questions:

• In the diagram, I show what I THINK the solution to this problem is (but I am not saying it IS the solution).
• Do you think I am right?
• Am I going in the right direction using NPN Transistors? Are they used correctly?
• If you did not use this method, What would the correct circuit be, etc,

Thank you very much in advance to all of you for helping me out. Don't hesitate to ask for more details.

Note: changed the schematics to use Q2 as a PNP following a suggestion from 'Captain Normal' below.

• You write that the ADC readings are not what you expected. Have you measured the voltages at the pin with a multimeter? Note the NPN is not a perfect switch. For ex, when "ON" the voltage across the transistor is not ZERO. – mike65535 Nov 20 '18 at 19:02
• These problems should be entirely avoidable as your circuit seems more complicated than the task requires. Why not put the resistor in series with the analog pin and use that as an output then switch it to analog input mode? – Chris Stratton Nov 20 '18 at 19:05
• Thank you Chris. I understand that the circuit might not be perfect. So what would you recommend to implement an ON/OFF switch on these Resistors, bearing in mind that I must be able to decouple them from the circuit so I can use the Analog Pin for another purpose (the Capacitive Sensor)? – Jeruinsky Nov 21 '18 at 9:05
• Do not repost - use the edit button to fix this question. – Chris Stratton Nov 21 '18 at 21:08
• As Chris said, it seems you are getting to complicated. The reason why someone said it was an X-Y problem is that this is your idea to the solution and it may not be the right one but without knowing underlying requirements it is hard for people to help. For example, what is the capacitive sensor for? – Drew Fowler Nov 21 '18 at 23:17

So, I have a questions: - Am I going in the right direction using NPN Transistors? - What would the correct circuit be, etc.

Measuring the capacitance of a real-world object by charging via a 1M resistor is probably not the right approach as the currents involved are very small and the circuit is not likely to measure reliably. Further, connecting external objects directly to Arduino pins is probably not a good idea due to the chances of ESD destroying your Arduino after some amount of use. For those reasons I think you need to first take a little step back.

I suggest starting with measuring the capacitance by using an oscillator whose frequency will vary depending upon the capacitance. You can then measure the frequency via a circuit that converts frequency to voltage for your ADC. Circuits for both of these you should be able to find elsewhere.

That done, you should be able to build a detector for the sensor contact on top of your capacitance detector. You will have several discrete states of contact for your various sensors - eg no contact, weapon A contact with lame B, weapon contact with wrist guard, etc. Draw a table for how the your circuit behaves when the sensors are in each and every possible state. You may have enough "information" in the existing circuit, or you may need to augment it slightly.

I hope this helps point you in the right direction.

• Thank you, Captain Normal. I had not thought about ESD. That's a very valid point. It stays that whichever alternative routes I am taking for the measurement of the Cap Sensor, this must be done being sure that the the Cap sensor remains totally neutral to the Analog section of the circuit, and especially, the fact that A1 is pull-up to 5V... And this is where I struggle. Thanks again for your contribution. – Jeruinsky Nov 22 '18 at 14:32
• Captain normal, your Oscillator suggestion is bloody brilliant... Can you recommend parts I could use for this project? (bearing in mind I am a hobbyist and not an EE...) – Jeruinsky Nov 22 '18 at 14:46
• There are many ways to do it, but my first thought in your case would be to use a 556 timer IC, which is a duel 555 - pun intended due to your application ;-). You can use one timer as an oscillator, replacing the capacitor to ground with your sensor, and the second timer as a one-shot triggered by each falling edge of the oscillating signal. Filter the output of the second timer with a simple RC to convert it to a voltage. Start with the Wikipedia page for the "555 timer IC", in particular the "Astable" section for the oscillator and the "Monostable" section for the conversion to voltage. – Captain Normal Nov 22 '18 at 15:06
• Brilliant. A BIG thank you Captain Normal. I am not sure I understand everything you just said, but this first "normal" answer I got here so far... – Jeruinsky Nov 22 '18 at 15:38
• Ah - you updated your OP, probably as I answered it. I see what you're doing - not bad! I would however stand by my suggestion for capacitive measurement, get that going, then work out how best to detect the tip switch on top of that without interfering with it too much - which should not be too hard. Do you have access to an oscilloscope? Get a 555 timer going as an oscillator, then swap out the capacitor for your sensor (or add your sensor in parallel to it) and see if you can get it oscillating at different frequencies depending upon what the sensor is doing. Then go from there. – Captain Normal Nov 22 '18 at 16:15