โ Back to Course
Understanding Analog Sensors and ADC
๐ Understanding Analog Sensors and ADC on ESP32
๐ What You'll Learn:
- ๐ Understand how analog sensors work with ESP32
- ๐ Learn ADC (Analog-to-Digital Converter) basics
- ๐พ Connect soil moisture, light, and temperature sensors
- โก Know which GPIO pins support analog reading
Analog sensors output a continuous voltage that the ESP32 can read to measure physical values like soil moisture, light intensity, or temperature.
๐ง What is ADC?
- Converts analog voltage (0-3.3V) to digital value (0-4095)
- ESP32 has 18 ADC channels on GPIO32-39 ONLY
- 12-bit resolution = 4096 discrete levels
- Formula:
Voltage = (analogRead(pin) * 3.3) / 4095
๐ฑ Common Agricultural Analog Sensors
| Sensor | Measures | Output | Cost |
|---|---|---|---|
| Soil Moisture (Capacitive) | Water content | 0-3.3V | $8-12 |
| NTC Thermometer | Temperature | Variable resistance | $1-2 |
| LDR (Light Sensor) | Light intensity | Variable resistance | $0.50 |
| pH Sensor | Soil acidity | 0-3.3V | $10-20 |
| Rain Sensor | Rain detection | 0-3.3V | $3-5 |
๐ Basic Analog Reading Code
const int analogPin = 34; // ONLY GPIO32-39 work!
int rawValue = 0;
float voltage = 0;
void setup() {
Serial.begin(115200);
Serial.println("ADC Monitor Started");
}
void loop() {
rawValue = analogRead(analogPin);
voltage = rawValue * (3.3 / 4095.0);
Serial.println("โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ");
Serial.printf("๐ Raw ADC Value: %d (0-4095)\n", rawValue);
Serial.printf("โก Voltage: %.2fV (0-3.3V)\n", voltage);
Serial.printf("๐ Percentage: %.1f%%\n", (rawValue / 4095.0) * 100);
Serial.println("โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ\n");
delay(1000);
}
๐ก ADC Pins Reference:
- ADC1: GPIO32, GPIO33, GPIO34, GPIO35, GPIO36, GPIO39
- ADC2: GPIO4, GPIO0, GPIO2, GPIO15, GPIO13, GPIO12, GPIO14, GPIO27, GPIO25, GPIO26
- โ ๏ธ ADC2 pins are used by WiFi - use ADC1 for reliable readings!
โ ๏ธ CRITICAL: ESP32 ADC Limitations
- Only GPIO32, 33, 34, 35, 36, 39 are reliable for analog reading
- GPIO36 and GPIO39 are input-only - cannot be used as outputs
- ADC is non-linear at extreme ends - most accurate from 10-90% range
- Use moving average filter for stable readings: average 5-10 samples
๐ Key Takeaways:
- โ ESP32 ADC = 12-bit resolution (0-4095, 0-3.3V)
- โ Use ADC1 pins (GPIO32-39) for reliable readings
- โ Formula: Voltage = (analogRead(pin) ร 3.3) รท 4095
- โ Always calibrate analog sensors (dry/wet values)
- โ
Use
map()andconstrain()for real-world values
๐ก Key Takeaways:
- Apply these concepts directly to your farm or project.
- Take notes on important details for the quiz.
- Use the button below to track your progress.
×