Raspberry Pi LESSON 15: Analog Input on the Raspberry Pi Using the ADC0834

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello guys this is paul mcquarter with toptechboy.com and we're here today with episode number 15. in our incredible new tutorial series where you're teaching your raspberry pi who's boss what i'm going to need you to do is pour yourself a nice tall glass of ice cold coffee that is straight up black coffee poured over ice no sugar no sweeteners none needed and as you're pouring your coffee as always i want to give a shout out to our friends over at sun founder sun founder is actually sponsoring this series of video lessons and in these lessons we are using the most excellent sun founder ultimate raspberry pi starter kit now hopefully most of you guys already have your gear if you don't look down in the description there is a link over to amazon where you can hop over there and pick a kit up now believe me your life and my life are going to be much much easier in this class if we are working on identical hardware so if you don't have your kit yet go ahead order it now and then you will be able to follow along with exactly the same hardware that i have but enough of this shameless advertising let's talk about what i am going to teach you today well before telling you that let's think a little bit about what we've learned so far what we've learned is how to flash uh sd card with the operating system we've learned how to configure the desktop so it's not quite as lame as it looks when it comes right out of the box we get the desktop configured to be a little bit more useful we learned how to run linux we've learned the essentials of linux we've learned the essentials of python and then we started playing with those gpio pins on the raspberry pi now that is what we want to talk about what have we learned to do we have learned to do digital writes to the gpio pins we've learned how to do digital reads from the gpio pins we've learned how to do analog out to the gpio pins what's missing what's missing hand log in to the gpio pins and so that is what we are going to talk about today now this is the problem the problem is the raspberry pi does not have any analog in gpio pins and there's no way for us to trick one of the gpio pins into acting like an analog end pin if you remember back in the days of arduino there were like five or six dedicated analog end pins and all you had to do was just go out and do a read value from those pins and you had your analog value it's not that easy on the raspberry pi and i'm really surprised that with all the advances that raspberry pi has made in the computational power of their boards that they've never gone in and added this what i consider to be really really really needed feature but never fear i'm going to show you how you can do it and the way we're going to do it is we are going to do it using an analog to digital converter chip all right now don't be fearful because luckily enough your sun founder kit has an analog to digital converter in it and that's what i'm going to kind of show you right now let's go ahead and see if we can find some of the equipment that we will be needing for today's lesson so let's get our kits and open them up and for me up here in the upper left you can see there is this little sack of electronic component goodness and if you look there you can see that there was one two three four dual inline packages well for me the adc 0834 which is the analog to digital converter was right here now yours might not be there but it looks like this it is a 1410 dual inline package now unfortunately these are labeled in very dark gray text on a black background and labeled very very small so the labels are hard to read but what you need to do is you need to look and you need to get the chip that says on it right here adc0834 and that is the chip that you want to use all four of those are different make sure that you get the correct chip okay also that this build is getting complicated enough that we need to go ahead and start using the most excellent sun founder gpio expansion board that comes in the kit and you can find it it's sort of t-shaped and then it has 40 pins and each one of the pins is labeled so what we're doing is we're taking the gpio pins from the raspberry pi and we're bringing them over to our breadboard and then all the connections at this point are just made on the breadboard okay hopefully that makes sense and then also you will find this 40 uh this 40 uh cable connector here this ribbon cable that has 40 wires on it and that will allow you to connect your gpio expansion board over to your raspberry pi now this is where i need to tell you something that's very important and that is when you connect this ribbon cable to the raspberry pi you have to be very very careful and very very deliberate and you do it with both thumbs you align the holes to the pins when you're sure they're aligned you press gently and then firmly at the same time with your left and right thumbs on the left and the right of the connector you want it to go in together and as i was teaching students in my real classroom live what i would see over and over and over they would press on one side and then the other and what that does is that torques and bends the gpio pins and you know how expensive these raspberry pi's are these days you don't want to damage your gpio pins on your raspberry pi so you want to press them down together and similarly when you take the cable off you want to get your fingernail under the connector at the right under the connector at the left and pry it off at the same time because it's really even easier to damage the pins taking it off and if you pull from one side and then from the other it's going to bend all those pins and once they're bent it is very very hard to ever get them back straight again and same thing on this side if you take this off you got to be really careful and also when you take this off of the board you don't want to bend those pins on the expansion board so when you take things with lots of pins off or on you want to do it parallel between the left and the right hopefully that makes sense so you want to get this basic equipment put together here and now we're going to jump over and start talking about what this does and how to get it working and this will be a little bit of a diagram here and what i've got is i've got the whole i've got the whole pin out up here that we are trying to the whole schematic that we are trying to get hooked together and you can see here is the adc 0834 analog to digital converter and let me talk about this a little bit because i don't think in this class we've used a dual inline package before first thing when you plug it in again make sure that you get it all the way in that with that many pins you've got to make sure that it is all the way into the board and now how do you know whether it goes like this how do you know whether it goes like this or like this you see there's two ways you could plug it in and i'm going to show you a little something that's going to be very valuable because there is a standard and really every single every single dual inline package that i have ever used has used this same standard and i'm going to call up this sketch pad here and see if i can show that to you so that looks like that fired up i will come over here and i will show you how this thing works and so i will need to come here and let's just see if i can that wrong tool okay so we'll kind of draw that dual inline package like that and then uh what i want you to see is is that all of these dual line inline packages always have a little notch okay and then this particular one has 14 pins seven on each side so that would be one two three four five six seven and then one two three that is really not neat is it so seven and then one two three four five six seven and this is how the pins are numbered you look at this little notch up here and then the pin to the left of the notch is pin one and then it goes two three four five six seven and then you go around 8 9 10 11 12 13 14. so you can see you start up here you go down and then you come back here and you go up and it doesn't matter whether you have 14 pins or 28 pins or 56 pins it always goes down the left side and then back up the right side and it all starts to the left of the little notch all right so that is important to know because a lot of these things can be kind of sensitive and what you do not want to do what you absolutely do not want to do is end up putting this chip in backwards and so what i have is i have the notch to my left here this is the notch okay this is the notch to my left and you need to have your notch to your left and then everything will be hooked up exactly like shown above and exactly like i have done it here all right before i go in and start going kind of through some of these connections and what we're trying to do i think it would be useful for us to actually look at the adc0834 chip up here and you can see it's oriented i wish they would have drawn the notch but the notch is at the top and then you've got over at the left pin 1 2 3 4 5 6 7 and then go to the right 8 9 10 11 12 13 14 coming back up and what i will say is you can see that we're going to need to hook this chip to three different gpio pins gpio 27 18 and 17 and those are not physical pins those are the bcm pin numbering scheme but the good news is if you look at this breakout board the labels on the breakout board are in the bcm numbering scheme so if you look right here this is gpio pin 17 the pin 17 the bcm pin 27 and then up here the bcm pin it looks like bcm pin [Music] 18. okay and that corresponds to this picture here 18 27 and 17. but you can see that we're connecting to those three pins and then up here shows you in more detail what we're doing over here now this is the good news when you interact with this chip it is actually kind of complicated that that d i in d o and c s you've got your control signal you've got your digital in you've got your digital out you're sending them high sending them low sending data in getting data back and there's just these streams of data coming in and out and that's kind of complicated the good news is there is a library that manages all of that bit banging that's going on back and forth between gpio17 gpio18 and gpo27 and the chip and the adc 0834 so on that part of it all you have to worry about is getting all of the wires hooked up right now the part that you have control of and what your job is is what's shown up here channel 0 that's where you read you're reading into channel 0 and you can see in the schematic they have have that hooked up to the center leg of the potentiometer and so channel 0 up there is pin 3. so we come down here we have pin 1 pin 2 pin 3 and where do i have that hooked to the center leg of the potentiometer but there's good news besides it being easy now to do a read we load the library the library does all of the heavy lifting and then what we've got to do is just go out and say do an analog read and then we've got to tell it what channel are we reading in this case we are reading channel zero okay if you wanted to have four potentiometers you could hook the next one to channel one and then the next one to channel two and the next one to channel three and you could have three more potentiometers which potentiometer do you read well you say read from channel zero read from channel one read from channel two read from channel three so the things you have to kind of keep track of in your programming is this pin three four five and six those are the channels you're reading from and you would hook them to the various things that you're trying to read from hope that makes sense okay and then everything else is going to be taken care taken care of for you with the library now on doing a build like this i'll kind of tell you how i like to do it and sort of what i do and what i like to do is kind of deal with power and ground and somewhat surprisingly this thing is going to operate at 5 volts and it works properly with the with the raspberry pi even though the raspberry pi is a 3.3 volt device in this particular case they play well together but you see i go from the 5 volt here at this pin and i establish this as a 5 volt rail all the way across and so there i've got a 5 volt rail and then i jump that five volt rail which is the top row i jump it down to the second from the bottom row and so now this is a five volt rail and this is a five volt rail now row two i've made that a ground okay i've made row two a ground all the way across and then i jump row two down to the bottom row and that row is a ground as well and so you can see like one leg of the potentiometer needs to be hooked to ground hook to that rail the outside other leg needs to be hooked to 5 volts it is and then the center leg over here and similarly this chip needs several five volts and it needs several grounds and i've got easy access on both sides of both sides of the uh of the circuit and again do you see here how easy it is how easy it is to get a nice clean build if we have these straight jumper wires and i've got the kit that i'm using for the straight jumper wires down in the description and i'm not trying to i'm not trying to sell you kids but what i know is is that when you get to something that's complicated you really really need a nice clean you need a nice clean build and imagine if you were trying to do this build with big loopy wires like this and you've got 20 of them running all over the place that would be very hard so the first thing i do i just go through and i very carefully hook all the wires up then before i run things i check things and the way i check things is the first thing i do is i look up at the schematic and i say all right how many let me switch over to this other view now okay so you can see the whole thing how many total wires do i have up here i have 16 total wires looking down here i count i've got 16 total wires i start trying to do a parity check i've got one two three components one two three components number of wires are right and then i go in and i look at everything that is connected to five volts and i say okay yep that that that that that that the number of five volts up there matches the number five volts up here the number of grounds up here match the number of grounds down here and then i say okay on this expansion board i've got three gpio pins three gpo pin swag i kind of try to do a parity check different than the way i thought about it when i was wiring it up the first time and on something that's complicated you've just got to sort of assume that you probably you've got to you've got to assume that you probably uh hook something up wrong and so you just got to kind of go into it thinking that you've got mistakes and try to find them those mistakes and then get it all nice and clean before you actually turn try to start building or try to start coding i mean yeah get it all your build check before you go in and start trying to code all right that is the tedious part i think at this point things get kind of fun and so let's switch over to the uh raspberry pi view and also this schematic here is what you're going to be needing to hook up and so you're probably going to want to get your own copy of that and you can do that by coming over here and you can actually do this on your raspberry pi as i am showing here and what you can do is you can go to the most excellent www.toptechboy.com and then you're going to want to use this happy little search tool here to search on something like reading analog signals with the raspberry pi and the adc 0834 you come to this page if you click on this diagram you will get a larger view of it and then even if you want it even larger you can click again you can right mouse click and copy and save it to your uh to your computer if you want but that way you'll have something big and crisp and clean to look at and having that will help you get an accurate build okay now the build is done next thing we need to do is get that library that controls the adc 0834 and on the same page on the same page i've pasted it and this is the library it's the first set of code here and what you'll want to do is double click or just single click on this double page icon and you can see that it copied it and now we need to go save it onto our raspberry pi so you see i'm showing you a browser in raspberry pi so i copied it from my browser in the raspberry pi and then what we're going to do is we're going to come over here and we need to create a python program with that code now you know how much i like old school and we have been using nano a text editor up until this point in this series of lessons but today we're going to move to a more modern ide and the reason for that is when you copy from the internet and you paste into nano it gets confused between the difference between four spaces in a tab and it messes up your indentation and then you can just end up with big horrible problems if you have indentation errors or tab versus space errors and so what we're going to do is we're actually going to we've learned old school we've learned the nano we're going to come up and we're going to go to programming and we're going to go to phony and thony is a more modern ide now when you open up phony you might not see these menus here because raspberry pi does a goofy thing it opens it up without showing you this menu and then the instructions that you're getting or say go to file or edit or something like that and you don't see them if you don't see them look up here in the upper right and on yours it might have a little blue text underline that says something like go to standard view click on that and then you will have your thony looking like my thony now the other thing you want to do is make sure that you're on python 3.7 interpreter and so you come down here and you right mouse click on that or i guess you left mouse click on that and you can say which one you want to use and i guess i'm going to have to scoot that over you guys didn't see that let me scoot it way over here so you can see it and so i click on that just standard click and then it says alternate and then can i get out of your way here where you can see it i think so and then uh i'm gonna go alternate and you could do either bin python 3.7 or user bin 3 or user python user bin python 3.7 probably any one of those three are going to get you to the python 3.7 interpreter which is where we want to be so we'll do that i'll switch this back over okay now where did we leave off we copied the library so now i'm going to come here i'm going to right mouse click and i'm going to paste and then this is the library code now what i need you to do is i need you to save that and let's start by saving it in our python file if you open up in home here remember we created this python folder for our python program so we'll go there and i need you to name this exactly what i am going to show you here and exactly what i'm going to show you is you name it adc 0 adc 0 8 3 4 dot p y dot p wise up is lower case the adc is upper case and so now we are going to save that and you can see over here you've got the ok and so now we have that saved now we're going to come over here to a terminal and i'm just going to ls down into python and you can see there it is the adc083 now i need you to move that and where i need you to move that to is i need you to move that to the directory that python looks into to import the libraries from and so that directory is going to be that directory is going to be we're going to move it to slash user slash lib slash python lowercase 3.7 like that so we're going to move it there so move what there we're going to move python the folder and then adc not the folder the path adc 0834 dot p y okay [Music] zero eight three four okay so we go down into the python folder we get the file adc0834.py and we're gonna move it to slash user slash lib slash python 3.7 like that now when we do this what you're going to see is you get a permission denied and that's because that system folder is a protected folder and so what you do is you come over here and you do a pseudo move for super user do some people say sudo sudo sounds silly to me so i say sudo and if you don't like it i am sorry but i am just not going to be a guy saying sudo and so we're going to do that and then boom look at that so now let's do a ls python it should be gone yep it's gone now let's do an ls and we will look in user and we'll look in lib and we'll look in python 3.7 like that we'll do an ls you can see that there is a lot of uh there is a lot of things in there and if i go up to the top because it looks like they're alphabetical there it is three four dot adc0834.p y so all that looks good right i am going to do just a quick little check here because i you know that was kind of like a lot of monkey business we were doing and so let's go ahead and do a python 3 to fire up the python 3 interpreter and what if i say now import import and i go [Music] adc0834 [Music] now notice you don't put the pi on the import okay you just it knows that you just you just give the library name and boom it imported it it found it so it knows it's there so that's what we really want to do and it's worth uh it's worth taking a second and checking when you do an installation like that so now we know yeah the library is installed so the library is installed so ladies and gentlemen we're ready to start coding we are getting really close to having this thing working so let's go back over it says yeah so it's it's complaining like hey you gave me this file and it disappeared so do you want to also close the editor yeah let's go ahead and close that and so now i'm going to come up here and let's just make a new program and uh let's go ahead and let's save it and what i'm going to save it as is notice that we're already in the python folder the python directory so that's good i'm just going to call it i'm just going to call it [Music] i'm going to call it n in like that dot p y for analog not n and n dot p y that looks good and i'll come over and say okay right so now that has been saved so now we're ready to start writing the program we're going to be working with the gpio pin so what are we going to need we are going to need our friend our friend who mr import import gp or import rp little i dot gpio and we'll import that as gpio that looks good now that library we just created import adc0834 like that that looks good we're going to need a delay so we are going to say from time import sleep all right we've got our libraries now we need to do our setup so i'm going to say g or i set mode gpio dot set mode okay and then we're going to do g p i o this time b c m for the broadcom numbering scheme now why are we using the broadcom numbering scene really there's two reasons one is that this uh gpio extension board is labeled in the bcm scheme and so that kind of makes it easy therefore to use bcm and the second reason if we look inside of the adc-0834 it uses the bcm numbering scheme and i'm afraid you could have great confusion if your main program is using board and the library you're using is using bcm so i think might be safer if we just use bcm everywhere and this is the first time that we've used bcm okay then what i need to do is i need to initialize that chip and i initialize it by doing the adc 0 8 3 4 like that and then i just need to do a dot set up no parameters that will just kick that thing on and again built into that library it knows which gpio pins are hooked to which pins and so as long as you wire things up like this then on the setup you don't have to send it any parameters at all now we're going to go ahead and start doing the main part of our program we want to exit cleanly so we're going to do a try and accept so i'll just start with a try and then what we would do is we would accept into uh key board interrupt and i like that it kind of changes color means i spelled it right all right and then what are we going to do on the accept we will do a gpio dot clean up and then we will do a print gpio good to go and why do i like to do that so that when we exit cleanly it'll print that and if it prints that then i know that it actually reached the gpio cleanup command there if that makes sense okay now we've just got to do our reads and we're going to be reading from we're going to be reading from the center leg of the potentiometer so we'll do that up in the try and we want to do it more than once so i'll say a while true when is true true true is always true so this is going to be an infinite loop and i'm just going to read into ano log val you can call yours whatever you want and how do we do a read well the library is adc0834 like that adc0834 dot get results open close now i got to tell it which channel which channel am i hooked to i'm hooked to pin 3 which was channel 0. so i just tell it channel 0 like that and now i'm going to print analog val that we just read and now we need to give it a little delay so i will just say sleep and i will sleep for 0.2 4.2 seconds okay like that could it really be that easy could it really be that easy i do believe that it is going to be that easy i will need everyone to hold their breath this time and we're going to run this thing and you run it with the happy little green play button here oh no module oh oh oh that was a rookie mistake you guys probably saw that not p r i r p i it was not my fault it was one of you guys didn't hold your breath i think you know who it was this time sir or madam i will need you to hold your breath okay so we're going to come over here get results what is this nonsense dot get results what is that that sure looks right adc 0 8 3 4 dot oh not get results get results get result okay again one of you didn't hold your breath this time everyone okay i'm getting a number now the question is if i turn this am i going to get this changing i'm turning it to the left all the way down to a one turn it to the right all the way up to 255 shazam who's the magic man i'm the magic man we are now doing analog reads on the raspberry pi now one of the things that you notice is is that on the arduino you read from 0 to 10 23 so you have an extra bit of pro of precision here we're just reading from 0 to 255 so you've got one let less bit in your resolution but for anything we're going to be doing 0 to 255 is more than enough precision for anything that we are going to be doing and so we can come down on this particular one i seem to it seems to not quite want to go to zero it yeah it'll go to zero but don't break your potentiometer trying to get it to zero i can't tell you how many students i would have bring their potentiometer and get it to a one and think that they needed to torque it to get to a zero and then to actually break the potentiometer don't be that guy or lady okay so that is pretty cool okay tell me the obvious thing that we would want to kind of check here and verify okay tell me the obvious thing that we would want to kind of check and verify let's see if we can really make those other channels work is it really as easy as we think it would be to read from the other channel so what we're going to do is we're going to come and i'm going to control c let's see how i would control c come down here okay gpio good to go that's good and then what i'm going to do is i'm going to take this wire out that was the wire going from pin three and i'm going to go this time to pin four to the center pin of the potentiometer well in that case i am now on channel what i am now on channel one and now let's try to run that okay and look at that you see i'm getting a reading that looks really good okay i'm going to ctrl c now what would happen if i came up here and i'm connected to channel 1 but i read from channel 0 what's going to happen well i get a random number there that doesn't respond to the potentiometer so i'm not reading it so now let's come in and let's try to do pin let's try to do channel two and i will control c channel two i will move this over to the next pin to see if i can do two let's run it okay working as expected and then just for good measure let's go ahead and let's do channel three and that will be the next pin over and then we will control c and we will run boom everything working is expected okay guys this is pretty cool so the wiring up and the hooking the circuit up was a little bit tedious but after that things were really easy and really very straightforward so i think what i need to do now is i need to go ahead and i need to give you your homework assignment and so what your homework assignment is is let's do something useful for with this let's make a dimmable led where the brightness of the led is controlled by the position of the potentiometer you will use a red led do not use the blue led the blue led is for special occasions only so i need you to go ahead and hook up a red led and then adjust your code and make your code where the brightness of the led depends on the uh position of the potentiometer does that make sense that should be a pretty straight forward lesson and it should be a pretty straightforward lesson because it's really just drawing on a bunch of stuff that you have already learned and a bunch of stuff that you already know hey guys uh as i'm wrapping things up here i want to again uh give a thank you to you guys who are helping me out over at patreon it's a really big encouragement to me to have you guys really get behind this channel and that support on patreon really helps me keep my gear up to date my studio up to date my equipment up to date and if you're not helping out yet take a look down in the description there is a link over to my patreon account think about hopping over there and hooking a brother up the other way you can help me is give a thumbs up on this video and leave a comment down below and comments and thumbs ups helps me with the old youtube juice and this video will end up being shown to more people if you do that okay so that is always appreciated also you need to post your homework solution to youtube and in the description of your homework solution leave a link back to this video so anybody watching it has context as to what you are doing and then down below in my comments leave a leave a comment with the link over to your homework solution and i do i go in and i look at every single homework solution that you guys submit i like to see how you're doing the homework if you're understanding how many people are actually doing it so i really i really don't do go in and look at every homework that is turned in other thing we need you to do is share this with people share this with other people on social media why because the world needs more people doing engineering and coding and fewer people sitting around watching silly cat videos paul mcquarter from toptechboy.com i will talk to you guys later [Music]
Info
Channel: Paul McWhorter
Views: 14,373
Rating: undefined out of 5
Keywords:
Id: _PnVt8XtFcw
Channel Id: undefined
Length: 39min 33sec (2373 seconds)
Published: Thu Jun 16 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.