0:09
hello and welcome to controller Tech
0:12
this is the eighth video in the esp32
0:15
series using expressive ID we have
0:18
already covered the uart i2c and SBI
0:22
peripherals so now we will see the ADC
0:26
es32 we will cover the ADC peripheral in
0:30
two videos today's video will cover the
0:33
oneshot conversion mode where each
0:35
channel can be converted
0:37
individually and in the next video we
0:40
will cover the continuous conversion
0:42
mode I am using the es room 32
0:46
development board which you can purchase
0:48
from the controller's teex store here I
0:51
am using the one with 38
0:54
pins let's see the pin out of this board
0:58
you can see there are a lot of pins that
1:00
support the analog mode basically we
1:03
have two adc's adc1 and ADC 2 each ADC
1:09
has a lot of channels and each channel
1:12
is assigned one pin here you can see ADC
1:15
1 channel 0 3 6 7 4 5 and 8 then we have
1:21
different channels of
1:23
adc2 there are a few more channels of
1:25
ADC 2 on the right side I am going to
1:29
use the two channels of adc1 Channel 0
1:32
and 3 the pins assigned to these
1:35
channels are VP and VN so I am going to
1:39
connect the potentiometers to these two
1:42
pins you can use other channels as well
1:45
just try to avoid the pins marked with
1:48
red color these are the strapping pins
1:51
and they are used during flashing the
1:53
board you can use them but they require
1:56
some additional configuration which we
1:58
will avoid at this time
2:00
there are other available pins so better
2:03
use them let's see the connection now
2:08
here is how the potentiometers are
2:10
connected to the board both the
2:12
potentiometers are powered with 3.3
2:15
volts from the board itself I have
2:18
connected the output pins of the
2:19
potentiometers to the ADC Channel 0 and
2:25
VN before we start writing the code
2:28
let's see the doc documentation
2:31
available here I have the es 32 docks
2:34
for the latest table version under the
2:37
API reference peripheral API you can see
2:41
the ADC one shot mode driver basically
2:44
the ESB 32 supports two different modes
2:47
for the ADC in one shot mode only one
2:51
conversion is performed once whereas in
2:54
the continuous mode the ADC keeps on
2:57
converting the desired number of
3:00
continuously in today's video we will
3:03
check the oneshot mode Let's Open the
3:06
Espress if ID and create a new project
3:10
we will create the project using the
3:12
templates so search for the ADC here
3:16
here we have the examples for oneshot
3:18
mode and continuous mode Let's select
3:21
the oneshot mode rename the project and
3:25
select finish all right here we have our
3:28
project which we will modify but let's
3:30
build the project first we will write it
3:33
an entire code from scratch so let's
3:36
remove this pregenerated code I am
3:39
leaving the calibration related
3:44
only let's remove this entire while loop
3:52
also this is again the calibration
3:55
function so leave this one remove this
3:58
to initialization part though all right
4:01
let's write the code now I am going to
4:04
configure two channels so the ADC raw
4:07
array should have two elements and so
4:10
does the voltage array let's define an
4:12
array of ADC channels that we will be
4:15
using in this tutorial I am using ADC
4:19
Channel Z and channel 3 now we will
4:22
Define the ADC onot handle which will be
4:25
used throughout the program
4:32
these calibration handles are defined
4:34
inside the main function so let's define
4:37
them globally the calibration handle is
4:40
defined for each channel so if you are
4:42
using six channels you need to Define
4:45
six handles here now we will Define a
4:48
new function to initialize the
4:50
ADC the parameters of this function will
4:53
be the pointer to the ADC channels and
4:56
the number of channels we want to use
4:59
you can refer to the ESB 32 ADC docs to
5:03
initialize the ADC they have provided
5:05
examples to configure everything we can
5:08
also refer to the ADC onshot header file
5:11
and configure everything provided in it
5:15
here first of all we have the oneshot
5:19
configuration the first member of this
5:21
structure is the ADC unit esp32 supports
5:25
two adc's adc1 and adc2
5:30
we need to declare which unit we are
5:32
using the next member of the structure
5:35
is the clock Source by default the
5:37
oneshot mode uses the RTC clock source
5:41
so if we leave the source clock to
5:42
default it will choose on its own the
5:45
next member is the ultra low power mode
5:49
by default the ulp mode is disabled so
5:52
we will not configure this too that
5:55
means we only need to configure the ADC
5:58
unit inside inside the ADC
6:00
initialization function we will Define
6:02
the onshot initialization structure here
6:06
we will just declare the unit ID is
6:09
adc1 the rest of the configuration is
6:11
kept to default after defining the
6:14
configuration call the function one shot
6:17
new init and pass the configuration to
6:19
this function the second parameter of
6:22
this function is the pointer to the ADC
6:25
handle we Define globally let's go back
6:28
to the one shot heading file the next
6:31
structure is for configuring the channel
6:34
the first member of this structure is
6:37
attenuation the ADC attenuation is
6:40
basically used to reduce the input
6:42
voltage before being input to the ADC
6:45
the higher the attenuation the more the
6:48
voltage range we can measure esp32
6:51
supports a maximum attenuation of 12 DB
6:55
which allows it to measure the voltage
6:56
from 150 MTS to 24 450
7:01
Ms you can find more details about the
7:04
attenuation in the ESB 32 data sheet go
7:08
to the ADC section under the analog
7:11
peripherals here you can see the table
7:14
showing how different attenuation
7:16
configuration changes the measurable
7:18
voltage range we want to measure up to
7:21
3.3 volts so we will use the attenuation
7:25
value of three if you want to measure
7:28
higher than 3.3 vol volts you need to
7:30
set up a voltage divider circuit which
7:32
allows only up to 3.3 volts to the
7:36
ADC we will discuss it in some other
7:39
video so we will configure the
7:44
DB the next member of the structure is
7:47
width this is basically the resolution
7:50
of the ADC channel the higher the
7:53
resolution the more precise the result
7:55
will be but it also takes more time to
7:58
convert a higher res resolution channel
8:01
the table shows how the ADC values will
8:06
resolutions I am going to use the 12-bit
8:08
resolution so the ADC values will vary
8:15
4,95 if you set the bit width to default
8:18
the driver will automatically select the
8:22
resolution all right we will now Define
8:24
the channels configuration inside the
8:28
function first we will set the
8:30
attenuation to 12 DB and then set the
8:35
bits this is the channel configuration
8:38
and therefore we need to configure both
8:42
configuration I want to use the same
8:44
configuration for both channels
8:46
therefore I am calling the channel
8:48
configuration function inside the for
8:51
Loop the first parameter of this
8:53
function is the ADC onot handle then we
8:56
have the channel which we want to
8:58
configure and find finally the pointer
9:01
itself that is all we have in the onshot
9:04
header file there are only two
9:06
configurations needed to initialize the
9:09
oneshot driver we can use the oneshot
9:12
read function to read the converted
9:14
Channel at any point in our
9:16
program let's copy this calibration
9:19
function inside the initialization
9:22
itself we will first read the data
9:25
without calibration so that we can
9:27
compare it with calibrated values later
9:30
therefore I have commented out the
9:33
functions all right let's write the main
9:36
function now here first call the
9:39
initialization function to initialize
9:42
ADC we have already defined the channels
9:46
and we want to convert two of them now
9:49
inside the while loop we will read the
9:51
channels convert them to voltage and
9:54
print the output on the
9:55
console the function ADC one shot read
9:59
is used used to read the ADC channel the
10:02
parameters of this function are the ADC
10:05
handle the channel that we want to
10:07
convert and the pointer to the variable
10:09
where the converted data will be
10:12
stored after retrieving the data from
10:14
the channel we will convert it to the
10:17
voltage for 12 bits resolution a value
10:20
of 4,000 95 corresponds to 3,300
10:26
Ms this is because we are powering the
10:29
potential iometer with 3.3 volts
10:31
therefore it is the maximum voltage ADC
10:34
can measure so we will use the direct
10:37
ratio to convert the ADC value to the
10:41
voltage now we will print the raw value
10:43
and the voltage value for each channel
10:46
on the terminal both the channels will
10:48
be converted and printed and then we
10:50
will give a delay of 500 milliseconds
10:54
conversion all right that is all let's
10:57
build and Flash the project to the board
11:01
let's open the terminal to see the data
11:04
console here are the two potentiometers
11:06
connected to the ESB 32 just like I
11:09
showed in the connection diagram the
11:12
rotary pot is connected to Channel Zero
11:15
and the multimeter is also connected to
11:17
this pot the sliding pot is connected to
11:20
channel 3 right now both the readings
11:24
are zero I am sliding the potentiometer
11:27
now you can see the channel 3 readings
11:31
increasing now the potentiometer is at
11:34
the extreme end the raw value is
11:38
4,095 and the voltage is
11:42
Ms now I am bringing it back the raw
11:46
value is reducing and so does the
11:49
voltage the pointer is near the middle
11:52
now the raw value is around
11:55
1,700 and the voltage is 1,400
12:00
M let's leave it here and now we will
12:03
vary the rotary pot which is connected
12:05
to Channel Zero keep an eye on the
12:08
multimeter as well you can see both the
12:11
raw value and the voltage are increasing
12:15
the voltage value printed at this point
12:17
is around 900 molts but the multimeter
12:20
is showing 1.03 volts so we have a
12:24
difference of around 140
12:27
Ms let's keep Rota in the
12:30
potentiometer now the voltage value
12:32
printed is around 1,700 Ms but the
12:36
multimeter is showing around 1.85
12:39
Vols again we have a difference of
12:42
around 140 Ms between these values let's
12:48
potentiometer the printed voltage is
12:51
around 2,700 molts whereas the
12:54
multimeter is showing around 2.8 Vols
12:58
now the potential iometer is at maximum
13:01
the voltage values are also at maximum
13:05
so we can measure the voltage using the
13:07
ADC which is approximately the same as
13:10
voltmeter now we will use the
13:13
calibration function to calibrate the
13:14
ADC and see if the data is more accurate
13:19
this function was part of the template
13:21
itself basically there are two
13:23
calibration schemes in ESP
13:26
32 some of the boards support curve
13:29
fitting calibration scheme while the
13:31
others support line fitting scheme you
13:34
can get more details about the
13:36
calibration in the sb32 docs itself I am
13:40
skipping this part and we will go to the
13:42
code directly after initializing the
13:45
channels we need to calibrate them so
13:49
let's include the calibration functions
13:53
initialization the unit ID is
13:56
adc1 we will pass the first channel of
13:59
the array here the attenuation is 12 DB
14:02
and the handle is already
14:04
defined similarly we will calibrate the
14:07
second channel of the array so we are
14:10
calibrating both the channels in the
14:12
initialization function
14:16
itself inside the while loop we will
14:19
first read the Channel Zero of the
14:22
ADC after reading the raw value we will
14:25
call the calibrated raw to voltage
14:28
function to convert the raw data to
14:30
voltage we need to pass the raw value
14:33
and the address of the voltage variable
14:36
parameters similarly we will read the
14:39
channel 3 and convert the raw value to
14:42
voltage now we will print both the
14:45
channels data on the
14:46
console that is all let's build and
14:49
Flash the project to the board
14:59
here you can see the output saying the
15:01
calibration scheme is line fitting and
15:03
the calibration was successful it was
15:06
printed again for the second channel
15:09
it's basically these logs getting
15:11
printed on the console now we will see
15:14
the voltage output here and on the
15:16
voltmeter also all right let's rotate
15:20
the potentiometer connected to Channel
15:22
Zero the voltage value printed is around
15:26
2,250 Ms and the volt also shows voltage
15:30
around 2.2 Vol I am rotating it in the
15:34
other direction now the printed value is
15:39
1,590 Mt whereas the voltmeter shows
15:42
around 1.56 Vols I am reducing it
15:47
further the printed voltage is around
15:50
555 MTS whereas the volt meter is around
15:54
0.54 Vols we have an error of around
15:58
only 30 m Vols after calibration which
16:00
was around 130 Ms before calibration
16:05
this much error is acceptable as ADC
16:07
values also fluctuate a lot you can read
16:10
multiple ADC values and then take the
16:13
average to reduce the error further the
16:16
second potentiometer is also responding
16:19
fine so we were able to read multiple
16:22
channels of the ADC using the oneshot
16:25
mode we also saw how calibration helps
16:28
reduce the ADC ERA this is it for the
16:31
video we will cover the continuous ADC
16:34
reading in the next video you can
16:37
download the project from the link in
16:39
the description leave comments in case
16:42
of any doubt keep watching and have a