OC
OceanRemote
Low-code IoT platform
← Back to Course

Understanding Analog Sensors and ADC

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

SensorMeasuresOutputCost
Soil Moisture (Capacitive)Water content0-3.3V$8-12
NTC ThermometerTemperatureVariable resistance$1-2
LDR (Light Sensor)Light intensityVariable resistance$0.50
pH SensorSoil acidity0-3.3V$10-20
Rain SensorRain detection0-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() and constrain() 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.