I often get questions about how to measure voltage with microcontrollers we will look at this topic at the quality of built-in and external analog to digital converters and I will show you some secrets so let's start pretty youtubers here is the guy with a Swiss accent with a new episode and fresh ideas around sensors and microcontrollers remember if you subscribe you will always sit in the first row voltages are analog and our controllers are digital this is why analog to digital converters short a DC's became so important they are the heart of digitalization the opposite by the way are digital to analog converters or DAC s they create an analog voltage out of a digital number in this video we will see how a DC's work look at how we can determine the quality of ADCs compare the a DC's of different microcontrollers with external chips learn how to tweak the calculations to get the best out of any ADC see how we can extend the range of ADCs discover some hidden things of microcontrollers and there a DC's look at different external boards and their usage in the pre-digital age transistors or even valves were very expensive and big so many different ADC converter principals were invented and used to save such components today with our silicon technology we do no more need to optimize for parts this is why a few exotic principals were left behind today we mainly use these two principals parallel comparator a DC's and successive approximation ADC s let's start with a parallel comparator a DC's they are fast expensive and not too accurate fast means they can convert signals up to gigahertz which is crazy if you ask me their main turf is software device and radios and other extremely fast processes if you are interested in what software-defined radio is you can watch video number 286 but how do these parallel competitor ABCs work they are quite simple and consists of only a few parts the first is a stable reference voltage this part by the way is vital for all a DC's then they divide the reference voltage into many voltage steps let's assume for simplicity we want to build a two bit ADC with a voltage range of 10 volts then we divide the 10 bolts into four steps of 2.5 volts we can do this with a simple voltage divider where we cut one resistor into like that we get voltages of 1.25 three point seven five six point two five and eight point seven five volts we now connect one input of a comparator to each of these voltages and connect the second input to the input voltage it is quite apparent what happens now a voltage of two volts for example is bigger than 1.25 volts so the first comparator triggers and all others not at eight volts three of four comparators trigger immediately when we apply the voltage if we add a few gates we get the two-bit signal at these pins of course all comparators have to read the signal at the same time otherwise we do not get accurate numbers for changing signals this is why we apply a clock to all comparators its most significant disadvantage is scalability for each additional bit of resolution we have to double the numbers of comparators a quick calculation for a 12 bit ADC shows 4096 comparators including the precision resistors and all the connections I would call this a nightmare for a chip designer and here we can discuss another important topic resolution and a see if we have a voltage of 1.3 volts we get a 1 if we apply 3.7 volts we also get a 1 we get such a staircase and see that this ADC has a resolution of 2.5 volts so it would not be suitable to monitor a lion battery for example we will need more pits to get more steps for this calculation I assumed that the resistors and the comparators are precise let's assume this resistor is a bit smaller than the others or this comparator does not switch at precisely 0 volts then this switching voltage would be smaller and we would get a 243 point 7 volts which would be wrong or not accurate you see there is a difference between resolution and accuracy resolution automatically increases with the number of resistors and comparators accuracy depends on their quality both are cost drivers for chips the more resolution or the more accuracy the more expensive not to forget speed which is also a significant cost driver the same thing applies if the reference voltage for example is only 9 volts the resolution would still be the same but the accuracy would be miserable do you think this is theoretical then look at the data sheet of the atmega328 used in Arduinos they use VCC as a reference and your USB power supply never delivers precisely 5 volts now we defined the voltage range the resolution and the accuracy of an ADC the next important thing is speed it is quite evident that for this ADC it mainly depends on the speed of the comparators plus the speed of the logic gates as said before parallel comparator a DC's go up to a few gigahertz an example is the 8090 208 which covers up to three gigahertz not bad also the price of $1700 for one chip is not bad you definitely pay attention to what you do if you work with one of those one remark if you read the data sheets of those fast ABCs you often see DP FS and not milli or microvolts this is because our f engineers mainly work with DB now we leave the expensive world of parallel comparator ADCs and continue with successive approximation ADCs
their function
in a way is very similar to what
we saw before the input signal is compared
to a reference signal what was done
in parallel before is now done sequentially
by only one comparator therefore
we have to supply it with an ever
changing reference signal and it takes
longer let's assume an input voltage
of 2.5 volts if our reference voltage
starts at 0 volt and increases slowly
at precisely 2.5 volts the comparator
changes it output signal if we
knew the reference voltage we would know
the input voltage simple if we use a
DAC to create the reference voltage we know
of course which output voltage it creates
because we apply the digital value
at its input we just have to transfer
this value to the output pins and
ready is our ADC nearly as before we have
to make sure that the value of the input
signal is stable during the whole measurement
otherwise the result would be
wrong and as said before the measurement
takes quite long this can be done
by a sample and hold circuit which is
mainly a capacitor and switch the switch
is closed till the ADC wants to read
a value and because the switch is closed
the voltage of the capacitor tracks
the input voltage as soon as the switch
opens the voltage at the capacitor
stays fixed at least if we make sure
that we do not discharge it with
our comparator another problem is quite
apparent if we assume we have a 12 bit
ADC for 10 volts and the input voltage
is nine point nine volts the resulting
digital value would be around 4050
our ADC would have to make 4049 wrong
in tests until the comparator would
switch from zero to one you can imagine
this would be extremely slow fortunately
we can do much better we can start
with five volts the nine point nine
volts are higher so the next value to
test is 7.5 volts still not enough next
eight point seven five and so on you
see the principal instead of more than
four thousand tests we will end with
maybe ten tests and this is basically
how these ABCs work just out of
curiosity I show you how a typical DAC
works it has a similar voltage resistant
etwork as the parallel a DC's with
two main differences the resistors are
arranged in a r2r ladder and instead of
a bunch of comparators we need a bunch
of switches this network creates a voltage
that is proportional to the binary
value applied to the switches you find
a link in the description if you want
to do the calculations in the end an
amplifier is added and ready is the DAC
and of course also here a voltage reference
but how about the resolution and
the courtesy of a successive approximation
ADC the same applies here the
longer the ladder the more resolution
of the DAC and with it also the
ADC of course the accuracy is mainly determined
by the accuracy of the DAC and the
comparator most of the ADCs we use
in our projects are of this type to save
money we can even go a step forward and
add a switch in front of the ADC like
that we get four eight or even 16 inputs
cool pay attention these chips
only have one ADC and therefore
can only convert one input at a
time so their speed is divided by
the number of analog inputs now we
know the important stuff about a DC's
let's apply this knowledge to the chips
in our lab and start with the Arduino
Uno the atmega328 has an 8 channel
10 bit successive approximation ADC
so it is the type we saw before 1 ADC
and 8 inputs it has a pin for a voltage
reference but because this pin is
not connected on the Arduino boards VCC
is used as a reference which for sure
is not optimal the Arduino has a built-in
voltage reference but only for one
point one volts with some tricks as shown
in video number 10 you can increase
its accuracy using this reference
if we divide 5 volts by 1024 available
steps we see that the resolution
is 4.9 VD volt the smallest difference
we can measure is around 5 millivolts
it's a courtesy is around 2.2 LSP
which is 2 point 2 times 4 point 9 equals
11 millivolt with an external voltage
reference without it it can be much
higher as we will later see its conversion
time is 65 to 260 microseconds
which is in line with what we learned
slow and depending on how fast it
hits the right value now we go on
with the esp8266 it also has a 10-bit ADC
without multiplexer so it has only one
input and essential its initial range
is only up to 1 volt which is vital
to know fortunately many board manufacturers
extend its range to 3.3 volts how
is this done and how can we use it
to extend the range even further they
use a simple voltage divider like this
one on the vemos d1 mini it reduces the
voltage by a factor of 3 let's now assume
we want to measure 24 volt our
panels for sure we want the marching and
set the maximum voltage to 30 volts we
use a voltage divider like that at an input
voltage of 30 volts we want an output
voltage of 1 volt for the ADC pin this
is the formula and if we enter the values
we get 2.9 mega ohms minus the 220
K already there we have to add 2.7 mega
and the remote should show the right
value of course it will not display
the proper value because the resistors
are not precise and maybe we do
not have the exact value this is why we
use a mapping formula we first apply 30
volts and note the value measured by the
ADC then we insert this value into the
formula and get adequate values the same
principle can be used for all other microcontrollers
I probably would not try to
measure about 50 volts with a simple
method because it can become dangerous
for humans and of course it only
works for DC and not for AC voltages
now we go on to the ESP 32 it also
has its secrets it has two different
12 bit ADC s with a total of 18
channels their quality gives rise to discussions
in my last video many people complained
about the in accuracy of this ADC
the datasheet has quite a lot to say which
usually is not good because the manufacturer
adds exceptions and prerequisites
we will see if this is true when
we lay to test it also keep in mind
that the ESP s have quite a strong RF
signal very close to the ADCs this
can easily influence its readings this
is why specifications are done with Wi-Fi
and Bluetooth off if you encounter issues
with the readings my first step would
be to switch off all radios and test
again another trick is used in the specifications
the addition of a 100 nano farad
capacitor this also reduces noise
on the analog line but of course it
also read measuring speed talking
about speed themaximum is two million samples per second
which translate in a conversion time
of 0.5 microseconds which is much faster
than the Arduino maybe this is partly
responsible for the inaccuracy unfortunately
this is not all in a small and
casual post Igor mentioned the fact that
all 10 pins of ABC 2 cannot be used if
we use Wi-Fi so it only has 6 pins left
GPIO 32 2 GPIO 39 fortunately some boards
connect GPIO 36 and 39 to sensor VP
and sensor VN so they should also work
with Wi-Fi on Aliexpress we find modules
with external ADCs the most common
one is the ad s1 1 1 5 breakout port
it has one 16 bit ADC with 4 input pins
and it is connected via I square see
it's sample rate is 860 samples per second
maximum and it has a specialty a so
called programmable gain amplifier short
PGA it can be used to increase the resolution
for smaller voltages we find a
bunch of data about the courtesy in the
datasheet and from the discussions before
we can now understand most of it and
see that this chip plays in a rather leek
than the integrated a DC's of course
you get other a DC's like the PCF 85
91 or the ad 7705 the PCF 85 91 is only
8 bits but it also has an 8-bit DAC so
if you want to control something and measure
the result this might be a suitable
module the 80-77 of 5 also is 16-bit
but only has two input channels and
is connected by the fast spi bus and is
primarily used for the digitalization of
fast signals let's now check the different
a DC's out I have here an exact
voltage reference which can
produce exactly two point five and
five volts let's start with the Arduino
Uno at 2.5 volts it displays 496 to
498 which is plus minus 1 LSB but is this
correct we map the values with this formula
and get mostly 2.5 or 2.5 1 volts
not bad but now I increase the supply
voltage of the Arduino to 5 point 2
volts which is still in the usb specifications
now it shows only 2.4 volts at 4.8
volts supply voltage I chose 2 point
6 volts interesting but of course not
good if I power the Arduino with 7
point 5 volts at the barrel check we
get quite constant 2 point 5 1 volts so
either you power your Arduino at precisely
5 volts or you watch video number
10 where I show how you can use the
internal reference to stabilize these
values next is the esp8266 it
definitely is less stable and has
glitches from 829 down to 821 but
it is faster than the Arduino so I
average across two values now we
get fewer twitches if I average across
10 values we still have glitches even
if I average across 100 samples we still
get an unstable signal but at least
we get 2 digits stable at 2 point 6
7 volts which is of course wrong you saw
now one way to reduce noise in a signal
by averaging and you saw how simple
the average formula is we quickly can't
correct the wrong voltage by adjusting
this parameter to 3.09 now we get
the desired 5 volts the chance all other
measurements are also correct is high
so let's check it with one bolt yes one
volt is also okay of course not more precise but at least no gain error we corrected it using this method if I connect the
AC rope in to ground it should
read zero volts but it reads 0.01
volts this is the small offset
error and we correct it here now
it reads zero volts of course we
should have corrected the offset before
the gain error with averaging and the
calibration formula we get at least the
maximum out of a particular ADC as promised
we want to extend the range to 30
volts we added 2 times 1 mega ohm resistors
are sufficient and the factor is
31.8 now our we mostly one mini can measure
30 volts not bad let's look at the
ESP 32 and do the measurements the raw
data looks scary indeed but let's look
at what we can do if we average across
100 values and adjust this factor to
3 point 6 1 we get a consistent 2.5 volts
of course we have to adjust this number
to the 12 bit of the ESP 32 average
over 1,000 values creates a quite
stable three-digit number but when we
go to one world we see the non-linearity
of the ESP 32 ADC it only shows
0.9 volts nearly 10% off so this
ADC has a high resolution of 12
bits but because it is very
inaccurate this resolution is useless
now we use a real ADC chip the ad
s1 1 1 5 connected to the ESP 32 we see
it is much more stable and it has four
bits more resolution impressive of course
it is also much slower if I average
across five values we hardly see the
noise but we do not need averaging here
it is good enough without only the fourth
stitch it moves a little I removed
the offset and adjusted its values
to precisely the value of the 5 volts
reference of course we have to
adjust this number to the 16 bits
of the ADC and the linearity at 2.5 volts
it shows two point four nine nine four
right on the reference overall
this ADC is much better than all the
built in a DC's tested before summarized
we saw how the most used a DC's
work and learned to determine their quality
compared to a DC's of an Arduino Uno
and to ESPs and saw that all of them could
not be used for serious work because
of their low quality we could apply
some tweaking to improve the results
we also extended the measuring range
of a WHMIS d1 mini to 30 volts we discovered
that the Arduino reacts on fluctuations
of VCC and how to avoid it a tiny
comment of Igor revealed that we could
not use most of the ADC pins of the
ESP 32 because they are used for Wi-Fi
stabilization we tested an ad s1 1 1
5 external ADC board and saw that it has
superior quality compared with all the
internal ADCs and it became clear that
we have to use external ADCs for serious
work one last thing this is my nicest
looking ADC in the lab it has eight
parallel channels and is quite fast
as always you find the relevant links
in the description I hope this video
was useful or at least interesting for
you if true please consider supporting
the channel to secure its future
existence thank you bye
No comments:
Post a Comment