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.