WEBVTT

00:00.080 --> 00:04.710
Now that everything is correctly configured, let's start the communication.

00:04.730 --> 00:11.090
In this lesson, we will just initiate the communication and in the following lessons we will exchange

00:11.090 --> 00:13.460
some data between the two boards.

00:13.460 --> 00:19.010
So we are going to open the Arduino IDE.

00:21.370 --> 00:24.430
So we can upload some code to the Arduino board.

00:26.170 --> 00:35.680
And we are also going to open the thorny python so we can write some Python code on the Raspberry Pi.

00:35.920 --> 00:39.610
So I'm going to organize like this.

00:41.860 --> 00:46.750
Okay, so I can have the Arduino code on one side and the python code on the other side.

00:46.780 --> 00:47.170
Okay.

00:47.170 --> 00:49.060
So this is going to run on the Arduino.

00:49.090 --> 00:51.420
This is going to run on the Raspberry Pi.

00:51.430 --> 00:56.680
And as you can see, that's very practical that we have the Arduino IDE on the Raspberry Pi so we can

00:56.680 --> 00:59.530
directly write the two programs on the same place.

00:59.530 --> 01:03.370
And we are going to start with the Arduino side.

01:03.400 --> 01:03.700
Okay.

01:03.700 --> 01:05.440
Usually that's what we're going to do.

01:05.470 --> 01:09.220
We're going to start from Arduino because that's going to be easier to debug.

01:09.220 --> 01:18.130
So to start serial communication, well, you just do serial dot begin with a baud rate.

01:18.130 --> 01:22.750
So that is quite classic baud rate 9600.

01:23.170 --> 01:23.500
Okay.

01:23.500 --> 01:27.910
And you put this line inside the void setup of your Arduino, Okay.

01:27.910 --> 01:33.280
You have void setup which is going to be executed once and void loop, which is going to be executed

01:33.280 --> 01:35.340
an infinite number of times.

01:35.350 --> 01:38.890
So with this, we have initialized Serial on the Arduino.

01:38.890 --> 01:40.180
So that's very simple.

01:40.180 --> 01:48.080
What we can do also, and this is going to depend on the Arduino board that you have is do while and

01:48.080 --> 01:57.470
then exclamation mark for not serial and then just open close curly brackets.

01:57.470 --> 02:03.890
So this basically on the Arduino Uno, for example, or Nano or mega, you don't need this line.

02:03.890 --> 02:11.390
But if you are using an Arduino with a native USB, for example, Arduino, Leonardo or Arduino zero,

02:11.420 --> 02:13.250
then you may need to use this line.

02:13.250 --> 02:15.890
Otherwise the style is not going to work correctly.

02:15.890 --> 02:21.200
So basically this is going to wait until the serial is ready.

02:21.200 --> 02:27.800
So I'm just going to include it here and in every program I do so that this code is going to work on

02:27.800 --> 02:29.510
pretty much any Arduino board.

02:29.510 --> 02:33.590
But if you are using Arduino Uno like me, you don't need this.

02:33.980 --> 02:34.370
All right.

02:34.370 --> 02:36.260
So that's the code for the Arduino part.

02:36.290 --> 02:41.870
Now I'm going to go on the Raspberry Pi part and write some Python program.

02:41.990 --> 02:49.220
So let's first start with this user bin env with Python three.

02:49.250 --> 02:49.490
Okay.

02:49.490 --> 02:55.640
So if we ever execute this from the terminal directly, we give the information that the interpreter

02:55.640 --> 02:56.990
is Python three.

02:57.020 --> 03:02.570
Now what we're going to do is we're going to first import serial.

03:02.600 --> 03:08.330
Okay, so the name of the library is Pi serial, but you're going to import Serial.

03:08.360 --> 03:08.930
Okay.

03:09.050 --> 03:11.310
On the Arduino, you have nothing to import.

03:11.330 --> 03:11.570
Okay.

03:11.570 --> 03:16.460
Soil is already there, but on the Raspberry Pi, you have to import that module.

03:16.460 --> 03:16.940
And then.

03:16.940 --> 03:21.380
Well, the first thing we're going to do is to open the serial communication.

03:21.380 --> 03:27.320
And the serial communication is going to give us something that I'm going to save as sell for sale.

03:27.350 --> 03:27.620
Okay.

03:27.620 --> 03:29.390
I do this because that's quite common.

03:29.390 --> 03:34.550
And if you find any help on the Internet, any tutorial, any documentation, that's what you're going

03:34.550 --> 03:34.820
to see.

03:34.820 --> 03:39.140
So I'm going to keep the convention that is on the Internet.

03:39.160 --> 03:39.620
Right?

03:39.620 --> 03:42.920
And so sale is equal to serial dot.

03:43.650 --> 03:49.280
Serial uppercase with the uppercase U open close parentheses.

03:49.290 --> 03:52.650
And first we are going to give the port.

03:53.100 --> 04:00.900
Okay, So if you remember the port was slash dev slash tty ACM zero.

04:00.900 --> 04:04.800
So that is the port I have found actually in the previous lesson.

04:04.890 --> 04:04.970
Okay.

04:05.010 --> 04:10.560
So if you are not sure what port is yours because that may be something different here, please go back

04:10.560 --> 04:17.250
to the previous lesson on how to set up serial communication and then come back here with the port that

04:17.250 --> 04:18.720
corresponds to your Arduino.

04:18.750 --> 04:21.360
The next parameter is the baud rate.

04:21.360 --> 04:24.660
So the baud rate is actually what you can see here.

04:24.660 --> 04:27.270
That's the speed of communication basically.

04:27.270 --> 04:30.090
And this is super, super important.

04:30.090 --> 04:34.800
You have to have the same baud rate on both sides of the communication.

04:34.800 --> 04:34.950
Okay.

04:34.950 --> 04:40.230
If you have a different baud rate, then the two programs can't understand each other.

04:40.230 --> 04:46.120
And actually speaking of baud rate, this is the pretty common classic baud rate you're going to have

04:46.120 --> 04:49.900
in every tutorial, but it's also a pretty slow one.

04:49.930 --> 04:55.030
You can go up to 115,002 hundred baud.

04:55.150 --> 04:55.540
Okay.

04:55.540 --> 04:57.430
And that's the baud rate I'm going to use.

04:57.430 --> 05:01.810
So if I use this here, I use this here also.

05:02.050 --> 05:02.440
Okay.

05:02.440 --> 05:04.390
So you can have different values, okay?

05:04.390 --> 05:06.550
You have different common values.

05:06.550 --> 05:11.620
And this is one of the most common one and also a quite fast one.

05:11.620 --> 05:14.800
And it's better than the classic 9600.

05:14.890 --> 05:21.070
And I'm going to add a third parameter, which is timeout, let's say 1.0.

05:21.070 --> 05:24.370
So timeout here is read timeout.

05:24.370 --> 05:26.980
I'm going to come back to that a bit later in the course.

05:26.980 --> 05:28.840
Okay, So don't worry about that for now.

05:28.900 --> 05:29.410
All right.

05:29.410 --> 05:36.970
So with this line, we open the serial communication and if it works, okay, we're going to get the

05:36.970 --> 05:38.950
serial inside this cell.

05:39.130 --> 05:43.600
If it doesn't work, we're going to get an error and the program is going to exit.

05:43.630 --> 05:50.140
Then what I recommend you to do is to wait a few seconds here after opening the communication.

05:50.140 --> 05:57.400
So I'm going to do import time and do time dot sleep for three seconds.

05:57.400 --> 06:00.130
So I recommend at least two seconds.

06:00.130 --> 06:06.550
Three is even better because, well, when you open the serial communication on that side, the thing

06:06.550 --> 06:12.970
is that on the Arduino side, the Arduino for Arduino Uno, for example, is going to restart.

06:12.970 --> 06:15.040
So the program is going to be restarted.

06:15.040 --> 06:21.460
So what you want to do on the Raspberry Pi side is to give enough time for the Arduino to have the serial

06:21.460 --> 06:23.800
completely ready to communicate.

06:23.800 --> 06:29.080
And so we give three seconds here, and I recommend you do that because if you don't do that and you

06:29.080 --> 06:35.740
try to read some data or just send some data just after that, you're going to experience some weird

06:35.770 --> 06:37.030
bugs in your program.

06:37.030 --> 06:39.160
And that's going to be hard actually to debug.

06:39.160 --> 06:43.300
So better have a time.sleep with 2 or 3.

06:43.300 --> 06:51.040
And then I'm going to do set dot reset input buffer.

06:51.850 --> 06:53.230
So what is this?

06:53.230 --> 06:57.760
So basically with serial, both sides can communicate with the other.

06:57.790 --> 06:58.000
Okay.

06:58.000 --> 07:04.090
So I can send data from the Raspberry Pi to the Arduino and I can send data from the Arduino to the

07:04.090 --> 07:06.940
Raspberry Pi when the data arrives.

07:06.940 --> 07:08.530
It's not directly in the code.

07:08.530 --> 07:11.590
When the data arrives, it's in a buffer.

07:11.620 --> 07:11.860
Okay?

07:11.860 --> 07:16.150
And when you're going to read, that's what we're going to see in the next lesson.

07:16.150 --> 07:20.110
So when you're going to read, you're going to read actually from the buffer.

07:20.110 --> 07:27.790
So here we reset the input buffer, which means that if the Arduino sends us some data before this,

07:27.970 --> 07:32.410
so when the program is actually paused, we're going to erase everything.

07:32.410 --> 07:37.630
So we start after this line with a fresh buffer that has nothing in it.

07:37.630 --> 07:38.050
Okay.

07:38.050 --> 07:42.130
And I'm going to come back also later in the course to explain more about it.

07:42.130 --> 07:42.490
All right.

07:42.490 --> 07:42.940
So that's it.

07:42.970 --> 07:45.130
Now let's maybe put print.

07:46.420 --> 07:47.110
Sale.

07:47.320 --> 07:47.950
Okay.

07:49.290 --> 07:51.240
To have a nice confirmation and then.

07:51.240 --> 07:53.130
Well, the problem is going to exit.

07:53.520 --> 07:54.870
Okay, Now what to do?

07:54.870 --> 07:59.280
Because we have the Arduino program here, the Raspberry Pi program here.

07:59.280 --> 08:04.730
But what you need to do first is to upload the Arduino code on the Arduino IDE.

08:04.890 --> 08:05.340
Okay.

08:05.370 --> 08:06.840
That's the first thing to do.

08:06.840 --> 08:09.390
Always is to have the Arduino ready.

08:09.390 --> 08:11.280
So I'm going to go on tools.

08:12.220 --> 08:18.670
Check that I have the correct board, the correct spot and let's do upload.

08:19.660 --> 08:19.930
Okay.

08:19.930 --> 08:25.030
Let's save it as first communication.

08:27.110 --> 08:27.350
Okay.

08:27.350 --> 08:28.190
You can see.

08:28.280 --> 08:31.610
Compiling and uploading and down uploading.

08:31.610 --> 08:34.460
So the code is now in the Arduino.

08:34.490 --> 08:39.980
Now that the code is in the Arduino, we go back here and I'm going to save the file.

08:39.980 --> 08:41.960
So save as.

08:43.100 --> 08:44.720
So let's create a new folder.

08:44.720 --> 08:46.210
Actually, let's go here.

08:46.220 --> 08:48.650
So I'm going to do directly from the file manager.

08:48.890 --> 08:49.430
Okay.

08:49.730 --> 08:56.360
Right click new folder, Let's call it Python programs.

08:56.690 --> 08:57.830
Okay?

08:58.010 --> 09:00.080
And let's go back here.

09:00.080 --> 09:02.930
Let's do save as.

09:03.950 --> 09:05.390
In Python program.

09:06.640 --> 09:11.230
First communication and dot pi.

09:11.260 --> 09:13.240
So that's the python file.

09:13.630 --> 09:14.110
Click on.

09:14.110 --> 09:14.840
Okay.

09:14.860 --> 09:17.950
So the program is saved and now I can run it.

09:17.980 --> 09:23.290
So to run it, I can just click on the play button here or run from the terminal.

09:23.290 --> 09:25.390
So I'm just going to click here.

09:27.080 --> 09:33.540
And you can see we have so we run the script and after three seconds we have cereal.

09:33.590 --> 09:34.330
Okay?

09:34.340 --> 09:39.350
Which means that the Raspberry Pi could correctly connect to that port.

09:39.380 --> 09:39.810
Okay.

09:39.830 --> 09:42.830
So it could correctly connect to the Arduino.

09:42.860 --> 09:51.470
Now, one important thing you have to do also on the Python side is that so here we directly exit after

09:51.470 --> 09:51.800
this.

09:51.800 --> 09:54.590
So we open communication and then we exit.

09:54.620 --> 10:01.790
What you have to do and what is better is to close the communication so you can do self dot close.

10:02.540 --> 10:02.830
Okay.

10:02.840 --> 10:07.490
Before you exit the program or whenever you want to close the communication.

10:07.490 --> 10:11.750
So with this, you're going to open the communication with this, you're going to close it.

10:11.750 --> 10:16.070
And on the Arduino side, you don't need to actually close the communication.

10:16.070 --> 10:16.250
Okay?

10:16.250 --> 10:20.870
The communication is going to be closed simply when you power off the Arduino.

10:20.870 --> 10:27.780
But here as a best practice, it's better to close it because then if it's not closed correctly and

10:27.780 --> 10:31.530
you try to run another program, you may have some troubles.

10:31.740 --> 10:33.480
Okay, So let's run that again.

10:33.990 --> 10:39.960
You can see we wait three seconds, say okay, and then we close the communication here.

10:39.990 --> 10:41.060
Everything is good.

10:41.070 --> 10:47.550
Now, let's say that, for example, I don't have my Arduino board connected to the Raspberry Pi, so

10:47.550 --> 10:53.880
I have physically removed the USB cable from the Arduino side or the Raspberry Pi side.

10:53.880 --> 10:54.990
It doesn't really matter.

10:54.990 --> 10:57.420
And I'm going to run the script again.

10:58.340 --> 11:00.170
And you can see we have an error.

11:00.470 --> 11:00.920
Okay.

11:00.920 --> 11:03.050
We have an error, which is quite big.

11:03.050 --> 11:06.680
But at the end you can see single exception.

11:07.700 --> 11:12.580
Could not open port slash dev slash zero.

11:12.830 --> 11:13.270
Okay.

11:13.280 --> 11:15.650
So basically the port is not found.

11:15.680 --> 11:16.100
Why?

11:16.130 --> 11:18.290
Because the Arduino is not connected.

11:18.320 --> 11:21.950
If I plug the Arduino to the Raspberry Pi again.

11:22.160 --> 11:23.450
Let's run the code.

11:23.450 --> 11:25.400
And now it's working.

11:27.090 --> 11:27.720
Great.

11:29.380 --> 11:32.800
So to recap on the steps here, that's very important.

11:32.830 --> 11:39.040
First, you will need to write some code for the Arduino where you initialize Serial, of course, and

11:39.040 --> 11:41.350
you need to upload that to the Arduino.

11:41.380 --> 11:47.320
When you upload to the Arduino, make sure that you are not currently talking to the Arduino from another

11:47.320 --> 11:48.640
program of your Raspberry Pi.

11:48.680 --> 11:49.080
Okay.

11:49.090 --> 11:54.290
Because when you upload and when you communicate, you use the same cellular communication.

11:54.310 --> 11:54.870
Okay.

11:54.880 --> 12:00.250
So if you are sending data from your Raspberry Pi to the Arduino with a Python program and you try to

12:00.250 --> 12:02.590
upload, you may have some problems.

12:02.890 --> 12:03.220
Okay.

12:03.220 --> 12:08.830
Now that the code is on the Arduino, the code is going to run as soon as the Arduino is powered on.

12:08.830 --> 12:13.930
And then you can run your Python script where you open the serial communication.

12:13.930 --> 12:16.840
So make sure you have the same baud rate.

12:16.960 --> 12:17.190
Okay?

12:17.200 --> 12:19.150
And make sure you have the correct port here.

12:19.150 --> 12:25.380
And basically on some boards like Arduino, Uno, Nano Mega, for example, when you open the serial

12:25.390 --> 12:30.800
communication from the outside of the Arduino, the Arduino program is going to be reset.

12:30.890 --> 12:32.350
It's going to be restarted.

12:32.360 --> 12:38.300
So at this point of the code, this is going to be restarted from scratch.

12:38.300 --> 12:41.060
So that's why you want to wait a few seconds.

12:41.060 --> 12:42.830
So here we wait three seconds.

12:42.830 --> 12:49.640
That's very safe so that the cell communication can correctly be initialized and everything can be initialized

12:49.640 --> 12:50.990
on the Arduino side.

12:51.020 --> 12:53.050
Then you reset the input buffer.

12:53.060 --> 12:56.960
You do whatever you want to do here, you can send and receive data.

12:56.960 --> 13:00.200
That's what we're going to see directly in the following lesson.

13:00.200 --> 13:07.550
And then you close the communication here on the Raspberry Pi side and you can exit the program.
