Now that everything is correctly configured, let's start the communication.
In this lesson, we will just initiate the communication and in the following lessons we will exchange
some data between the two boards.
So we are going to open the Arduino IDE.
So we can upload some code to the Arduino board.
And we are also going to open the thorny python so we can write some Python code on the Raspberry Pi.
So I'm going to organize like this.
Okay, so I can have the Arduino code on one side and the python code on the other side.
Okay.
So this is going to run on the Arduino.
This is going to run on the Raspberry Pi.
And as you can see, that's very practical that we have the Arduino IDE on the Raspberry Pi so we can
directly write the two programs on the same place.
And we are going to start with the Arduino side.
Okay.
Usually that's what we're going to do.
We're going to start from Arduino because that's going to be easier to debug.
So to start serial communication, well, you just do serial dot begin with a baud rate.
So that is quite classic baud rate 9600.
Okay.
And you put this line inside the void setup of your Arduino, Okay.
You have void setup which is going to be executed once and void loop, which is going to be executed
an infinite number of times.
So with this, we have initialized Serial on the Arduino.
So that's very simple.
What we can do also, and this is going to depend on the Arduino board that you have is do while and
then exclamation mark for not serial and then just open close curly brackets.
So this basically on the Arduino Uno, for example, or Nano or mega, you don't need this line.
But if you are using an Arduino with a native USB, for example, Arduino, Leonardo or Arduino zero,
then you may need to use this line.
Otherwise the style is not going to work correctly.
So basically this is going to wait until the serial is ready.
So I'm just going to include it here and in every program I do so that this code is going to work on
pretty much any Arduino board.
But if you are using Arduino Uno like me, you don't need this.
All right.
So that's the code for the Arduino part.
Now I'm going to go on the Raspberry Pi part and write some Python program.
So let's first start with this user bin env with Python three.
Okay.
So if we ever execute this from the terminal directly, we give the information that the interpreter
is Python three.
Now what we're going to do is we're going to first import serial.
Okay, so the name of the library is Pi serial, but you're going to import Serial.
Okay.
On the Arduino, you have nothing to import.
Okay.
Soil is already there, but on the Raspberry Pi, you have to import that module.
And then.
Well, the first thing we're going to do is to open the serial communication.
And the serial communication is going to give us something that I'm going to save as sell for sale.
Okay.
I do this because that's quite common.
And if you find any help on the Internet, any tutorial, any documentation, that's what you're going
to see.
So I'm going to keep the convention that is on the Internet.
Right?
And so sale is equal to serial dot.
Serial uppercase with the uppercase U open close parentheses.
And first we are going to give the port.
Okay, So if you remember the port was slash dev slash tty ACM zero.
So that is the port I have found actually in the previous lesson.
Okay.
So if you are not sure what port is yours because that may be something different here, please go back
to the previous lesson on how to set up serial communication and then come back here with the port that
corresponds to your Arduino.
The next parameter is the baud rate.
So the baud rate is actually what you can see here.
That's the speed of communication basically.
And this is super, super important.
You have to have the same baud rate on both sides of the communication.
Okay.
If you have a different baud rate, then the two programs can't understand each other.
And actually speaking of baud rate, this is the pretty common classic baud rate you're going to have
in every tutorial, but it's also a pretty slow one.
You can go up to 115,002 hundred baud.
Okay.
And that's the baud rate I'm going to use.
So if I use this here, I use this here also.
Okay.
So you can have different values, okay?
You have different common values.
And this is one of the most common one and also a quite fast one.
And it's better than the classic 9600.
And I'm going to add a third parameter, which is timeout, let's say 1.0.
So timeout here is read timeout.
I'm going to come back to that a bit later in the course.
Okay, So don't worry about that for now.
All right.
So with this line, we open the serial communication and if it works, okay, we're going to get the
serial inside this cell.
If it doesn't work, we're going to get an error and the program is going to exit.
Then what I recommend you to do is to wait a few seconds here after opening the communication.
So I'm going to do import time and do time dot sleep for three seconds.
So I recommend at least two seconds.
Three is even better because, well, when you open the serial communication on that side, the thing
is that on the Arduino side, the Arduino for Arduino Uno, for example, is going to restart.
So the program is going to be restarted.
So what you want to do on the Raspberry Pi side is to give enough time for the Arduino to have the serial
completely ready to communicate.
And so we give three seconds here, and I recommend you do that because if you don't do that and you
try to read some data or just send some data just after that, you're going to experience some weird
bugs in your program.
And that's going to be hard actually to debug.
So better have a time.sleep with 2 or 3.
And then I'm going to do set dot reset input buffer.
So what is this?
So basically with serial, both sides can communicate with the other.
Okay.
So I can send data from the Raspberry Pi to the Arduino and I can send data from the Arduino to the
Raspberry Pi when the data arrives.
It's not directly in the code.
When the data arrives, it's in a buffer.
Okay?
And when you're going to read, that's what we're going to see in the next lesson.
So when you're going to read, you're going to read actually from the buffer.
So here we reset the input buffer, which means that if the Arduino sends us some data before this,
so when the program is actually paused, we're going to erase everything.
So we start after this line with a fresh buffer that has nothing in it.
Okay.
And I'm going to come back also later in the course to explain more about it.
All right.
So that's it.
Now let's maybe put print.
Sale.
Okay.
To have a nice confirmation and then.
Well, the problem is going to exit.
Okay, Now what to do?
Because we have the Arduino program here, the Raspberry Pi program here.
But what you need to do first is to upload the Arduino code on the Arduino IDE.
Okay.
That's the first thing to do.
Always is to have the Arduino ready.
So I'm going to go on tools.
Check that I have the correct board, the correct spot and let's do upload.
Okay.
Let's save it as first communication.
Okay.
You can see.
Compiling and uploading and down uploading.
So the code is now in the Arduino.
Now that the code is in the Arduino, we go back here and I'm going to save the file.
So save as.
So let's create a new folder.
Actually, let's go here.
So I'm going to do directly from the file manager.
Okay.
Right click new folder, Let's call it Python programs.
Okay?
And let's go back here.
Let's do save as.
In Python program.
First communication and dot pi.
So that's the python file.
Click on.
Okay.
So the program is saved and now I can run it.
So to run it, I can just click on the play button here or run from the terminal.
So I'm just going to click here.
And you can see we have so we run the script and after three seconds we have cereal.
Okay?
Which means that the Raspberry Pi could correctly connect to that port.
Okay.
So it could correctly connect to the Arduino.
Now, one important thing you have to do also on the Python side is that so here we directly exit after
this.
So we open communication and then we exit.
What you have to do and what is better is to close the communication so you can do self dot close.
Okay.
Before you exit the program or whenever you want to close the communication.
So with this, you're going to open the communication with this, you're going to close it.
And on the Arduino side, you don't need to actually close the communication.
Okay?
The communication is going to be closed simply when you power off the Arduino.
But here as a best practice, it's better to close it because then if it's not closed correctly and
you try to run another program, you may have some troubles.
Okay, So let's run that again.
You can see we wait three seconds, say okay, and then we close the communication here.
Everything is good.
Now, let's say that, for example, I don't have my Arduino board connected to the Raspberry Pi, so
I have physically removed the USB cable from the Arduino side or the Raspberry Pi side.
It doesn't really matter.
And I'm going to run the script again.
And you can see we have an error.
Okay.
We have an error, which is quite big.
But at the end you can see single exception.
Could not open port slash dev slash zero.
Okay.
So basically the port is not found.
Why?
Because the Arduino is not connected.
If I plug the Arduino to the Raspberry Pi again.
Let's run the code.
And now it's working.
Great.
So to recap on the steps here, that's very important.
First, you will need to write some code for the Arduino where you initialize Serial, of course, and
you need to upload that to the Arduino.
When you upload to the Arduino, make sure that you are not currently talking to the Arduino from another
program of your Raspberry Pi.
Okay.
Because when you upload and when you communicate, you use the same cellular communication.
Okay.
So if you are sending data from your Raspberry Pi to the Arduino with a Python program and you try to
upload, you may have some problems.
Okay.
Now that the code is on the Arduino, the code is going to run as soon as the Arduino is powered on.
And then you can run your Python script where you open the serial communication.
So make sure you have the same baud rate.
Okay?
And make sure you have the correct port here.
And basically on some boards like Arduino, Uno, Nano Mega, for example, when you open the serial
communication from the outside of the Arduino, the Arduino program is going to be reset.
It's going to be restarted.
So at this point of the code, this is going to be restarted from scratch.
So that's why you want to wait a few seconds.
So here we wait three seconds.
That's very safe so that the cell communication can correctly be initialized and everything can be initialized
on the Arduino side.
Then you reset the input buffer.
You do whatever you want to do here, you can send and receive data.
That's what we're going to see directly in the following lesson.
And then you close the communication here on the Raspberry Pi side and you can exit the program.