Arduino - Bidirectional 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 show you how to do bi-directional communication between your arduino and your raspberry pi so up until this point i've shown you how to send communication one way so i've shown you how to send communication from the arduino to the raspberry pi and i've shown you how to send communication from the raspberry pi to the arduino in today's setup we are going to be doing a bi-directional communication so basically what we're going to have is we're going to have an analog temperature sensor that is going to be connected to the arduino and we are going to have a set of leds also connected to the arduino what is going to happen is the arduino is going to send the temperature that it is reading to the raspberry pi the raspberry pi will process that information and then based off of what that temperature is it will then send a command back to the arduino to say what led light should be turned on so basically what is going to be happening here is that the arduino is going to be used for both the input and the output uh components of this project but the raspberry pi is all actually going to be used for the compute now this right here seems probably pretty simple a lot of folks out there probably thinking alright you know i why don't you why don't you just use the arduino to do that if essentially all you're doing is is looking for thresholds to to turn on an led all of that can be done with the arduino and yes yes it can the thing that i'm trying to show you here though is that you can send the values that the arduino is pulling in from a sensor to a to the raspberry pi to a python script that python script can then turn the values it's receiving from the arduino into values for a variable it can then process against those values and then valuables and then it can actually send commands to the arduino so imagine a more complicated system here where the values are being sent to the raspberry pi the raspberry pi is then inputting those values into a database and then the raspberry pi can look and you know based off of what has happened in the past hour in the past 10 minutes in the past day it can then determine uh what should occur what physical events should occur and then from that it could then send a command to the arduino again for a lot of the smarties out there a lot of the people that are probably too smart to be watching these classes a lot of the things that i do seem very simplistic and it's like well well if i if i'm going to do that i would just use the arduino why add the raspberry pi the point of adding the raspberry pi is to show you that you can turn those arduino values into variable values within the python script you can then take actions based off of those values to then send back a command to your arduino so this project today is very simple but it shows you basically it gives you the outline on how to make much more complicated things if you understand you know things such as you know my sequel and the rest of it so with that i'm going to be showing you how to do bi-directional communication today between your arduino and your raspberry pi uh i think this will be a pretty fun time so the only warning warning for today's class is just to have a basic understanding of what is going on here because you can run into some problems with the the python when the python tries to parse the data coming in from the arduino if you start giving the python script crap data you're going to get crap output well you're not even really going to get crap out but frankly the entire damn thing is just going to fail right so back in a previous class i'm using a parts of a code that we used in a previous class in order to be able to send serial commands to the arduino and in that previous class i put in a lot of extraneous information basically information for the user to be able to understand how to work with a project right so basically i put in a little command prompt that said you know type in type in the command um you know red white blue all or off right and so that was actually being printed out on the serial monitor well realize realize what we're going to be doing today is we're going to be doing floats so floats are numbers with decimals in them and basically we want floats to be sent to our python script and the python is then going to parse those floats and then determine actions based off of you know ill if else if conditions well if you send text if you send text if you try to do conditionals of numbers versus text you're going to run into all kinds of problems right and so the thing to understand is when you go and you look at your code especially if you're going to reuse the code i've showed you in the past you're going to have to rip out um anything that you're printing out on the serial monitor that is not a number and is not relevant for today's class so if you printed out user prompts before for the serial monitor realize we're not actually reading this through a serial monitor the python script is reading it so basically eliminate anything so eliminate any strings eliminate any text eliminate anything that would be printed out through the serial connection that doesn't have to do with the temperature readings again if you don't um best case scenario you'll just get some extraneous numbers and get some weird readings from it uh worst case scenario scenarios the entire python script just fails and it might be a little bit difficult to figure out what is going on so just make sure your arduino is only sending the information that it needs to be needs to be sent so let's go over to workbench i will show you how this project works then i will show you how to build this project and then i'll show you the arduino and the python code to make it function okay so here we have the project we have an arduino and we have our raspberry pi one side of the arduino we have the analog temperature sensor so the analog temperature sensor is basically connected to a0 on the arduino and then on the other side we have this display with the leds so we have a blue led for when it's cold a white led for when the temperature is okay and then a red led for when it's hot so the temperature reading is going to come in through the arduino it is going to be sent to the raspberry pi over the usb connection the raspberry pi is then going to process the values if received and then it's going to send a command back to the arduino to turn on the led so it's a little cold down here as you as you may remember let me warm up my hands a little bit so if i put my my finger on the temperature sensor what we're going to see is that after a second the temperature goes above 60 degrees and so the light then becomes white and then if we wait another few seconds the temperature should go above 70 and then that point the the little led will then turn red and of course this is taking a couple of extra seconds and there you go so now it's going to turn red so the important thing there is the temperature is coming in from the temperature sensor getting process getting turned into a value by the arduino that value is being sent to the raspberry pi raspberry pi is then sending the command back to the arduino to turn on the leds and so that's basically how this project works so with that let me show you just how to build the project and then we'll go and take a look at the code so this is the physical build for the project and there should be nothing complicated here we are using our analog temperature sensor so this is the dynamic sensor i use for almost all of these test projects again it's just an easy way to get give you like a dynamic input for any of your projects and then on the other side basically we simply have the leds that are connected we're using the 220 ohm resistor so we don't blow out the leds red goes to digital pin 10 white goes to digital pin nine uh blue goes to digital pin eight they're all connected on the ground and then the ground cable thing here goes back to the ground on the arduino so this particular bill for the project again at this point should be very simple and run-of-the-mill for you this is all you have to do as far as the raspberry pi is concerned it's just a standard raspberry pi uh we're using the default installation of raspberry pi os on this we're using the default python nothing additional has been added to it so this is all that's required in order to build this particular project so with that let's go over and take a look at the arduino code itself so here's the arduino code if you've been following along with the serial communication classes i've done in the past you'll notice that this looks very similar the real difference here between what we've done in one of the other projects is that the only time we're going to be printing out so serial.print line here we are simply going to be printing out the temperature f value you will notice i am not printing out any additional text i am not printing out anything additional because anything that i'm going to be sending to the python script on the raspberry pi is going to get processed by the the python script and basically i just want to be processing the value of the temperature so again if you if you're using any projects from the past do just make sure to rip out anything that might be extraneous that's getting printed out to the serial connection we go up here and so we have the string command so basically this is going to be the string that we are going to be using for turning on the led lights so the command is going to be coming back from the raspberry pi it'll be red white all right red white blue all or off and so that command is going to get processed so we need to create the string here we are then defining all of our pins so what we have here is define a sensor pin to a0 so the temperature sensor is connected to analog 0 blue is the digital 8 white is the digital nine a red is the digital ten we then come down here we then have to start the serial monitor or the serial communication do remember to start that and it's important that you put in the right speed so by default it should be ninety six hundred the main thing is the speed that is in this this sketch has to be the same speed that's in the python script so just make sure that they're the same we then set the pin mode so basically for blue white and red led we're going to be setting those digital pins to output so that we can actually turn the leds on then we're going to come down here this is the this is the standard math that we use uh to turn the values coming in from the analog temperature sensor into either temperature celsius or the temperature fahrenheit what we really care about here is a temperature fahrenheit we don't really worry about the math that math you just kind of copy and paste then what we're going to do here is serial dot print line and then the value for temperature f so here is where we send the value to the raspberry pi so this is where we send the value to the raspberry pi then the raspberry pi is going to process that value and then send a command back to the arduino for what to do so with here what we're going to do is if serial is available so essentially if the the serial communication is actually active command so that string we created before is going to equal serial dot read string until next line until n so basically the raspberry pi is going to send a command and so command here is going to equal whatever is in front of the slash n command trim so what this does is it trims off any white space if there's any white space for any reason we're going to get rid of that again from a troubleshooting standpoint sometimes when the computers the devices are communicating they add in a little bit of white space just because you know they just there's a little white space would look good here the problem is if you're doing conditionals if you're doing if else's and you get white space again white with a white space at the end is different than white with no white space at the end so if you're testing against one of those you can run into problems so we're trimming that out so we're going to do here is then if command dot equals white so if the command that comes in is white then digital right the white led is going to be on blue is going to be off red is going to be off else if the command equals blue white is going to be off blue is going to be on red is going to be off if else if command equals red white off blue off red on else if command equals all they are all on else if command equals off all will be off uh else so if for some reason you get some other weird thing coming in then it will just turn them all on just it's kind of sort of like an error if that type of thing then we're going to put a delay down here we're just going to delay this whole loop for one second an important thing to remember again from a troubleshooting standpoint is don't don't put the delay here don't put the delay here right so you're going to send a command the the raspberry pi is just going to compute compute what should happen and we'll send we'll send a command back so if you put a delay here this entire system will fail because basically it'll send a command it'll delay it'll wait the raspberry pi will send its response but this will be delaying the script and so it won't actually read the response and so everything will fail so if you're going to put a delay in this particular loop put the delay at the very end or put the delay at basically at the very beginning put it outside this whole little command structure or you'll run into a problem so with that let's go over to the raspberry pi and i will show you what the python script looks like so here we are at the raspberry pi again this is a stock standard raspberry pi this has the latest raspberry pi os with the latest updates i did not have to install anything additional for the python to make this work so this is just the stock standard raspberry pi to get to thoni you just click on the little icon up here program and you go to thoni and you can open this up and then you can write your code so here the first thing that we're going to be doing is we're going to be importing that serial module so the serial module allows you to actually do serial communication then like we've done in the past is so if name equals main then we're going to continue so serial so the serial connection is going to equal serial all lower case period serial one upper case on the s and then we're going to do dev you know forward slash tty acm and this will be either acm1 or acm 0. so i un in order to do this class i unplugged the arduino and plugged it back in and so when i did it iterated this up so i go to acm acm1 originally it was acm 0. so again just from a troubleshooting standpoint realize if you have this as acm 0 and it doesn't work or if you have it at acm 1 and it doesn't work just change it to the other one and it will probably work for you then we're going to have the 9600 here so again that is the speed that we talked about in the arduino sketch so just make sure that that's the same as what's in the sketch and then we're going to timeout equals one second so basically if there's an issue with a serial connection after one second it will simply timeout it won't freeze up the entire script we're then just going to flush so we're going to flush the buffer of any additional information and then we're going to go into the while loop so the while loop here is uh pretty simple it's all within a few lines of code so while true so this will just continuously loop loop bloop bloop bloop so if serial is in weighting is greater than zero zero so basically if there's a serial communication then what we're going to do is the first thing that we're going to do is we're going to create a value for the variable line so what is being sent from the arduino that the first thing that we're going to do is process that so here what we're going to do is line equals serial or scr dot read line dot d code to utf hyphen 8. so this will turn it into a string i'll turn it into text and then r strip this will get rid of the white space then what we're going to do just from a troubleshooting standpoint is then we're going to print out on the little shell screen down below uh what the line is so when we're doing troubleshooting if we're trying to figure out why the leds aren't lighting up an important thing to know is you know what temperature is is the sensor actually reading so this will print that out on the shell down here just so we know what's happening then what we're going to do is if and here i just replicated this a couple of times there's probably an easier way of doing this but basically that line right here is going to be a string so even though it's a number it's a string and you can't do a can you can't do a numeric comparison on a string right uh 40 f o r t y is different than 4 zero essentially right so when this comes in basically this turns that that number value into a string and so if you simply try to test so if line is greater or equal to 60. if you don't have the float in there then then it's going to fail out because you're trying to compare a string to a number and it'll fail so what this function does here is float turns this string into a float so a float is a number with a decimal point in it so if float line so line gets turned into a float is less than or equal to 60 then serial dot right and then we use this b this b basically says how to encode the text so you're almost always going to use b and then we send the command within the double quotation marks so if it's less than or equal to 60 we're going to send the command blue and this this slash in that that basically tells our arduino where the command is and so this is going to be sent to the arduino and the arduino is going to process it it's going to be blue and so it's then going to turn on the blue blue led l if so again in the python world it's not else if it's e-l-i-f that flow line thing going on so we turn that line value into a float is greater than 60 and float line is less than or equal to 70 serial.right so send the command the command we're going to be sending is white uh lf float line what we're doing before is greater than 70 serial dot right you know red so it'll send red so else again another troubleshooting thing that you can do in case you really hash up the conditionals and sometimes it happens right print be all and so what will happen here is this will send the all commands so all the leds will light up so if i'm sitting there and i'm looking at my arduino project and all the leds light up i know that there's probably something wrong with the conditionals in here right so i'm gonna when i'm doing greater thans and less thans and equals twos it's really easy to screw it up so like there's one temperature in there where nothing happens right so this will just print everything up and so with that that's basically what we have so let me uh increase the size of the shell here and then we will go over so we have this on the screen so we have the actual project on the screen and then i'm going to hit run script so it's running the bidirectional arduino dot pi and so we can now see this is the temperature reading so again we do print line this is the uh what the temperature reading is is if i go over and i put my finger on the temperature value the sensor the temperature will go up so we now see us in the 60s and so the white led is on and then hopefully the temperature will go up it's a little cold down here come on there we go my hands are cold and so now the temperature goes above 70 and we can see the red led is on and so we can verify we can verify with the output in the shell what the temperature is and that the leds are responding how they're supposed to so i take my finger off of the temperature sensor we see it goes down to the white again goes down with my finger off the temperature sensor in another few seconds or so it should then go below 60 and it should go be back down to being blue hopefully if it's cold enough down here it's a cold room it's cold hey if you're gonna be a technology professional you gotta get used to working in a cold environment you know working down in my basement it reminds me of a server room it's a less equipped server room i suppose uh okay so we're going down to dot six five there we go down to five nine and then there we go so we saw the blue and then it goes up a little bit and then it goes down and we again we can see that that that part is actually working whether or not the sensor is being that's its own thing so anyways that's basically uh how this project works so there you go now you know how to get your arduino and raspberry pi communicating a bi-directional way the communication goes both ways again i think this can be very interesting because now essentially what you can do is you can use your arduino as a subsystem so for all the sensors or the motors or anything like that you can design a system just specifically designed for those things and then essentially you can use the raspberry pi more or less as a compute module so it can process what's coming in from the raspberry pi it can put that information into a data store such as a database or maybe communicate with apis all that type of thing and then it can send commands back to the arduino again for the love of crikey i don't know what it is i don't know what it is i get like these 20-year professionals watching these introductory videos oh and they're always like eli that's stupid that's too you could do all of you could do that entire project on the arduino it's like yes yes basically i could the point that i'm trying to show you here the point i'm trying to show you is how using python you can take these values in you can then you can do conditionals you can do if else statements you can print them out you can store them to data stores you could do something and then based off of that you can then send commands to the arduino so to be clear here you you could have a thousand lines of code on your raspberry pi to determine what to do with the readings that are coming in from the arduino i'm just showing you how to do it in 18. i'm just i'm just giving you a basic idea here but you know something to be thinking about like you could have numerous sensors on your arduino so you could have you could have an array of like ultrasonic distance sensors so if you're going to do an autonomous vehicle you could have 10 ultrasonic distance sensors you could have eight uh infrared uh distance sensors for for close distance you could have a gps you could have a few other sensors all of that information could be brought in from the arduino essentially sent to the raspberry pi have that raspberry pie look at all of that information and then based off of other things if they can it can then determine what your vehicle should do and then send the command down to the vehicle so when you're looking at this i'm just showing you the basics here come on man come on so uh so but i think this is very cool i think it's very cool especially again if you start thinking about like the raspberry pi zero so uh i'm using a raspberry pi 4b here i think they cost about 45. so that's kind of expensive for a compute module for something like an arduino project but again i think the raspberry pi zero comes in at like ten dollars i think you can buy it for ten dollars so realistically that's as much as many of the other modules would cost for it for an arduino project so that's what that's why i say think think of the raspberry pi more for this type of thing as a compute module just like you would have a motor module or you might have a sensor module think of the raspberry pi essentially for this type of project as a compute module it just simply allows your project to have a little bit more of a brain you can actually program in a real freaking programming language again i've been doing a lot of arduino projects for you folks showing you how you can use an arduino as a web server showing you how to send the commands and all that kind of thing over the web and let me tell you yes can the arduino be a web server yeah it can be i'd rather be coding and a little bit more complex uh coding language uh than what you've got for the raspberry pi again you run into a lot of issues with the raspberry pi like for for the uh for the data logging module again a data logging module or data logging shield is going to cost you 10 or 15 on its own right but like with the data logging module with the raspberry pi i think it's called the 8.3 naming convention so basically uh your your the name for any file that you're going to create can only be eight characters with a period with three characters for the extension you're locked into that so you you are essentially locked in to 11 characters for whatever the hell you're going to be naming your files which doesn't seem like a big deal until you're actually trying to create projects and realize oh all right that could be a pain in the butt so so instead of having a data logging module uh connected to your your arduino project if you simply have a raspberry pi and then that raspberry pi is able to store those values just like a normal computer stores the values because essentially it is just a normal computer again like those little things make your life easier again a raspberry pi you know zero uh cost you ten dollars and a data logging module cost you ten dollars the raspberry pi zero just gives you a hell of a lot more functionality then you get the web stack on top of it then you get a lot of other things so that that's why the raspberry pi in doing this type of communication gets very interesting and although what i'm doing may look like i'm over building a project i would say once you start understanding what i'm doing it really opens a hell of a lot of doors for you so i do think it's a very interesting thing so as always i enjoy teaching this class i look forward to seeing the next one
Info
Channel: Eli the Computer Guy
Views: 40,206
Rating: undefined out of 5
Keywords: Eli, the, Computer, Guy, Repair, Networking, Tech, IT, Startup, Arduino, iot
Id: OJtpA_qTNL0
Channel Id: undefined
Length: 24min 44sec (1484 seconds)
Published: Sat Dec 12 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.