Arduino - Send Serial Commands from Raspbery Pi

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back as you know i am elon the computer guy and in today's class i'm going to be showing you how to control your arduino using serial communication with a raspberry pi so with the raspberry pi we're going to create a python script that python script is then going to send commands over the serial connection to your arduino your arduino is then going to be able to process those commands and today we are simply going to be turning on and off leds so basically what's really cool about this is you can have a full compute stack so basically you can have your raspberry pi you can have a mysql database you can have a web server you can have all that stuff going on your your raspberry pi can compute to figure out when things should occur and then if you need a physical event to happen your raspberry pi can then send communication to your arduino to say do something again such as turn on an led turn on a fan turn on a pump turn on an hvac system so on and so forth so in today's class i'm simply going to be showing you how to send commands to your arduino using serial communication with the raspberry pi so there's no real warning warning for today's projects you do need a basic understanding of arduino and you need to have a basic understanding of the raspberry pi and how to code and python for the raspberry pi again to be clear a very basic understanding here you do not have to be an expert the only place that you might run into a little bit of trouble is when you identify the serial connection to your arduino uh when you originally plug in the serial connection to the arduino it's most likely going to be tty acm 0 but here's an interesting thing if you unplug the arduino and then you plug the arduino back in it depends based off of the time of the day and of a cliff coin and a whole bunch of other things that connection may iterate up to be acm1 right so if you're sitting there and you do the code and the code seems to work okay but then you want to make a modification so you unplug the arduino to plug it into a computer with the arduino ide you modify the code a little bit and then you plug it back into the raspberry pi if you get some kind of like communication failure error all you have to do is go from amc 0 to amc one or if you already have amc one then you go back to amc 0. it's just one of those weird quirks from the troubleshooting process i really think that's probably the only real issue you will run into uh beyond that this is just a stock standard arduino we're dealing with we're dealing with stock standard uh leds we're dealing with the basic uh the standard installation for raspberry pi so it's a standard uh raspberry pi os that's been installed the stock standard python we haven't installed any additional libraries or modules beyond what comes with python so everything else should be relatively simple as long as you understand the basics of raspberry pi and arduino so let's go over to the workbench so i can show you how this project will look at the end once we've created it then i'll show you how to physically build the project and i will show you the code so this is a pretty simple project we're not going to get a lot of fancy results we're just simply going to be turning on our led lights but again when you turn on an led light that's the same thing as being able to turn on a relay or that type of thing if i hit run for the script on the raspberry pi the raspberry pi is now sending commands to the arduino and the arduino is now interpreting those commands and turning on and off the leds so the important thing to understand here is the commands themselves are actually coming from the raspberry pi so red is coming from the raspberry pi white is coming from the raspberry pi blue is coming from the raspberry pi all is coming from the raspberry pi off is coming from the raspberry pi those commands are coming from the raspberry pi the arduino is then simply taking those commands and then we have an if else or if else if statements to then process based off of what those commands are so this is what we're going to be doing today we're going to be sending these commands from the raspberry pi to the arduino the arduino then processes those commands and then takes an action based off of the the if else if statements so with that i'll show you how to physically build this particular project and then i'll show you the code so here's a physical build for this particular project there's really nothing to it at this point you should understand how to be able to plug in leds and make them light up so when you take a look at this we're using our standard breadboard we're using our standard 220 ohm resistors so that we do not blow out our leds basically we're using uh digital pins eight nine and ten digital pin 10 comes to the one leg of the resistor that leg of the wrist resistor then goes to the positive side of the led then the led is then connected to the ground on the breadboard here and the ground is connected to the ground on the arduino same basically then we have nine nine comes to the positive input on the resistor for the white that resistor goes to the white positive then the negative goes to the the grounding line same is true for the blue led so this is all you have to do to build this particular project and it's relatively easy so with that let's go over and take a look at the arduino code and then i will show you what the raspberry pi python code looks like so for the arduino code you may recognize this because we are literally using the exact same code we used for our initial project when we were manually sending uh serial commands to the raspberry pi so basically this is simply what we did before where we are going to be reading from the serial until next line and then if the command equals white this is what we're going to do if the command equals blue this is what we're going to do with the command equals red so on and so forth so just to uh go over this again so you remember what's happening here the first thing that we're going to do is we're going to create a variable string and that is going to be for command so the command red white blue all off right that's going to be the command then we're going to define our pens we're going to define blue led to 8 white led to 9 red led to 10. the next thing that we need to do is we need to do serial.begin most important thing if you forget this none of it's going to work and then we have to plug in our speed so we're just going to do the default standard of 9600 the most important thing with this speed here is this speed has to match what you have in your python script if they don't match you're going to have problems past that we're then going to do the pin modes for the leds blue white and red led we're going to be setting them to output and then we're going to simply delay for two seconds to make sure everything processes properly here we're going to do serial.print serial.printline and this is going to print out that line that we had before so this was giving us our commands initially for today's particular project we are not going to see this at all this actually is not going to matter but again since we already have this code from the previous project i'm simply reusing the code then we're going to go down to the loop itself and then with the loop basically we're going to say if serial is available so if the serial communication is available whether that is plugged into the serial monitor of this particular computer or it's plugged into the raspberry pi uh basically if there's a serial communication then command is going to equal serial dot read until and this is a new line so basically when we're plugging into the serial monitor we plug in a command and then i would hit enter that is a new line that's that's forward slash n um or backslash n and so that is what we're going to be looking for then we're going to do command dot trim so what trim does is it trims the white space around a command so one one of the problems you can have with a serial monitor and trying to process commands is if there's additional white spaces like like white is different than white with a space at the end right if you try to say if the command equals white and the command that comes in is white with a space at the end those do not equal so that will never be true so what we what we're doing here is we're simply eliminating the white space around the command just to make sure we don't get any kind of weird issues then we just go into the if else if statements that we did before so command so the command that we have the string equals white so then we're going to do digital right uh white led is going to be high basically on blue led red led is going to be off low else if command equals blue then the white led is going to be off blue led is on red led is off uh command equals red same thing only red led is on command equals all all of them are going to be on command equals off all of them are going to be off else if there is a bad command we should not see this this is not going to matter for today's particular project this is simply for using the serial monitor uh it would then print out bad command and at the end serial monitor would print out a command but basically with a project that we're doing today we're not having the raspberry pi actually read anything from the arduino it is simply pushing out commands so none of the serial prints will matter for this particular project today all we're really cared about caring about here is that the command comes in and then we process based off of what that command is so with that let's go over to the raspberry pi and i can show you the python code there so here we are at my raspberry pi again we are using phony that's just a standard python ide for raspberry pi so for these simple projects that was what we're going to be using uh default standard installation of the raspberry pi os and i have not installed anything additional for python in order to get the thoni you just go up the raspberry pi icon go to programs go to thoni and this will open up thoni for you so we go down here and we look at this particular project we can see it's relatively simple about 18 lines of code the first thing that we're going to do is we're going to be importing the serial module so the serial module allows us to do serial communication then the next thing that we're going to do is we're going to import the time module so the time module is required uh basically in order to simply put some delays in there so when the red light turns on for one second and then the white light turns on for one second and the blue light turns on for one second that all has to be timed uh basically we get that functionality uh from the time module then we're going to come down here if name equals main so this is going to be the standard thing that we're going to be using for these serial projects we're going to create a variable called ser for the serial it is going to equal and it will equal serial all lowercase period serial with one uppercase so s is uppercase and then it is going to connect to dev tty acm 0. again this will iterate up or down when you're plugging in your arduino and you're unplugging it so it may be acm 0 or it may be acm1 if you have a communication problem just switch it to whatever it's not currently if you want to go to the command prompt and just see what this looks like at the command prompt we can do ls basically we can search for what the arduino is currently connected to forward space forward slash dove forward slash tty and then we do star for the wild card so this comes up and basically what we can see here is dev tty acm oh look at that it is now one right so basically here this is what we're going to be looking for it's either going to be acm 1 or acm 0. so actually to make this project work today if i was going to run this project that actually has to be modified to acm 1 to make that work so if you're having any issues go do that little search then we're going to set the speed to 9600 again that just has to be the same as what you have in the arduino code so they're talking at the same speed and then a time out equals one so if any of the commands or anything lock up they'll they'll simply time out after one second it doesn't lock up your entire system then we're going to uh cert.flush basically flush out everything in the buffer to make sure we're dealing with a clean environment and then we are going to come down to the loop itself so when we're dealing with raspberry pi um again it does it's not the code it doesn't have to be in a loop so when you're dealing with arduino the code is always running in a loop like it's literally a loop it's hard-coded to loop with raspberry pi you can simply just have a python script run and end and that's it right so if you want to loop so if you constantly want something to happen or if you constantly want your sensors to to get information or whatever else you have to make that a loop so we're going to do is we're going to have a while loop so while true so basically this is just a loop that will just continuously go then we do colon and so what we're going to do here is we're going to serial so that is our variable ser dot and we're going to use the write function and then here we're going to use this so b what this does is this formats the command in bytes so the serial communication actually happens in bytes so if you do not put b here it gets all squiggly and it gets all nasty so basically whenever you're going to be formatting like like text commands to your arduino you simply put b before the text command then past that we use a double quotation mark to say a text command is coming and then we do red and then uh then the backslash n so remember with the arduino with our code it reads until it sees that backslash n so here we're sending the command red backslash n so the arduino knows this is the end of the command not to look for anything else and that this is the command itself and then it processes that we're gonna close all of that out and then we're gonna do time uh basically dot sleep for one so we're gonna sleep for one second and then we're going to do serial.right white so b then white and then sleep for one second blue sleep for one second all sleep for one second off sleep for one second so the big one big things here to remember is you do the seer.write do make sure to put that b in and then again if you want this to be delayed make sure you do time dot sleep so basically we're just simply delaying for one second and this gets us our project so then all i have to do is we go over to uh the split screen here we go to the split screen i can simply go up to run i can start running the script we can send a serial it's running the commands and now we look at it so red one second white one second blue one second oh one second off one second red one second white one second blue one second oh one second off one second and so basically these commands are all coming from the raspberry pi and then the arduino is processing the commands and that's really all there is to it again yeah okay it's goofy leds who the hell cares but again the same the this you turn on and off leds the exact same way as you would turn on relays or something more significant and so that's basically how this project works so there you go now you know how to control your arduino using serial commands from a raspberry pi so this might be interesting for you if you want physical events to happen like in the real world so let's say you have a business and at your business whenever the business closes you want certain lights to be turned on and certain lights will be turned off so basically the security lights to be turned on and the main lights to turn off basically what you can do is you can have a crime job uh running on your raspberry pi the the crown job triggers at whatever time you wanted to trigger and then it would could send commands to the arduino and the arduino could then go out and it could have you know different physical events happen certain lights can be turned on certain lights can be turned off so on and so forth so that's one of the interesting things you can do with the raspberry pi communicating with the arduino where this gets really interesting is in our next class we're going to be doing bi-directional communication and with bi-directional communication with that what we're going to do is we're going to have a sensor that is going to be connected to our arduino that sensor is going to send its values to the raspberry pi the raspberry pi will then process those values determine an action that should be taken and then it will send a command back to the arduino to say that what should occur so what we'll be doing in the next project is we'll have a temperature sensor and so with a temperature sensor the temperature readings will be sent to the raspberry pi the raspberry pi will then look at those temperature readings and based off of basically if else if statements it will determine what should happen it will then send a command back to the raspberry pi and then the raspberry pi what we're going to be doing in the next project will be turning on specific leds so again this could be very valuable for let's say an environmental monitoring system so if the temperature goes above a certain level you turn on a fan right if the temperature goes even further above the level then you can send a command to turn on the the air conditioning unit if the temperature continues to go above the level then you could connect using pyth the python api to twilio and send off an sms message right that's where you start to get into some really interesting things where basically what you're able to do is you're able to use the arduino as a sub system for sensors and motors and that type of thing and actually have the compute and you know the intel the quote-unquote intelligence be in the raspberry pi and you can start to get to some really really cool projects when you do that type of thing but today all i showed you how to do is how to control your arduino using serial commands with your raspberry pi it's relatively simple so you know play with it see what you can do with it see see how much fun you can have again from a troubleshooting standpoint if you run into any issues acm 0 and ac m1 if you have if you're having a problem it's most likely that is iterated one way or the other so just keep that in mind as always i enjoy teaching this class look forward to seeing the next one
Info
Channel: Eli the Computer Guy
Views: 13,697
Rating: undefined out of 5
Keywords: Eli, the, Computer, Guy, Repair, Networking, Tech, IT, Startup, Arduino, iot
Id: CSUFpPlSbbY
Channel Id: undefined
Length: 17min 11sec (1031 seconds)
Published: Thu Dec 10 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.