Arduino - Read Serial Communication with Raspberry Pi

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back as you know i am eli the computer guy and in today's class i'm going to be showing you how to use an arduino to communicate with a raspberry pi using a serial connection so this can be a very valuable to you because basically you can use a raspberry pi to store information coming in from your arduino in a standard way that you would in a normal computer right so when you're dealing with an arduino again all this is is a microcontroller so you can use a data logging module in order to be able to store data in something like a csv file basically a standard text file with all the values are separated by commas but you really in order to store data it can be pretty difficult when you're just using an arduino but if you can automatically send out the information that the arduino is reading to a raspberry pi the raspberry pi is a full-fledged computer what that means is you can have my sequel uh installed onto your raspberry pi you can have mariadb you can have mongodb you can have whatever type of database software that will run on the raspberry pi on your raspberry pi and so all of the readings coming into your arduino can be sent to the raspberry pi the raspberry pi can then process that information and then store it into whatever type of database you want or any kind of other data store also the raspberry pi once it takes the values coming in from the arduino uh in in the serial communication and turns that into values for a variable it can then test against those values for a variable to do things such as do api calls do something like twilio right so basically when i have my my little arduino here let's say the temperature goes above a certain degree so let's say i have a little temperature sensor arduino an environmental sensor and let's say the temperature goes above 100 degrees so the arduino it might be able to turn on a fan so it can do that it can turn on a light it can turn on a buzzer there are a lot of things that the arduino can do but you may need some other types of actions that you need something more than a microcontroller so by this communicating with a raspberry pi the raspberry pi can pull in that value you can see the value is over 100 degrees fahrenheit and then it can trigger a script uh that makes such a twilio api call to send out an sms mesh message to your administrators right that's one of the cool things is by having the arduino communicate with a raspberry pi now you get a full-fledged computer that's able to do processing that's able to store data that's able to make api calls so on and so forth so in today's class i'm going to simply be showing you how you can read from the serial communication from the arduino to the raspberry pi we are simply going to print that out on the screen at this point but all you have to understand is basically we are printing out the value for a variable so right now we're going to be printing out the value on the screen but again since it's now going to be a variable you could test against it you could store it into a database so on and so forth so that is the class we're going to be doing today so there's no real warning warnings for today's projects you just simply need to have a basic understanding of how to code and build projects for the arduino and you need a basic understanding of the raspberry pi and python code this is all very simple beginner level skills that you need but you do need those basic skills the one place where you might get screwed up with this particular project is the actual connection we're going to be writing into the python code so we're going to be using tty acm 0. so that is the identifier for the for the arduino so that the python uh script can communicate with the arduino and pull out the information one of the things just to realize is if you're if you're playing around with a project and then you decide to unplug the arduino uh and then re-plug it back in it would that that number will iterate by one and it will go to ttty acm1 instead of being acm 0. uh it's just one of those things so if you're just sitting there and let's say you're playing around you're getting the readouts uh from the arduino you go oh i want i want to modify that code a little bit you know change what the the serial output will be so you unplug your arduino from the raspberry pi you plug it into whatever computer you have your arduino ide on you modify the code you upload the code you then unplug it and plug it back in the raspberry pi just realize most likely uh the connection will simply iterate up by one so basically if you plug it back in you hit run and for some reason it fails out just go to that acm 0 change it to acm 1 and most likely it will work for you so with that let's go over uh to the workbench i will actually show you how how this project looks once we are finished then i will show you how to build a project and how to write the code so here's the finished project and i will show you the results so basically here is my arduino uno this is connected to a simply an analog temperature sensor i then have a usb cable here that is connecting from the arduino to the raspberry pi it is important to know the raspberry pi then is giving power to the arduino so you don't need any kind of other power connections and so this is the basic physical setup here so what i'm having is these the arduino basically it's reading what the analog temperature sensor is telling it it is then printing that out so that that serial.print or the serial.print line it's printing that out and we're having the raspberry pi using this python script being able to read what that serial output is so if all i do here is i click on the run button so this is now running and so this down here this is the output that is coming from the arduino uh if i put my finger on the uh the temperature sensor we can see the temperature sensor now goes up so now reading 65 66 so on and so forth this is one of the reasons i like using the analog temperature sensor it's just an easy way to give me a dynamic sensor to play with just to verify that the readings that i'm i'm getting are truly new and aren't some kind of like old readings that are just simply getting replicated so basically what we can see here is that the arduino is telling us what the volts are it's telling us what the degrees in celsius are and it's telling us what the degrees and fahrenheit are and so all that's happening is the arduino is simply printing this out that serial.print and print line that is getting sent to the raspberry pi the raspberry pi is then able to read it using this python script so with that i'll just just take a second to show you how this project gets built and then i will show you the code so here's a physical build for this particular project and there's really not a lot to it essentially we're just using the uh analog temperature sensor project that we've used in the past we have this being connected to our arduino uno so this is just a standard arduino uno board uh it's going to the 5 volt it's going to the ground and it's going to the analog 0 the a0 connection on the arduino then from there basically we just simply have a short little usb cable here the usb cable is going from the arduino into the raspberry pi and then in the raspberry pi we just have the standard monitor connections keyboard mouse so on and so forth uh the raspberry pi i am using the default uh standard installation so basically i just simply use the raspberry pi imager i imaged the operating system onto the raspberry pi and then i updated it to today's current date so basically what i'm showing you now is a simple clean installation you shouldn't need to do install anything else on the raspberry pi in order to make this work python will already be installed thoni will already be installed the serial connection module will already be installed uh so this is all that's required in order to build this project and with that let's go over and first we're going to just take a quick look at the arduino code and then i will go over the the python code with you so here's the arduino sketch that we're dealing with today it is important to understand all we really care about is that we're printing out something on the serial connection so if you have another sketch that's already created a light sensor sketch a water sensor sketch a distance sensor sketch whatever else if it's printing something out to the serial monitor that will be fine all we need to really do here is basically do the serial.begin and then do serial.print that's all we really care about so if you have a different project you can use that and again if you want to play around with different projects it's very easy to do we're using the temperature sensor here just simply because it's very i find it to be a very easy easy little sensor to use for these types of projects to verify things are working properly anyways up here we're simply going to be defining the analog pin that we're going to be connecting to for the analog temperature sensor that is going to get connected to analog pin zero then down here the important thing is within the setup serial.begin you gotta you gotta begin the serial communication and we're setting it to a speed of 9600 now 9600 is kind of like the default standard in the arduino world there may be a reason to modify that we are leaving it with 9600 the important thing for the code today for projects going in the future is this has to match what you put in the python script so in the python script you are going to define what speed the communication is at and so this this here this number here and what is in your python script has to be the same right if it's not the same it'll probably fail out so that's the only thing begin your serial connection and begin it with the same speed that you're going to plug into your python script down here we're going to go into the loop uh basically all of this here is just simply the calculations to turn the voltage coming in from the analog temperature sensor into a temperature celsius and a temperature fahrenheit value so we can print those out uh then we're going to come down here and this is where we're actually printing uh serial printing out the information so we'll do serial.print the value for voltage and then we're going to do serial print and then we're simply going to say volts so we'll say like you know 13 volts space hyphen and then it'll do serial.print the value for temperature c and then it'll say disagree c space hyphen then serial.print the value for temperature f uh space and then this is printline so basically it'll go to the next line and then it says degrees f and then what we're going to do here is we're going to delay for one second so again this is just a very simple project the main thing that we're trying to do is simply print something out to the serial connection and we're going to be printing it out at that 9600 speed so let's go over to the actual python script to see what's going on there so here we are at my raspberry pi again it's a standard default installation i simply use the raspberry pi imager to install the operating system onto the micro sd card and then once i booted up the raspberry pi i simply uh updated it to whatever the the standard uh updates are uh so basically thani is the default standard thoni python is the default standard uh python there are no additional modules that have been had to be installed or anything like this this is just a plain vanilla installation of of the raspberry pi uh so in order to get to uh the thani basically you go up to raspberry pi a little icon up here you go to programming you go to thani python ide and this is where we get our ide from there all we have to do is simply write about 10 lines of code the first thing that we need to do is we need to import the serial library so this gives us or the module so this gives us a library for all the serial communication so we can actually read from the the serial communication we're then going to do the if you name equals main and then under that we are then going to create our variable for the the serial connection so s e r this is how we're going to reference a serial connection going forward and that is going to equal serial all lowercase period uppercase s serial and then we're going to plug in all of the information so basically this is the connection dev forward slash tty acm 0 then we're going to be setting it to the 9600 speed again it doesn't matter really what speed this is more or less as long as this speed matches what's in the arduino code right so this has to match was in the arduino code arduino code has to match with this the final thing is we're simply going to put a time out here for the communication so if the arduino locks up for some reason communication locks up for some reason basically this will simply timeout after one second so everything doesn't freeze up on us now in order to get this tty acm0 now to be clear if you're using a standard raspberry pi you should just be able to copy and paste this code that i'm giving you but just in case uh if you need to figure out where you can get that information you can go to terminal once you've gone to terminal what you can then do is list and you do forward slash dev forward slash tty star hit enter and this will give you all the tty connections and you're going to be looking for this so tty acm 0. again for some reason this may iterate up to acm1 so you may have to use the acm1 just in case so if you run the script if everything looks fine and it's not running this most likely has iterated up so basically you've unplugged the the arduino plugged it back in maybe a multiple arduino is plugged in not quite sure why but this it will either be acm 0 or asm 1. if you use a linux machine so a standard linux machine this may actually be like a usb connection right here but we'll do that in a different class this is what we're looking for do not do ama so ama0 this is the bluetooth stack that you're dealing with and the rest of these we're just simply not going to worry about it's not part of our project today uh then what we're going to do is the the the variable that we've created that connection we're simply going to flush the buffer on that so that's all nice and clean and then we're going to go down to the while loop so while true so we are setting this while loop to continuously be true so it's just going to keep looping looping looping looping looping and so but we're going to say here if and then the serial connection so that serial connection that we've created up here if in weighting so basically if clients or communication and weighting is greater than zero so if there's something there then what we're going to do is we're going to have a variable named line we're going to set the value of that variable to the read line so ser dot read line so this redline function is going to read whatever is coming in through the serial connection we are going to decode it to utf hyphen 8 so basically hopefully the text will come out in a version of the text we can actually read and then we are going to run this through r strip at the end the r strip function will rip out extraneous uh trailing characters so generally like a space so in the project today you probably don't actually need the r strip because all we're doing is we're um we're printing out just a line of text we're printing out a string but it's important to understand if all you're doing is passing variables so what if i was just passing the temperature number just the variable itself and then within this python script i was going to test against that variable number well if if i'm trying to test against let's say 100 again 100 is different than a 100 space 100 space is a different thing so it's like if this is equal well it'll never be equal because if it's passing 100 space then you run into problems so again whenever you're passing a variable information especially between different devices or whatever make sure to strip out things like white space so that you don't run into things where basically conditions are never true right so basically that's that's why the r strip is here then all we're doing from that point is print and then the value of line so the line value we've created here we're reading from this connection we're decoding it and we're stripping out the extraneous trailing white space then again as i've shown you before all you do is you click run and then basically that's that's what we're getting so it's printing out the information that's coming in from the arduino uh i will touch the temperature sensor just so i can show you that this is live this is dynamic and so we can see it's going up 67 68 69 then i take my finger off and it should start going down so down to 67.66 so this is all that's required to make this particular project work again the most important thing here again this particular project whatever it's 10 lines of code it doesn't really matter a lot the important thing here is this right here so we are creating a variable we are assigning the value of that variable from what's coming into the serial connection and once again once i have a value and a variable at this point i can dump this into a mysql database at this point i can dump this into other kinds of data stores with this i can test against it to see whether enough whether or not i need to fire off an sms message right the most important thing is that you're simply assigning a value to this variable past that you know whatever comes after this that's when it gets really interesting today all i'm doing is printing it out on the screen so that's all there is to this particular project so now you know how to read a serial communication from your arduino with the raspberry pi again basically you simply connect the two together using a usb cable whatever sensors you have connected to your arduino you format all the values however you want you print them out using the serial communication serial.print and serial.printline that will then get sent the raspberry pi will be able to read that information will be able to turn that into a value and its own variable and then it can do parsing and storing and if else's and conditionals and all of that kind of stuff so this is really interesting now some people out there wonder well why why would i use a raspberry pi to read the serial communication from an arduino a raspberry pi that has gpio pins itself the arduino that can do some computation itself why would you connect these two things together the first thing to realize is that simply with an arduino you can have a metric crap ton more gpio pens than you can have in your raspberry pi your raspberry pi has a few gpio pens that you can use uh if you use an arduino mega board you can have 54 digital pens so if you wanted to have 54 different types of sensors for whatever your particular project is you have all of that information getting getting pulled into the arduino the arduino could simply plug that into an array so instead of printing out volts and celsius and all that it could literally just print an array and send that array to the to the raspberry pi the python script could then parse that particular array and then test or store information based off of that so that's one of the things that makes having the having the raspberry pi connected to the arduino uh pretty interesting is because you can get all that computational power in the raspberry pi and all the gpio pins out of the arduino and have them come together now when you use something like i am here i'm using the the raspberry pi 4b so this is kind of an expensive raspberry pi it's like a 45 raspberry pi so you're sitting there going well wait a minute there's only forty dollars on an arduino uno and forty five dollars on a on a raspberry pi at a certain point i could just buy my own computer one thing to realize though is again i'm i'm showing you everything using using equipment that i know that works and i know that that'll work optimal for for the projects that we're doing but building a project like this you can make it a lot less expensive you could get a generic arduino uno board for like five dollars off of amazon right not only that but with raspberry pi i'm using the 4b but you could use like the raspberry pi zeros the raspberry pi zero only cost ten dollars right so if you got an arduino for five dollars and a raspberry pi zero for ten dollars right all of a sudden you could create a project like this for twenty dollars and that starts making a little bit more sense from from a price point so what i want you thinking about right now is more of how can you use the arduino as a subsystem so how can you use it as a subsystem for sensor array so again let's say you had a vehicle that was running around right so you want to have an autonomous vehicle so that autonomous vehicle might need you know 20 different ultrasonic distance sensors plus it might need 10 different infrared sensors plus it may need a couple of bump sensors plus you add a couple of other motion sensors or whatever all of a sudden it gets pretty easy to get up to like 50 different uh different uh digital pins that you'll need for your particular project and so basically with that autonomous vehicle you can have all of that connected into an arduino mega board right that mega board can then process everything coming in uh from all of those sensors again send that up as an array instead of a string send that up to his array to the raspberry pi the raspberry pi could then process again based off of all those 54 different sensors plus whatever code you plug in you could figure out what the most appropriate action to take is and then the cool part the cool part is is then the raspberry pi can then actually send the commands again using the serial communication back to an arduino and then have that arduino uh you know take specific action so you could have you could have a subsystem of an array of sensors using one arduino and you could have a subsystem for motor controls with another arduino or maybe even on the first arduino and basically information can go from the arduino to the raspberry pi the raspberry pi can process it and then send appropriate commands back this is how you get a real really cool cool project again for autonomous vehicles and and larger or more more powerful devices that you might be trying to create so if you want to go out uh definitely start playing with this particular project again uh any any arduino project that is outputting using the serial monitor you can use for this particular project you grab the raspberry pi you plug in the python code and you should be good to go so as always i enjoy doing this class and i look forward to seeing the next one
Info
Channel: Eli the Computer Guy
Views: 44,471
Rating: undefined out of 5
Keywords: Eli, the, Computer, Guy, Repair, Networking, Tech, IT, Startup, Arduino, iot
Id: 3QSsnnbJYFc
Channel Id: undefined
Length: 21min 42sec (1302 seconds)
Published: Tue Dec 08 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.