Multiple Analog Inputs Using One Analoge Pin

67K8949

Intro: Multiple Analog Inputs Using One Analoge Pin

Hi!

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;
  • Arduino;
  • Broadboard;
  • Some wires.

Connections:

  • 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!

Vincent

34 Comments

I really encourage you, but Arduino has enough analog pins yet, so you better do that on NodeMCU (ESP8266-12E), because i really need some tutorial like this on that board.
Dear Sir,

I am getting below error message. Pls suggest how to rectify it.

collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board NodeMCU 0.9 (ESP-12 Module).
Vivek
Nice schematic and idea! Only problem is that you loose 0,7V from the analog scale due to diode voltage drop. For example the Arduino has an analog scale of 0-5V. This means that the processor will give an AD value of zero until the potentiometer voltage is > 0,7V. In AD numbers this means (0,7/5)*1024 = 143 or 14% of full scale. Quite big loss.

Also as you can see in the example picture from serial monitor the analog values are from 0-908. Lets do the maths: (908/1023)*5 V = 4,44 V. 5 V - 4,44 V = 0,6 V <- diode voltage drop.

This analog multiplexer is a very good idea but you have to consider the physical restrictions when writing the code.
Hello I have a question how many potentiometers can I plug into one analog input ? And if I have one set of 16 potentiometers axial and another set of 16 pots horizontal is it neccessery to use 2 diodes 1resistor for every analog input ? or is there a "easier" solution need clarification on this thank you and great post thanks alot

In that cases use relay to switch the swicth between two potentiometers.

We have multiple analog pins in Arduino use that pins.

how i can know how many resistor for sensor ?
Hello I have a question how many potentiometers can I plug into one analog input ? And if I have one set of 16 potentiometers axial and another set of 16 pots horizontal is it neccessery to use 2 diodes 1resistor for every analog input ? or is there a "easier" solution need clarification on this thank you and great post thanks alot

you really saved my life !!

but node MCU supply 3v , what if a sensor usess 5v ?

Edit, the last technic with only the power supply defeats the purpose of this instructables

You can either have a transistor in a switch configuration to switch higher voltages with only 3.3V (or less) needed to activate, or you take the easier way and buy a 3.3V to 5V step-up converter. What you can also try is just to have an external 5v power supply connected to the sensor and just have the Output wires connected to the nodemcu, but remember to connect ground of the power supply to that of the nodemcu.

if i use other diodes, not the 1A 50V diodes, would it still work?

Please give me the suggestions.
1. Would it work if I connect the pin in the middle of both analog sensors directly to the analog pin 0 without a diode?
2. Would it be possible to use other resistor rather than 10K ohm for the pull down resistor? What will happen?
Thank you.
Or you could just buy a quad ADC and use it for four inputs, no multiplexing needed. After researching this today I picked up a quad ADC from Aliexpress for $2.25. Just search for ADS1115 for 16 bit and ADS1105 for 12 bit. You can read the channels with I2C.

Without the diodes you'd have both sensors in the circuit at the same time, and the one powered down could be shorting the output of the other to ground.

Thank you for your reply.
Is that means sensor values would be unreliable?
Yes. The output would be influenced by both sensors.

Since diodes have a drop across them that varies by current, for more accurate readings you'd be better off using a multiplexer chip. You can get 8x1 and dual 4x1 multiplexers for a dollar or less.

Great ible!

The only problem in using diodes is that you have a voltage drop, so you have to offset this to get a correct read.

BTW, you could add this idea to your ible: Get multiple button presses in a single analog port, like this video:

All the four buttons are linked to a single ATTiny85 AnalogIn, due to the lack of sufficient DigitalIO to the circuit.

Forgot to mention that the different reads are get through different resistor values attached to the buttons.

More Comments