I'm wanting to analogRead the value off a MQ-135 gas sensor. I have tired this with both an Arduino Uno and an TTGO ESP32 SX1276.
When reading the value of the sensor off the Uno, I get values approximately equal to 400 which is expected, as shown in this video. When doing the same on the TTGO, I get values close to 2000. For some reason, this person gets a similar value range as shown in this video.
What I don't understand is how these values are different when only the board is being changed. Do different boards analogRead differently?
More or less, how do I get the TTGO to read the same values as the Uno?
Below is my code for both boards. Nothing is changed between the boards:
#define sensor 2 //sensor pin
int gasLevel = 0; //int variable for gas level
void setup() {
Serial.begin(9600); //start serial comms
pinMode(sensor,INPUT); //set sensor for input
}
void loop() {
gasLevel = analogRead(sensor);
Serial.println(gasLevel);
delay(1000);
}
The following things I have tested:
- Multiple boards of both the Arduino Uno and TTGO
- Different pins, both analog and digital
I also am powering the sensor using 3.3V pin, so there is no variation in the voltage the sensor is receiving.
1 Answer 1
Do different boards analogRead differently?
Yes.
Your typical Arduino has a 10-bit ADC. That can give values between 0 and 1023 (210-1), with 400 falling just short of half way.
The ESP32 has a 12-bit ADC. That gives values between 0 and 4095 (212-1), which means that your 2000 falls, yes, just short of half way.
It is not the value from the ADC that is important - it is what the value represents. It is meaningless until you convert it into something meaningful, like a voltage.
Like asking "How heavy is it?" -- "It's 23." Completely meaningless, until you qualify it with units, and the units are different because of the different resolutions, so you need to "convert" it into units that have a meaning.
Typically the value represents a proportion of the supply voltage (but not necessarily: it's actually a proportion of the ADC reference voltage, which is often the same thing as the supply voltage). On the Arduino the value is 1/1024th of the supply voltage. On the ESP32 it's 1/4096th.
-
That makes complete sense. Thanks for the clarification.Lachlan Etherton– Lachlan Etherton09/28/2018 09:30:32Commented Sep 28, 2018 at 9:30