Read Analog Voltage with the PICO

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
konichiwa as my japanese friends would say welcome to making stuff with chris day-hut today we're going to take a look at the analog to digital converters on the raspberry pi pico what we're going to cover today we will look at which pins are the analog to digital converters on the raspberry pi pico we'll take a look at how to wire up a typical device such as a potentiometer and then we'll look at that in micro python with an example program so we'll dive right in on a one of the drawings or sketches that is in the getting started with the raspberry pi pico manual and this is the one that shows the pin diagram uh for all the i o devices on your pico now what we're looking at specifically would be the adc output pins or input pins adc stands for analog to digital converters and the pico has four of them there are three that are brought out to pins for us to use and they are labeled adc 0 80c 1 and adc 2. number 4 goes to an onboard temperature sensor that's on the raspberry pi pico so we don't have access to that one but nonetheless uh for today what we're going to be using is pin number 31 which is gp general purpose pin 26 and it is analog to digital converter number zero in order to get a variable voltage for which to experiment with we're going to use one of the typical devices that you would use with an adc channel and that's a potentiometer and that's what's shown here on the fritzing diagram wiring it up is very simple you'll have three terminals on a potentiometer one of them at the we'll say in this example the left end one we will run up to the ground pin of the pico the opposite end in this case the far right one we will wire up to our 3.3 volt output and that's on pin 36 the middle connector the middle terminal is connected to the pico's analog to digital input in this case we're using pin 31 which is general purpose 26 in analog to digital channel or analog to digital converter channel number zero wiring them up is very simple and this is very typical of other sensors that would be using an analog output now uh in our case uh we're using a potentiometer and that would typically be used on a project to yours where you want to have infinitely variable amount between fully off and fully on typical digital inputs are simply on or off either 0 volts or 3.3 volts with an analog signal we're looking at anything between 0 volts and 3.3 volts so you get a very wide range of input values from one control device in this case such as our potentiometer now there are many devices that utilize an analog output signal and that would include certain temperature sensors pressure sensors load meters flow meters various other things such as uh an analog matrix keypad that we've done a video on that'll be here on the channel as well pretty soon so there's a lot of uses for analog to digital conversion so we're going to take a deeper dive into that and look at it here on the breadboard real quick before we go into the python code here we are wired up exactly as we would see it on the fritzing diagram um here are our three leads our three terminals on our potentiometer and in this case i'm using a 10k potentiometer 10k is a very good choice because it really restricts the amount of current that can go to the input and it gives us a very good fine resolution between 0 and 3.3 volts there are two varieties of potentiometers that you will find if you're buying them there are the linear taper meaning that from 0 to 3.3 volts your graph would be a straight line from low to high the other type would be an audio or a log style and it would be logarithmic so it'd be a curve so instead of going straight up at a line from 0 to 3.3 volts it would go very gradually up and then accelerate rapidly and then slow down as it's getting near the high voltage side for most applications that we would do we would use a linear potentiometer the log or audio types are really more often used in audio equipment now i've also hooked up a volt all meter so that we can monitor the voltage coming out of the potentiometer the purpose of the potentiometer in this circuit is to create a variable voltage output so it's wired up as what is commonly referred to as a voltage divider we're giving it a ground reference and 3.3 volts and we're monitoring the wiper position which will be at a voltage other than zero or 3.3 volts anywhere mid travel so we'll monitor that here on the voltmeter and if i turn it all the way down to zero we've got zero volts go all the way up and 3.19 volts as you can see there's a little bit of a bias offset towards the zero volt side and that is not uncommon now let's take a look at the example program we're going to use for reading uh the analog to digital converter information there's a few notes at the top telling us what this program does um we've already covered uh which adc channels are available for us to use that are on the pico and this uh summarizes it here this next bit of information these three lines i would like to talk a little more about um the analog to digital conversion uh is restricted the value that it will feed to you is restricted by the number of bits of resolution they've engineered into the microcontroller in the case of the raspberry pi pico it is limited to 12 bits of data to handle the value so that value would range from 0 to 4095 that's the largest number that can fit in 12 bits of data however micro python will take that 4095 and map it up to a 16 bit value and it'll do a conversion for us so that we get zero to six thousand sixty five thousand five hundred and thirty five discrete values within that full range and that's uh beneficial if you work with different microcontrollers using micro python your code would be more portable because we're always looking at that 16 bit range which is very common across many different microcontrollers 10k linear taper pots or potentiometers are really about your best choice for manually putting input into an adc such as we're doing here connections just as we described in the fritzing diagram same thing mentioned here now we're down to the actual program to read and tell us a value for an adc it's really very simple we're going to import the machine library which gives us access to the hardware pins of the pico and we're going to import the micro time version of the time library so we can put a sleep in here to slow down our loop we're going to create an object called pot short for potentiometer and that object will be a machine uh analog to digital converter signal and it'll be the channel 0 you have available to you 0 1 and 2. we're going to go into an infinite loop as typical of all uh microcontroller programs and we're going to read that pot value and that's done with uh the assignment of a value to a variable in this case the variable will you we will use is a underscore val equals pot dot read underscore 16. the underscore 16 denotes that we're going to get a 16 bit value or a range from 0 to 655 35. we'll print that value and then we'll sleep for a tenth of a second or 100 milliseconds and then repeat that way we don't get too much data running by here that we can't see as we're demonstrating this so we'll turn on the voltmeter at this time you will notice that we are all the way counter clockwise on the pot i've got zero volts showing up here and in our program we're reading a value of some are about 128 to 144 and you'll that's a typical amount of float you'll get in an analog to digital read um now it's not going to always go back to zero up to a full 655 35 range uh potentiometers have tolerance in them and that tolerance keeps us from sometimes not getting to the full extremes of their rating so now at zero volts we're getting a very low value right around 128 pretty consistently so we'll turn the potentiometer up and as you'll see my voltage is following it going up and will go all the way to the max and in this case the max voltage i'm getting through this is 3.168 volts very close to 3.3 volts and the value that we're getting from the analog to digital converter is 65 30 240 right in there so that's very close to the max value that we could read and that's how you kind of get to understand that 655 535 would be your max voltage if we turn it down back to as low as voltage as i can get which is showing up as 0 volts we're reading a 128 now right in the middle of that 65 500 would be 32 000 and change right so we'll turn this up to about 1.6 should be close to about halfway of between three point three and zero um and we're at uh right on the edge of 32 33 000 for our value and that's how you can correlate between a low value a high value and the values in between you have really infinite adjustment on that input between 0 and 3.3 volts and we can extract any one of these numbers as a value we would use in our program to perform an action or to compute another value in fact if you take a little time to calibrate this routine by maybe adding an offset to it so that exactly 3.3 volts is giving you the value of 65 535 you will get very precise voltage to digital conversion to obtain a voltage value so you can do a lot with this data analog signals are wonderful things they can be a little tricky in noisy circuits such as those that are driving small hobbyist dc motors and so forth where you get a lot of static we'll call it uh that can really sway some of your analog to digital conversion values so you'll have to try it see how it behaves and then if needed you may have to dive a little deeper into ways to moderate that through additional components in your circuitry so this pretty well wraps up how to get and use get analog values into a microcontroller into a data format a number in this case that we can do uh calculations with or perform actions on that's going to wrap it up for this video hopefully you've enjoyed it if so please subscribe and if you liked it uh maybe give us a like on the on the rating system here i'd like to point out that there's about 50 or 60 videos in this whole series on the raspberry pi pico in micro python and we're covering a lot of different uh devices that you interface to the pico and items such as this analog to digital threading pulse width modulation all broken down in a nice simple uh presentation such as this one well that's it for this one and i look forward to seeing in the next video
Info
Channel: Making Stuff with Chris DeHut
Views: 7,797
Rating: undefined out of 5
Keywords: Analog, Analog voltage, Analog signal, Raspberry Pi Pico, RP Pico, Microcontroller, Electronics, Electronic components, Python, Micro Python, Slide Switch, machine, Interface, Real world computing, Maker, Making, Chris DeHut, POT, Potentiometer
Id: X5Q-9ieLZPE
Channel Id: undefined
Length: 14min 25sec (865 seconds)
Published: Fri Aug 19 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.