ADC blocks and REGISTERS | Internal Reference | Internal Temperature Sensor Arduino101

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
here we are with another episode of the arduino 101 series which if you remember are tutorials about arduino but not basic anymore here we learn some more in-depth stuff about this microcontroller and today we'll learn about all the blocks of the analog to digital converter or adc what registers we need to work with how to define the analog reference the sample rate prescaler select the inputs with a multiplexer and quite interesting how to read the internal temperature sensor because yes the arduino has an internal temperature sensor so basically we will learn what the annual grid function does when this video is over you should know what each of these blocks will do so guys let's get started these pcbs were manufactured by pcb way and the finish quality is very good especially with these gold-plated pads if you want to finish your product faster you could also ask pcb way to make a panelized order where you receive multiple pcbs on a single panel together with this you can also order the smd stencil from pcb way and use it to add solder paste and then solder all the components at once and by that you save time and effort all the orders are high quality and you could select a lot of settings such as the thickness flexible pcbs the color of the solder mask the amount of layers the material the surface finish and more so upload the gerber files directly on pcbway.com and make the order in just a couple of minutes what's up my friends welcome back we will use the arduino uno for the examples and if you want to know more about any other arduino model just check the same registers in the datasheet of the other microcontrollers remember that the atmega328p microcontroller has 8 analog inputs for the au version and 6 analog inputs for the pu version we use these inputs to measure voltage and pass from analog to digital values which in this case the arduino uno has a 10 bit adc so digital values from 0 to 1023 but there is a little bit more than the well-known anal grid function so let's dive in these are all the blocks that we use for the analog to digital conversion and by the end of this video you should know what each of this block will do here we have two main registers and we will learn what each of their bits will do in the code registers admox and the adc sra these blocks control the analog reference and in a moment we will see what is that this other block here controls the analog input from a0 to a7 and by the way if you didn't know the atmega328 has an internal temperature sensor that we could use and in a moment we will see an example now here is where the conversion is made and by setting these three bits for the prescaler we can change the sample rate of the adc conversion and finally here is the digital output divided into two bytes because the adc has 10 bits and a byte has only 8 bits so we need 2. ok so let's start step by step with each part and with examples for each one let's say that you want to read the analog input on a4 so you would do in the code something like this right but using registers it would be something like this basically the annual grid function already does this internally so let's see what each of these lines in the code is supposed to do first let's select the input of the adc we have just one adc conversion block so we need to select the input separately and make the measurement as you can see the multiplexer for the input is controlled with these 4 bits from mach0 to max 3. these bits are from the admax register so if these bits are 0 0 0 0 the adc input will be the a0 pin but if the bits are 0 0 0 1 it will be a1 0 0 1 0 it will be for a2 and so on as in this table so in the code if you want to read the a4 input for example we make this line admax or equal to zero zero zero zero zero one zero zero so now a4 is connected to the adc conversion block next we need to indicate the voltage reference but what is that well in order to make a conversion the adc needs to compare the analog input with a known value which by default is the microcontroller supply the vcc line otherwise you wouldn't know what you are measuring usually the vcc for the arduino is 5 volts so in this way the arduino knows that the maximum so 1023 would be for five volts and the minimum would be for zero volts so imagine that you make a measurement and the analog grid will give you a value of 614 let's say but what voltage is that well what we usually do in the code to pass from digital values to voltage values is to divide the analog reference voltage by the maximum digital value which is 1024 then we multiply that value by the analog grid which was 614 and we get a rate of 3 volts but this process implies that the reference voltage is 5 volts but i can assure you that this is never true the arduino board uses a voltage regulator to get 5 volts but this voltage is sometimes 4.9 volts and sometimes is more than 5 volts or a value in between but it's quite impossible to have a voltage exactly at 5 volts so that means that the analog grid we have made before could not be precise and another example is when you supply the arduino with a battery because as you know the arduino could work with no problems with voltages above 2.5 volts so for example we connect a 4.2 volts battery to the vcc pin and now the analog reference is not 5 volts anymore it's 4.2 volts so the code before would be affected for some more precise rates the arduino chip has an internal voltage reference that usually is more precise for the arduino uno we have a 1.1 volts internal reference but the arduino mega also has a 2.56 volts reference the reference could also be external and in this case it will be any voltage that you connect at the af pin of the board so if you connect for example 3 volts the reference would be 3 volts but in order to select these analog references we need to set them in the code as you can see here to select between these three reference inputs we need to use the re fs1 and the re fs 0 bits if both these bits are 0 that means that these max and this fet are disabled so the reference will be external from the aref pin if the refs 0 is a 1 then we can select between the vcc and internal reference with the re fs1 bit if the re fs1 is a 0 then the reference is vcc and if this bit is a 1 then the reference will be the internal 1.1 volts these two bits are from the admax register bit 6 and 7. so in order to select the internal reference in the code we make the ad max or equal to 1 1 0 0 0 0 000 so now those two bits are one one if you don't want to use registers you can also do these lines with this function in the code analog reference and then you add the type which could be any of these values depending on the arduino model ok so now we have the adc input selected and the reference as well all we need is to make the conversion which is controlled with the adc sra register to start the conversion we need to put the aden and the absc beats to 1. then we have to wait till the conversion is over and we will have the digital value on the adch and adcl bytes where h and l stands for high and low sides of the value so now in the code we make the adc sra or equal to one one zero zero zero zero zero zero and that will start the conversion but now how do we know when the conversion is done well we can make a while and inside this we can place if bit is set and select the adsc bit of the adc sra register because this bit will automatically go to zero when the conversion is over so while the adsc is a 1 the conversion is still going and finally our digital value is equal to the high side of the read plus the low side of the rate with a total of 10 bits so we make the sum of the low side with a high side but shifting 8 bits to the left so when we merge them together it will represent 16 bits and that's it that's how we make the android grid using registers so look i upload this code to the arduino and i get the analog grid on my monitor and this is the same if i were to use the analgrit function but this time is made with registers so we have learned something new but now let's see why you should use the internal voltage reference of 1.1 volts remember my smartwatch project in that video i was measuring the voltage of my battery in order to print that value on the screen because it's quite common for electronic devices to do that even your smartphone will show you the battery percentage right but this is the problem i want to measure the battery voltage but the analog reference is the battery itself because the battery is the supply of my arduino so let's say that the battery is charged to 4 volts for example so i make the adc conversion and the chip will compare the measured 4 volts with the analog reference which if it's defined to be by default the vcc value it will be 4 volts as well so the adc would give us the maximum value which is 1023 but let's say that after a while the battery voltage decreased to 3.8 volts so now we measure with the adc 3.8 volts but the vcc is also 3.8 volts so the annual grid will give us maximum once again so you see the problem without using an external voltage reference the arduino would think that the battery is always full but if i define in the code the adc reference to be the fixed value of 1.1 volts the voltage won't change its value with the battery so now we could measure the battery voltage while supplying the arduino with that same battery all we have to do is to add a voltage divider from the battery to the adc input in order to lower the voltage to below 1.1 volts because that is our maximum value if we use the internal reference so this would be the code if you want to read the voltage using the internal reference and a voltage divider of 1k and 4.7 k so here i supply my arduino with 5 volts from the power supply to simulate the battery and then i run this code in this case the reference is the default one which is vcc on the display i print the supply level and as you can see i start changing the supply but the value on the screen is always the same and that's not good but now i run this code where i define the internal reference of 1.1 volts i also multiply the rate by the inverted value of the voltage divider which is made with a 1k resistor and a 4.7 k now when i change the supply value the value on the screen changes as well so we have solved the problem but now remember that i've told you that the arduino has an internal temperature sensor if we take a look at the adc blocks that sensor is connected at the input 11. so if we set the ad max to have the max bits to 1 0 0 0 then the sensor is connected to the adc conversion block the sensitivity of the sensor is approximately 1 millivolt per celsius degree and this is a quite bad sensor with an accuracy of plus minus 10 degrees but anyway we make this code and we set the ad max or equal to 0 0 0 0 1 0 0 0 so now the sensor is connected to the adc according to some internet values we have to divide the annual grid by 1.22 and also subtract the offset and this will be the temperature in celsius degrees so i upload this code and run the serial plotter and as you can see when i hit the chip the temperature goes up and that's it you could use this to monitor more or less the ic temperature okay so one more thing if you want to change the sample rate of the adc you can do that by changing the prescaler that is controlled with the adc sra register as well with the first three bits so according to this table you can set the prescaler to be from 2 up to 128. the arduino is running at 60 megahertz so for example if we set the adc sra to be or equal to 0 0 0 0 0 1 1 1 so the prescaler is 128 the sampling rate will be 16 megahertz divided by 128 so 125 kilohertz and by the way the maximum sampling rate of the arduino adc is 200 kilohertz so now you know how all these blocks work and which register you need to control in order to make the conversion also why the internal reference is so important i hope that you have learned something new and if so give me a like or comment below in order to help my channel thanks again and see you later guys hey guys so we are at the end of this video so some of you guys are supporting me on patreon and thank you very much for that because thanks to you i'm able to buy all these components and the modules that i use for my tutorials and if you would like to support me as well you have the links for my patreon for my website and my shop below in the description thank you for everything
Info
Channel: Electronoobs
Views: 28,850
Rating: undefined out of 5
Keywords: ADMUX, ADEN, ADCSRA, ADSC, REFS0, INTERNAL1V1, internal, reference, voltage, ADC, analog input, registers, temperature sensor, AREF
Id: ettV5zorZfQ
Channel Id: undefined
Length: 14min 41sec (881 seconds)
Published: Sun Jul 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.