Introduction: Multiple Analog Inputs Using One Analoge Pin
In this instructible I will explain how to read values of more than one sensor by using only one analog input pin.
You might wonder why you want to this.
When you are using a Nodemcu board, as illustrated in the picture, only one ADC pin is availible. For a small weather station I would like to read values of multiple analog sensors: a rain sensor and a light intendsity sensor. This is unfortunately not possible with this board.
A solution is to multiplex the sensors. This works as follows: You will turn a sensor A on, read sensor A, then turn sensor A off. After this you will do the same for sensor B.
The idea is based on the instructable of Breagan. As his code was written in LUA I have writen a sketch in C++. This is handy when you are programming your Nodemcu in IDE (the arduino environment). For ease I tested the sketch and wiring on the arduino UNO/duamilanove.
please see also the intructable where I used this technique!
Step 1: Wiring
For this project you need the following:
- Two 10K ohm potentiometers;
- Two diodes;
- One 10K ohm resistor;
- Some wires.
- Connect one side pin of each potentiometer to ground;
- Connect the other side pin of potentiometer A to pin 10 of the arduino;
- Connect the other side pin of potentiometer B to pin 11 of the arduino;
- Connect the pin in the middle of both potentiometers via a diode to the analog pin 0;
- Add a pull down resistor (10K ohm) between the ground and the analog pin 0.
The potentiometers do have the same characteristics as analog sensors and are easy to use for testing. The diodes are the key for isolating the sensor circuits. Diodes restrict current to only one direction. Without diodes the sensors still work, however they interact with each other.
Step 2: Coding
In order to multiplex, only one sensor can have a complete circuit at a time. By setting the the GPIO pin 10 to HIGH, we are sending 5v to sensor A and completing the circuit. The other pin (GPIO11) is being set to LOW and is therefor sensor B is turned off.
The sketch will read both values and print a value between 0 and 1023 in the serial monitor. You can open the monitor in the IDE software by pressing CNTRL+M.
As one can see in the illustration above the first potentiometer (A) is fully open while the second one is closed. After this potentiometer A is slowly closed until both values approach zero. After this potentiometer B is fully opened.
Have fun trying this out!