For 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.
I 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.
I 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.
The 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.
The assembly was done through a microscope using a tiny soldering iron tip – hence my earlier comments.
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.