Raspberry PI Arduino Serial Communication - Part 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome to sim tech channel in this tutorial i'd like to demonstrate how to use an arduino together with your raspberry pi and show a quick serial communication between the two using the python language we know what's a raspberry pi most of us we know so it's a small computer basically it's a mini computer as you can see here i do have my arduino already connected to the pi via a usb just like you would normally connect the arduino onto the computer so now the raspberry pi now is our computer and it has uh an internet or an ethernet port to connect it on the network there is an hdmi i can cast it into a screen some audio port and a lot of other things so and there are some basics ios here that you can toggle some gpio functionality so it is a mini computer that can have some functionality of a microcontrollers but you still going to need some sort of dedicated microcontroller to interface with a raspberry pi so that you can get a better result like interfacing a raspberry pi with an arduino so you're going to package yourself a nice unit that can be able to do data logging and all sorts of things so i'm just going to demonstrate a quick serial communication using python between the two now to normally what we do is we use our arduino with the computer so i'm going to disconnect the arduino from the pipe and i will plug it onto the computer now the arduino is on the computer now as you can see on my screen here if i come into the arduino ide now it's showing the port there is an unknown port at comnan that is a serial port for the arduino so i'm gonna connect it and then i'm going to link it to arduino uno so this is a new arduino ide the new version of the arduino ide it seems to be okay so i'm going to demonstrate a quick chord here i'm going to write a small chord just to demonstrate a serial communication on the computer and then i'll do the same on to the raspberry pi so what we normally do you come into your your your setup okay just a quick chord so i'm gonna write serial begin so basically i'm starting a serial communication so serial that begin to open the serial communication port and i'm going to pass in the board right into the serial begin function so the code right here will be your 9600 okay and then after i'm done doing that you can put in a small delay if you want to you can delay like for 100 microseconds and then you come out where everything should be happening inside the main loop now into here i just want to print something into the serial console so serial that print line so that i can print onto a new line every time i'm printing so i'm going to say syntax channel just like that and do not forget your semicolon there and from here you can just delay because we want to print onto the serial console every second okay so that is 1000 microsecond of delay now this code is complaining here what are we missing here serial dot begin 9600 delay there is no problem there let's just um compile it see if it runs so because this is in the new arduino ide there is still some issues of library and linking with the intellisense okay so the error is gone now let's see what's going to happen okay as we can see the compilation is completed and there is no error so like normally i'm going to now upload it into the arduino so so it's going to rebuild it again and then it's going to upload the code okay the upload is done into the arduino board now i'm going to open the serial monitor so that i can see if i'm able to serially stream the sim tech channel okay so i'm streaming syntax channel basically every second now let's say this was some data i was logging and if i want to have like a live view of what i'm logging from the arduino then the the only one thing is to make a long serial communications with the computer but then the arduino is somewhere outside measuring and the computer is in the house because it can't be outside and you can see it's becomes a problem but with a raspberry pi here you can then take the raspberry pi with the arduino package them together and your arduino is dumping data into the pi since your pi here have some sort of internet connection via wi-fi it can then dump the data into a network and then you can get it also from there so things become much easier now let's see how we're going to demonstrate this into the pi itself so i'm going to close this now and i'm going to now remove the arduino from the computer and i'm passing it now into the raspberry pi so now the arduino is now back connected into the raspberry pi so and my raspberry pi is connected onto my network here so to access my raspberry pi via the vnc view i'm just going to go ahead and click onto the ip address of my pi since it's already connected onto my network so here we are we are now inside the raspberry pi now to do the the serial communication or what we just demonstrated onto the computer you should also have install the arduino ide into the raspberry pi so we installed the windows version onto the pi you should install the linux version of the arduino ide so the installation is not that difficult but it's a simple steps you can do it via the console application so once it's installed you come into this raspberry pi sign here under programming you're going to find the arduino ide here okay there is some other two ide these tony python ids especially we're going to be using it for writing python script okay so i'm going to go ahead and now start my arduino so the arduino ide just powered up and normally when you go into tools you should see the com port if you are on the computer now into the raspberry pi here you're going to see the dev slash tty usb0 so usb 0 that is where our raspberry pi uh this is where the arduino is connected so from the console application of the raspberry pi here if you sell less and then you buy for backslash dev backslash tty and then you pass the asterix under you hit enter you should see all the uh the connections the gpio the ports of your uh raspberry pi and you're going to see usb zero day if you connect any other usb uh a device into your your raspberry pi it's going to be listed here as usb 1. okay now we know that our arduino is actually connected into the usb0 if i disconnect here okay if i disconnect and just run this command again you're going to see that the usb 0 is no longer there it's gone okay because the arduino has been disconnected so i'm going to reconnect the arduino again into the the pi and run the same command again okay and then usb 0 is back so we know that the usb 0 is where our arduino is connected okay so i can just go ahead and hit the serial monitor here since we know that we've got the uh the code already running and as you can see i'm streaming outputting the same port or the same information that i load into here now we can also demonstrate that we can program these things so so i'm going to quickly write the same code that we wrote into the arduino from the computer so just quickly so it's serial that begin and then i pass the board rate 96 okay and delay 100 okay then i come into the main loop there then i'm going to say serial that print line okay then here i want to modify it so that we can see that we are able to write the code from the raspberry pi and print it into the arduino so i'm going to say i seem take okay like that okay then we're going to also delay for one second which is 1000 milli second okay so the code is finished now before we write it in let's just write it run again you can see we're printing syntax channel okay now i'm going to now load it in straight upload into the arduino okay now asking me to first save the file since this is the first time i'm running the file so i'm gonna just say just test okay cool so we save it in there and it's going to compile and load the code into the arduino as you can see it's blinking and it's loading so it's downloading now if i run the serial monitor again so it's no longer synthetic channel and it's not high syntax so we are indeed able to uh modify write code from the raspberry pi so this unit here can be deployed so i can take this build it in assemble it and take it then because they would do pi here go to wi-fi modules so i can use a wi-fi module to dump data and then get them from some other uh methods okay so now i'm going to close this and i'm going to just leave the arduino there then i'm going to now open a python programming environment so that is python tony ide now i'm going to write a python script that will be able to capture the serial data coming out of the arduino and then capture it with python and then maybe log it into some csv file or that rtl files so that you can analyze your data later so now to do that we have to write a small title script that must be able to connect with arduino using the serial connection okay so now we can go ahead and start writing our code so the first thing is to also specify our python uh program here which python environment we are actually going to be using so that is very important so it usr um forward slash bin forward slash m for environment and then space python three so this will tell our ide which python environment we are using otherwise you have to specify okay so now we can go ahead and import the serial module for python so import serial after that we have to write a line of code to establish the communication so to do that we can say uh s data so that is for serial data is equal to serial dot serial okay so now we have to pass the parameters into this uh serial function so the parameters that we need to pass in is your your com port okay so we know that according to uh what we've done here so remember python is still connected into our arduino here so it's dev okay uh uh forward slash dty usb0 so that what we need to pass this so that's the communication port okay so it's forward slash dev forward slash tty usb 0 okay then we need to specify the border rate and this part here is very important because if you put in a different board right then what you use on the arduino because that's the board right at it that are being dumped okay so you you realize that if let's say we run the serial monitor here you can see the baud rate for reading the data is also the same but if i change here like that all of a sudden you see i'm starting to get some debris here the data are no longer correct because i just change a new border rate so you have to remain within the correct border rate for you to continue receiving the data so that's important so you have to pass the same baud rate day and then some time out is also important because if the connection is not good then it should time out instead of you wasting resources waiting for a connection that's not there so that's a one timeout once the connections have been established you have a serial co connection window open you need to wait a little bit maybe one or two seconds just to make sure that if there is any data into the buffer that data must just be removed so to do that you can just call the time function time that slip okay and then you're going to pass two seconds in there or three seconds one second up to you but this function here you need to import also the time module so we're going to import time correct okay now that that have been established we can then go ahead and reset the buffer because anything that was in the buffer that need just must just be cleared okay so i'm going to say s data okay that we we we now know that this s data is now containing the information about the serial communication data so we're just going to say s data reset okay input buffer like that okay and then we can test that we are getting there if everything else is fine if the connection is correct we should be able to to get there and print something so i'm going to print and then what i'm going to say i'm going to say arduino connected like that so arduino connected so if the connection is have been established we should be able to print arduino connected so let's just try this quickly okay we need to save the file first let's go save it into the document and which file i'm gonna also just say test okay test that pi why that's what it is it's a python file so just test okay so let's run okay arduino connected so which means everything is fine so you can see that it took about two seconds before it's print okay you see it took about two seconds after establishing the connection before it print because it needed to time out a time that slip and then clear the buffer all right now we know that we've established the connection now imagine if i have the serial port open like this here if i try to access again run this python script i can tell you that okay you can see i'm not streaming so let's see what's going to happen here ah you see there is a problem there is a problem here why because the serial port here is already being accessed with the arduino ide itself okay now you're trying to access it again using python now there is a clash it's saying that the port is is busy or is not available so you have first to close this okay close it and now if i run i should be able to get the arduino board connected because you can only one at a time okay cool now that we have that we can and and you can also do a try catch so that you don't crash like this a try catch a statement here so that you can catch these era there but okay now now we can go ahead and write a small program so that we can be able to actually get these high sim tech to print into our python program here so that we can be able to log it into some data also okay so to do that we have to run into uh we must run like what a while loop a continuous loop that need to to keep on running so we're going to say well what well true okay so while true what we're going to do i'm going to wait for another timeout okay and this time out is just a small one once i access the while loop so sleep and i'm going to sleep for like zero comma zero one this is just a small timeout okay and then i need to check if they add data into this area okay now to do that you have to say if s data okay that in waiting okay so that means they add data into the serial buffer if the that value is greater than zero okay that means they add data then you have to go ahead and do something with that data so i'm not going to declare or define another variable that must hold my serial data so i'm just going to say my data okay just to make it clear so my data my data is equal to s data so that's a serial data that read line so we need to read the line okay we need to read the line after we read the line 10 we need to decode this thing because it's coming into the data are being sent like bytes of data from the arduino now we must decode it so that we can read them as normal data that we can store so to decode it that are you just going to redline that the code like that and then you need to pass the format for decoding the data so so that is your u t f dash eight okay so these will decode your data and then also after decoding the data you need to remove like the end of line because when the the data are being sent here from the serial there is the end of line like a new line okay because you are printing line so to remove that you have to add a dot r strip so that will strip that new line from your data and only give you the data that you actually want so you can then say print my data like that okay so if we are able to print like this here then well i think things going to be okay now let's let's try let's try to run see if we're going to get that idea okay well you see arduino connected hi symtech i'm getting the data from the serial communication i'm getting the data now let's say if i disconnect uh my arduino from the raspberry pi you see as soon as i disconnect the arduino the program here just clash because why um there is no more serial communication so maybe the port yeah the communication is lost so you have to be able to handle that because even this one here should be handled as well otherwise your program will be crashing so let's just include a a a catch statement here okay that is a try accept okay so to do that i'm going to come here and include try okay then i need to select all my data here all my code here and then press tab one time so that is to get the correct intent there otherwise we're going to be in trouble okay then now i need to do the catch part since i've done the tripod i need to do the catch part so that is to to catch my exception so that would be accept okay then in in case it's a keyboard interruption if it's a keyboard interruption so then you can call keyboard interruption okay so that is your keyboard interrupt like that so keyboard interrupt and then you need to print something here onto your console so let what can we say here we can say serial communication close serial comes closed that's fine like that and then you can now close the serial port itself so it's data that close that's also important like that okay now let's see if i can run this if we're going to get a different result here okay we're still getting a problem here what a problem are we getting here it says um okay let's see okay so what is going on here let's see if we can access it with the arduino board the arduino ide itself we said the thing is not available what's happening here maybe it did move but it is onto the port where is a terminal let's see let's see what's going on here so ls dev tty um okay let's just leave you and then we see okay so we now go to usb one it moved to usb 1 it's no longer into usb 0. this is quite strange let's see ah yes it's now usb 1. okay because i did move it to a different port shouldn't it be just zero okay no that's fine okay now let's just change here to one let's change that to one and run again see if we're gonna get something okay we connect it okay now if let's see if i disconnect what will happen here okay you see because this is a different exception so this try except here is for the keyboard interrupt so if we want to catch this exception then well we need to make a try catch for this exception otherwise what we've done here is for the keyboard so let's run again that's also not available so these pi keep on changing every time you change okay now it's a usb 0 back again okay usb 0 now we run okay it's connected now i'm going to do a keyboard interruption so that is ctrl c so now if i hit the keyboard interrupt it's going to catch this exception with my try except here and then it's going to print serial communication or syria comes close and then it's going to close the data let's try that ctrl c serial communication closed serial comes closed so this is it guys this is how you basically do arduino and raspberry pi merging together to do a quick serial communication so this is just a quick line of code here for testing otherwise you should have a long line of code way you can actually be saving the high syntax data so this data could be coming from a sensor on your arduino board or you measuring a voltage or anything or a temperature and you dumping them using your python to save them okay so and and here your python will be saving the file into the raspberry pi so the the file can get very large where your arduino will not have the memory to hold such a file that would normally be doing on a computer now your raspberry pi is able to do that so this is the advantage of merging arduino and python and obviously there are plenty more uh applications that you can do merging arduino with a raspberry pi for now this is it guys if you find anything useful don't forget to subscribe to my channel and give the tutorial a thumbs up so that would be highly appreciated thumbs up thumbs up that would be highly appreciated thank you for watching stay tuned for more arduino and raspberry pi uh merging together tutorial coming up on sim tech channel until next time cheers
Info
Channel: CMTEQ
Views: 10,832
Rating: undefined out of 5
Keywords: raspberry pi, arduino, serial communication, raspberry pi 4, raspberry pi 4 projects, serial monitor, electronics, arduino projects, arduino project, learning arduino, raspberry pi 3, raspberry pi zero, raspberry pi tutorial, raspberry pi 4 setup, raspberry pi camera, arduino vs raspberry pi, raspberry pi alternative, raspberry pi 4b, python tutorial, cmteq, Raspberry Pi Arduino Serial Communication, UART Interface Tutorial, IoT Projects, Embedded Systems Communication
Id: jD4tHHnE4_w
Channel Id: undefined
Length: 28min 28sec (1708 seconds)
Published: Sun May 08 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.