Arduino UNO R4 Lesson 06. How to Read Analog Sensors? Potentiometer | Bits, Bytes, & Binary Numbers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone Welcome Back to educationist Life your go-to channel for unlocking the wonders of learning it is me Joe edgo and today we're diving into lesson six of the arduin Uno R4 Minima ultimate training series focusing on analog to digital conversion highlighting bits bytes and binary numbers in this lesson you'll fully understand how binary numbers are being used in digital devices through the use of a potentiometer you'll have a good graph as of how analog signals typically analog voltages or symbolize in digital form finally you learn how to write a program using W Loop construct to convert decimal numbers into binary numbers easily as always I'd like to extend my sincere gratitude to Sounder for sponsoring this course and enabling everyone especially beginners to embark on this comprehensive educational Adventure sanf founder has kindly donated this Arduino Uno R4 Minima ultimate sensor kit which will be our constant companion throughout the series so for those who are still searching for resources to Aid them in their Journey with the new Arduino Uno R4 I recommend you give this one a look this is available on the sandf founder website as well as on Amazon the links are in the description below so now let's begin for this session you need the following materials a 10 km potentiometer and a 1 km resistor however if you have your sandf founder kit you can simply use the potentiometer module here it is labeled as rotation sensor you'll also need eight LEDs with eight 1 kilohm current limiting resistors a breadboard some connecting wires and your Arduino Uno R4 Minima in our previous lesson you learned how digital input pins work in Arduino Uno you discovered how easy it is to use push button switches in your projects when working with digital inputs particularly you've seen that when a pin is configured as digital input the arduin Uno board will report high if it reads a voltage of about 3 to 5 volts and it will report low if it RADS a lower voltage somewhat close to 0 volts in digital circuits we simply deal with two states high or low true or false on or off logic one or logic zero digital circuits use transistors to form logic gates in order to perform Boolean Logic the microprocessors inside your computers smartphones microcontrollers and other Computing chips or digital circuits designed to operate in the digital domain depending on the digital logic family of the microchip you're using these logic levels of high and low is slightly vary for example TTL or transistor transistor logic is is one of the widely used digital logic families in the design of integrated circuits the acceptable input voltage range for a TTL device to be considered high or logic one is from 2 to 5 volts and to be considered as low or logic zero it should be within the range of8 to0 volts digital circuits use a binary system for digital signaling A system that assigns only two different logic levels for two different voltages a high voltage that is typically 5 volts 3.3 volts or 1.8 volts and a low voltage that is usually Zer volts however you must understand that we live in an analog world in which practically everything we see hear taste feel and smell is analog there are infinite colors that are indistinguishable from our site infinite tones that we can hear and countless fragrances that we can smell so when we we work on an electronic project like this where it needs to interact with the real world we have to deal with both digital and analog signals understanding how real world analog signals are converted and represented in digital form is very important microcontrollers like Arduino Uno although considered digital often have internal circuitry that allows them to interact with analog circuitry in Arduino when we talk of analog signals just think of them as a voltage that is changing over time arduin Uno has six analog to digital converter pins that can be used to connect to a variety of analog sensors to read analog voltage to demonstrate this concept let's start with a potentiometer potentiometers come in many shapes and sizes and are used in many parts of your daily life the volume setting on your car radio is a very common example this is the symbol for a PO tensometer it is a manually adjustable resistor with three terminals both terminal ends or fixed ends are connected to a resistive element and the middle terminal or the variable end is connected to an adjustable wiper so how exactly does it work the resistance between the outer two pins pin 1 and pin 3 will not change for example since we are using a 10 km potentiometer the resistance between pins 1 and three will always be 10 kilohms regardless of the wiper position now the control of a potentiometer is in the middle terminal pin 2 whose resistance varies depending on the potentiometer wiper position think of a potentiometer as a device containing two independent resistors R1 and R2 that always sum to a total resistance which is the potentiometer total resistance value in this case 10 kilohms notice that as I move the wiper position position in a clockwise direction or to the right R1 resistance increases as R2 resistance decreases similarly I can measure the R2 resistance and as I move the wiper position in a counterclockwise direction or to the left R2 resistance increases as R1 resistance decreases there are two common ways to use a potentiometer in your circuit it can work as either a realat just a variable res resor or as a voltage divider to use a potentiometer as a rat only two terminals are required the middle pin and one outer pin you can use any of the two fixed end terminals pin one or pin 3 to illustrate this let's implement the circuit diagram using an LED I'll connect its positive terminal to a 1 kilohm resistor in series and then I'll connect the variable terminal or pin two of the potentiometer in series with the 1 kilohm resistor to complete the circuit I'll connect the fiall source of the arduin Uno board to one of the fixed end terminals of the potentiometer say pin one then the ground of the board to the negative terminal of the LED so now using it as a variable resistor the position of the wiper determines how much resistance the potentiometer is imposing on the C it as I rotate the knob clockwise thereby changing the wiper position we can get a value from 0 to 10 kilm resistance at R1 in effect the LED gets dimmer as the resistance gets higher reaching 10 KMS since we also have a 1 km resistor here in series this makes a total of 11 kilohms on the other hand the LED gets brighter as the resistance gets lower to toward 0 ohm that is the reason why I placed a 1 km resistor here in series so that when the variable resistor reaches 0 ohm we still have a minimum resistance of 1 kilm to protect our LED from burning out we can also use the other fix and terminal pin 3 instead of pin one and it will have the same effect the only difference is the orientation of the knob with respect to 0 ohm and 10 kilohm here since we are now referencing R2 as I turn the knob clockwise the resistance on R2 decreases thus the LED gets brighter and as I turn the wiper counterclockwise R2 resistance increases and the LED gets dimmer now to use the potentiometer as a voltage divider all three terminals are required here voltage must be applied across the outer terminals pin 1 and pin 3 for this illustration I'll connect one of the outer pins pin 3 to the fol source or VCC and the other outer pin pin one to the ground or 0 volts here the center pin acts as the voltage output when a potentiometer is used as a voltage divider the wiper position determines the value of the output voltage in the middle pin but how this is the equivalent circuit diagram when you use a potentiometer this way it's like having two resistors in series and this in between Junction represents the center pin that is used to measure the voltage across the resistors in case you would like to refresh your knowledge of the voltage divided circuit please refer to the video links below I'll use a voltmeter for you to see how the circuit divides the 5 volt Source depending on the ratio of the two resistances as I change the wiper position as I turn the knob toward the fol terminal of the potentiometer the output voltage also increases and as I turn the knob toward the terminal connected to the ground the output voltage decreases understanding this simple concept is very important for you to grasp how microcontrollers such as Arduino Uno read analog signals and then eventually interpret them in your code these analog signals in the form of voltage that is changing over time could be coming from different sensors and this is what we are going to explore in many of our future lessons with the help of this Sounder ultimate sensor kit in place of the potentiometer circuit you can also use the rotation sensor included in the kit this sensor uses a 10 kohm potentiometer and a 2.5 kohm resistor in series with a middle pin however please note that this module already has a pinout arrangement of ground VCC and out from left to right for easy connection so I'll replace this potentiometer with the sunfounder rotation sensor and it is expected to perform the same now to read an analog signal from arduin Uno we can use any of the six analog input pins labeled a0 through A5 if we choose a z all we have to do is to connect the output voltage pin of the rotation sensor to pin a0 of the arduin Uno in our sketch we Define the sensor pin a0 then in the setup function we don't need to set the pin mode when reading a value from an analog pin here we just have to initialize the serial monitor for displaying the analog to digital readings later inside the loop function we can simply read the analog value from pin a0 using the analog read function and pass the analog pin a 0 as an argument this function measures the analog voltage from 0 to 5 volts at Pin a0 converts it into a digital value and returns it as an integer so I'll create an intype variable called ADC value and use it to assign the return value of this function and then using the serial. print function I'll display the text ADC value in decimal here I use the print function instead of print Ln it means that the cursor remains on the same line and does not move to a new line it is because on the same line I will print the actual ABC value before moving to the next line let's have a short delay of 50 milliseconds before it Loops back and reads again now I'll upload a code and see what happens notice that as I rotate the knob clockwise the analog raid function returns a digital value from zero all the way up to 1023 this is the converted digital value from the analog voltage ranging from 0 to 5 volts so this analog read function simply Maps the input voltages from 0 to 5 volts at Pin a0 into integer values from 0 to 1023 but y 0 to 1023 well this range of values depends on the resolution of of the analog to digital converters used by the Arduino board the default resolution for the Arduino Uno R4 Minima is 10 bits but it supports up to a maximum of 14 bits resolution so what is a bit and what is a resolution a bit is short for binary digit it is the smallest unit of data that can be represented on a digital Computing device it's a single unit of information with a value of either one or zero so if we only have one bit to represent something in the real world we only have two possible outcomes either one or zero if it is a light it could be bright or dim if it is a sound it could be loud or quiet if it is a texture it could be rough or smooth if it is an analog voltage it could be 5 volts or 0 volts there's nothing in between no variations there are no levels of granularity but then again in an analog world there are infinite possible values of sounds colors textures temperatures and everything and using only one bit to represent something from an analog world would be inaccurate that is why increasing the number of bits to represent analog signals would increase the Precision of the measured values for example instead of just one bit if we use two bits to represent the analog voltages from 0 to 5 volts we will have four POS possible outcomes or combinations 0 0 would represent 0 volts 01 would represent 1.67 volts 1 Z would represent 3.33 volts and 1 one would represent 5 volts if we use four bits to represent the analog voltages between 0 to 5 volts we will have 16 possible combinations which means more ways to represent information that is 00 to 11111 and this is how we count in a binary or base two number system similar to how we count on a base 10 or decimal number system that we have been very accustomed to since grade school the base also called radics determines how many different symbols are required to represent a number system in the decimal number system we've got 10 numeral representations of 0 1 2 3 4 5 6 7 8 and 9 and if we we exceed 10 counts meaning we run out of symbols after nine then we've got to reset all the digits to the right to zero and increase the left digit by one the same thing happens when we reach 99 these two digits reset to 0 0 and increments the third digit by one making it 1 0 0 or 100 and it goes on now for the binary number system we only have two numeral representations of 0 and 1 it means that after one it resets to zero and increments the left binary digit by one making it one Zer and Then followed by 1 one again since we run out of symbols we reset both bits to 0 0 and increase the left bit by one making it 1 0 0 and the counting goes on thus if we have four bits that is base 2 raised to 4 bits giving us 16 Counts from 0 0 up to 11 1 one1 and this is a direct equivalent of decimal numbers 0 to 15 for example 9 in the decimal number system is equal to 1 01 in the binary number system so as you noticed as we increase the number of bits to represent an analog value the more precise it can get and this is exponential for every additional bit the number of discrete counts or levels to represent analog signals doubles such that if we have five bits we have 32 discrete levels for six bits we have 64 discret levels for seven bits we have 128 discret levels and for 8 Bits we have 256 discrete levels that is two raised to the number of bits with this we can Define that the number of bits an analog to digital converter uses to digitize an analog input signal is called resolution resolution determines the Precision of a measurement the greater the ADC resolution the more precise the measurement values in Computing terms a group of eight bits is called a bite historically a bite is considered the smallest addressable unit of memory in many computer system architectures because it is used to encode A Single non-unicode Character a bite is used to represent a single character like a b or c thus 8 Bits can only represent up to 256 characters at most so going back arduin Uno R4 has a default ADC resolution of 10 bits that is 2 to 10 which means a 10bit ADC resolution would give us 1,24 different discret LEL lels this is symbolized in a decimal number system from 0 to 1023 now if we want to know the analog voltages that correspond to each of these 0 to 1023 discrete levels we can have a simple computation in our program that Maps the values being read first I'll declare a local float variable analog volts then map the ADC value being read by multiplying 5.0 volts the this is the maximum voltage and by dividing 1,23 this is the maximum ADC value and that's it then using the serial. print method I'll print analog input voltage column and on the same line I'll print the computed analog voltage with four decimal places now I'll upload a code and see what happens as as you can see the analog input voltage ranging from 0 up to 5 volts is displayed together with a converted ADC value in digital form for example an analog value of 1. 2463 volts is equal to 255 in decimal when using a 10bit resolution ADC note that this Precision can be changed with using a different resolution although the default is 10 bits we can set it as high as 14 bits and to do this let's define another constant resolution with a value of 12 so 2 to 12 which means that we can have 4,096 possible discrete levels to represent 0 to 5 volts analog signals that is 0 to 495 here instead of hardcoding 495 when we compute the analog volts I'll simply declare an INT variable Max Val and compute the maximum decimal number depending on the resolution that is 2 raised to the number of bits minus one for that I'll use the pow function the first parameter is the base and the second parameter is the exponent then subtract the answer by one it is like saying 2 ra to 12 = 496 - 1 = 495 now in the setup function I'll simply call the analog grid resolution and pass in the desired resolution also I'll change this hardcoded value of 1223 and replace it with Max Val variable and that's it I'll upload a code and see what happens as you noticed using a 12-bit resolution is more sensitive in Reading slight variations in the input voltage an input voltage of 1. 2491 for example is converted as 1,23 in decimal instead of 255 while 5 volts is converted as 4,95 in decimal now aside from this we can also display the converted binary data to do this first I'll declare another local variable of Type U 8core T bit count and assign the current resolution well basically this is the same as of type bite it is shorthand for unsigned integer of 8 bit length you can use the bite type if you want to and then I'll print ADC value in binary now there is a function called bit read and what it does is that it reads the bit of a variable this function accepts two parameters the first one is the number to be read so it will read the ADC value the second one is the bit position from which to read so for example if we have a decimal number 2023 the equivalent 12bit binary number number is 0111 1111 0 0111 if you're not that familiar with binary to decimal conversion and vice versa you can simply reference this table showing the power of two geometric sequence that is 1 1248 16 32 64128 256 512 1024 2048 and so on if you have more than 12 bits ideally you'll be able to convert fast if you memorize the sequence all you have to do is sum up all the values that correspond to logic one and ignore the values that correspond to logic zero so24 + 512 + 256 + 128 + 64 + 32 + 4 + 2 + 1 is equal to 2023 as simple as that here the least significant bit is the rightmost bit denoted as Bit Zero and the most sign ific bit is the leftmost bit denoted as bit 11 so if I call the bit read function and pass in 2023 and 11 it should return zero and if I call the bit read function and pass in 2023 and 10 it should return one although we can call this bitri function 12 times to display bit 11 down to Bit Zero that's not a very efficient way of coding it so to efficiently display all these bits at once we need to perform a loop we have several looping statements available in C language the first that we are going to use is the Y Loop similar to an if statement a y Loop evaluates a condition inside this parenthesis however in a y Loop while the condition is true the code inside the while block is performed and then Loops back so the algorithm to accomplish this is to start from bit 11 and then display each bit one by one on the same line until we reach Bit Zero so I'll have a condition that checks if bit count is still greater than zero at the start this is 12 greater than zero which is true thus it performs the code inside the loop now inside the loop I'll decrease bit count by one this becomes 11 and then I'll print the return value of the bitri function here I'll use the bit count as the second parameter so during the first iteration bit 11 gets displayed and then it Loops back to perform the same operation but for the second iteration bit 10 gets displayed so this y Loop gets to be executed 12 times until bit Z is displayed let's upload a code and see if it works and as you can see it works perfectly now let's test using a different resolution a smaller resolution of 8 bit so in this illustration you can see how an analog voltage from 0 to 5 volts is converted into an 8bit digital value from 0 to 255 for your challenge activity I want you to implement a similar circuit connection but with an additional 8le EDS connected at pins 0 to7 improve this program by sending the 8bit binary value to the 8 LEDs aside from just displaying it on the serial monitor this is not hard but it is a bit tricky as compared to the previous challenge activities as a hint you'll have to modify the Y Loop to accomplish this [Music] challenge [Music] [Music] [Music] [Music] [Music] again thank you so much for joining me in this lesson on analog to digital conversion highlighting bits bytes and binary numbers for our succeeding lessons we'll Explore More sensors and digital to analog converters if you found this lesson enlightening don't forget to hit the like button subscribe for more knowledge pack sessions and ring the bell to stay updated so keep learning keep experimenting and always remember education is life see you in our next lesson happy coding
Info
Channel: Education is Life
Views: 1,434
Rating: undefined out of 5
Keywords: potentiometer, ADC, analog, bit, bits, byte, resolution, analog to digital, arduino, arduino uno, minima, binary-to-decimal, binary to decimal, decimal-to-binary, decimal to binary, number system conversion, analogRead, analogReadResolution, bitRead, while loop, uint8_t, binary number system, decimal number system
Id: CCPfCgj8RkE
Channel Id: undefined
Length: 30min 19sec (1819 seconds)
Published: Thu Dec 28 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.