📘 Analog Programming (ADC & Sensor Reading)
🎯 Lesson Objective
By the end of this lesson, students will:
-
Understand what analog signals are
-
Understand what ADC (Analog to Digital Converter) is
-
Learn how ESP32 reads analog values
-
Use
analogRead()properly -
Understand 0–4095 range
-
Convert raw values into meaningful data
-
Build threshold-based sensor logic
1️⃣ What is an Analog Signal?
Unlike digital signals (HIGH or LOW),
analog signals can have continuous values.
Example:
-
Temperature sensor voltage varies gradually
-
Gas concentration changes gradually
-
Light intensity changes gradually
Analog signal is not just 0 or 1.
It can be any value between 0V and 3.3V.
2️⃣ What is ADC?
ADC = Analog to Digital Converter
ESP32 cannot understand voltage directly.
It converts voltage into numbers.
Example:
0V → 0
1.65V → ~2048
3.3V → 4095
This conversion is done by ADC.
3️⃣ ESP32 ADC Resolution
ESP32 uses 12-bit ADC.
12-bit means:
2¹² = 4096 values
Range:
0 to 4095
This gives good measurement precision.
4️⃣ Reading Analog Value
To read analog value:
Example:
GPIO 34 is commonly used as analog input.
5️⃣ Basic Analog Reading Code
int sensorPin = 34;
void setup() {
Serial.begin(115200);
}
void loop() {
int sensorValue = analogRead(sensorPin);
Serial.print("Sensor Value: ");
Serial.println(sensorValue);
delay(1000);
}
Output example:
Sensor Value: 1234
Sensor Value: 1250
Sensor Value: 1302
These numbers represent voltage levels.
6️⃣ Converting ADC Value to Voltage
Formula:
Voltage = (ADC_Value / 4095.0) * 3.3
Example:
int rawValue = analogRead(34);
float voltage = (rawValue / 4095.0) * 3.3;
Serial.println(voltage);
Now you see real voltage instead of raw number.
7️⃣ Real IoT Example – Gas Sensor (MQ3)
Gas sensor gives analog output.
int gasValue = analogRead(34);
if (gasValue > 2000) {
Serial.println("Gas Detected!");
}
Here:
Threshold-based automation logic.
ADC enables environmental sensing.
8️⃣ Mapping Values (Important Concept)
Sometimes we want to convert:
0–4095 → 0–100%
Use map() function:
Now:
Raw value becomes percentage.
Useful for:
-
Water tank level
-
Soil moisture
-
Battery percentage
9️⃣ Real Example – Water Tank Level
int waterLevel = analogRead(34);
int percentage = map(waterLevel, 0, 4095, 0, 100);
Serial.print("Water Level: ");
Serial.print(percentage);
Serial.println("%");
This converts raw data into human-readable format.
🔟 Calibration Concept
Sensors are not perfect.
Example:
Gas sensor may give:
Clean air → 400
Gas present → 2000
You must:
Test and observe values
Set threshold accordingly
Calibration is part of IoT engineering.
1️⃣1️⃣ Best Analog Pins for ESP32
Common analog pins:
GPIO 32
GPIO 33
GPIO 34
GPIO 35
GPIO 36
GPIO 39
Avoid using pins used for boot or output conflicts.
1️⃣2️⃣ Noise in Analog Signals
Analog readings may fluctuate.
Example:
Gas Value:
1200
1210
1195
1220
This is normal.
To smooth data:
You can:
-
Take average
-
Use delay
-
Filter readings
Example simple averaging:
int total = 0;
for(int i=0; i<10; i++) {
total += analogRead(34);
delay(10);
}
int average = total / 10;
This reduces noise.
1️⃣3️⃣ Combining Analog + Conditions
Example:
int temperature = analogRead(34);
int threshold = 2500;
if (temperature > threshold) {
digitalWrite(26, HIGH);
} else {
digitalWrite(26, LOW);
}
This builds automatic control system.
1️⃣4️⃣ Digital vs Analog Comparison
| Feature | Digital | Analog |
|---|---|---|
| Values | 0 or 1 | 0–4095 |
| Example | Button | Gas sensor |
| Function | digitalRead() | analogRead() |
| Usage | ON/OFF | Measurement |
Understanding difference is critical in IoT.
1️⃣5️⃣ Common Beginner Mistakes
❌ Applying more than 3.3V to analog pin
❌ Using wrong pin
❌ Forgetting Serial.begin()
❌ Setting wrong threshold
❌ Not calibrating sensor
Always check:
-
Voltage range
-
Proper wiring
-
Stable power supply
📌 Lesson Summary
In this lesson, we learned:
-
What analog signal is
-
What ADC is
-
12-bit resolution (0–4095)
-
How to use analogRead()
-
Voltage conversion formula
-
Using map() function
-
Calibration logic
-
Noise handling
-
Real IoT automation examples
You now understand how to read real-world sensor values.