Although the ESP8266 -07 and -12 only have one ADC pin, it doesn't mean that you are limited to only one analog sensor per module. You can use many!

However, in order to use multiple sensors you will need to 'multiplex' the sensors. Multiplexing simply means that you will turn a sensor on, read the sensor, then turn the sensor off then move to the next sensor.

Here is a quick, down and dirty Instructable that should get you started with multiple analog sensors.

Lets begin!

Step 1: Connect Your Sensors.

For this project I am using three analog sensors. A photoresistor, a thermistor and a sliding rheostat.

These are what I had in the parts box. Feel free to substitute.

I used:

ESP8266-12, a -07 will also work. Three available GPIOs are needed for this project.
ESP Flashed with NodeMCU, required for this demo.
2 10K resistors
1 680R resistor
3.3v voltage regulator (if necessary)
1 Photoresistor (LDR)
1 10K Thermistor
1 10K Slider
3 Diodes (I'm using 1N4007s)

The values of the resistors used are dependent on the analog sensors. Notice I am using no resistor with the slider.

There is considerable trial and error in setting this up, trim pots come in very handy.

The Diodes are the key to isolating the sensor circuits not being read. Diodes restrict current to one direction. Without diodes the sensors still work, however they interact with each other. For instance, shining a light on the LDR will raise the value at its GPIO, however it will also reduce the value of the other sensors. Diodes isolate the circuits.

Step 2: The Multiplexing Script.

In order to multiplex, only one sensor can have a complete circuit at a time. By setting the the GPIO to HIGH, we are sending ~3.0v to that sensor and completing the circuit. The others, being set to LOW send no voltage. This essentially creates a ground to ground connection. No current.

After we send the voltage, we read the ADC pin and print out the value. We can also save that value in a variable.
This code 'var1 = adc.read(0)' will work. We can then manipulate the value and convert as required.

After we read or save the reading from the sensor, we turn it off and move to the next sensor.

The delay is not necessary, but it makes me feel better.

All the GPIOs being used with analog sensors should be set to LOW except when reading them.

There is no special reason for using GPIOs 16, 14 and 12. Any three would do. Avoid using GPIO0.

Source file here.


-- multisensor.lua
-- ---------------

--configure pins
gpio.mode(0,gpio.OUTPUT) -- GPIO 16
gpio.mode(5,gpio.OUTPUT) -- GPIO 14
gpio.mode(6,gpio.OUTPUT) -- GPIO 12

--set all pins to LOW - no voltage

-- print the ADC value with all pins low
print("\nADC: Both GPIOs LOW start: "..adc.read(0))
-- Send voltage to GPIO 16 completing the circuit for this sensor.
-- short delay before reading, 100ms
-- Read the ADC pin or assign to varable var = adc.read(0)
print("\nADC: GPIO 16 HIGH LDR: "..adc.read(0))
-- Return GPIO 16 to LOW, no voltage.
-- short delay 100ms
-- repeat for the other two sensors.
print("ADC: GPIO 14 HIGH Temp: "..adc.read(0))
print("ADC: GPIO 12 HIGH Slider: "..adc.read(0))
-- again read ADC, this reading should be very close to the 'start reading'
print("\nADC: Both GPIOs LOW end: "..adc.read(0))

Step 3: Fine Tuning.

Upload this script to your ESP and 'dofile'.

You should see values for the sensors! Each time you run the script the sensors are read and the results printed.

I put a flashlight on the LDR and ran the script. The LDR registered 1024. In the last run I had moved the slider.

As I mentioned earlier, finding the right combination of resistor values is crucial. Keep in mind that adding sensors, including diodes and resistors will alter the voltage across other sensors. This will test your 'Tweak and peak' skills.

There is a discussion about voltage and voltage measurements at the ADC pin here:


For those of you reading this that possess advanced electronics skills and are able to calculate correct values for these variables, I applaud your skills and apologize for my lack of knowledge! Perhaps you can share with us?

So, here is a good jumping off point. Thank you for reading this and good luck with your projects!

<p>Hey breagan, do you think is possible to measure the battery level and other sensor with your method? you can give me a clue on that?</p>
<p>You could certainly measure the voltage output of a battery. By 'level', if you are referring to the amount of charge remaining, that may be a bit more difficult. </p><p>Don't forget that ESPs are only 3.3v tollerant AND the ADC reference is 1.0v.</p><p>For example; if you wanted to check/monitor the output voltage of a common AA battery, you might use a simple voltage divider with R1 220ohm and R2 440ohm. </p><p>This should divide the AA's max voltage from 1.5v to 1.0v.</p><p>The ESP ADC would read the 1.0v as ~1023. </p><p>As the battery discharges, its current and output voltage will reduce, and so will the ADC reading.</p><p>With a load, the AA will discharge to a 'dead' state, however the AA will still be outputting some voltage. Depending on your application that 'dead' level could be just about any voltage below 1.5v.</p><p>With the above Rvalues. It would appear that the AA outputting .75v will read ~512 at the ESP's ADC pin. So, the usable ADC range is less than optimum. The arduino folks have a nice map() function. You may have to code your own. </p>
<p>I've a doubt, I've a voltage divider with 10k and 32k at 3.3v, it gives me 0.77v in the division and the ADC reads 221, at 1.0v it will read 295 approx. Why? </p><p>It's not taking 1.0v as reference? I'm using Nodemcu v2</p>
<p>Something always overlooked in these discussions; amperage.</p><p>I'm guessing 10K and 32K is way too much resistance for your 3.3v source.</p><p>Try 100ohm and 320ohm . Same ratio. Lets more current flow to the ADC pin.</p>
<p>interesting question - how much current is needed? We don't want the act of measuring a battery voltage to drain it!</p>
<p>How much current is needed? Great question!</p><p>I looked around for more information on current draw. Without much luck.</p><p>Current is directly related to Voltage and Resistance on a load, as per Mr Ohm.</p><p>An answer to your question relies on some variables.</p><p>However in the interest of discovery, I pushed forward and measured some data.</p><p>In order to measure Voltage, the ADC pin must sink some voltage to create a load.</p><p>I put my trusty voltage meter in series with the ESP8266's v3.3 and the ADC and measured the current. My result was 9.8 Micro-amperes.</p><p>This seemed to be constant whether I was reading the pin (adcread) or not.</p><p>That makes sense to me.</p><p>Next I hooked up a v1.5 AAA. My result was 4.9 Micro-amperes.</p><p>Finally I hooked up a v1.5 LR44. Result was 4.8 Micro-amperes.</p><p>So yes, the ADC will drain your battery.</p><p>BUT it is going to take a while.</p><p>A typical LR44 is rated at ~200mAh </p><p>1 mAh for 200 hours and its dead.</p><p>1MicroAh for 200,000 hours and its dead.</p><p>200,000/4.8 </p><p>So consuming 4.8 Micro-amperes, the ESP8266's ADC pin will kill the LR44 in about 41,666 hours.</p><p>Or, 4.75 years.</p><p>IF the ADC pin is the ONLY current draw from the battery.</p><p>Caution your mileage will vary greatly with batteries!!</p><p>That was fun :) </p>
<p>Weird thing, same result with 100-320ohm... But first, I want to know if your method will allow me to have the voltage divider and the lecture of one sensor. I've the doubt with voltage divider because I can't connect one pin to the gpio, so I don't know how to do it, can you help me about that?</p>
<p>Can you upload a schematic? </p>
<p>Thanks for your great explanation, I had readed about this, however, you pointed to something I was not considering; ADC only reads up to 1.0v</p><p>Knowing this, the way to wiring this voltage divider and the other sensor will be same as you posted above?</p>
<p>Great work!</p>
<p>Chad, You are on the right track. The sensor needs to be in series with your circuit. Either side should work. R1 and R2 are fine. Here is an example:<br></p>
<p>Greetings Chad,<br>That 680 is part of the output voltage divider for the ldr and the thermistor.<br>Can you show us a schematic of your project? I'm sure someone here will point you in the right direction.</p>
<p>Hmm, wouldn't the 680 resistor divide the sliders voltage then? For my project, the sensor acts a resistor (R1) and I think I need to experiment with R2 so that the range of voltages stays in between 0-1. </p><p>Attached is the incorrect schematic I drew up without thinking of the thevenin equivalent. I also have not incorporated the second temperature sensor I wanted to use yet.</p><p>Thanks!</p>
<p>Hello breagan22,</p><p>Why do you have the 680 ohm resistor at the left end of the board near the adc pin out. </p><p>I am also creating a a soil moisture sensor that already uses a voltage divider and it still needs to be stepped down to the 0-1 volt range. Having two voltage dividers will cause some interactions with the resistors (add in parallel), any suggestions on how to overcome this issue? I have tried using a transistor as a voltage buffer but I am a noobie at electronics so I was not getting anywhere.</p><p>Thank you so much</p><p>- Chad</p>
<p>Thank you very much for this. My garden monitoring project requires multiple soil moisture sensors, and this makes perfect sense. </p>
<p>can you update the sketch to be used on arduino language instead of LUA. becase now you can easily program ESP8266 with arduino language using IDE; <a href="https://www.instructables.com/id/Programming-the-ESP8266-12E-using-Arduino-software/">https://www.instructables.com/id/Programming-the-ES...</a><br></p>
<p>Greetings Akin</p><p>Sorry, I don't plan on using an Arduino with the ESP in any of my projects at this time.</p><p> Arduinos are great microcontrollers and adding an ESP to access the WiFi capabilities of the ESP is certainly a inexpensive way to accomplish communication. </p><p>There are some good Instructables already written for this. </p>
<p>Hi, I had the same problem with the NodeMcu and written a sketch in IDE based on this instructable. Feel free to use it or adjust it: https://www.instructables.com/id/Multiple-Analog-Inputs-on-Only-One-Analoge-Pin/</p>
<p>amazing work my friend. i understand you used UNO as an example but the same is applied to any arduino board, including ESP8266 and nodemcu :) genius</p>
<p>Yes, you can program the NodeMcu board within the arduino environment, just like an arduino. Sketches made for the arduino work also on this board :) For that reason I used an arduino for testing</p>
<p>i'm using soil moisture , humidity sensor,lm35 and ldr sensor. im getting temp and humidity value constant but while ldr and soil value changes . so i need help how to make soil and humidity sensor constant along with these above sensor in ESP8266E.</p>
Hmm...<br>Do you have a schematic we could look at?<br>
<p>New to the ESp8266, so bear with me. I do of course want these values available over wifi. Is this instruction just to view the values in the dev tool, or am I missing something? </p>
<p>No you aren't missing anything.</p><p>These values are being sent to the dev tool via serial output. This is similar to serial.print() in the Arduino world.</p><p>Yes, the point of using the ESP8266 is to connect and transfer data to the internet via WiFi.</p><p>So, instead of print(adc.read(0)). We can assign the value of the ADC pin to a variable.</p><p>For instance while working with a temperature sensor, we may use the line: temp = adc.read(0)</p><p>The variable &quot;temp&quot; now holds the value at the ADC pin.</p><p>We can now add that variable to a URL such as: http://www.somewebsite.com/?temperature=temp</p><p>The website &quot;somewebsite.com&quot; could then decode via GET the value.</p><p>PHP code: $temperature = $_GET['temperature'];</p><p>The value from &quot;temp&quot; in the url is now assigned to the PHP variable $temperature and can be manipulated by the script for saving, graphing or displaying.</p><p>There are many great Instructables on how to send data to and from ESP8266s and internet infrastructure in general.</p>
<p>Hi! Nice job and very well explained. I only have a question: the diodes have a forward voltage drop. Small, but important on such small voltages. I haven't seen how handle that voltage drop on your code. Do you add it somehow to the reading?</p><p> Thanks!</p>
Yes the voltage drop from a diode makes a significant impact when it comes to planning your circuit.<br> The reference voltage for the ESP is 1.0v.<br> The goal is to provide voltage to the ADC pin in a range from 0 to 1.0v.<br> Your analog sensor or variable resistor (pot) should have enough range to allow the full 1.0v or when adjusted to MAX it should stop all voltage and present the ADC with 0v.<br> So yes, in this situation the diodes have a forward voltage drop of about .6v. &nbsp;That means that we would want to present the diode with about 1.6v &nbsp;1.6 - .6 = 1v.<br> So adjusting the voltage divider resistors, using 220ohm and ~210ohm with a 3.3v source should provide ~1.612v to the diodes and ~1.0 to the sensor or directly to the ADC pin for a ~1023 reading.<br> <br> Software wise, one could certainly deal with 0 to .4v. &nbsp;Arduino has a very useful map() function to expand these kinds of readings. &nbsp;One could certainly employ something similar in LUA. &nbsp; However, for my money providing usable voltage is preferable.
<p>Thanks for your greatly useful answer!!! Have a nice weekend!!</p>
Hi breagan, <br>I already done the sensor using dht11 but i found that only firmware version 20150127 can be used.. If i used other version of firmware the output will become - 128 degree and 0% RH.. Why did this happen? <br><br>For co2 MQ135 sensor, i still not manage to find the coding in lua that can be used. The signal from the sensor should be connected to analog input at the NodeMCU or digital input? <br><br>And can you help me by give the link or sample coding for the co2 sensor? I want to display the reading in ppm using web server. <br><br>Your help will be very meaning full to me because this project will be my final year project for my bachelor.. Thank you once again..
Greetings,<br> Using the DHT11 you may need to experiment with both the Float and Int versions.<br> I would recommend building an INT version using the latest build.<br> Also, make sure you have a delay in your loop. The DHTs can not be read too fast.<br> <br> You wont fine a NodeMcu library for the MQ135 sensor.<br> This sensor has an analog output. Some modules have a digital TTL that could be used.<br> You will need to connect the analog output of the MQ135 to the ADC pin of the ESP.<br> Read this via TOUT() command.<br> The MQ135 will put out a voltage between 0 and 5v.<br> The ESP will be convert it to a 10bit value '0-1023'.<br> <br> You will need to calibrate the readings against a known CO2 concentration.<br> Pure CO2 saturation should be at or near '1023' (10bit)<br> Pure O2 or other gas should be near '0'.<br> If you are within +-100, that would be very good.<br> <br> Normal air : 350 - 450 ppm should be somewhere in between.<br> I suspect the the ppm saturation from 0 to 100% is not linear.<br> <br> You may need to do many calibrations at know levels to make an algorithm that works.<br> <br> The goal is to convert that 10bit reading back to PPM.
<p>Hi there. I would like to integrate 3 analog sensor into my nodemcu ESP 12E.</p><p>The sensors are humidity, temperature and CO2 gas sensor. I want to send the reading for each sensors after being converted into their respective units(degree, %RH and ppm) into web server using ESP. </p><p>Is it possible to conduct it using only nodemcu ESP12E as the controller?</p>
<p>Greetings!</p><p>The short answer is, yes.</p><p>This instructable is doing part of process, collecting the data. Once the data is collected, there are a few ways to send the data to a server depending on your specific needs and internet accessibility.</p><p>An easier was to collect temp and humidity is with a DHT22.</p><p>A DHT22 will provide temp and humidity. NodeMcu has a library for this sensor and the temp can be output in C or F. Of course simple math can provide K. This is a very easy solution and works well. I have a couple of these uploading data to a server every 30 mins. Works flawlessly. </p><p>I haven't used a CO2 sensor, yet. Can you be specific on the sensor you intend to use? A sensor that sends an analog signal would be very easy to use concurrently with the DHT22.</p><p>A digital CO2 sensor, depending on the interface (1-wire,SPI,I2c) can also work but may need some more intense programming to work with a DHT22.</p><p>If you have an analog CO2 sensor than any ESP with an ADC pin will work fine.</p><p>Sending the data to a web server is pretty straight forward. I find that sending the data as part of the URL and letting the server parse the header via GET is simple and effective. There are some Instructables that cover this. </p><p>Good luck!!</p>
For CO2 sensor, i intend to use MQ135 gas sensor. It has analog input signal and also use library if implement in arduino. Where can i find the library for dht22 and also this MQ135?. <br><br>I also want to know the resistor needed for each of this sensor. Can I use 10k resistor like the example you provided for my sensors?
<p>Ok, for the DHT22, NodeMcu has the DHT module (library) built in. If you build your own .bin file, just ensure that the DHT module is included, then you are good to go.</p><p>The MQ135 dosn't need a library or NodeMcu module. This is strictly an analog sensor and the voltage can be read via TOUT. HOWEVER, the MQ135 is a 5.0v module. It needs the 5v to heat the sensor element. Therefore the analog output from the sensor would be from 0v to 5v. </p><p>ESP8266 is strictly 3.3v. You will need a voltage divider for that analog output. A couple of resistors and a zener to be safe. The upper level of the analog output needs to be 3.3v. And you want to maintain the full 10bit voltage range.</p><p>Since the MQ135 requires some serious calibration for any environment, perhaps you could try to run it at 3.3v, it may give results that meet your needs. However the datasheet is clear about 5v.</p><p>No resistors needed for the DHT22, this is a digital sensor.</p><p>You will need a 10k to ground for the MQ135. </p><p>Calibration for the MQ135, as stated in the datasheet involves a range of recommend R values and trial and error. </p>
many many thanks for reply...<br>serial-value is nothing but some serial data (&quot;hallow world &quot;,etc )<br>i wanna add up these two (force &amp; bend) sensors and plot their serial data in a web browser.after that i will try for make excel file with these data.
<p>Ok, There two ways to do this.</p><p>1. Collect the data and write that data to a file in the ESP. Simply open a file to w+ and write the TOUT for each sensor. Perhaps:</p><p>file.open(&quot;adcvalue.lua&quot;,&quot;w+&quot;)<br>w = file.writeline<br>w(&quot;adc value&quot;..adc.read(0)..&quot;\n&quot;)<br>file.close()</p><p>Do this every time you loop.</p><p>2. If you plan on having many,many readings (data sets) perhaps you should send it to a real server that can save the data.</p><p>Send a header to your server with the readings. Perhaps:</p><p>header = &quot;<a href="http://www.yourserver.com/" rel="nofollow">http://www.yourserver.com/</a>?adc1value=&quot;..adc.read(0)..&quot;&amp;adc2value=&quot;..adc.read(0)..&quot;</p><p>Then send the header and have your server parse this &quot;get&quot; request.</p>
<p>Thanks again..</p><p>Is there any way to see serial readings of arduino via ESP8266?</p><p>more clearly, i want to setup a wi-fi enabled arduino for serial readings.</p><p>expecting coding,schematics,links whatever u can share!!</p>
<p>i want to see these serial-value in any serial monitoring s/w.i need to plot these data wirelessly.can u help me!!??</p><p>Thanks..</p>
<p>Can you be more specific about 'serial-value'? The ESP can send ADC or any other internal data via WIFI. The ESP can 'post' to a webserver or have a browser send a request to the ESP and exchange data.</p>
<p>if i used the adc in the esp8266 the wifi is working?</p>
<p>Can you be more specific?</p>
<p>As far as I know, WiFi and ADC can't be used simultaneousely on ESP8266</p>
<p>Using NodeMcu, I find that the ADC and WiFi work together with no issues. Perhaps some internal chip timing disables one or the other during a cycle. The esp is single threaded. At 80 or 160Mhz it would be hard to detect. </p>
<p>Thanks a lot for your info! </p>
<p>i'm making huge progress.. wiring is done, now working on the code. check it out, thank you so much for your help.!!</p>
<p>This is a really interesting and creative approach. Is there any measurable interaction between the sensors readings? Meaning if I have a large resistance on one of the three sensors, and a high capacitive load on another, a third measuring smaller resistances... Are there parts that suffer in accuracy?</p>
<p>The diodes do a very good job isolating the sensors. With the above schematic, I see less than 2% variance between readings. I don't see any interaction based on the readings of individual sensors. That is for example, shinning a light on the LDR and bringing it up to 1024 does not change the others. The sensors are isolated from each other. I mostly attribute this small variance to less than optimal breadboard connections. Soldered connections are optimal.</p><p>Without diodes all the sensors act as one. Applying voltage to one sensor connects the circuits in the other sensors. I believe its doubtful that one could create an algorithm to determine the correct reading not knowing the resistance presented at the other sensors. </p><p>Even with diodes, adding more sensors will change the capacitance of the circuit and will change the readings. However, this would be a fixed calculable change and will affect all sensors attached. Adding more sensors would require more fine tuning. I would start with the pull down resistor on the ADC wire.</p><p>As for accuracy, the sensors are extremely accurate. That is, presented with a constant voltage and stimulus, they provide constant results. </p><p>I do find some oddities that I believe are causing issues. One being the ADC pin outputs about +1.5v. I would like to see no voltage at an 'input' pin. Another is that the GPIOs output 3.0v when set HIGH rather than 3.3v. However, I suspect that the module is acting correctly, its probably my knowledge and understanding of this module that needs to catch up. :) </p>

About This Instructable




More by breagan22:ESP8266 WiFi OTA Remote File Management ESP8266 - NodeMCU - CPU Speed Test ESP8266 Irrigation Controller / Echo Alexa integration 
Add instructable to: