Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
156 views12 pages

ESP32 ADC & DAC Guide

The document discusses analog to digital conversion and digital to analog conversion using the ESP32 microcontroller. It describes how to read analog sensor values using the ADC and generate analog voltages using the DAC. Formulas to calculate digital values from analog voltages and vice versa are also provided.

Uploaded by

Keabetswe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
156 views12 pages

ESP32 ADC & DAC Guide

The document discusses analog to digital conversion and digital to analog conversion using the ESP32 microcontroller. It describes how to read analog sensor values using the ADC and generate analog voltages using the DAC. Formulas to calculate digital values from analog voltages and vice versa are also provided.

Uploaded by

Keabetswe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

DIGITAL SYSTEMS IV

S1 2024

Chapter5
ESP32 WROOM Analog to Digital
Conversion & Digital to Analog
Conversion www.vut.ac.za

1
ESP32 ADC – Read Analog Values with
Arduino IDE
Analog Inputs (ADC)

Reading an analog value with the ESP32 means you can measure varying
voltage levels between 0 V and 3.3 V.

The voltage measured is then assigned to a value between 0 and 4095, in


which 0 V corresponds to 0, and 3.3 V corresponds to 4095. Any voltage
between 0 V and 3.3 V will be given the corresponding value in between.

ADC is Non-linear

Ideally, you would expect a linear behavior when using the ESP32 ADC pins.
However, that doesn’t happen. What you’ll get is a behavior as shown in the
following chart:
This behavior means that your ESP32 is not able to distinguish 3.3 V from 3.2
V. You’ll get the same value for both voltages: 4095.

The same happens for very low voltage values: for 0 V and 0.1 V you’ll get the
same value: 0. You need to keep this in mind when using the ESP32 ADC
pins.

analogRead() Function

Reading an analog input with the ESP32 using the Arduino IDE is as simple
as using the analogRead() function. It accepts as argument, the GPIO you
want to read:

analogRead(GPIO);
The ESP32 supports measurements in 18 different channels. Only 15
are available in the DEVKIT V1 DOIT board (version with 30 GPIOs).
Grab your ESP32 board pinout and locate the ADC pins. These are
highlighted with a red border in the figure below.

These analog input pins have 12-bit resolution. This means that when you
read an analog input, its range may vary from 0 to 4095.

Note: ADC2 pins cannot be used when Wi-Fi is used. So, if you’re using Wi-Fi and you’re having
trouble getting the value from an ADC2 GPIO, you may consider using an ADC1 GPIO instead, that should
solve your problem.
Read Analog Values from a Potentiometer with ESP32

To see how everything ties together, we’ll make a simple example to read an
analog value from a potentiometer.

For this example, you need the following parts:

 ESP32 DOIT DEVKIT V1 Board


 Potentiometer
 Breadboard
 Jumper wires

Schematic

Wire a potentiometer to your ESP32. The potentiometer middle pin should be


connected to GPIO 34. You can use the following schematic diagram as a
reference.
Code

// Potentiometer is connected to GPIO 34 (Analog ADC1_CH6)


const int potPin = 34;

// variable for storing the potentiometer value


int potValue = 0;

void setup() {
Serial.begin(115200);
delay(1000);
}

void loop() {
// Reading potentiometer value
potValue = analogRead(potPin);
Serial.println(potValue);
delay(500);
}

This code simply reads the values from the potentiometer and prints those
values in the Serial Monitor.

In the code, you start by defining the GPIO the potentiometer is connected to.
In this example, GPIO 34.

const int potPin = 34;

In the setup(), initialize a serial communication at a baud rate of 115200.

Serial.begin(115200);

In the loop(), use the analogRead()function to read the analog input from
the potPin.

potValue = analogRead(potPin);
Finally, print the values read from the potentiometer in the serial monitor.

Serial.println(potValue);
Upload the code provided to your ESP32. Make sure you have the right board
and COM port selected in the Tools menu.

Testing the Example

After uploading the code and pressing the ESP32 reset button, open the
Serial Monitor at a baud rate of 115200. Rotate the potentiometer and see the
values changing.

The maximum value you’ll get is 4095 and the minimum value is 0.
ESP32 ADC – Calculations
ADC CHARACTERISTICS
• A transducer/sensor converts a physical quantity e.g. Temperature, to an
electrical (voltage, current) signal.
• Most sensor outputs are analogue voltages or current. An ADC is required to
translate this to a digital signal.
Resolution
• The ADC has n-bit resolution, where n can be 8, 10, 12, 16 or even 24 bits.
• Step size is the smallest change that can be discerned by the ADC.
• The higher the resolution the smaller the step size.
• Resolution cannot be changed, but the step size can be changed by changing Vref.

N-bit Number of steps Step size (mV) for (Vref=5V)

8 256 5/256 =19.53


10 1024 5/1024 = 4.88
12 4096 5/4096 = 1.2
16 65,536 5/65,536 = 0.076
ADC Calculation
𝑽𝒔𝒆𝒏𝒔𝒐𝒓
 𝑫𝒊𝒈𝒊𝒕𝒂𝒍 𝑽𝒂𝒍𝒖𝒆 = × ሺ2𝑨𝑫𝑪𝒓𝒆𝒔𝒐𝒍𝒖𝒕𝒊𝒐𝒏 − 1ቁ
𝑽𝒓𝒆𝒇
 𝑽𝒔𝒆𝒏𝒔𝒐𝒓 = 𝑽𝒐𝒍𝒕𝒂𝒈𝒆 𝒄𝒐𝒎𝒊𝒏𝒈 𝒇𝒓𝒐𝒎 𝒕𝒉𝒆 𝒔𝒆𝒏𝒔𝒐𝒓

 𝑽𝒓𝒆𝒇= 𝑽𝒐𝒍𝒕𝒂𝒈𝒆 𝒇𝒓𝒐𝒎 𝒕𝒉𝒆 𝒎𝒊𝒄𝒓𝒐𝒄𝒐𝒏𝒕𝒓𝒐𝒍𝒆𝒓

 𝑨𝑫𝑪𝒓𝒆𝒔𝒐𝒍𝒖𝒕𝒊𝒐𝒏= 𝒏𝒖𝒎𝒃𝒆𝒓 𝒐𝒇 𝒃𝒊𝒕𝒔

Suppose you are interfacing an external LDR sensor to the ESP32 WROOM 32's Analog to Digital
Converter (ADC). The LDR sensor outputs a voltage signal in the range of 0 to 5 volts. The ADC has a
resolution of 8 bits, and the reference voltage (Vref) is 5 volts. Calculate the digital value obtained from
the ADC when the LDR sensor outputs a voltage of 2 volts.

Given: Voltage output from temperature sensor (Vsensor): 2 volts, ADC resolution (bit): 8 bits, Reference
voltage (Vref): 5 volts
𝑽𝒔𝒆𝒏𝒔𝒐𝒓
𝑫𝒊𝒈𝒊𝒕𝒂𝒍 𝑽𝒂𝒍𝒖𝒆 = × ሺ2𝑨𝑫𝑪𝒓𝒆𝒔𝒐𝒍𝒖𝒕𝒊𝒐𝒏 − 1ቁ
𝑽𝒓𝒆𝒇

2 8
𝑫𝒊𝒈𝒊𝒕𝒂𝒍 𝑽𝒂𝒍𝒖𝒆 = × ሺ2 − 1ቇ
5
2
𝑫𝒊𝒈𝒊𝒕𝒂𝒍 𝑽𝒂𝒍𝒖𝒆 = × 𝟐𝟓𝟓ቇ
5
2
𝑫𝒊𝒈𝒊𝒕𝒂𝒍 𝑽𝒂𝒍𝒖𝒆 = × 𝟐𝟓𝟓ቇ
5
𝑫𝒊𝒈𝒊𝒕𝒂𝒍 𝑽𝒂𝒍𝒖𝒆=102
DAC (Digital to Analog converter) ESP32

What is DAC (Digital to Analog Converter)?


 A Digital to Analog Converter is a device that converts digital
values (0 and 1) to a set of continuous analog voltages.

 There are two types of methods which are converting digital


signals to analog signals one is a binary weighted method and
another is the R/2R ladder method.
 DACs are mostly used in DSPs (Digital Signal Processing), music
players, mobile phones, televisions, power supplies, etc
 ESP32 has two 8-bit DAC (digital to analog converter) channels
internally which are connected to GPIO25 (Channel
1) and GPIO26 (Channel 2).

 8-bit DAC means, ESP32 can convert the digital input (0 to 255)
to equivalent analog output.
 With 3.3Volt, our ESP32 will provide the 0-volt for digital 0 and
3.3volt for digital 255. However, in practical, output by DAC is a bit
lower i.e. max 3.25v for 255.
DAC (Digital to Analog converter) ESP32

Important DAC Functions for ESP32


dacWrite(pin, value)
This function is used to set the DAC value for a given pin or DAC channel.
pin: Set the DAC pin number
value: Set the value in the range of 0 - 255 (equals 0V - 3.3V).

dacDisable(pin)
This function is used to disable DAC output on a given pin or DAC channel.
pin: Set the DAC pin number
DAC (Digital to Analog converter) Calculation

The formula to convert the voltage to Analog value is:


𝑫𝒊𝒈𝒊𝒕𝒂𝒍 𝒊𝒏 𝒑𝒖𝒕 𝑽𝒂𝒍𝒖𝒆
𝑨𝒏𝒂𝒍𝒐𝒈 𝑽𝒐𝒍𝒂𝒕𝒂𝒈𝒆 𝒐𝒖𝒕𝒑𝒖𝒕 = × 𝑽𝑹𝒆𝒇𝒆𝒓𝒆𝒏𝒄𝒆
ሺ𝟐𝑫𝑨𝑪𝒓𝒆𝒔𝒐𝒍𝒖𝒕𝒊𝒐𝒏 −𝟏

𝑫𝒊𝒈𝒊𝒕𝒂𝒍 𝒊𝒏𝒑𝒖𝒕 𝑽𝒂𝒍𝒖𝒆 = 𝑫𝒊𝒈𝒊𝒕𝒂𝒍 𝒗𝒂𝒍𝒖𝒆 𝒊𝒏𝒑𝒖𝒕 𝒐𝒇 𝒎𝒊𝒄𝒓𝒐𝒏𝒕𝒓𝒐𝒍𝒆𝒓

𝑽𝑹𝒆𝒇𝒆𝒓𝒆𝒏𝒄𝒆 = 𝑹𝒆𝒇𝒆𝒓𝒆𝒏𝒄𝒆 𝒗𝒐𝒍𝒕𝒂𝒈𝒆 𝒐𝒏 𝒎𝒊𝒄𝒓𝒐 𝒄𝒐𝒏𝒕𝒓𝒐𝒍𝒆𝒓

𝑫𝑨𝑪𝒓𝒆𝒔𝒐𝒍𝒖𝒕𝒊𝒐𝒏 = 𝑵𝒖𝒎𝒃𝒆𝒓 𝒐𝒇 𝒃𝒊𝒕𝒔

Suppose you are interfacing a ESP32 with a DAC to generate an


analog voltage signal for controlling the brightness of an LED. The
DAC has a resolution of 10 bits, and the reference voltage (Vref) is 5
volts. Calculate the analog voltage output from the DAC when the
digital input value is 768.

 Given:
 Digital Input Value = 768
 Reference Voltage (Vref) = 5 volts
 DAC Resolution = 10 bits

 The formula to convert the voltage to Analog value is:


𝑫𝒊𝒈𝒊𝒕𝒂𝒍 𝒊𝒏 𝒑𝒖𝒕 𝑽𝒂𝒍𝒖𝒆
 𝑨𝒏𝒂𝒍𝒐𝒈 𝑽𝒐𝒍𝒂𝒕𝒂𝒈𝒆 𝒐𝒖𝒕𝒑𝒖𝒕 = × 𝑹𝒆𝒇𝒆𝒓𝒆𝒏𝒄𝒆
ሺ𝟐𝑫𝑨𝑪𝒓𝒆𝒔𝒐𝒍𝒖𝒕𝒊𝒐𝒏 −𝟏
𝟕𝟔𝟖
 𝑨𝒏𝒂𝒍𝒐𝒈 𝑽𝒐𝒍𝒂𝒕𝒂𝒈𝒆 𝒐𝒖𝒕𝒑𝒖 = ×𝟓
ሺ𝟐𝟏𝟎 −𝟏)
𝟕𝟔𝟖
 𝑨𝒏𝒂𝒍𝒐𝒈 𝑽𝒂𝒍𝒖𝒆 = X5
𝟏𝟎𝟐𝟑
 𝑨𝒏𝒂𝒍𝒐𝒈 𝑽𝒂𝒍𝒖𝒆 = 0.751x 5
 𝑨𝒏𝒂𝒍𝒐𝒈 𝑽𝒂𝒍𝒖𝒆 = 𝟑. 𝟕𝟓𝟓

You might also like