ESP8266 analog read


#1

Types of analog sensors:

In general, analog sensors are variable resistors that allows higher or lower current depending on the real variable we are measuring, meaning the pin voltage from the sensor will flow between 0V and Vcc. This analog measurement will be converted by the ADC and stored in a register as a 0-1024 value. 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"

ThingerESP8266 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

void setup() {
  pinMode(BUILTIN_LED, OUTPUT);

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

}

void loop() {
  thing.handle();
}
  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:


The REST commands and sintaxe of Thinger.io
#2

Great! thanks for the tutorial.


#3

should be:

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

#4

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


#5

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


#6

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


#7

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?


#8

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


#9

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.


#10

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


#11

"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.


#12

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.


#13

Hi @joesgarage !
The Sketch uses Arduino libraries and IDE. I recomend you to read next tutorials for begin learning how Thinger.io works:
https://community.thinger.io/t/esp8266-with-arduino-ide/20/12
https://community.thinger.io/t/register-a-device-in-the-console/23

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.


#14

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


#16

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


#17

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


#18

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


#19

Can I sample data in bucket @ 1ms interval


#20

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


#21

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 thinger.io features and code functions here: Thinger.io Arduino documentation