Great.
You have now successfully initiated serial communication between your Raspberry Pi and Arduino boards.
Let's now see how to send some data starting from the Arduino side to your Raspberry Pi.
So this is going to send data.
This is going to receive data.
So once again, we're going to start on the Arduino side because that's going to be easier to debug.
So on the void setup, we initialize the communication with that baud rate and we make sure the serial
is ready.
So that's pretty much it.
Now what we want to do is to send some string, let's say hello every one second.
So we're going to do that in the void loop.
We are going to do serial dot println with Hello from Arduino.
As you can see, when you use Serial.println or if you use silent print, if you have ever used that
before to debug your Arduino, well, that's the same thing.
To send data over Serial and I'm going to use println here so we can add a new line character after
this string.
And I'm going to add also a delay.
So let's use delay for simplicity here of one second so we can send that every second and not all the
time.
All right.
Make sure that nothing is running on the Raspberry Pi.
So no Python script.
Let's upload the code to the Arduino.
Okay, done uploading.
And now what we can do before we write anything with Python, we can open the serial monitor to debug
what we have and you can see we have hello from Arduino every second.
So by starting on the Arduino, when you use serial communication, you can make sure that this side
is working with the serial monitor.
So this tool is super useful.
So now we know that the code is correctly working and correctly sending this string over here.
So that's going to be easier to debug when we actually write the Python program because we know that
this side is working.
So now here what are we going to do?
So we want to be able to read this string that we receive and to print it on the terminal.
How can we do that?
Well, we are going to create an infinite loop here to read the string.
And if you see here, basically on the Arduino, we have a void setup which is going to run once to
initialize stuff.
And here that's pretty much what we have also.
Okay, we are going to run that once to initialize communication.
So this part of the code on the Raspberry Pi with Python is actually similar to the void setup we have
on the Arduino and then we have an infinite loop to read and send data.
So we are going to also create our own infinite loop, our own void loop on the Raspberry Pi with Python.
Okay.
So we can have a structure that is similar to what we have on the Arduino and how do we create a void
loop?
Well, we simply do.
While true.
And that's it.
We have an infinite loop.
Okay.
While true and in this loop we are going to read serial communication.
And actually the first thing I'm going to do is do time dot sleep with, let's say, 0.01 second.
Okay.
So here you can run the void loop as fast as you can, basically as fast as the microcontroller can.
That's not a problem because anyway, the Arduino is just doing that.
But on the Raspberry Pi, if you run an infinite loop at full speed, what's going to happen is that
it's going to take all the resources of your CPU for just one Python program and it may make the Python
program very slow or anything else that you do very slow.
So that's why I'm going to add a time dot sleep with.
Let's use this, which means that this is going to be executed 100 times per second, which is more
than enough for this lesson and also for this course.
So we have our own kind of void loop.
What do we do in that void loop?
Well, we are going to check first if we have received something from the Arduino and if yes, we are
going to read that thing.
Okay.
So we can do.
If say so.
We use the cell that we have created here dot in waiting.
This is going to return the number of bytes that have been received from the Arduino.
So we don't really care about the number of bytes.
What we care about is have we received some data?
If we have received some data, it means that the number of bytes is simply greater than zero.
We don't need to count them, but just knowing that it's greater than zero means we have received something.
Okay, one very important thing here.
Don't put parentheses, okay?
This is not a function.
This is an attribute of cell.
So press enter with the indentation here.
If we have received something, let's say here we are going to read the next line.
Okay.
So line is equal to cell dot read line with parentheses.
This is going to read the next line.
What is the next line?
So basically the next line is going to be everything until we get this backslash n character, which
is basically a new line character.
And now you can see that's very important here.
That's why I have added line.
So print l n So basically this is going to send hello from Arduino with a new line character after this.
And so on the Raspberry Pi with read line it's going to read Hello from Arduino until that new line
character that's going to be here.
So now we have our line.
But basically this is encoded okay?
Cell is not just going to send the string like this, it's going to encode it so it can better communicate.
Okay.
So basically what you receive here are just bytes.
Okay?
If you want to read them as a string, you can do dot.
Decode and then UTF eight that is going to decode what you get with ReadLine into a correct string that
you can read here.
And then let's do print line.
All right.
So basically, this is going to run forever.
And until you press Ctrl C at 100Hz, it's going to check if you have received something of a cell.
If yes, it's going to read it and decode it and print it.
Now I'm going to do something here is of course, I'm not going to close the communication before we
actually start to use it.
So I'm going to put that at the end of the program.
And maybe you will see a problem here is that if we press Ctrl C inside this while loop, we are never
going to go to line 16 and we are never going to close correctly the communication.
Okay.
So if I put this line here, it's never going to be called.
So what I am going to do before I run the program is I'm going to put this while loop inside a try except
structure.
Okay.
So I can catch the keyboard interrupt exception.
And basically when we price control C, we are going to close the communication.
Okay, so let's do that now.
Let's do try and put that with one more indentation.
So you select everything and you press tab and then come back to new line except keyboard interrupt
like this.
So basically, for example, when we press control C and we do C dot close inside the handle of the
interrupt.
And I'm going also to just add here, print close cell communication.
Okay.
So we know that this has been executed.
All right.
So now let's run this.
So the code here is already running on the Arduino.
I'm going to run here from the Raspberry Pi.
Okay.
So you can see.
Okay.
And then hello from Arduino every second.
Great.
Now I'm going to press control C, I'm going to click here and press control C.
And you can see close cell communication.
Okay.
So if you are running the script from the terminal, well, to kill the script, you just do control
C and if you are running it directly from Thonny Python ide.
Well, you need to click here and press control.
C Okay.
If you click on the stop button, it's not going to do the same.
So make sure that you click here and you press control.
C Okay, close communication as you can see.
Well we receive the string every second.
That's great, but we still have some new line and some characters that actually are not part of that
string.
So when you receive something with Red Line, you may often have some new line characters that are added
or some carriage return.
And in that case, what you can do is after the code, you can do dot r strip.
So this function is going to remove any new line character or any carriage return that you have on a
string.
So you just have the hello from Arduino and nothing else.
Okay, let's run that again.
Sale.
Okay.
You can see hello from Arduino every second without any other character.
I click here, I press Ctrl, C, close cell communication.
And one thing I want to show you is if you look at the so I'm going to run the script if you look at
the Arduino board.
You will see the text is going to blink every one second at the same time that you send the string.
All right.
You can see here and now I'm going to do control C and it stops blinking.
And now, well, it's just pulling on.
So basically, you have this led that's going to help.
You also see when you are sending data from Arduino to Raspberry Pi.
So T means transmission.
And so it's yet another tool to help you see what's going on in your application.
And one last thing I want to show you is that actually, let's say you use a different baud rate here.
So let's say I use 9600.
I still use this one on the Arduino, but I use that one on the Raspberry Pi.
So let's run the program.
And let's see what happens.
Okay, so the communication can be started.
Great.
But then.
Well, it seems that we don't receive anything.
Okay.
The communication is not working, so you can see that if you don't have the same baud rate, you're
going to have weird behavior because the communication is going to be opened.
But then anything else here is not going to work.
Okay, press Ctrl C close communication, but we haven't received anything.
Okay.
I go back to this one.
Let's run the script and say, okay, hello from Arduino.
Cetera Press Ctrl C and it's working correctly.
So baud rate.
Super, super, super important.