ESP32 Wroom32 DevKit Analog Read Example

Analog to digital conversion is the ability to read a voltage level found on a pin between 0  and some maximum value and convert that analog value into a digital representation . Varying the voltage applied to the pin will change the value read. The ESP32 has an analog  to digital converter built into it with a resolution of up to 12 bits which is 4096 distinct values. What that means is that 0 volts will produce a digital value of 0 while the maximum voltage will produce a digital value of 4095 and voltage ranges between these will produce a correspondingly scaled digital value.One of the properties on the analog to digital converter channels is attenuation. This is a voltage scaling factor. Normally the input range is 0-1V but with different attenuations we can scale the input voltage in to this range. The available scales beyond the 0-1V include 0-1.34V, 0-2V and 0-3.6V.

ESP32 Wroom Pin Out

ESP32 DevKit Pinout

Here is an example application using the Arduino IDE. What this example does is print the
value read from the ADC every 100m second.

Arduino Code for Reading ESP32 Analog Input

/*
  AnalogReadSerial ESP32 ADC example

  Reads an analog input on ADC1_0, prints the result to the Serial Monitor.
  Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +3.3V and ground.

  
Zero to Hero : ESP8266
*/ // the setup routine runs once when you press reset: void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); } // the loop routine runs over and over again forever: void loop() { // read the input on analog ADC1_0: int sensorValue = analogRead(A0); // print out the value you read: Serial.println(sensorValue); delay(100); // delay in between reads for stability }

Note: that only a subset of ADC pins and functions are exposed. First, the supplied drivers expose only ADC1. The board layout of the ESP32-DevKitC only exposes some of the pins. Specifically, the following are exposed: ADC1_CH0 , ADC1_CH3 , ADC1_CH4 , ADC1_CH5 , ADC1_CH6 and ADC1_CH7 .

Results

Open Serial monitor from tools menu and set it at baud rate of 9600. If you see some garbage manually reset the ESP32 DevKit board.

ESP32 analog read example results

 

Leave a Reply