When you need multiple analog inputs for your ESP8266 application

IMG_4488sFor a project that I have been working on (which is extremely space-constrained) I needed a second analog input – but the ESP8266 has only one. Whereas additional digital I/O lines can be accommodated quite easily using an external shift register to convert them into a serial stream, this is not as simple for analog signals.

In specific situations you can get away with having only one ADC by multiplexing your sensors. In my scenario, because I was measuring totally unrelated things, this was not possible.

The most obvious approach is to add an external ADC. The drawback is that this requires you to get to grips with how to control your particular ADC chip, and beyond that to get serial communications working correctly. A further negative is that these ADCs usually require additional components in the form of stabilisation capacitors.

FSA3157_sizeI therefore came up with a simpler alternative – using an analog switch ic. I was pleased to discover that they are now available in extremely small form factors. Actually, my choice turned out to be rather too small – at a size of 1 mm x 2 mm, soldering directly to each pin (without the benefit of pcb mounting) proved more of a challenge than I was looking for.

FSA3157I settled on the FSA3157, which can operate off 3.3V and which allows you to select between 2 analog inputs using a single digital control output from the ESP8266. You are then able to feed that selected analog input to the single ADC input on the ESP8266. Further good news is that they cost well under a dollar each.

3157circuitThe image above shows this chip soldered together with a capacitor and 2 resistors, whose purpose is to reduce the input voltage (on the 1st analog line) to within the 0-1V range of the ESP8266 ADC, as well as to provide smoothing via a low-pass configuration. IMG_4494

The assembly was done through a microscope using a tiny soldering iron tip – hence my earlier comments.

 

FSA3357

You can easily expand to multiplexing 3 inputs by instead using the FSA3357. The only additional requirement is for another digital output from the ESP8266 to allow selection between the 3 analog inputs.

Also be aware that along with that extra input, the package size increases to 2 mm x 2 mm (!)

From an ESP8266 perspective, there is still only one ADC input – so you read the ADC in the usual way, using system_adc_read().

The difference is that you also need to control the external analogue switch to “preselect” the input that you wish to sample. This is done by setting ESP8266 GPIO output lines:  one output is needed to toggle the “S” control, which selects between the B0 and B1 analog inputs.

//toggle adc input
adcSelect ^= 1;
GPIO_OUTPUT_SET(ADCSELECTPIN, adcSelect);

For a scenario of 3 analog inputs, you would use 2 GPIO outputs to control both S1 and S2 on the FSA3357 to select between B0, B1 and B2 analog inputs.

9 thoughts on “When you need multiple analog inputs for your ESP8266 application

  1. I have added a few paragraphs to the post, which I hope makes this clearer. The ESP8266 still only sees one ADC input, so the method to read the ADC does not change.

    The additional step is to select an analog input by using output “control” lines from the ESP8266 to the external switch.

    1. Many thanks. Just wetting my toes in this area; do you see using lua with this method doable?

  2. Definitely – this is an external hardware solution rather than a software one. So you could still use adc.read(0);

    But, as my experience with lua has not been great, I would suggest you look at the Arduino environment that can now be installed on the ESP8266 (you can even still run it on NodeMCU hardware if you like): https://github.com/esp8266/Arduino

  3. Well done, this looks like a nice simple way of multiplexing analog inputs; and hopefully an extra bonus is that since I need voltage dividers to drop the voltage, I can also use the switching to avoid a current drain of the dividers from sapping battery power!

Comments are closed.