ESP8266 analog read

Types of analog sensors:

Analog sensors are components that vary their resistivity depending on a real variable, meaning the sensor pin voltage will flow between 0V and Vcc when it is exposed to higher or lower temperatures, light, humidity, etc. This analogic signal will be converted by the microcontroller in a numeric value and stored in a register. Some analog sensors are the LDR (vary according to the luminosity they receive), the thermistor ky-013, and some water or humidity level sensor.

The variable resistors must be wired up with a pull-up or pull-down resistor, with which we could measure the lecture.

There are also linear measurement devices, which allow balanced analog reads more precise than the ones with the variable resistors, there are the thermistor LMP35, the hall-effect amperemeter ACS712, or the new Texas Instrument LMP91200 pH device.

All those sensors can be read with the ESP8266 with the same code, and a similar wire up.

Analog read with ESP8266:

Be carefull! not every ESP-X format can read analog values. In fact, the ESP8266 chip supports it, but depending on the board layout, the analog pin is not connected to any terminal pin. The formats 07, 08, 12 and 12E, are the only one that have the necessary analog pin. We are going to take as an example the reading of a LDR photo-sensor:

  1. So the first step is to wire a pull-down resistor, so if the LDR is disconnected we have an expected output. Take a look at this documentation to learn more. It has only a resistor from GND to one sensor pin (the pin we connect to the analog input).

  1. We can read then the analog pin by using the well-known arduino functions (analogRead).

  2. So now we can build a simple sketch to allow reading analog values from the internet by making a simple thinger sketch like the following:

#include <ESP8266WiFi.h>
#include <ThingerESP8266.h>

#define USERNAME "your_user_name"
#define DEVICE_ID "your_device_id"
#define DEVICE_CREDENTIAL "your_device_credential"

#define SSID "your_wifi_ssid"
#define SSID_PASSWORD "your_wifi_ssid_password"


void setup() {

  thing.add_wifi(SSID, SSID_PASSWORD);
    // LDR resource
  thing["LUX"] >> outputValue(analogRead(A0));


void loop() {
  1. Enjoy thinger interface! In the device API, you can see the raw data that is being send by your device.

or you can create a real time dashboard with a time series chart, like this that is showing the luminosity of last ten days:

Great! thanks for the tutorial.

should be:

thing["LDR"] >> [](pson & out){

Nice tutorial!
To use ACS712, what I could change?

Hy Augusto!
Same code could be used with all analog devices, but remember to use the ADC pin (A0) :smile:

Good evening,

I am trying to measure curent with acs712 as mentioned above but i get a static measure of 1024 which is th max value of the ADC.
Does anyone know why is thiw hapening?

Thank you in advance

Maybe your shield don’t have a pullup/down resistor to create a current comparator in the output line… or it is possible that 1024 is the output value for 0A input current.
Can you expecify the exactly version of your acs shield?

I dont think it is due to the version. I have to measure AC curent…

Hi @giorgiol, I think that the problem while reading the acs712, is that the ESP8266 has an input voltage range from 0 to 1v. I suppose that the acs712 is providing more voltage, so you get always 1024. Check the voltage output with a multimeter to be sure.

Yes you are right…I need to create a lower voltage rate for th acs…

"The formats 07, 08, 12 and 12E, are the only one that have the necessary analog pin"
hi, the esp8266-01 have the analog pin? can read analog values? I can do this project with the esp8266-01? thanks.

Hi, what are you doing the sketch on ? The arduino IDE ? or do you write lua code and use esplorer to upload ?
Im very new, and there are different ways people are uploading tot his device, but no one says how they do it. Thank you.

Hi @joesgarage !
The Sketch uses Arduino libraries and IDE. I recomend you to read next tutorials for begin learning how works:

This video from PDAControl explains the same process:

i’m sure that, if you follow this instructions, you should be uploading data in two hours.

@hkjavier! I fear that ESP-01 don´t have analog pin.

Can I get a output file from thinger interface in text format

Hello @arpit! Yes you can do it making a data bucket and downloading it into a CSV file.

Can you explain me the above statement. More specifically, please explain me the general syntax

Can I sample data in bucket @ 1ms interval

Basic account is able to sample data 1 time per minute.

ok, this three lines of code are the definition of a C++ lambda, it is a way to create a non instance function that you can call using the ID that you have written inside the ["…"].

thing["LDR"] >> [](pson & out){
out = analogRead(A0);

In this case, LDR function will create an output Pson and fill it with an analog read.

I advise you to read the documentation and use the library examples to learn all features and code functions here: Arduino documentation