Measuring current from a digital pin with analogRead

Hi.

I want to be able to check if some pieces of a puzzle is placed or not. To do this as easy as possible, I've set up 5 digital pins to output HIGH with digitalWrite(). The wires from the digital pins is connected to one analog pin each. Instead of a closed circuit, I have a gap for each piece of puzzle which is covered in aluminium foil.

The logic is that when the puzzle piece is placed, the circuit will be complete - and the Arduino will give some sort of output. Since I have a lot of pieces, I need some logic to test if each specific piece is placed or not.

I've tried to read the output from the digitalWrite() with analogRead() and map the values of 0-1023 on a 0-100 scale. This gives me a high variation in readings, and sometimes a reading of 100 (1023) even though the circuit is not closed by the puzzle piece.

Is it possible to get a more precise reading that doesn't vary so much? So far I haven't used any resistance between the different pins.

All you need to do is to make the pin into a digital input and enable the internal pull up resistors. Then the digital input goes to one piece of foil and the other is connected to ground. Then you read a 1 when there is no piece in place and zero when there is.

In effect, each puzzle piece is acting as a switch.

If you need to support more puzzle pieces than you have digital pins available then there are techniques available to combine a series resister with each switch in such a way that you can calculate which switches are open and which switches are closed, using a single analog input. These techniques do assume that the switch has negligible resistance when it is closed. The approach you're using is much simpler to use, copes with the switches not being perfect conductors and is the best way to continue as long as you can afford to use a separate digital input for each puzzle piece.

These techniques do assume that the switch has negligible resistance when it is closed.

Well no.

With a voltage required for a logic zero at 1.5V, and assuming the worst case (lowest ) pull up resistor of 30K, then your switch just has to have a resistance of 9K or less to register.

He just didn't state it well, after talking about digital inputs, he switches to discussing an analog input.

Grumpy_Mike:

These techniques do assume that the switch has negligible resistance when it is closed.

Well no.

With a voltage required for a logic zero at 1.5V, and assuming the worst case (lowest ) pull up resistor of 30K, then your switch just has to have a resistance of 9K or less to register.

You say 'no', but we're saying the same thing. The comment of mine which you quoted was about the technique of combining a series resister with each switch and then reading overall resistance of a circuit that combines all the buttons into a single analog input - any significant variations in contact resistance could make that approach unworkable. I contrasted this with the straight forward one switch per input approach which, as you point out, is far less fussy about contact resistance.

You say 'no', but we're saying the same thing.

Sorry, my wife is always telling me off for the same thing.