Introduction
Analog Signals are signals that change in nature with time. Unlike digital signals they have many states, and can be used to control intermediate states between LOW and HIGH of a digital signal. Simplified : if 0 —> 0 Volts and 1 —> 3.3Volts, then analog can get many states in the range of 0 to 3.3V. It can get many states depending on the resolution of the signal.
Simplified – Analog Values are basically anything that changes in nature with respect to time. For example your age, it keeps on changing every year. Another example is the size of your finger nails – keeps on growing. So basically these can be understood as analog in nature.
Working Principle
We will be reading basic analog readings from a potentiometer. The potentiometer will be connected to one of the analog pins on the ESP32. The ESP32 will then convert the analog signals in the range from 0 to 4095. Analog readings that are read by the ESP32 are in the voltage range from 0 – 3.3V. Which only means that 0 refers to analog 0 and 3.3V corresponds to 4095.
With the above line we may assume that the ADC of the ESP32 is linear, but this is not the case. ESP32 is not able to differentiate the maximum and minimum points. For example 3.2V and 3.3V, will give the same analog reading 4095.
You cannot distinguish between a 3.3V and 3.2V as their analog readings of the upper threshold is non differentiable and even at the lower thresholds the problem with the ESP32 remains the same. The analog readings range from 0 to 4095 because of the resolution of the ADC is 12.
Note : All ADC pins have multiple functions. Please note while using the ADC2 pin you may have difficulties specially incases when you are using the WiFi. Use another ADC pin to avoid the problem.
Components Required
- ESP32 DEVKIT V1
- 10kΩ Potentiometer x 1
- Breadboard x 1
- Jumper Cables
How does the ESP32 read signal?
The Potentiometer should be connected to the analogPin GPIO26. The ESP32 then will map the current values in the resolution of 0 ot 4095. These signal levels can then be viewed on the Serial monitor.
Reading analog input on the Arduino IDE is simple with the analogRead( GPIO) function. There are many more functions listed below.
Analog Functions | Use |
---|---|
analogReadResolution(resolution) | set the sample bits and resolution. It can be a value between 9 (0 – 511) and 12 bits (0 – 4095). Default is 12-bit resolution. |
analogSetWidth(width) | set the sample bits and resolution. It can be a value between 9 (0 – 511) and 12 bits (0 – 4095). Default is 12-bit resolution. |
analogSetCycles(cycles) | set the number of cycles per sample. Default is 8. Range: 1 to 255. |
analogSetSamples(samples) | set the number of samples in the range. Default is 1 sample. It has an effect of increasing sensitivity. |
analogSetClockDiv(attenuation) | set the divider for the ADC clock. Default is 1. Range: 1 to 255. |
analogSetPinAttenuation(pin, attenuation) | sets the input attenuation for the specified pin. The default is ADC_11db. Attenuation values are the same from previous function. |
adcAttachPin(pin) | Attach a pin to ADC (also clears any other analog mode that could be on). Returns TRUE or FALSE result. |
adcStart(pin), adcBusy(pin) and resultadcEnd(pin) | starts an ADC conversion on attached pin’s bus. Check if conversion on the pin’s ADC bus is currently running (returns TRUE or FALSE). Get the result of the conversion: returns 16-bit integer. |
Code – Simplified
const int analogReadPin = 26; // Potentiometer is connected to GPIO 26
int analogValue = 0; // To store the analog reading
void setup() {
Serial.begin(115200); //to begin the serial communication at the baud rate of 115200
delay(1000); // a small delay for our esp32 to respond and be ready
}
void loop() {
analogValue = analogRead(analogReadPin); //reading the analog signal value
Serial.println(analogValue); // displaying the analog value
delay(500); // taking readings after every 1/2 second
}
Conclusion
We have now learnt how to read analog signals with the ESP32. You could use different versions of the ESP32, but make sure you have the pin out of your ESP32 version. In several places you may even have to alter the code. For more tutorials click here