Raspberry Pi and Arduino - Go to the Next Level | Edouard Renard | Skillshare

Playback Speed

  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x

Raspberry Pi and Arduino - Go to the Next Level

teacher avatar Edouard Renard, Software Engineer and Entrepreneur

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

71 Lessons (8h 48m)
    • 1. Welcome!

    • 2. Raspberry Pi vs Arduino, Raspberry Pi and Arduino?

    • 3. List of Materials for this Course

    • 4. How to Follow the Course

    • 5. Installation Steps

    • 6. IMPORTANT: Don't Install Raspberry Pi OS Bullseye

    • 7. Install Raspberry Pi OS Without a Monitor - Recap

    • 8. Install and Configure Arduino IDE on Raspberry Pi OS

    • 9. PART 1 - Serial Communication Between Raspberry Pi and Arduino

    • 10. Hardware and Software Setup for Serial

    • 11. Initiate Serial Communication

    • 12. Send Data from Arduino to Raspberry Pi

    • 13. Send Data from Raspberry Pi to Arduino

    • 14. Create a Bidirectional Communication

    • 15. Debug Serial Communication if Can’t Connect

    • 16. Retry Connecting with Python if Failed

    • 17. PART 1 - Practice

    • 18. Activity 01 - Power on/off Arduino LED from Raspberry Pi

    • 19. Activity 01 - Solution

    • 20. Activity 02 - Bidirectional Communication

    • 21. Activity 02 - Solution

    • 22. Activity 03 - Asynchronous Bidirectional Communication

    • 23. Activity 03 - Solution

    • 24. PART 2 - Arduino Functionalities (Hardware Components)

    • 25. RGB LED - Circuit

    • 26. RGB LED - Code

    • 27. Push Button - Circuit

    • 28. Push Button - Code (With Debounce)

    • 29. Buzzer - Circuit

    • 30. Buzzer - Code

    • 31. LCD Screen - Circuit

    • 32. LCD Screen - Code

    • 33. Servo Motor - Circuit

    • 34. Servo Motor - Code

    • 35. PART 2 - Practice

    • 36. Activity 04 - Debug Serial Using LCD Screen

    • 37. Activity 04 - Solution

    • 38. Activity 05 - Set RGB LED from the Raspberry Pi, when Pressing on Push Button

    • 39. Activity 05 - Solution

    • 40. Activity 06 - Sweep Servo From the Raspberry Pi

    • 41. Activity 06 - Solution

    • 42. PART 3 - Raspberry Pi Functionalities (Camera and Telegram Bot)

    • 43. Camera - Hardware Setup

    • 44. Camera - Software Setup and Get Photo with Python

    • 45. Telegram Bot - Setup Telegram Account

    • 46. Telegram Bot - Create a Bot and Save the Token

    • 47. Telegram Bot - Handle Remote Commands

    • 48. Telegram Bot - Send Notifications and Messages to Your Phone

    • 49. Telegram Bot - Complete Program

    • 50. PART 3 - Practice

    • 51. Activity 07 - Send a Notification to Telegram when Arduino Board is Connected

    • 52. Activity 07 - Solution

    • 53. Activity 08 - Choose RGB LED Color and LCD Text From Telegram

    • 54. Activity 08 - Solution

    • 55. Activity 09 - Take Photos and Send Last One to Telegram when Button is Pressed

    • 56. Activity 09 - Solution

    • 57. PART 4 - Complete Intercom System (Final Project)

    • 58. Project Specs

    • 59. Step 1 (Protocol) - Define the Protocol we will Use (Serial, Telegram)

    • 60. Step 2 (Arduino) - Init Hardware Components

    • 61. Step 3 (Arduino) - Write to Serial when Button is Pressed

    • 62. Step 4 (Arduino) - Get Commands from Serial and Execute Them

    • 63. Step 5 (RPi) - Get Button Pressed Message from Serial

    • 64. Step 6 (RPi) - Take Photo when Button is Pressed

    • 65. Step 7 (RPi) - Send Message and Photo to Telegram

    • 66. Step 8 (RPi) - Open the Door From Telegram

    • 67. Step 9 (RPi) - Deny Access From Telegram

    • 68. Step 10 (systemd) - Make the Intercom System Start on Boot

    • 69. Project Conclusion - Going Further

    • 70. Build an App with Raspberry Pi and Arduino - Step by Step Process Recap

    • 71. What to do next

  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels
  • Beg/Int level
  • Int/Adv level

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.





About This Class

You’re already familiar with Arduino and Raspberry Pi?

You want to go further with those boards, and combine them to create more complex and powerful projects?

Welcome to this Raspberry Pi + Arduino class!

I will take you from an intermediate level on the two boards, to an advanced level, not only on each board, but on the combination of both.

At the end of the class, you will be able to create your own custom projects with Raspberry Pi and Arduino.

Note: this class is not for complete beginners, for example I will not explain to you how to write basic Python or C++ programs. You definitely don’t need to be an expert, but having a good understanding of both Arduino and Raspberry Pi will really help you get started.

→ Why this class?

Working only with Raspberry Pi, or only with Arduino, is already nice: you can do a lot of projects.

But sometimes, this is not enough. You will need to combine the strength of both boards: the power of software with Raspberry Pi (brain), and the closeness to hardware with Arduino (muscles).

When looking for tutorials/classes about this on the Internet, I found that they are all very simplified, and work with a very simplistic setup, and only for that setup. If you add any component, any functionality, you’re going to be stuck. 

So in this class, you won’t get a copy/paste example, but a complete (and efficient) step by step process to create any application with Arduino and Raspberry Pi. 

I will show you how to start a successful Serial communication between the 2 boards, and how to build an application with new components and features around this bridge.

→ How will you learn, how do I teach?

My method is simple but efficient:

  • WHY: Focus on the “why” so you understand what you’re doing.
  • HANDS-ON lessons to learn in a better way.
  • STEP BY STEP: Every lesson is built on top of previous ones.
  • PRACTICE with many activities and projects.
  • NO COPY AND PASTE!!! I write all the code from scratch.

→ What will you do in the class?

After an installation section to get all the tools ready for the class (including installing Arduino IDE on your Raspberry Pi OS), you will progress through 4 different parts:

  1. Communicate with Serial between Raspberry Pi and Arduino.
  2. Build the Arduino circuit for the project, one component at a time. Control Arduino components from the Raspberry Pi
  3. Discover new Raspberry Pi functionalities - Camera and Telegram bot. Control the camera from the Arduino, and send commands to Arduino from a Telegram chat.
  4. Complete Intercom Project (see preview video for an overview of the result). 

For each part of the class (1-3), you get several additional activities, so you can practice on what you’ve just seen, and link the new knowledge with the previous sections.

For the final project, together we will design a complete communication protocol, and we will also practice on the steps design: basically, how to plan/organize your application before writing code. I will give you some tips to help you design and organize your future projects, so you can start them and know what you need to do, in what order. This is something super important that I don’t often see in other classes or tutorials, and I’ve made sure to focus on this when creating the steps for the final project.

So, you want to build more complex applications with Raspberry Pi and Arduino? Don’t wait any longer and start this classe today!

As always, my goal is to give you the freedom to start and finish your own projects. With this freedom you won’t have to rely on some copy/paste examples from the internet, because you will have the foundation that allows you to really understand how things work, and build anything from scratch.

And that’s not all: all along the class I will share with you the best practices I personally use for my own projects. You will be able to create robust and scalable code - both for Arduino and Raspberry Pi.

See you in the class!


This class is for:

  • Students, Engineers, Researchers, Teachers, Developers, Hobbyists.
  • Arduino and/or Raspberry Pi makers who want to combine the 2 boards for more fun and complex projects.
  • Anyone interested in going further with Arduino and Raspberry Pi.
  • Anyone who wants to build serious robotics or home automation projects.

This class is not for you if:

  • You are a complete Arduino or Raspberry Pi beginner (in this case, watch first my “Arduino for Beginners” and “Raspberry Pi for Beginners” here on Skillshare).
  • You want a copy/paste solution to your problems and don’t want to take the time to understand.

Prerequisites for this class:

  • Arduino board
  • Raspberry Pi board
  • And a few hardware components (the list of components is available in a lesson at the beginning of the class + PDF “List of Materials”)

Meet Your Teacher

Teacher Profile Image

Edouard Renard

Software Engineer and Entrepreneur


Hi, I'm Edouard. I’m a software engineer and entrepreneur.

I’ve been working on programming robots for years. When I first started I really had a lot of trouble trying to properly learn. I found what works and what doesn’t work, what I needed, and what I didn’t need. So now I’m sharing that with you, so you can save the precious time you have.

Also I have co founded a robotics startup and programmed an entire robotic arm from scratch, with ROS, Raspberry Pi and Arduino. My view on software and robotics is very practical: I’m interested in how to best use a language/framework to build useful applications. 

And thus the approach I have in my online courses is really down to earth and practical.

I like to make complex stu... See full profile

Class Ratings

Expectations Met?
  • Exceeded!
  • Yes
  • Somewhat
  • Not really
Reviews Archive

In October 2018, we updated our review system to improve the way we collect feedback. Below are the reviews written before that update.

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.


1. Welcome!: Welcome to this intermediate course on Raspberry Pi and Arduino! So, you have already experimented with both Arduino and Raspberry Pi? And now you wonder how you can go to the next step, and how you can create cool projects, using them together? Well, you are in the right place: This course is for you. I am Edouard, a software engineer, entrepreneur, and robotics teacher. In the past, I have built the software for an entire six axis robotic arm with a combination of Arduino and Raspberry Pi. I have also already taught both topics. In fact, you can find here on Skillshare, my "Arduino for Beginners", and "Raspberry Pi for Beginners" courses. If you are still a beginner, I would recommend you to watch them first as this new course is basically built on top of them. At the end of the course, you will be able to create any custom project of your own with confidence using both Raspberry Pi and Arduino. And I'm not just going to give you a copy-and-paste project. No: I will focus on making you understand the fundamentals you really need through a practical approach with hands-on videos and a ton of practice. Everything is step-by-step and built on top of what you've seen before. Now, what will you do in the course? Well, the course is divided into four main parts. First, you will make the Raspberry Pi and Arduino talk to each other using serial communication. We will then focus on a few Arduino components that you will control from the Raspberry Pi. And a few additional Raspberry Pi functionalities that you can interact with from the Arduino, for example, a Telegram bot, so you can send notifications to your phone. This will lead us to the final project of the course, a complete intercom system, uniting the best of Raspberry Pi and Arduino in one single application. This project will make your practice on everything you will see in the course, plus more. And that's not all! As always, I'm starting with the best practices in mind that I share with you all along the course. And more than just saying "do this and then do that", I focus on making you understand the "why" and making you think by yourself to be more independent for your own future projects. Alright, so if you want to go to the next level with Arduino and Raspberry Pi, let's get started right now! 2. Raspberry Pi vs Arduino, Raspberry Pi and Arduino?: Before we get started with the clues, let's understand why. Why use both Raspberry Pi and Arduino, and why not just Raspberry Pi are just Arduino. In this video, I will explain this to you with a practical mindset. So you can start with a better understanding, which will help you through how the, cuz the first and maybe the biggest difference is that Arduino is run by a microcontroller, whereas the Raspberry Pi is run by a microprocessor. So the microcontroller on the Arduino is quite simple. It's going to run just one program at the maximum speed capacity. That's it. The Raspberry Pi, you have a microprocessor, which is something completely different. A microprocessor is what you also have on your computer, on your phone, etc. You usually install an operating system on it. And then you can run many programs to do many different things. If you compare the specs of the microcontroller on the Arduino Uno and the microprocessor on the Raspberry Pi 4. Well, there is a world of difference, but, and that's what we're going to see now with those specs and a few more details I'm going to add later. You will see that the Arduino is better suited for controlling hardware components. The Raspberry Pi is better suited for software applications with more processing power. Now, you might say, well, there are some applications that you can do with either an Arduino or a Raspberry Pi. For example, if you try to control an LED with a push button, this can be done with both boats with the same result. So yes, for a few applications, both bolts are going to overlap. And for those kinds of applications, there is no right or wrong solution. Now, let's focus on what the Arduino can do better than the Raspberry Pi. First, there are some hardware functionalities that are only possible with Arduino. The GPIO panel on the Raspberry Pi is great, but quite limited when you compare it to the pins on the Arduino. For example, only with Arduino, you can read from an analog sensor that can be a potentiometer, a photoresistor, etc. There is no way to do that directly with the Raspberry Pi GPIO. Also, some of the pins have a PWM functionality, which very basically allows you to send a custom voltage to a component. Instead of just for example, 05 volt on Arduino, PWM is native and is handled by the hardware, which means that it's not going to take away any software resources. On the Raspberry Pi. You could use PWM, but this is not a hardware one. This is a software PWM, which means that it's going to take resources from your CPU. And it's maybe not going to be as stable as on the Arduino, then the Arduino is best suited to control models. And one additional thing here, which is quite important, is the capability to respect real-time constraints. So what is the real-time constraint? Let's use an example here. So let's say that you are controlling a motto. I need to give a command to the motto every 20 microseconds. If you fail to give the next command before 20 microseconds, the motor will not behave very smoothly. So with the Arduino, no problem because of the nature of the microcontroller is just running one program. And it's what we call deterministic, which means that you know exactly how long the command will take to execute every time. So if you write your code well, you can respect the 20 microseconds delay every time. On the Raspberry Pi, things are completely different. The operating system you are running has a scheduler for all the tasks. The scheduler will try its best. All the tasks run as smoothly as possible, but it can't guarantee that one particular program is going to respect exactly 20 microseconds between each commanded sense. If our example, another independent program is taking too much. We're Susie's. Then the first program which controls your motto is going to slow down. And that's a problem, especially when you control physical hardware components. So in this course for the activities and projects, we won't have to deal with this real-time constraint. But I wanted to explain it briefly here so you can understand that it's very important parameter which you will need to take into account for some of your future projects. All right, you can already see that the Arduino is superior to the Raspberry Pi for quite a few things that are related to hardware control. Now let's give a chance to the Raspberry Pi and see when it is better than the Arduino and Raspberry Pi, you get a complete operating system, for example, Linux with the Raspberry Pi OS, and you can install many more OS's. For example, you went to Android and even some versions of Windows if you want to with a complex operating system will, instead of running just one program, you can see that we have a world of possibilities that are not available on the Arduino and Raspberry Pi, you can program with C plus plus. But also with Python, which we are actually going to do in this course. In fact, you could use any programming language you want. And now a few more examples. We've Raspberry Pi. You can control a camera, for example, the Pi Camera or any USB camera, a webcam that you just need to plug in and do that. You can add computer vision, artificial intelligence with machine learning, etc. You can also host a complex web server if you want to. And well, on the Raspberry Pi, basically you can just run many different applications and programs simultaneously with scheduling and multithreading. Multithreading is super powerful. It's something that doesn't exist on the algorithm on Arduino. By writing your code, well, you can kind of fake it and create a multi-task programmer. But nothing to see what you can do with the Raspberry Pi. Okay, I'm going to stop here because the list could go on and on and on forever. But what you can see here is that the Raspberry Pi is superior to the adrenal when it comes to software. To recap, how do we know is better for hardware are low-level control and Raspberry Pi is better for application software or high level control. So depending on your project, maybe you just need one bolt if your obligation is to take some photos, process them, and publish them on the web page, then just go with a Raspberry Pi. If however you want to just open a door with a motto when you detect a presence nearby, Arduino will do a 100 percent. But if you need to combine both hardware control and high level software applications, then why not use the best of both worlds in use the combination Raspberry Pi and Arduino. With this combination, you can do a much more complete and complex projects by taking advantage of each boards superiority. That's what we're going to do. Intercom projects where we will use the Raspberry Pi to handle a telegram bought and take some photos. And the Arduino to control a settlement or as well as other hardware components that use, for example, PWM. The Raspberry Pi will send comments to the Arduino. And the Arduino will also send some data back to the Raspberry Pi and you may not be aware of It's, this combination of microprocessor. Micro-controller is actually used everywhere. If you have a phone, a computer, a TV, well, all of them are using a microprocessor, are the main software control. And then this microprocessor is connected to many microcontrollers which are handling the hardware. A more robotic example, maybe if you take a mobile robot with autonomous navigation, well, you would have a microprocessor to handle localization, high-level motion planning, image processing, et cetera. And you have some microcontrollers to control the wheels with real-time constraints and also get data from different sensors. To conclude, you can see the Raspberry Pi, our microprocessor at the brain of your application. And the Arduino or microcontroller as the muscles of your application. All right, now that you have a better understanding of the big picture, you are ready to start the course. 3. List of Materials for this Course: Here is the list of materials you will need in order to complete 100% of the cookie. You can also download a PDF containing all the information as an additional resource. So first, you will need, of course, a raspberry pie board. I suggest you get the Raspberry Pi 4 version, but if you have version three or two, that's also fine. Now, if you get the Raspberry Pi 4, you can choose between 248 gigabytes of RAM. The mold better, of course, but it also gets more expensive. I personally use the two gigabyte version and it works perfectly fine for me. For your Raspberry Pi, you will need also a power supply. Don't Powell your Pi directly from your computer, okay. Your computer can't deliver enough power for the Raspberry Pi to work correctly. So if you have a power supply included when you buy the Raspberry Pi, That's perfect. Otherwise, you can also use a phone charger with at least 20 amp and five volt, and that's what I personally do. I use my own phone charger. Now you will need a micro SD count, so you can install an operating system on your Raspberry Pi. Makes sure you use a micro SD card that is at least class 10 and with minimum eight gigabytes of space, 16 or 32 gigabytes of space is better if you can, and you really don't need more than that. And we will also use the Raspberry Pi camera module. This, I would say can be optional. If you can't get one, you will still be able to finish the course, but the final project won't be complete without a camera. So if you get one, note that you have two different models. The standard one with a green ball and the NWA version with the blackboard. The black version is perfect for walking in the dark, but won't produce good-looking pictures in daylight compared to the standard green version for these goods, I will be using the standard one, and this is the one I recommend if you're going to buy one and don't have a specific project which requires working in the dark. Either way, all of the instructions will work for both cameras are right. The components for now are the ones related to the Raspberry Pi side of the coops. Now let's look at the Arduino side. And of course you will need first Arduino baud plus a USB cable that you usually get when you buy the board. Here you can use pretty much any Arduino board you want. It doesn't really matter as long as you can connect to it with a USB cable and that you can have at least 12 pins that you can use to connect components. I will personally use an Arduino Uno board and actually not even the official one, but a clone. So you can use any Arduino version and gets a none official clone if you want to, that's going to work without any problem. And then we need a bunch of hardware components that we are going to control with the Arduino. One note for those components, you can usually find them all, almost all in either an arduino starter kits or a breadboard kids. In fact, all the components are used for the course projects are part of an arduino starter kits I previously bought, which you can see here on the screen. I want to keep things simple so you can reduce the amount of stuff you need to buy. So either in Stata kids are both separately. You will need a breadboard. Some male to male wires, as well as some resistors. We will use 220 ohm as well as 10 kilo Ohm resistors. And RGB LED, which is an LED with four pins. And well, if you don't have one, you can also replace it with three standouts, LEDs, that's going to work the same. You will need a push button, a passive buzzer that you can recognize with the PCB that you can see on the bottom, you should see at the bottom of the passive buzzer is green. You will need an LCD screen. The one I will use these 16 by 20 character screen. It's pretty common. That note here that if you can get it's already soldered, that's better so you don't have to do it's by yourself. And then a potentiometer, which we will actually use to calibrate the LCD screen. And a several motto If you can also, but that's not mandatory for this course. You can also get an external power supply for the several motto. If you do so, make sure to respect the max voltage you can apply to the server. You can usually find this voltage on the servomotor description specs are datasheets. Well, that's it for the list of parts we will use. If you already have an Arduino board and a Raspberry Pi bold, you can already start the course while waiting for the other components. 4. How to Follow the Course: Please watch the entire video before going to the next section. So this course is mainly divided into four parts. The first part, on sale communication starts after the installation and configuration. The second part will target Arduino functionalities, and the third part, Raspberry Pi functionalities. Each of the paths 123 are divided into a hands-on explanation section and a practice session with mini-projects that I also called activities. Finally, the fourth part is the final project of the course, which is the intercom system. You had a preview off. Now a few recommendations so you can get the most out of these groups. First, follow the course in the other, because all the sections and listens are built on top of each other, then you can come back to any lesson. But first, start from the beginning. When I write code and do experiments. First, watch the lesson, and then write the code and do the experiments by yourself. Watching plus doing is much more efficient than just watching. And finally, I will give you many activities and projects during the course. They can be quite challenging, but make sure to give them a try and spend a fair amount of time on them before you watch the solution. This is the only way you will truly make progress. In one additional word of caution will pay attention when manipulating the hardware. Always bought off everything before you modify the secret. And also for the Raspberry Pi, makes sure you correctly shut it down like a normal computer before you remove the power cable. And we've got Sayed, let's start with the installation and configuration for the schools. 5. Installation Steps: In the following lessons, you are going to install and set up the Raspberry Pi and the Arduino. So then we can focus on the important stuff folder, cuz so if you are here and check the prerequisite for the course, you should already know how to install Raspberry Pi operating system. But I will still provide you with installation instructions just in case. So you won't be stuck and you can follow the coups. Now if you already have Raspberry Pi OS installed, then go directly to the lesson on how to install Arduino IDE. I know you may already have Arduino IDE installed on your computer, but here I will do something slightly different, which is to install Arduino IDE directly on the Raspberry Pi operating system. This is going to be very practical. So we can upload code to the Arduino as well as communicate with it from just one place. And let's get started. 6. IMPORTANT: Don't Install Raspberry Pi OS Bullseye: This is a quick update for this course just before you start to install Raspberry Pi OS on the SDK. Okay, This is very important. Please watch the entire video. So what's happening? Well, there is actually a new version of Raspberry Pi operating system, which name is bull's eye? Can. A previous version was busted. How to recognize it very quickly. When you install it with both eyes, you get this wallpaper with the cloud ones. If you install Bestor, you get this wallpaper with the temple. And so what is wrong with this new version? Because, well, the nutrition is actually cool, right? You get new functionality is et cetera, et cetera. But, well, this new version is actually a bit broken. For example, you may have some problems with screening resolution on the NC at, we're going to see later. You will also have problems when using the camera functionality on the terminal end with the Python module that we will use in the coast. So the camera functionality is completely broken. What can we do about it? Well, not much. The only thing we can already do is to wait until the fix, all the problems. And that can take maybe a few weeks, a few months. And until then, well, there is no really point to just try to fix stuff like that. It's going to take so long. And until then when I just recommend is to use the previous version, raspberry Pi operating system duster, which is still completely valid and works for everything in this cooks. Okay, so use this version and then I'm going to update the cross again when this new version is actually stable. And so I made this video to explain you this and also to show you what you need to change for the future lessons of the course, because we're going to use the Raspberry Pi imager. I'm going to explain that just in a few moments in the course, okay? But at this point where we will be there and we will click on operating system and choose the first one. Well, you would not choose the first one because the first one is actually for hi here. So what we're going to do is we're going to download busted and to download Buster. Well, there is no way to actually downloaded from the Raspberry Pi major. There is no way to download it from the Raspberry pi.com website because you can only download the latest edition. So where to get it? Well, he lets go. Actually, here. You will go to this exact URL, okay, downloads dot Raspberry Pi.org. And here you have all the download history for, well, Raspberry Pi Foundation. And while you have a lot of stuff, you're gonna go down beat until you find this, this folder, Raspberry Pi. So raspy OS, ARM H, F, okay, this exact one, you click on that and then you're going to click on images. Okay, You should be in that folder. And then well, the latest one he is. So if you click on that, you will see raspy OS, bull's eye. This is the one that they are going to make you install with the Raspberry Pi in major, but that's not the one you want. So we go back and you're going to click on this one, 2021 528. And you can see here we have buster. This is the latest version of buster, which is super stable and will work for everything we need in the coops. And so you're going to download this one. So you just click, Save the file and you download it. Okay, once you have downloaded this, you are going to extract it. So I'm going to extract here because you have dot zip. What you want is the dot m, g that is inside the dot zip. Okay? And once you have the dot IMG in the Raspberry Pi major, you will be able to choose here with US Customs at the very end. And where you have this, this one. So the PNG, make sure you choose the EMG and the zip file. Click on Open. And this is the operating system you're going to install. And then, well, you can just follow all the steps we're going to do in the next lessons. 7. Install Raspberry Pi OS Without a Monitor - Recap: The very first step before using the pie is to flash an operating system on the SD card, you have to do some configuration. In this lesson, I will show you how you can easily install the Raspberry Pi operating system on your micro SD card, but also how to set up the WiFi and an SSH connection so we can get access to the pie for further configuration. So to install Raspberry Pi OS, the first thing you do is you go on raspberry pi.org or you have the URL, and then you can click on software, okay? And you're gonna get this page are something that is maybe similar, okay? And you're going to have here install Raspberry Pi place using Raspberry Pi imager. So we're going to download the Raspberry Pi imager, which is a software that is going to do everything for us. So that's going to make our life simpler. So you can click here on download for Windows if you're on Windows currently on MacOS, are you going to, I'm going to download for Windows. Okay, so I'm going to save the file. And once it's done, I'm going to install the Raspberry Pi imager. Okay, so you click on yes, if you have a pop-up and then click on Install. Okay, Finish. And if you want to find it easily, well, you just type Raspberry Pi imager on your bar here at the bottom and just, just find the application here, or maybe it's on your desktop. So once you're here, you're going to close that. Once you're here, you're going to first choose an operating system that you're going to flush in the SDK. And you're going to choose the first one, okay, Raspberry Pi or ice here. So just to the first one you can see you have many different ones. For example, you can choose, well, you can see many different operating system, but we're going to go with the main one, Raspberry Pi OS, which was previously named Raspbian. Okay, So if you are looking into installing Raspbian, well that's not a new name, Raspberry Pi place. And then what you are going to do. So I'm going to put I'm going to physically put my SD card in the computer. Okay. Maybe that's going to open something like this. Well, you don't care about that. Okay. You just close the windows. You can see I already have stuff on that. I'm going to just close and I'm not going to format anything for now. Okay, and I'm going to click on Choose storage and choose the SD card here. And you have the SD card. And one more thing I'm going to do now, and this is a configuration that you can't see on the screen. I'm going to need to use a shortcut Control Shift and x. Okay, So you do Control Shift and x and you see we have a new window here. We've more configuration. And so what I'm going to do is I'm going to click on enable SSH. And we're going to use, use password authentication with the password for the user, which is raspberry. So you can just type raspberry here, okay, to set the password for the user data, the default password. And then you can go down and you are going to click on Configure Wi-Fi. And these may be, is going to show you the current Wi-Fi you're connected in. So make sure that, well, here I'm on Windows, you can see I have, so I have named my network, your WiFi network, okay, for simplicity, for its schools. And the password is for me, your password, okay, So you just replace that. And so those values have to be the same ones you are using here for your computer, okay? So that then you can access the Raspberry Pi from your computer because you are going to be in the same network. Okay? And well, that's okay. So you can click on Save and then you can click on right. But before you do that, make sure the SD card doesn't contain anything that you want it to keep. Because once you click on right, that's going to erase everything. So you click on right. Okay. Everything will be erased. Are you sure? Yes. And then it's going to download the operating system. So you can click on Console if you need to. And it's going to download the Raspberry Pi operating system if it's already downloaded before it's going to keep it and retrieve it from the cache. And then he's going to write it inside the SD card and then you can wait until it's done. And that's going to take a few minutes. Okay? And once it is done with if you have any of that, you can just click on Okay, Council. Okay. Just don't click on format everything. And then you can see successfully written on SDK continue. And then what you can do is remove the SD card from your computer. And well, that's it. Now, you will be able to boot up your Raspberry Pi with the Raspberry Pi operating system. With the configuration from the last step on boot, your Raspberry Pi should automatically try to connect to the Wi-Fi network you've provided. So what you can do now is first, make sure that your Raspberry Pi is powered off. Okay, This is very important. Now, you can put the SD card in the SD card slot of the pie, okay, make sure it is correctly inside the slot, like you can see here on the screen. And then, and only then you can pour on your Raspberry Pi. So what you should see is first, you will have the red LED which should be pulled on. And then you should also seen just next to it the green LED blinking quite randomly. And if it's blinking randomly, it means simply that the Raspberry Pi is booting. And so you can wait a little bit and after a few seconds, maybe one minute top. The Raspberry Pi should be connected to your WiFi network. And now that is where we need to actually find the IP address of nice Raspberry Pi under network so we can get access to it. So to find the IP address, we are going to use a tool, okay, this is software you can download. Here I'm using the Angry IP Scanner, okay, So these two is quite good to find all the IP addresses and host inside a network. So I've chosen this one because simply it's available for Windows, Mac, or Linux, so anyone can use it with this course. You may have already used some specific software. For example, for Windows, you have advanced IP Scanner, which is as good as Angry IP Scanner even better. So if you already know other IP Scanner softwares for your operating system, feel free to use them. And if you are going to use Angry IP Scanner as I'm doing here, you can simply here, well, you can click on that and download and install the software just like you would install any software. And you will also have two, install Java here because the software uses Java. So you can click on here and just download and install the latest version here, paste. So you have to install Java plus this software. And if you are using Mac OS, you have the instructions here. And Linux you have the instructions here. And now that the software is installed well, you simply have to start it. And you might have another pop-up asking you to accept some stuff related to the network. So you have to press yes. Okay. So otherwise it will not work. So you have this software study then before we even do anything, please go back to check your WiFi network here and make sure that your laptop, so this laptop over there, is connected to the same Wi-Fi network as the Raspberry Pi. So as the configuration you have setup inside the SD card of the Raspberry Pi, okay? If it's not in the same network, you will never be able to find the Raspberry Pi form this computer. So this is actually a very important first step you need to check you. So once this is done, we'll go back to Angry IP Scanner. And before we press stamp, we have little bit of configuration here. So as you can see, we have the hostname. The hostname is the actually the host name of this measure him. So that is my host name. It will be different for you and you are going to click on that IP button with an arrow here. So you click on that and you will see different stuff here. So you may have just one or two or more like, like me. And you're going to select the one with Wi-Fi here. So this is actually, This is the IP address of that computer in the Wi-Fi network. I have a few other ones here, for example, with virtual machines. But if I chose them, will not be able to connect to the Raspberry Pi because it's not the same network. So I have to choose the Wi-Fi IP address. And as you can see, this will automatically update the IP range here. Okay? And what you're going to do is actually so you have four different numbers from IP address. You're going to change the last one here for the beginning of the range to, let's see 1 and the last word here to 255. So when you will scan, this will scan between 1255 in that network here. And your Raspberry Pi is somewhere between those two numbers. So when you scan, you will see the IP address, the host name, and port. But one thing actually you can do is also click on that. And we're going to add the Mac vendor here. I will show you why later. So you click on mag vendor, you put add here, so it should be under left and you click on Okay, so it shouldn't be here, that colon should media. And not just press Stat. Okay, and once this is done, you will have that kind of window message. So for me it took 30 seconds. It can take more or less time. So you can click on Close and you have, as you can see, 123456, et cetera, until to 55. What you can do is simply click on ping and sought by pink. So you have all of the different IP address which are actually corresponding to real machines. Okay? So what is blue? There is something when it's red, it means that nothing has been detected for this IP address. So we can see that actually that is the, the first one here. This is the laptop I'm using here. And I can see here the IP address finishing with 56 for me. So of course, this can be completely different for you. Tell me completely different number here. I can see a Mac vendor here, Raspberry Pi training. So it means I have found my Raspberry Pi on the network. Okay, and the reason I have asked you to add the mag vendor here is because, well, for some reason, it depends. Sometimes you may not be able to see the host name, which will be a Raspberry Pi. So if you use, for example, the advanced IP scanner software on Windows, you will see the hostname, but not with Angry IP Scanner. I mean, at least not for my specific situation. So here you can clearly see that this is the Raspberry Pi you're looking for. If you don't have any information here, you can still guess by looking at the blue dots here because you will only have a few, okay, So this is my computer. The first one will not be Raspberry Pi, it will not finish with a one. So here, even if I didn't have that, I could guess that this would be the Raspberry Pi IP address. Great. You've successfully installed the Raspberry Pi operating system, configured the Wi-Fi and you know, the IP address of the Raspberry Pi in the network. What we can do now is to use SSH to take control over the Raspberry Pi and really get access to it. So first of all, I'm going to open a file explorer here and go to my main partition here. Go to users to select your username, okay? And here you may see a dot SSH folder here. If you don't see it well, you have nothing to do and you skip the next 20 seconds or so. If you have an SSH folder, you're going to go inside and you may have known hosts, fine. What you can do is if you don't know SSH or stuff, you can just simply remove that file. No. It may prevent some errors when you try to connect to your Raspberry Pi. Okay? And now what you can do is to open a terminal, okay? So Windows key and then CMD or Command, and click on that and this will open a terminal, windows. So if you are using Linux, I guess you know how to use a terminal. And if you're using Mac, you can also open a new terminal. Okay? And if you're using Windows 10, windows 10 with a quite recently updated version, you can simply connect to the Raspberry Pi with SSH on the terminal. So you can easily check if you have SSH available for you on Windows 10, okay? Simply you can type SSH terminal and press Enter. Okay, Here I have a message, use age SSH. So if you have that, it means you have SSH and you don't need to do anything else. If you don't have that in. If instead you have a message like SSH command not found, then it means you won't be able to use SSH in the Windows Terminal. So in that case, in that very specific case on Windows, you'll need to install another software to use SSH. And you can, for example, use buddy login, go on, Perdido All. And you can simply download it, install the software, and then studied. So pulley is an SSH client. You can use it the same way we are going to use this common line here. This will just make you use a GUI tool. So you will have to fill up the IP address, the username, and the password, as we are going to do here. So coming back to the terminal, what you can do to get access to your Raspberry Pi is to type SSH and then space. And then you need to provide the username. So what is the username for Raspberry Pi, for your Raspberry Pi, well, the default username that is already here when you flush the OS into the car, is named simply by okay. So there is no way for you to discover that it's simply the default one that I'm giving it to you. Here. You can also find it on the Raspberry Pi website. So we are going to be connected as the user named pipe. Then you can put at the IP address that you found here with your IP scanner software. So for me it would be a 100, 92, that a 168, that 43 dot 56. So of course that will be different for you. Okay? So just use the IP address you found there. And then you can simply press Enter, okay, for the very first connection, you will have that message. Are you sure you want to continue connecting? Because actually the host here your machine doesn't know, has not been connected to your Raspberry Pi before. So you can just press yes. Okay. And now it's asking you for the password. Okay, so we have the user Pi and we need to give the password. So what is the password? Well, do password is simply raspberry. Okay. Like that. No uppercase, no space, no, nothing. Just raspberry like that. So you can go ahead and type Raspberry. You will not see anything. Okay? As you can see when I type, you will not see anything written on the screen. And then you press Enter. And if it works, if you've provided the correct password, then you in, you can see here you have the Pi user and then the hostname is Raspberry Pi. And you are connected to the Raspberry Pi for the first time. Great. Let's now install and configure VNC. Vnc will simply allow you to use the Raspberry Pi desktop from your own computer. This is great, so you don't need to only use the terminology with command lines and you also don't need to have a monitor plugged to your Raspberry Pi. And even if you have a monitor, you will see that it's sometimes more convenient to work without it. So the reason why we set up SSH just before is simply because we need to access the Raspberry Pi through SSH so we can actually set up VNC. So that's what we're going to do here. So SSH, then you put the username, which is Pi here AT and the IP address. So of course, this will be different for you. Okay? I center and the password is still raspberry. Okay. Don't worry about the warning message here about buzzword. We are just going to change the password later. So here to enable VNC and make it work correctly, there are a few different steps that we need to do in the order, so please follow along. First, you are going to type. So here you are in a terminal inside the Raspberry Pi. You're going to type sudo space, raspy dash config, okay? And if you see that, for example, if I press tab here, I can have the auto-completion to make sure it is the right command. So pseudo will make you run the command as an administrator on the machine, which is required here. And raspy configures interests so you can get access to a configuration. So you press Enter and it will bring you to that menu here so you can navigate using the arose or key from your keyboard is very simple, and then just press Enter to choose whatever you want. And the first thing we're going to do is go to interface options. Okay, press Enter and then go to VNC. Press Enter again. And would you like the VNC server to be enabled? You're gonna select yes, press Enter. And you can see the VNC server is enabled. So I'm going to use the right arrow key to go to Select and then finish, and then press Enter on Finish. And I am going to reboot the Raspberry Pi with the command sudo root. So you do sudo robots Enter and you can see now, so the Raspberry Pi is rebooting and we are back to where we were before. So here I'm back on Windows, okay. I lost the connection to the Raspberry Pi. You could, because of course it's rebooting. So now you have to wait a few seconds until the Raspberry Pi has boots again and is connected to the Wi-Fi network. Okay, I'm going to use the same command as before in wait a bit, and then press Enter. And you can see when you have the password, it means that the Raspberry Pi has correctly root. So you just put the password, raspberry wants more and you're back to a space age, okay, on the Raspberry Pi with the user. And now you're going to go back to raspy configured, Sudo raspy complete. And so here the VNC server is enabled, but that's not going to just walk like that. You need to go here we are in one. So when we press enter and go to boot auto login, and I'm going to choose so by default that may be one of those, okay, console, but you're going to choose desktop, desktop auto login, okay? So it means that it's going to boot on the desktop. And if you use auto logging is going to boot on a desktop and it's not going to ask you for the password too. Good. So I'm going to use that because it's just simpler. I press Enter. Okay. And then what I can do is I can just go to finish again. Would you like to recruit? No. Okay. They ask you that because you have changed an option that needs a root. So do you like to robot? No. Yes, you press yes. And he's going to reboot Oh yeah. This I'm going to make the steps one by one gate and reboot every time so that you get see each step one by one. So now you have to wait a bit until the Raspberry Pi has. So I put the password raspberry again. Okay, and I'm going back to raspy config. And the last step, we are going to go on display options and resolution. And you're gonna choose a different screen resolution than the default work good because for some reasons, if you have the default resolution, the VNC server may not work. So you have to choose a different resolution here and in which one to choose. Well, basically choose the one that corresponds to your current screen here. So on my laptop here, I have a full HD resolution on the screen. So I'm going to choose the full HD resolution. So you press Enter on the resolution you want. Okay? The resolution is said that a lot. Okay. And then go back to finish. Would you like to robot no. And yes, and you reboot one more time. Okay, so those are the three steps we need to do. So we can enable the ANC and make it work. So now we have set up the NC on the Raspberry Pi. So this is the server site. What we need to do now is to get the client. So here I'm using Windows, I need to get a client so I can actually connect to the Raspberry Pi and get access to the desktop. And to do that, we are going to actually installed the Real VNC, VNC viewer, okay? So you can go to that URL here, Real VNC.com, you can simply so tight VNC viewer, Real VNC, basically the first link. And as you can see it is, is multiplatform. So you can choose whatever OS you're using right now for Windows, Mac OS, and Linux. As you can see, there is a Raspberry Pi here, but don't mix things up, okay? If you choose these indices, the client to connect to the Raspberry Pi. So if you choose this, basically it means that you are controlling a raspberry buy from another Raspberry Pi, okay? Here we are controlling the Raspberry Pi from windows here because my laptop is running Windows at the moment. So I'm going to choose the Windows Client and simply download it. David, and when it is done originally before. So you can just run it and go through the installation setup. And once it is installed, you simply have to launch the application, okay, and you will get that you hear. So what are we going to do now? First, make sure that your Raspberry Pi has been robots, so it is powered on, it is running. And also make sure that I am going back to my network configuration. You should have the correct configuration because you could just previously connect to SSH. But make sure you always your computer and your Raspberry Pi on the same network. Otherwise you won't be able to connect to the Raspberry Pi from your laptop. So now I'm going to go here to fine and new connection where you can just press control any shore. And here I'm going to put the IP address of the Raspberry Pi. Okay? So really make sure that you know that somewhere so you don't forget it. And I'm giving names to just name so we can recognize the connection here. Raspberry Pi. You can put whatever name you want here, okay? And let's click on, Okay. And now I'm going to double-click on that. Okay, You may have a warning like DAT and let's don't worry about that list continues. You don't have the warning at spine 2, and now you have to give the username and password. So the username here is by and the password is raspberry. Okay, Just like we did for SSH, okay. You can choose to remember the password on that. I'm not going to do that now because we are going to change the buzzword later. Okay, you can now have access to your Raspberry Pi desktop without any external monitor. So first, you may have that warning. Ssh is enabled in the default password for the user has not been changed. So you would only get that. Of course, if you have SSH like we did, just click on, Okay, Dylan, mother. And you have the welcome screen to set everything up. So I'm just going to get you through the different steps here. Click on Next. So you choose your country. For example, for me, it is France, okay. Too long way. I'm going to use the English language so you can also set your keyboard. Anything that is related to your country came next. Okay, and here they will ask you to enter a new password. So we could have done that before, but because we have this option to do that here, I didn't do it before, so we can just change the password now. So simply, simply to the password that you want, and that will become the new password for the user. Okay, so your user is still name, okay? But then the password will not be raspberry anymore. The post-World would be the password that you set here, okay, So make sure that you remember the possible after you sell it here. Then you click on Next. If you, Okay, If the screen shows a black border around this doc, you can click on that. Or me, in my situation, I don't have the black border, so I'm just going to click on Next. And then they will search for wireless network. So if it is the first time you boot your Raspberry Pi with an external monitor. Of course here you have to choose which network you want to connect to. Setup the provider password. Here. I'm not going to do that because I'm already connected to that network because that's what we previously did in another step. So I'm going to click on skip here, but you can pick on next if you're not connected and could date software, you can click next. It will check for updates. So after fetching the new label update, it will download the new packages. So come install the packages may take some time here depending on your Internet connection speed. And after all, the updates have been downloaded, our Raspberry Pi, we install those updates. So here again, you have to wait a few minutes. Okay? And finally you will get this message system is up to date, so you click on, Okay. And now the setup is complete so you can actually press Restart. And as you can see when they press Restart here because I'm using VNC, I get a message attempting to reconnect to VNC server, okay, because of course the Raspberry Pi know is shutting down. So the VNC client can't connect to the VNC server. So what you can do here, you just need to wait, okay? If the Raspberry Pi is disconnected and the client, the VNC client will continue to try to connect to the Raspberry Pi. And when the Raspberry Pi with boot, again, it will enable the VNC server and the client will be able to connect. And here as you can see, we have a message either was not recognized or the password was incorrect. And you get this in the first time because the password actually you have changed the password, okay, the password is not raspberry anymore. The password is the new buzzword that you have set. So you click on OK and you can now double-click on the VNC client again. And you can put your new password. Okay? And there you can actually click on remember, password. Okay? And it will connect again as the user. Okay? So now if I click here, I close it. I can connect again and it will not ask for the password. One thing that you may experience is if I try to put full screen, as you can see, it doesn't really work. Okay? It's not good. As you can see, the resolution is not really a BET correctly, okay? It's either too big or not fitting the screen. So to fix that, simply follow what I'm doing here. You can click here to open a terminal. So you can use the command sudo nano, nano, which is a text editor. And then slash. Boot slash, config that. Okay, So you can just type this exact command and this will open the file name, configure the extra which is located in the slush mood. So slashes the root of the file system. So this is, we'll find is located. Okay? And what you can do is use the rows from the kibble to navigating the final. So you can use the down arrow to go down until the end of the file, okay, at the end of the file you will see that like DT, overlay, the C4, etcetera, etcetera. What you can do hashtag before. So this will simply comment the line. Okay? And you can press Control S, Okay, you will see that message here. Control S will save the file and then Control X will exit the file. So now that we have changed that, as you can see, it's in boot directory, so you can guess that it will apply on boot. So you can reboot your Raspberry Pi. So you have two choices. You can do sudo reboot like we did before with SSH. And you can also go here and click on logout and shadow, just like you would shut down any computer. Okay, So if you want to actually reboot the Raspberry Pi, you have to pick on reboot. Here I have clicked on shut down. So what it will do is simply showed on the Raspberry Pi. And so you have to wait a few seconds. Let's say that when you shut down Raspberry Pi like this, wait, something like ten to 20 signals after night. What you do is you take off the power cable from the Raspberry Pi, okay? And then you put the poor cable back in the Raspberry Pi. So it will put again, okay, for now, this is the only solution you have to put a raspberry buyer is when it's straight down, is you take off the cable and you put it back inside. So the pie is no robotic. You can wait a few seconds or maybe up to one minute. Okay, and now the VNC client has connected again to the Raspberry Pi, and now it's much better. Okay, So you can go here and click on Enter full-screen mode. Okay, and now you have your Raspberry Pi full-screen with the same resolution as your monitor. And you can click here to exit full screen. Okay, so great. You have setup everything. And one last very important thing is actually when you want to shut down and pour off the Raspberry Pi, don't just take off the power cable when it's running, okay. You just don't unplug your computer when it's running, okay, first what you do is you go here, you shut down your computer, and then when it's completely shut down, you can remove dipolar cable. Okay? So if you don't do that, you may correct the ethical and when you put again, you might have an error. And in that case, you would have to completely reinstall your Raspberry Pi. 8. Install and Configure Arduino IDE on Raspberry Pi OS: In this lesson, I will show you how to install the Arduino IDE on the Raspberry Pi. So you can program your Arduino directly from there. And let's first understand very quickly why this can save you a lot of time. So if you already have the Arduino IDE on your computer, what you can do is to write some program for your Arduino. Let's say you are on Windows, Mac, or Linux, it doesn't really matter. And then you upload the program today, Arduino. Then when we will make the Raspberry Pi and Arduino communicate between each other. You will need to unplug the Arduino from your computer, plug it to your Raspberry Pi, and then run a Python program to communicate on the Raspberry Pi. And every time you need to modify something in the Arduino code, you will need to do this process again, which is basically to unplug from the pipe, plug to the computer, upload, unplug and plug to the Raspberry Pi again. Now what you can do is directly install the Arduino IDE on the Raspberry Pi OS. By doing that, you can keep the Arduino board plugged to your Raspberry Pi board. Anytime we need to modify the Arduino program, you simply need to stop any currents program running on the Raspberry Pi, which is communicating with the Agnew. And then you just upload the code and run the program again. That's it. Alright, now let's go to the Raspberry Pi OS desktop and install the Arduino IDE. So Raspberry Pi desktop is actually Linux, so we're going to do the Linux installation. And if you open a terminal where you could install Arduino IDE directly with APT, but don't do that. Okay, So you could do sudo APT install arduino. You may see that in some tutorials, but don't do that because this version is actually old and not updated. So you're not going to have the new features and maybe that's not going to work. So instead we are going to open a web browser here on the Raspberry Pi. And just type, you know, I suggest search on Dr. go on Google or whatever search engine you want. Let's go to the Arduino website. And here we are not on the right page, so let's go to software. So you click on software and you have downloads Arduino IDE. So just download the latest Arduino IDE version of doesn't really matter which version you have for now. So on the right you can see the download options, okay, So if you were to use Windows or macOS instead of the Raspberry Pi to actually program. You are then download one of those. Here we are on Linux. We aren't Raspberry Pi, so we are on links, so we're going to download one of those heat of Linux. And to know if you have 32 or 64 bits, well, it's simple. Just open a terminal again. And do you name space, dash M. This is going to show you that. And if you see a V seven L, It means you have a 32-bit. If you have something like X86 64, you have 64-bit. So we know we have 32-bit here. And we're going to use a RPM 32. You don't know that that one. Okay, you can just click on, just download. And you can see it's going to download it here. So you just wait until it has been downloading. Alright, we now have the Arduino IDE downloaded. So I'm going to close this web browser and let's open the terminal again. Alright, and as a remember here, you have to have some basics on Raspberry Pi, including the terminal. So I'm going to show you exactly what commands are used, but I'm not going to explain everything a 100 percent from scratch. So we are in the home directory. I'm going to go to the downloads folder. In the downloads folder we have this. So that's the archive will have downloaded from the Arduino website. This is a tar. Okay, so we're going to do dash, space, dash f with the name of the archive. So you can just use the other completion we've tagged. If you want to go faster, you press Enter. You wait a few seconds, maybe even a few minutes. All right, and when the command returns ls, you'll see we have a new folder which contains the Arduino IDE. Now what you're going to do is to move this inside the slash OPT folder of your Raspberry Pi. So you're going to do sudo. Sudo because we are going to put that somewhere else than in your home directory. So pseudo M, V to move, you just use the auto-completion to select the folder. And then you do slash OPT. With auto-completion or so, you press Enter. Right now the folder is gone. You go to city. So cd slash LS. We have, so we have other stuff here as well. And we have the Arduino IDE. So we are going to go inside this folder. And now you can see we have this, we have a few scripts here in some folders. But basically what we're going to do is to run this install dot SH. So pseudo dot forward slash installed on a stage. And you wait a few seconds. Okay? And when you see down where Arduino IDE is successfully installed, just one more thing is if you want to uninstall it, you just go back to this folder here and you just run the uninstalled scripts. And then you can also remove that folder if you want to write. But let's keep Arduino IDE installed. I'm going to close the terminal now. And you can see here we should have and the menu programming and we have a new Arduino IDE icon here. So I'm going to click on that. And Arduino IDE is not open NDRC, you have a new program here. And now just one or two things about configuration, okay, So that you can have a better looking IDE and that the code can be more readable. You're going to go in File Preferences. And you can edit here different things. For example, editor font size. I'm going to put 14. I'm going also to use display line numbers. Okay, so I'm going to change those two options here. Let's click on OK and you can see it's already better. I'm going to go back to Preferences. And where you already have another information here is where your sketchbook. Sketchbook is basically where your Arduino programs are going to be saved. So you have a new Arduino folder here and your home directory. And the last thing I'm going to do is I'm going to go down here. More preferences can be edited directly in the five here. So I'm going to click on that. This is going to open a new file manager. And I'm going to close that there are important, you going to close the Arduino IDE. Okay? We are going to modify preferences that TXT here. And before you open that file, you close the Arduino IDE. Very important. So you double-click, It's going to open the file. And this is because we're going to find this line editor font. So you can just scroll and find this line editorial font. You see we have monospaced by default. And this may actually not be the most readable font for Arduino. So what you can do if you want to change to console us here, like this, you write like this counselors, and then you have playing, so don't touch anything he anyway, that setting doesn't work. And then you see you also have the font size that we have just said before. But for the phone, basically there is no way to change it on the Arduino settings. So we have to open this preferences, that TXT file. So I'm going to save with control S in. I quit the file and quit that. And let's go back to Arduino IDE. All right, now we have a new font size and a new font, and we have the line numbers. So everything is good for the course. Now if you plug your Arduino to your Raspberry Pi with the USB cable. Then you go on tools. You select board, Arduino Uno. So if you have an Arduino Uno, of course, if you have something different like a nano omega, you choose correct board. And then on port, you're going to choose the Arduino Uno port. So usually that's going to be something like TTY ACM 0, TTY USB, or with one or something like that. Okay, so you select this and then let's just upload a EMT program to see if it works. So compiling, uploading, down, applauding, great, everything is currently working. Alright, so now we have all the tools we need. Let's jump straight into the cell communication so we can make the Raspberry Pi and Arduino balls talk to each other. 9. PART 1 - Serial Communication Between Raspberry Pi and Arduino: Let's directly dive in and make the Raspberry Pi communicate with the Arduino. This first part of the course, we laid the foundations you need in order to build any project using both Arduino and Raspberry Pi. Now, how are we going to make those two boats communicate between each other? Well, we're going to use serial communication. And what is cell communication? Basically, cellular communication is simply a way to transfer data between two devices. More specifically, cycle is based on the uart protocol, which means universal asynchronous resection and transmission. If this sounds complicated, well, this is not asynchronous reception and transmission simply means that you can talk from the Raspberry Pi to the Arduino, and from the Arduino to the Raspberry Pi. At the same time, just like when you are chatting with someone in your phone, you can send messages and the other person can send messages at the same time. Now, don't worry too much. We won't need to dive into the low-level details because there are some high-level libraries that we can directly use in our code and which will make things easier. In fact, you probably already know cellular communication if you were using the serial monitor on the Arduino IDE to debug your Arduino. Well, this is cellular communication between your Arduino board and your Arduino IDE on your computer. What we will do now is that instead of communicating between Arduino and the serial monitor, we will create a communication between Avenue and a Python program on the Raspberry Pi. So in this section, I will first show you how to do the required setup for cellular communication to work. We will then initiate a communication between the Raspberry Pi and Arduino board. And step-by-step through several iterations, you will discover the different ways to communicate with Syria. After this, I will give you some activities so you can practice on everything you will see now. All right, and let's get started. 10. Hardware and Software Setup for Serial: Let's start with the hardware setup for science communication. So you have your Raspberry Pi here and you can see it's already pulled on for me. Okay? And you have your amino and the USB cable. So what you're simply going to do is connect the Arduino to the Raspberry Pi with the USB cable. As you can see, you can do that when the Raspberry Pi is bullet on our Powell off, it doesn't really matter. And that's pretty much it. Now, you can see also on the Arduino here I'm going to unplug it here. You can see you have some pins. Here on the Arduino Uno, you have 01, which are RX and TX, and those are also pins for cellular communications, so you could use those eventually, we have some GPIOs on the Raspberry Pi, but this is much more complex, okay, you need a voltage level shifter between 50 volt and 3.3 volts. It's also less robust, less reliable. And USB cables are really working well. Okay, I've never had any problem with cellular communication that way. And one more thing is that, so here I have an Arduino Uno. You may have a different kind of ball. And depending on the boat, the USB connector here may be different. So here I have like a standard classic one, but you may have a micro-USB. Well, it doesn't really matter as long as you can connect the USB connector here to the USB connector here on the Raspberry Pi, everything is going to work correctly. Now that the hardware setup is done, let's do the software setup. And for that you go to your Raspberry Pi desktop. So make sure of course, that you have correctly plugged your Arduino board to the Raspberry Pi using the USB cable. And so for the Arduino side, there is nothing to do. Psi l is already configured and ready to be used with the Serial library on the Raspberry Pi board, we will need to do a few setup steps. So first of all, you can open a terminal here. We'll click on the terminal icon and do ls, dash, dash, TTY. And in this so we can list all of the stuff that starts with slash day slash TTY. You press Enter and you can see all of that. So what is this? Well, basically we are going to try to find the port of the Arduino and Raspberry Pi. Okay? So the port is basically the name that is given to the bolt here so that we can recognize it and then we can connect to it. And you have quite a few. I'm going to give you the answer. The port is that one, okay, slash, slash TTY ACM 0. So you should have something like this. Tty ACM or TTY USB depends. With a number that can be 0, 1, 1. Usually you will start with this. Now, how to be sure you have this port? Well, you simply disconnect your Arduino. So I just unplugged my Arduino board. Okay? I just unplugged the USB cable and now I'm going to run this command again. And you can see that King, we don't have the TTY ACM animal. I'm going to replace the Arduino. Uno is plugged. I run the command, you can see now we have this. So basically you just disconnect your Arduino, you run that command, you connect your arduino and you just find what is the new part that is here. So now we know that this is the name of the port. What you could also do is if you have the Arduino IDE install. You can see here on tools and port, we can find also the same name. All right, so now we have the pot, I'm going to do clear on a terminal. The second step we need to do is to make sure that our users, so the pie user, which is here you can see, has the permission to access to cell communication. So we're going to do groups to see all the groups we are in. Okay, so as a user, you can see we are already in the diet out group, and this is the one that gives you access to cell communication. So if you don't have this when you type groups, so what you can do is sudo add user and then the name of your users. So it's pi. And the name of the group. You press Enter. And you can see here the user is already a member of dial out. But if it was not the case, it would add you to dialogue group. If you just added your user to the dialogue, you need to log out and log in. So what you can do is look at login or you can also reboot your Raspberry Pi, so you can be sure. Alright, that was the second step. The third step is to install a Python library. So on the other side, we have the civil library called sale, which is already installed on the Arduino, but on the Raspberry Pi side, we need a Python module. And this Python module is by serial, okay, so we are going to use the Pi serial module and to install it. So first make sure you have PIP3 to, if you don't have PIP3, sudo APT install Python 3, dash BIP. Okay, it's already here. And then PIP3, install, buy cereal like that. Okay, press Enter. And already installed. So actually it may already be installed on your Raspberry Pi. So if that's the case, that's wonderful. You can do PIP3, show by serial to see the version, to see more details. And maybe the page with documentation and everything for this library. And that's pretty much it. Okay, great. So now everything is correctly set up. The Arduino board is plugged to the Raspberry Pi with the USB cable, you have the correct permissions on the Raspberry Pi. And you also have a cellular library ready to be used on both sides. Let's now initiate the communication. 11. Initiate Serial Communication: 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 boats. So we're going to open the Arduino IDE. So we can upload some code to the Arduino board. And we are also going to open the funny Python IDE so we can write some Python code on the Raspberry Pi. So I'm going to organize like this. 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 it'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 advent of Sita, okay, Usually that's what we're going to do. We're going to start from an adenoma because that's going to be easier to debug. So to start sale communication, well, you just do serial.begin. Begin with a baud rate. So that is a great classic baud rate, 9,600. Okay? And you put this line inside the void setup of your argument. 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 cereal on the audio. 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 y. And then exclamation mark for not cereal. And then just open close curly brackets. So this basically on the Arduino Uno for example, or nano omega, you don't need this line, but if you are using an Arduino with a native USB, for example, Arduino Leonardo or aluminum 0, then you may need to use this line. Otherwise the silo is not going to work correctly. So basically this is going to wait until the cycle 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 amino part. Now I'm going to go on the Raspberry Pi and write some Python program. So let's first start with this user bin, Python 3. Okay, So if we're ever execute this from the terminal directly, we get the information that the interpreter is Python 3. Now what we're gonna do is we're gonna first import cellular. So the name of the library is buying cereal, but you're going to import cellular, okay? On the Arduino you have nothing to import, okay, Sale is already there, but on the Raspberry Pi, you have to import that module. And then, well, the first thing we're gonna do is to open the serial communication. And the sale communication is going to give us something that I'm going to save as search for sale. Okay, I do this because that's quite common and if you find any help on the internet, any tutorial, I need 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 Sarah is equal to cereal. Cereal, uppercase, Y, uppercase, you open, close parenthesis. And first we are going to give the port. Okay, so if you remember, the port was slash slash TTY eight cm. So that is the port I have found actually in the previous lesson, is to, if you are not sure what port is used because that may be something different here, please go back to the previous lesson on how to set up science communication and then come back here with the port that corresponds to your Arduino. The next parameter is the baud rates. So the baud rate is actually what you can see here. That 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,200 boat. Okay. And that's the baud rate I'm going to use. So if I use this here, I use this here also. Ok, so you can have different values, okay? You have different common values. And this is one of the most common one and also equate fast one and it's button and the classic 9600. I'm going to add a third parameter which is timeout. Let's see 1. 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. Alright, so with this line, we open the serial communication. And if it works okay, we're gonna get the cycle 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 gonna do import time and due time sleep for three seconds. So I recommend at least two seconds. Three is even better because well, when you open the sale communication on that side, the thing is that on the Arduino side, the Adreno, for our own, 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 Adreno to have the cycle completely ready to communicate. And so we give three signals 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 divert. So better have a time dot sleep with two or three. And then I'm gonna do that. Resists input buffer. So where is this? So basically we sale both sides, can communicate with the other artists. 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, not directly in the code. When the data arrives, it's in a buffer. And when you're going to read, that's what we're going to see in the next lesson. When you're going to read, you're going to read actually from the buffer. So here we revisit the input buffer, which means that if the Arduino sends us some data before this, so when the parameters actually post, 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 body. Alright, so that's it. Now let's maybe put print. Okay, to have a nice confirmation. And then we'll do program is going to exit. Okay, now what to do? Because we have the Arduino program here, the Raspberry Pi prime here. But what you need to do first is to upload the Arduino code on the Arduino IDE, Okay? Does 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 bolt, the correct spot. And let's do upload. Okay, let's save it as first communication. Okay, you can see a compiling, uploading and done 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 us. So let's create a new folder. Actually, let's go here. So I'm going to go directly from five Manager. Right-click new folder. Let's call it quite fun programs. Okay? And let's go back here. Let's save as in Python program first, communication and dot p-y. So that's python file. Click on, Okay, so the program is saved. And now I can run it. For to run it, I can just click on the play button here are read 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 sale, okay? Which means that the Raspberry Pi could correctly connect to that both, 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 science communication and then we exit. What you have to do and what is better is to close the communication so you can do say dots, 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. Communication is going to be closed simply when you pull off the audio. But here, as a best practice is better to close it. Because then if it's not closed correctly and you tried to run another program, you may have some troubles. Okay, so let's run that again. You can see we wait three seconds, You're 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 are the Raspberry Pi's 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 serial exception could not open ports slash dev slash TTY ACM 0. Okay? So basically the point is not found. Why? Because the Arduino is not connected. If I plug the Arduino, the Raspberry Pi again, let's run the code and 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 the aisle of goods and you need to upload that to the Arduino. When you upload to the aluminum, makes 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. 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 gonna run as soon as the Arduino is powered on. And then you can run your Python scripts where you open the serial communication. So make sure you have the same bond rate. Again, make sure you have the correct port here. And basically on some boats 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 resisted or 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 is very safe so that the cell communication can correctly be initialized and everything can be initialized on the audio side, then you represent 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 sale communication here on the Raspberry Pi side and you can exit the program. 12. Send Data from Arduino to Raspberry Pi: Great. You have no successfully initiated cellular 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 initialized the communication with that baud rate and we make sure the style is ready. So that's pretty much it. Now what we want to do is to send some string, let's say hello every 1 second. So we're going to do that in the void loop. We're going to do serial.print LN with hello from Adrienne. As you can see when you use say a dot print LN, r. If you use cyan, don't print. If you have ever used that before to debug your Arduino. Well, the same thing to send data over cellular, and I'm going to use a print LM here. So we can add a new line character after this string. And I'm going to add also delay. So let's use delay for simplicity here of 1 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 arena. 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 or so. Starting on the Arduino. When you use science communication, you can make sure that this side is walking with the Silurian. Also, this tool is super useful. So now we know that the code is correctly working, incorrectly sending this string of a sale. 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're going to create an infinite loop here to read the string. And if you see here basically on the Arduino, we have a Vault 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 wants to initialize science 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're 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 while true. And in this loop we are going to read science communication. And actually, the first thing I'm gonna do is due time that sleep, we've let say 0.012. 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 a 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. Anything else that you do very slow. So that's why I'm going to add a tiny 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 the schools. 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 it. So we use the seller we have created here, dots in waiting. This is going to return the number of bytes that have been received from the algorithm. 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 0. We don't need to count them, but just knowing that it's greater than 0 means we have received something. Okay? One other important thing here, don't put parentheses, okay? This is not a function, this is an attribute of sale. So press enter with the indentation here. If we have received something, let's say here we are going to read the next line. This line is equal to serve dot. Red 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 it's very important here. That's why I have added alien. So print LN. So basically this is going to send a hello from Arduino with a new line character after this. And so on. The Raspberry Pi with readline, it's going to read hello from avenue until that newline character that going to be here. Now we have our line, but basically this is encoded, Okay? Sal is not just going to send a string like this, it's going to encode it so it can better communicate. Okay, so basically what you receive here are just bytes. If you want to read them as strain, you can do dot decode and then UTF eight. That is going to decode what you get with red line into a correct string that you can read here. And then let's do print line. Alright? So basically this is going to run forever until you press Control C at 100 hertz. He's going to check if you have received something of a sale. Eps is gonna 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 program here is that if we press control C inside this while loop, we are never going to go to line 16 and we are never going to close correctly the serial communication. So if I put this line here, it's never going to be cold. So what I am going to do before I ran 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 price control C, We are going to close the communication. Okay, so let's do that now. Let's do try and put that. We've 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 self dot close inside the handle of the interrupt. And I'm going also to just add here print close communication. Okay, So we know that this has been executed. All right, So now let's run this. So the goal here is already running on the Arduino. I'm gonna run here from the Raspberry Pi. Okay? So you can see SIADH, okay, and then hello from Arduino every second. Great. Now I'm going to press Control C. I'm gonna click here and press Control C. And you can see close silo communication. Okay? So if you are running the scripts from the terminal wealth to kill the script, you just do Control C. And if you are running a directly from fully Python IDE, well, you need to click here and press Control C. 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, a closed cycle communication. As you can see, well, we received the string, every signal, 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 we've rigged line, you may often have some newline characters that are added are some courage written. And in that case, what you can do is after the code you can do dot strip. This function is going to remove any newline character are any carriage region that you have on a string. So you just have the hello from Arduino and nothing else. Okay, let's run that again. Okay. You can see Hello from Arduino, every single without any other character, I click here, I press control C, closed sale communication. And one thing I want to show you is if you look at the Somme gonna run the script. If you look at the Arduino board, you will see the TX LED is going to blink every 1 second at the same time that you send the string. All right, In C here, and I'm going to do Control C. And it stops blinking and now, well, it's just bought on. So basically you have this TX 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 applications. And one last thing I want to show you is that actually let's say you use a different board right here. So let's use 9600. I still use this one on the Arduino that I use that one on the Raspberry Pi. So let us run the program. And let's see what happens. Okay, So the communication can be studied, great. But then, well, it seems that we don't perceive 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 a weird behavior because the communication is going to be opened. But then anything else? He is not going to watch a price control C, closed cell 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, etc. I press Control C and it's working correctly. So baud rate, super, super, super important. 13. Send Data from Raspberry Pi to Arduino: Okay, you can send some data from your Arduino to your Raspberry Pi. And let's start again to see how to send data from the Raspberry Pi 2D algorithm. And so we are not going to start from scratch. Okay, I'm just going to remove that code here and that code here. So we keep the same structure on the Arduino. We're going to initialize cycle and make sure it's up. And on the Raspberry Pi, well, the same thing. We open the cilia, we wait a bit, resists input buffer, and then we have this infinite while loop, which is basically the same as void loop with the except keyboard interrupt to close the sale communication when we kill the program. Again here, we're gonna do our stuff here. So other ways, Let's start with Arduino. Here what we want to do is we don't want to send data, we want to receive data. And to receive data. Well, we're going to do basically the same as what we did produce it with the Raspberry Pi, which is to first check if we have received some data and if yes, read the next line. Okay, So what are we going to do is if syria dot, so we have a function which is cellular dot available. And you can see it should turn orange. So parenthesis, if this is strictly greater than 0, then we have received some data. So this function is going to return the number of bytes that the Arduino has received in the buffer, which is the same thing as the sale in waiting attribute we had in the Raspberry Pi. Okay, that's the same thing. So this is the number of bytes. We don't really care about the number of bytes. We just cared that the number of bytes is greater than 0, which means we have received something. If this is the case, well, I'm going to just put string, say Miss age is equal to, we can do serial dot, read string. This is going to read the next string. So it's going to also decode it and put that inside a string message. So very simple. Now, if you do that and that's where I'm going to talk about the timeout. So basically there is a reading timeout of 1 second by default. So when you use this, the program is going to be stuck for at least 1 second. Okay? So basically, as long as you receive some strings, It's gonna wait 1 second to be sure that it hasn't received more than what you've already received. And so if we just run like this, and then we send data from the Raspberry Pi to the Arduino. Well, we're going to see some delay in the processing on the other. So what we can do, there are two options. The third option is to do serial dots sets out with. So the default is one hundred, ten hundred milliseconds. What we can do is maybe put 10 milliseconds. So that's going to be much faster, but still, it's going to be stoked for at least ten milliseconds. And the lower you go, actually, the higher the risk of missing some data are of just reading so fast that you don't receive everything and you'll receive an incomplete message. So ten milliseconds should work. But I'm not going to spend too much time on this because we are not going to use setTimeout. Instead, I'm going to use another function which is red string until. And I'm going to put the backslash n character with. And this is important. Single quotes and not double quotes or single quotes. Again, this is super, super important. So this function is going to read the string until it gets to this character. And this function is going to written as soon as it detects this character. So the timeout still applies. So we still have a timeout on 1 second that's going to block this 1 second. But if you're receive that character, the diagonal doesn't apply anymore and you just get the string. So basically if we seen here from the Raspberry Pi, a string with a backslash n character at the ends or new line, then we're going to be able to read it immediately with dysfunction. And so this is basically the function I'm going to use for all the cool red string until which is quite powerful, quite efficient. Gonna give us directly string that we can process afterwards. Okay? And then once we have received dismissive, well, we can process it and do whatever we want in the afternoon. Right? Now, I'm going to go back to the Python code on the Raspberry Pi. So first I'm going to upload that to the Arduino or not. It's running and it's waiting for data. On Python. If you want to send something on the sale, what you can do is do cell. So your user object we have here, okay, from sale, sale dot right. And then you're going to put what you want to write. Let's say Hello from Raspberry Pi. And we're going to add a backslash n character at the end. So that is very impotent, okay? Because if you don't send that, this is going to wait 1 second. So it's still going to receive hello from Raspberry Pi. But after 1 second, if you add the backslash n, as soon as this function detects, the backslash n is going to written with this exact string and nothing is going to be stuck. And then we're coming back here. As you have seen previously, when we read data from Siegel, we need to decode it. Here. When we write data, we need to encode it. So you're going to do dot InCode. We've u, f 8. So the same UTF-8 as before. And it's very important that the dot encode is directly on the string. You don't do this, okay? You don't do sell that, right? And then dot encode normally do settled right with the string dot encode. And then you end the parentheses here. Because so make sure you have the correct order for the parentheses as super, super important here. Alright, so this is going to send Hello from Raspberry Pi to the algorithm. Maybe we can change the time. Sleep here, let's put one seconds, so we send this everything. Now, the code is running on the Arduino. I'm going to run the colossal on the Raspberry Pi and let's see what we get. Okay? Okay, so it's running, actually, let's do Control C, and let's add a print here. Print same message to. All right, so we can have a better idea of what's going on. Same, same mistakes to Arduino, same message, Giordano, etc. Now, well, what we did before when we actually send some data from Arduino to Raspberry Pi, is that we could directly print the data we got on the terminal here, on the shelf. But now we send data from Raspberry Pi to Arduino. And how can we actually check and print and debug data we've received? We could do serial.print LN with the message. Okay? But the thing is that he on the Arduino, we are using print LN to send message to the Raspberry Pi and also to debug. So you can just use these to print the message, for example, on the serial monitor, because you're already using the serial communication with the Raspberry Pi. So that's one thing that is very important is that on the Arduino side, well, when you receive something from cellular, don't just print it out. You can just print it like this if you want to test just with the Arduino and the same radar. But then when you communicate with the Raspberry Pi, don't just print some logs of some debugs. For example, here, if you print style communication ready, here if you print, received some string, well, everything that you print actually is going to be sent back to the Raspberry Pi j. And that's something you don't want if it's just for debugging. So, well, we are left with this and how to debug that. That's actually a good question and there are different ways to do this. So first of course, as I told you, you could just print what you receive. Run with the silane magneto, check if everything good uterus removed the print. Another option would be to use an external component, for example, an LCD screen that you plug to your audience. And that's actually what we're going to do in the following of the schools. So you could receive some stuff from sale and print that on the LCD screen. And what you can also see as a conformation that things are working. I mean, at least that you receive data here. So let's run the script again. Let's look at the Arduino board. And you can see the X is blinking every 1 second and I'm going to kill this. And our x is not blinking anymore. So our x is four reception. So as you can see if the LED is on, it means you are receiving data on the audio. Alright, and now that we have seen how to send data only from one side to the other. Let's check how to do a bidirectional communication. 14. Create a Bidirectional Communication: Let's now see how to combine together the two sides of cellular communication. And we're going to start from the previous program where we send something from the Raspberry Pi to the Arduino. What we're going to do is we're going to send back the message to the Raspberry Pi. We have something that we add heat, okay? So this program is already working. If you don't have this, please go back to the previous video. So what I'm going to do is I'm going to add something to the message here. For example, a counter that we are going to increase every time, okay, so we can see a progression on the messages. So I'm going to create here and global variable int counter is equal to 0. Let's initialize it to 0. And so once we receive the message, what we are going to do is to do miss age is equal to, so message plus, let's add a space plus, let's add to counter. The counter is an integer, so we're going to need to convert it to a string. So string with counter. So if we receive hello from Raspberry Pi, we're going to transform it to hello from Raspberry Pi 0, and then 1, 2, 3, 4, etc. Of course, if we want to increase the counter will counter plus plus after we have used it. And then, well, here we receive some data. We transform the data and we can do cellular dot print LN. Let's do sale dot print LN another time with message. All right, and let's run that on the Arduino. Let's upload this down, uploading. And actually before we write anything on the Raspberry Pi side, we can already debug this program because here we will be able to see the message with print LN, which means that we can do with the serial monitor. Okay, So I opened a silent monitor and I'm going to send the string, Let's say ABC. And you can see I have ABC 0. Okay? I send another string, random string. I have one gig. Hello, hello, hello, three. So we know that the Arduino program is currently working. So we send the string, we also have, so that's very important. We have new line. So with new line, that's going to add a backslash n at the end, okay? A newline character. If you want to add nothing, you go with no line ending, but I'm going to keep new Lankin. So you send a string and you receive the string with the space and with a counter that increases every time. All right, so now that we know it's working on the Arduino side, Let's go to the Raspberry Pi sine. And here what? We're going to send a message to the earlier. So let's keep hello from Raspberry Pi, great. And then we're going to receive this message. So as you can see here, we directly read the string, we modify it and we send it back. Switch be almost immediate. Okay? So I'm going to stay just after the right and then read the string. But before that, let's see a way to be sure we have received the data. I'm going to do Why? Sir, dots in waiting is lower or equal than 0. Which means that here we check if we have received some bytes. If this is equal to 0, well, I'm going to do time dots, sleep 0.01, okay, so we're going to wait 0.01 seconds in an infinite loop, as long as we haven't received any data. Once we have received data, what I'm gonna do is I'm going to do. So basically the while loop is going to exit. I'm going to do several red line dot decode with UTF eight. That S trip. And I'm going to put that inside a viable named, let's say a response is equal to for that. And then let's print response. Okay, So every 1 second we send a message to the Arduino. We wait until we receive something. You read that thing, we print it. We go back to the beginning. What you can see on the Raspberry Pi, and I'm coming to that now is we also have a timeout is equal to 1 here when we open the cellular communication. So basically we said a 1 second timeout, the same that we have under Arduino. But on the Arduino we don't need to write it to have one signal. If you don't put this, it means you have infinite time out. And the timeout is actually not a timeout for everything. It's a timeout for when you read with cycle. So when you use this red line, basically, it's going to do the same thing as here, is going to read the next line, okay? Until basically this backslash n character. So if you receive something that ends with a backslash n character, it's going to return immediately. If not, it's going to wait at least one signal. So normally if you correctly put backslash n with print LM here, you should not have any timeout problem, but I have still included here, okay, just in case so we don't have an infinite timeout that's going to completely block our code if we have one arrow coming from the Arduino, okay? The Arduino is correctly running the code. Let's run the code on the Raspberry Pi. Sorry, locate, send message, shoulder you can see and hello from Raspberry Pi, 1, 2, 3, 4, etc, etc. Press Control C. And you can see it is correctly walking. So now you have the bidirectional communication. So great. Now you can communicate from Raspberry Pi to Arduino and from other little Raspberry Pi in the same program. And so to conclude on that, the examples we have seen here are actually super, super simple, okay? But they are good to make you familiar with cellular. In the following of the course, you will practice more with real applications so that you can better understand how to use cellulose in a more complex situation. There are so many possibilities. And so instead of listing them all here, which is going to be quite boring, well, you will simply discover them while going through the course and through the activities. 15. Debug Serial Communication if Can’t Connect: There is one more thing I wanted to add in this section, which I think is quite important. So basically what to do if the sale communication counts be established. So let's say you have something like this is a wrong port. Arduino is not connected. You run the script. And you have this era. We've so foodstuffs with basically say an exception could not, open ports are something that is similar as well. In this lesson, I'm first going to go through the different things you can do to debug. And then you will see how to make your Python program automatically retried to connect. So here to the Arduino when it fails the first time. Alright, so let's start with the debugging part. First of all, make sure of course, that your Arduino board is correctly plugged to your Raspberry Pi with the USB cable. Okay, That sounds quite obvious, but it has happened to me a few times that I was like, why is not working? Well, if you look on the side, there had been no is not connected. That's why. Okay. Then of course, make sure that you have the correct port here, that you can check on the terminal. So we've and ls slash dev slash TTY like this. And check the parties here so you can connect, disconnect and find what is the difference in here. You're going to find these ports. Make sure also you have the same baud rate. Okay? Well, if you have a different baud rate, the communication is going to be opened anyway, but then everything is going to not walk. Basically. Also wait a few seconds, okay, after opening new cell communication, so you don't get any garbage and reason the input buffer. So you start here, the while loop with a fresh input buffer on the Raspberry Pi. And then when you run your program, well, You are currently, for example, uploading the promulgates, let's say use the serial monitor here. So I have the same Monitor which is a silo communication with the Arduino. Okay, on this, but you can see on the spot, now I'm going to run the script. You can see we have the same era. Well actually, we don't have the exact same error. We have the arrow that says Device, our resource is busy. Why is busy? Because we are currently using the serial monitor or maybe we are currently uploading a code. Okay, So if I close the sale monitor and if I run again, now, it's going to work. Why? Because the soil is actually free. So make sure you have only one program at a time that is using cell communication. So that can be uploading the sketch here, that can be using the sine monitor. All that can be using the Python program here. All right, And one more thing also is, of course make sure that the program has correctly uploaded to the Arduino. Don't forget to upload it before you try to communicate with it. And make sure you have this line. Begin. Okay, if you don't begin the cycle on the Arduino anyway has not going to work on the Raspberry Pi. So well, basically if respect all of that, each should work fine. 16. Retry Connecting with Python if Failed: And now let's see how to make your Python program retried to connect to sale after it has fade cue that can be super useful. So basically when you start the program, so any program where you're going to connect to the Arduino with cell communication. You can choose what you want to do if the communication is not successfully walking. So you can leave it like that, of course. And if it's not working, you're just gonna get an error. And then you need to start the program again. Or you can add a mechanism Just on that line here to try again if it can't connect. And that's what I'm going to show you here. So how to do that? Well, first, I'm going to unplug my Arduino, so the ordinary is not connected. Let's see again, what error do we have? We have a serial exception here. Okay? So we are going to get this exception. Alright, so I'm going to do try with this and then exits. And the exception is serial. Serial exception. You can use that. Okay? So if you have a cellular exception, which means that you have a problem when you connect, which can be, for example, that the board is not collected, all that the port is already busy. Then instead of exiting the program, right now you're going to go inside this code to handle the exception. And so what you want to do is that if you reach that, you want to go back to here. And if you want to go back to here, then I'm going to use a while loop. So let's do while, true. I'm going to put that inside the while loop because we have a column here. So while true means that first we are going to enter this. Anyway. We're going to execute this code. If this is successful, which means that we don't have the exception, then in that case, what we want to do is we want to break out of the, okay. So I'm going to add break. So if this is successful, we directly go after the while loop and we go to Time dot sleep. And if it is not successful, we are going to get the exception. And the exception, well, let's bring something. Let's say print could not connect to sail. Trying again. And let's add, let's say Time dot sleep one. So if it's not working, we're going to print something, sleep for 1 second and then come back to the beginning of the loop. So we try again every 1 second, okay? And just going to add another print here. So we can know directly after we have successfully connected that the clinician is okay. So let's say success, fully connected to sail. Okay, and that should be it. So now I'm going to run this code. The Arduino is still disconnected, so you can see what we're going to have is could not connect to sail trying again. And now I'm going to connect the Arduino. The program is still running and you can see successfully connected to say up cyan, okay? And then we have the application which is starting, I press control C to kill the application. Okay, So with this go, it's going to try again and again and again until the connection can be established. And that can be very useful. We're going to use that actually in the final project of this course. I'm not going to use it in every activity because that adds a bit of coal if we want to just focus on one or two functionalities. But for the final project, we're going to use that. And actually as an improvement that I'm not going to code here, but you could try to do that. You could try to add a maximum number of tries, okay? Let's say you want to try for maximum 10 times, which means that after 10 seconds, the prom is going to exit anyway. So you would add a mechanism here with another variable that will keep the number of tries and continue to try again until you reach the maximum number of tries. 17. PART 1 - Practice : Let's now practice on cellular with a few activities. What you have seen funnel is super important because that's the foundation for any program we will write in the future of the cooks. But still, you may be a bit lost and wonder how you're going to apply this to your own products. Maybe you don't really know how to put this knowledge into a real application. And well, don't worry, because that's exactly what I'm going to focus on from now on apply this knowledge to real applications and improve the code step-by-step. First, through activities all the way up to the couplet intercom product at the end of the course. All right, enough talking. Let's just start with the first activity. 18. Activity 01 - Power on/off Arduino LED from Raspberry Pi: This is the first activity of the schools. What I'm going to do now is to give you a challenge and explain some steps, are give you some tips to solve the challenge. In the next video, you will find the solution. Of course, I really encourage you to try to do the activity by yourself before you go to the solution. That's the only way to make progress. If you have some doubts, feel free to re-watch some of the previous lessons. All right, so your challenge for this activity is to power on our pool of an LED on the Arduino from the Raspberry Pi and so on the Arduino, we actually, well, we haven't created a secret yet, but we have the built-in LED. So you have the built in LED, which is on pin 13. Okay, so you can first create, for example, a defined for that and then use pin mode with 13 and output. And then what you are going to do in the void loop is basically two, read a string from Syria. So you're going to check if you have some data with psi l dot available, you're going to read a string. And then depending on the value of that string, you are going to power on our pool of the LED. So of course, you will need to agree on a common value uracil here and the value you sent here, okay, you need to send the same value that you're going to receive here. So for example, that can be on and that can be off. So when you receive on from sale, you power on the LED. When you receive off from sale, you power off the LED. Alright, so that would be the code on the Arduino, now on the Raspberry Pi. So first you are going to need communication. So you have two choices to need to communication. You can just open the sidebar and see what's happening if it works or not are you can also use the try again mechanism that you would have just coded in the previous section. And then well, what you are going to do is you're going to send, so you're going to send off and how you are going to send that. Well, what you could do is, for example, send on every second and then sent off and on again, sent off every single array. But what we're going to do here is we're going to use the input function, okay, So the input function in Python, as a reminder, we'll ask for the users. So in the terminal we ask for the user to give some data, so for example, a string. And then you can get the string into your code. So you can put, for example, input with select on of like this. We've maybe some quotes. So you can ask the user to select on or off and then you read that. And if the user gives our new sand onto the avenue, if the user gives off, using off to the algorithm. Okay? So what you can do is put that input function inside the while true loop, okay, to make kind of a void loop like we have on the Arduino. So you first initialize communication and then you create a while loop to continuously get the user input. And whenever you get the user input, you send that input to the area. Alright, so here what we are going to be able to do is to get user input on the Raspberry Pi to pour on hardware component and the amino. All right, and now once again, make sure to work on that challenge before you watch the solution. Okay, so spend some time working on this. However, don't stay stuck for house and the house. Okay. If you can't make it work, no worries at all because, well, for this course, the activities that have picked can be quite challenging. So in this case, if you can't make it work, just watch the solution and then try to do the activity again on your own a bit later. 19. Activity 01 - Solution: This is the solution of the first activity of the schools where you have two power on our power of an ad beyond the Arduino from a user input you get from the Raspberry Pi. And let's get started. So I have created a new project file in Python here on Thursday, Python IDE, and also a new project on admin. Okay, so start from a blank page. Now the first thing before we even start to write some code is to define the protocol. So I've already done that actually for you in the first activity. Now I'm going to talk about that just a few more seconds, okay? So as you can see, it's super important to know in advance what data you are going to send and receive from both sides. As you can see here with ONE OF we send that and reroute. See that if we don't agree on both sides, okay, to send the same data, then we are going to have some problems, okay, think about what data you need to send and receive and then write this in advance in a text document or in a comment. This is the protocol you're going to use. And for each application you are going to do, you are going to define a custom protocol before you write any code. Let's say for example here I wanted to send LED on and off. Well, if I send this and if I tried to read on well, you can see that if I send LED on, I'm not going to be able to detect on because this is not the same string. Okay, So I need to make sure that I send something that is the same on both sides. All right, so now let's start with the Arduino code. Okay, so once again, we start from Arduino because that's simply easier to debug. We can start with this code, debug it with the serial monitor, make sure that the sale communication is currently working on that side, and then we can create the other side. So here we have two things to do. We have to set up the LED and we have to set up the sale communication. I'm going to start with the edit, okay, so we first start with the LED and then we use the sale and fungicidal. We can directly make the LED blink so that we can check that the cell is currently working. So let's create a defined LED pin 13 for the AD. And then in the setup, I am going to do pin mode, LED pin output. All right? So we have defined, we have set output mode for the LED. So now we can do digital rights on the LED with high or low. And so when, when are we going to do digitalWrite with high and low, we're going to do that when we receive some data from the sale. So in the while loop, what I'm going to do is I'm going to read from sale. But before I'm going to, of course, initialized sale, we say a dot begin. Let's use these moderate pain and let's do while. Cilia like this would exclamation mark. So not needed on Arduino Uno. But for example, if you have a Leonardo or a 0 bode, well, you're going to need that, so I'm going to put that anyway. All right, cyan is initialized. We have everything in the void setup now in void loop, I'm going to do if sale dot with a label like this with parentheses is greater, strictly greater than 0, means that we have received some data over sale. Then we can read this and we're going to do string. Let's create a cmd viable, which means comment. So usually I prefer to write the full name for the variables. But when you have something like command, will c and d is pretty clear. And because we are going to use the C and D variable a lot in the following. So I'm just going to go with string, cmd, dot, read, string until, and backslash n with impotent single quotes here, alright, and semicolon. So basically we use the same function as before. And now where we have the command, so we receive a command from sale. We are going to do is we're going to check that command. So if the command is equal to 1, okay, so if you want to comparison with string, with the Arduino string, you can simply use the equals sign. So if the command is on, what do we do? Well, we do digital, right? With the LED pin and high. And if the command is off, so let's do an else. If actually command is off. We're going to do digital rights. Led pin with LOW. So if we receive and we put her on the LED, if we receive off, we pull off the LED and then I'm going to add an else. Because, well, you could receive any string here. You could receive on, you could receive all, but you could receive anything, okay. You don't know what's going to happen, you don't know what the other side is going to think, Okay? And as a basic principle, when you write some code like this, you don't trust the other side, okay, Here you are writing the other side, so that's good. But usually don't trust what the other side sense. Yeah. Okay. So make sure to always check your received on erosive of IRC. Something else that can happen, okay? So if you receive something else, well, what we're going to do in that case is just well, nothing. So I'm going to keep it like that for now. But for example, you could print an arrow, so you can send back an error, for example, or you can print an arrow on an LCD screen. Or you could do another behavior for your policy, could flush the input buffer. We're going to do that earlier, bit later, okay? But formally you can see we check if we have some data, if yes, we read the data, that's going to be our command string. We check if it's equal to o1. Then we do an action. We check if it's equal to off with an action, and then everything else, we can do, something else that we want, but for now we do nothing. All right, let's now try this code. We don't need to write the Python code. We can just start this. Let's make sure that we have the amino plucked. Okay, let's, whoops, I have selected the wrong board. So to Arduino Uno end, the port is correct. Now I'm going to upload this. So let's save it as Activity 1, Okay, and compiling, uploading. And note that the code on the Arduino is running. What we can do is we can already try these behavioral wave, the sale magneto. So we're going to open. So instead of having a Python program running on the Raspberry Pi, we just have the serial monitor running on the Raspberry Pi and we can send common from there. Alright, so make sure that you have the symbol right here. Take these is correct. All right. And then new line, because we are going to send a newline character at the general case, so we don't need to write it manually. And then I'm going to send some comics. So let's say I send on. You can see the LED here is on. No, I sent off. The LED is powered off. I sent on again and I can put her on the LED. Now if I send let's say LED on, which is not correct, nothing's going to happen. Ad off. Nothing is going to happen. Any other string, nothing's going to happen. You can see we still have the x, which is blinking because we are receiving some data, but then the data is processed in the code. It goes directly to the S, which does nothing. So you have to send exactly the on and off command to do something. And you can see that our experimentation, the behavior is correct here. So we can know that the Arduino code is correct. And now we can write the code on the Raspberry Pi. So let's start by initializing the communication. So I'm gonna go with, so this is a Python 3 for the interpreter. And then let's import cereal. Let's also import time because we're going to need that library. And I am going to do SAR is equal to Serial.read cereal with the port. So the part is still the same for me. Slash dev slash TTY, ACM 0, okay, the baud rate, very important. Exact same heat. And then the red timeout. Let's put 1 second. Well, for this case, we don't really need the timeout here, the rectangle because we're not going to read anything, but I still put it anyway. So here we open the serial communication and we get the object here. I'm not going to do the retry code, okay? Just to keep it simple for this activity, but you can do it if you want to. So once we have this, I'm going to do a time dot sleep with three seconds, okay? To wait. Because this is going to connect to the Arduino and he on the Arduino Uno, it's going to restart the program so we make sure that we have spent enough time so the Arduino is ready. And then we're going to do dot rigid input buffer, which is well not needed as well here. Because we don't have anything in the input buffer because the Arduino doesn't send anything. But still, I'm gonna keep it for best practice because we're going to initialize the cycle the same way every time. All right? And let's say print. Okay. Ladies, now what we can do is we can do our while. True. Okay? So this is kind of the void setup of the Raspberry Pi indices. The volume loop works it up while loop, void setup and void loop. So while true. So what I'm gonna do is of course I'm going to do a try. And except, so let's just put bass here. Tried exit, keyboard, interrupt. And when we press Control C, I'm going to do so. I'm going to say print close, serial communication and said dot close before we exit the program. Okay? So again, this is going to be the same every time. And in the y true, what do we do? So this is the behavior of our void loop. Well, basically we want the user input and from that user input we send data to the admin. So I am going to do, let's, the user. Input is equal to input. Okay? I can put some text here to send some instruction. For example, select on or off, right, with a colon and a space. So we have this instruction. The input is going to block the program until we give something. Then we press Enter and we're going to get that inside the user input variable. Once we have this, we could directly send the string to the Arduino because we validate the data here also. But we can also say, for example, if user input and how to validate this. Well, we want it to be on or off. So we quit. We can do is in. So basically, if the user input is in that list which contains two values, then we're going to do something. If not. So basically we don't do anything. So here I'm going to put an else. So if the user input is correct, then we do a dot, right in. Well, actually, I'm not just going to send user input, I'm going to add a backslash n. Okay, so let's do, for example, STR $0.02, which is a string to send to user input plus backslash n. Okay? And we sent STR to censor. We write as TR to send dot encode. We've UTF eight. Okay, make sure to do the encode on the string and not on the right function. There are important, so you should have open parentheses, open parentheses here, and close and close at the end. Well, it seems that pretty much it. Maybe we could add a print here so we can see on the Raspberry Pi side what's happening. So let's see. Let's bring it here. Print sin comments to deal with. So plus user input. So we know that we are sending a command to the Arduino, and then we can check if that works on the Arduino if we correctly blink the LED, but we already know it works. So let's run that. I'm going to click on Run here, and let's save this as Activity 1 dot. Okay? So we have the activity one sketch for Arduino and activity one Python file. Let's press Enter. And let's see what we got here. Okay? Okay. Select on or off. You can see the promise tech now the Arduino has reboot. And now I'm going to send on. And you can see we have the LED key which is on, I send off. The LED is pulled off. Let's send anything else? Let's say LED on. Nothing happens. Let's say on again, it works. And now if I press control C, You can see close serial communication. All right, so that's the end of the first activity of this course. Now, if you have a slightly different code than me on both sides, and if it still works, well, that's great too. Okay? This to my solution is just one possible solution, right? It is not the solution. If you couldn't find the solution by yourself. Well, don't worry, because I told you the activities here in this course are quite challenging. And what I recommend you to do is to actually come back to this activity maybe in one or two days or one week. Just watched the introduction of the activity again and then do it by yourself. And you will see that's going to be much easier for you and you're going to get more understanding. All right, and now let's go to the next activity. 20. Activity 02 - Bidirectional Communication: This is Activity 2 of the schools. And in this new activity, what you are going to do is you're going to send some data from the Arduino to the Raspberry Pi, and then process that data on the Raspberry Pi and send back something to the Arduino. Okay, So that's something new because what we've done before is to just send data directly from one side to the other one all to send some data from the Raspberry Pi and respond from the Arduino. Here, we initiate the communication from the amino end re respawn from the Raspberry Pi. But as you will see, there is not more complicated than what we have done before, okay? So if you correctly understood to produce things, that's going to be okay for you. So what is your challenge? Your challenge is to send some temperature from the Arduino to the Raspberry Pi. So the temperature here, we don't have a temperature sensor and I'm not going to use one. We're just going to bake some temperatures, okay, for this example. And so you send the temperature from the Arduino and then you're going to check this temperature in the Raspberry Pi in the Python program. And if this temperature is lower than a certain threshold, you are going to pour on the AD. So the built-in LED on the Arduino, if the temperature is above the threshold, you are going to pour off the LED. On the Arduino side is to send temperature. And then on parallel is to receive data from sale and poor on or off the editing. So here I have given you some code structure here you can already use. So basically to send the temperature, let's say we want to send the temperature every one seconds. So every 100 milliseconds, we are not going to use the delay. Because if we use the delay in void loop and then we tried to receive data from sale. Well, the delay is going to block the program and we are going to receive data from sale with some delay actually. Okay, so if you block the prom for one thing, you're going to have a delay of maximum 1 second, receive data from the sale and you don't want that. You want to receive data as soon as it is here. Okay? So what we're gonna do is we're going to do some kind of multitasking. And as a recap, I have shown you is 22 here for multitasking. So basically that's very simple. Can you create? So for an action you want to do, you create this variable and sign long with the last time you have done the action, you initialize it to 0 or minutes doesn't matter. And then you add an action delay, which is the delay you want to spend between two actions that you do. So here, for example, 100 milliseconds. Then in the while loop, you get the current time with Millis. And then you take the difference between the current time. So the time now and the last time you have done the action, this gives you a duration. And if this duration is greater than the action delay, then you can do the action. And of course, you update the last time the reaction was done to the current time, and then you do the action. So here you can select a random number for the temperature and send it to the side. And here, parallel. You can do. So. Say a basic piece of this is going to be very fast, this is going to be very faster. It's going to go very, very, very fast. So it's just like if you were doing some kind of multithreading on your Avenue and to get a random number, I have given you the random function here of the Arduino. So basically if you want a random number between five and 25, you just do random five comma 25. I'm going to use Celsius degrees here, but doesn't really matter. So if you do this, the random number will be between five and 25. Now on the Raspberry Pi, what you're gonna do is read sale, okay? And then you're going to receive some temperature, so perceive number and then process that number. So basically you're going to say if number is, for example, lower than 15, you're going to pull on the elif and else you're going to pull off the LED. So in your own kind of void loop on the Raspberry Pi, you first read from sale. And once you have gotten some data, you process the data and you write directly to the agreement. Okay? And well, what I'm going to do is I'm going to actually get the call from activity one. I'm going to get that. I'm not gonna rewrite everything in this case. Why is that? Because simply, we are going to need to use this. We are going to need to initialize the cell communication with the exact same code. And then we have the exact same wild true structure. I'm just going to remove all of that. Let's put bus here. And while this code is going to be the same, okay, for the previous activity and this one. So I'm not gonna write it every time because we have basically here 14 lines of code that are identical. All right, and now we'll see you in the next lesson for the solution. 21. Activity 02 - Solution: This is the solution of the activity to where you have to send some random temperature from the algorithm. Process that in the Raspberry Pi so that you can choose to put on our power of the LED. If for example, you can pull on the LED if the temperature is too low. So let's start with the Arduino side. Once again, I'm going to remove that. So I already have this unsigned long last time. Actually, I'm going to move this and just rename it last time. Let's say temperature set, okay? Which makes more sense. And then I'm going to do since delay. And let's keep one. Okay? So no void loop unsigned long we get the time now we've missed. And we check if the time now minus the last time temperature cent is greater than Temperature. Send delay. And in that case, we don't forget to update the last time temperature sent to the time now. So basically this here, this code here is going to be called every 1 second and we don't block the void loop with the delay. So to get the temperature I'm going to do in temperature is equal to n. Choose the Wrangham, five to 25. Okay, Let's remove that. And what we're gonna do now is we're going to send these to the sale. So I'm going to do in the void setup sale. So same code. And then like this. And here I can do sale dot print, LN. We've temperature. Alright, let's actually test this code. So I'm going to do upload, activity. Don't uploading and how to test the score. I just opened 70, 80 and let's see what we receive. 121214182315179. So you can see it's correctly working. Where receive a random number between five and 25 every second. Okay? So the first part of the advocate is done now in parallel. So I think parallel because we're going to do the two actions, one after the other, but very fast. So it's just like we are doing them in parallel. I'm going to do if Serial.read available is greater than 0. So if we have received some data, what we're going to do is the same thing we did in the previous activity. So I'm going to do that very fast. String command is equal to say l dot, read string until backslash n with one single quote. And then if command is equal to and we're going to pour on the LED. Else, if command is equal to F, we pull off the LED and then else. We can process the other comment. And for example, print an error or something like that. Okay? Now for the AD, I will need to initialize relative to its creator, define LED pin 13. Here. Let's do so. I can do the pinMode before, after cellularly doesn't really matter. He pinMode LED pin with output. And here I do digital right in with high because we pull on the LLVM digital rights LED, pin, low because we pull off the LED, okay, so we have this action and this action running separately. As you can see here, we send data with serial.print LN and we received data with Sayadaw registering until, but the two are not directly linked. Okay. We don't just send data when we receive data, okay, we have separated the two actions. Let's upload district to the arena. Again, what you can do, for example, is you can just open the San Benito. You're going to see that you receive some numbers. Okay. And let's just get on. And let's see that is poor. That's walking. And as you may see, the random here is maybe not random because we have the same data as before. So you could make it more random, but I'm not going to spend time on this, okay, that's outside of the scope of this course. If you want, you can search more about dysbiosis. Alright, so the Arduino code is done. Let's go to the Raspberry Pi sine kx. So we'll read from sale and we receive a number. So if we want to read from soil, what we are going to do is to do if in waiting is greater than 0. And here, no parenthesis. You have parenthesis here. We say L-dopa available, no parentheses here. So if we have received something, then we read that thing. Okay? So let's say temperature is equal to self dot readline dot decode with UTF eight and as trip. Okay, So the same thing we did before. So we read the line, we decode it because he is biased, so we decode it into a string and we remove any other special character we have at the end. And what I'm gonna do is I'm also going to cast this into an integer because well, we want to process the temperature as an integer. So I going to cast it as an integer. And to do that, I simply do int and I put everything in the parentheses. And now if temperature is lower, 22, lower than 15, let's say we are going to pull on the LA. So let's put a comma here, pore on LED and then else. So basically if the temperature is greater or equal than 15, we are going to pour of a. So let's actually do a print. Print temperature lower than 15. Led on. In here. Print temperature lower, greater actually than 15. And then of course, we send the command, so the command is still on and off. So we've backslash n dots encode with UTF eight, like this. And then I'm going to copy this here. And instead of we send. So as you can see the structure here. So we continue with our while, true. So the infinite loop, if we have received some data, we read that data. So we cast it as an info here it's very important as you see here, that we also have defined the protocol. The protocol is simply that here we send the number, we receive a number, okay? And here we send on an off, here will receive on an off. Okay? So on both sides, we know what we receive and what we sent. Okay, let's know, try this code so it's already uploaded on the Arduino. I'm gonna run that on the Raspberry Pi. So activity two dot p-y, it's going to be added automatically. And let's see what we get. Okay, you can see that when the temperature is lower than 15, we have the LED on. And when the temperature is greater than 15, we have the LED. Alright, and that's the end of this activity. 22. Activity 03 - Asynchronous Bidirectional Communication: Here is yet another activity for you to practice more on sale. And in this activity here is what you are going to do. So from the Arduino, you are going to send a counter, so you're going to initialize a counter. So counter and then send it every 500 milliseconds. So every time of course, you're going to increase the contact and so incremented. You send 0, 1, 2, 3, etc. And that, of course, from the main loop on the Raspberry Pi, what you're going to do is, of course receive and print the counter. So that is the first action. Now this would be a little bit too easy for you at this point. So what you're gonna do is also that every ten seconds, so every 10 seconds you are going to revisit the content. So to reset the counter, you're gonna need to send a special comment to the Arduino. And the Arduino is that when you receive, when received resists contact command, then you reset the counter of course, so you reset it to 0. And the thing is that those two actions here are completely independent. Okay? So here, as you did in the previous activity, you are going to send the counter every 500 milliseconds without using delay in Uganda in parallel and in a different action. Check if you have received that reset counter comment on the Raspberry Pi. You're gonna do the same. Okay? So in Python, you are not going to use Time dot sleep for ten seconds, of course, because if you do that, You're not gonna be able to print the counter for ten seconds. Okay? So in Python, you're going to need to do also a non-blocking action every 10 seconds with basically the same structure as we did previously undiagnosed. So in your, while, true in the void loop of your Python program, the first action or the second okay, doesn't matter. The other is to receive and print the counter. And the second action is to reset the counter whenever you have passed ten seconds. Alright? And I will see you in the next video for the solution. 23. Activity 03 - Solution: This is the solution of the previous activity where you need to send a counter from the Arduino to the Raspberry Pi, and then every 10 seconds. So independently from that first action, you're going to raise it, that counter from the Raspberry Pi to the adrenal. And let's get started. In other ways. I'm going to start with the Arduino code. So let's create a int counter is equal to 0 here as a global variable. And I'm going to initialize. So I'm going to set up sales of sale. Dot begin with the baud rate. And why? Cereal. Latest sale is ready. I am going to create two new variables here, unsigned, long. Last time. Let's say cent is equal to Millis are 0. And then unsigned long counter. So I actually sent counter delay is equal to 500 milliseconds in the void loop. Unsigned. Long time now is equal to MRT, which is the current time. If the time now minus the last time we have sent the counter. So this is a duration is greater or equal than the delay. What we do is we enter the if n. We say that the last time we have sent the counter is equal to time. Now, so I got pretty fast with that because that's a common structure we have already used before. And then what I do is I simply do sale dot print LN with the counter. So it's going to send 0 the first time and then a loop counter plus, plus. So basically this is going to send 0, and then 1, 2, 3, 4, 5 every 500 milliseconds, okay, in the counter is going to increase indefinitely. Now let's add the second action, which is when we receive the reset counter command, we reset the counter. And here we need to choose exactly the string which will correspond to that commit. Now actually let's just use that string reset underscore contract. Okay? So what I usually do when I send data over, say a light that we've comments is I put underscore clip, I need to separate two worlds. So here we have the first action which is going very fast. N is non-blocking. And then we do if psi dot the re-label strictly greater than 0. And then we do string command is equal to 30 dot read string until backslash n with single quotes and semicolon. And then if command is equal to visit counter. So this command should be exactly equal to reset counter. Okay, So if you think, for example, contraries it or any other string, it's not gonna work. Um, so open, close curly brackets. And then what we do is simply do counter is equal to 0. And that's it. And then we have the else. But in the else, we do nothing here. So any command that you send that is not reset counter is just going to go nowhere. Okay. We're not going to process that. We just going to process the reset counter. So that's it for the Arduino code. We can do is we can already try this. So let's run this on the Arduino activity. And three, compiling, uploading. Okay, down, uploading. I'm going to open the same. Leto will receive 0. Ok. You can see here we have restarted the program actually. And we have 0, 1, 2, 3, etc. Great. Now if I send anything, nothing happens. If I send a reset counter. The counter is being reset to 0. So it's working. Now, let's go to the Python program in. I'm going to go back to the previous activity and just copy this folder structure. Ok. And let's remove everything in the wild true. Okay, so once again, I use the same structure here, so I'm not going to write it every time. And in the while loop, what do we do? So the first thing we need to do is to print the counter that we receive. So if, say a dot in waiting is strictly greater than 0, once again, don't forget, there is no parentheses here. So if we have received something we do. So. Counter is equal to, say adults read line dot decode with UTF 8 dot S trip. And let's cast that to integer also because k is an integer and we simply do print, let's say received counter plus n. We print the counter and we cast it back to string, of course, because now that's an integer. All right, let's try this code. So the code here is running on the Arduino. Lets try this code. I run the script. Let's limit activity three. Okay, It's going to open this cyan, okay, received counter 2, 3, 4, etc. So you can see, I'm going to tell it when to start again. So we have say, okay, and we start to receive the counter at two here. Why is that? Because, well, you have the time dot slip to give the time for the amino to actually start and set up correctly. And then the Arduino is going to directly start to send data, okay? So it's going to start to send 0, and then 1, 2, etc. But basically the time that you sleep, the arduino already has sent 01. And at that time after time dot slip, you do Serial.read that input buffer. So the 01 here are not processed because basically they are sent before you reach that line. Okay, so actually let's run this increment. The reset input buffer will run that again. And you can see now we receive 01, but check very carefully, we receive that directly at once. Okay? The 01 is going to go in the buffer, and this is going to be triggered for 012 directly without any delay because 0 and 1 are actually already inside the buffer. So what's going to happen is that if you don't reset the input buffer, any data, any comments, anything that's going to be received. Basically during this time dot sleep is going to be processed at once, everything basically at the same time. So that's why I basically also add the reset input buffer, is that you don't process many comments at the same time. At the beginning and new starts. This point, you start with a clear input buffer and then you only process any command that you receive after. Okay? So we're going to continue like this. Okay, You can see received contract 2, 3, etc. And now we're at, the second part is that so we receive the contract correctly. Now, what we're going to do is we're going to revisit that counter every 10 Sigmund. And to do that, we're not going to use Time dot sleep. Because if we use time that sleep, well, what's going to happen is that this person is going to be stuck for ten seconds. Okay? We're not going to go with this line. And so when we go to this line after 10 seconds, this is going to process everything in the buffer. So we're going to see received contour, for example, two until 11 at once, and not just when we receive it. Okay, We're going to see that late and all at once because they are going to be stuck in the input buffer. So what we do is, I'm going to do, I'm going to add a viable last time reset counter is equal to time dot time. If you do that, you get the current time with Python and then revisit counter. Delayed is equal to 10 signals. So here we use signals, okay? And then here you have while loop. So while true, if you have this, you enter this block of code. This is going to be very fast. And what I'm gonna do is I'm going to add another action. So here we actually two in here. Actually one. So we get the current time. So I'm now is equal to time dot time. So as you can see, the structure is very similar to what we do in C plus plus with Arduino, right? If time, now minus the last time, we reset the counter, if this is greater or equal than the delay, we enter this. If we do last time, reset counter is equal to time now. Okay, So exact same structure. And what is the action? The action is to send. So server.js, right? Reset counter. So the exact same string here. Backslash n dot encode with you. Alright? And that should be it. Let's actually do print, sends, reset, counter decrementing. So we know exactly when it has been sent from the Raspberry Pi. And let's try this program. Okay, let's see if it works. Run scripts, received counter. So basically we're going to wait 10 seconds, which would be around, okay, you can see ST reset counter coming in will receive 0 PE. Let's continue. And you can see around 20. So just before 20, where is it? Contact comments? So we can actually see that it's correctly working. Okay, because here we send the counter every 500 milliseconds. So ten seconds means that we have sent 20 times the counter, which is what we have done here, from 0 to 99. And then send the results coming from the Raspberry Pi to the Arduino. And the Arduino, we reset the counter to 0 and we still continue to send a counter and receive on the Raspberry Pi. Alright, and that's the end of this activity and also the end of this section. Let's now continue by adding more functionalities to the Arduino and use them in collaboration with the Raspberry Pi. 24. PART 2 - Arduino Functionalities (Hardware Components): Great. You can now make your Raspberry Pi and Arduino bolts communicate between each other using serial communication. This is already a strong foundation you have. And with this foundation, you will be able to create all kinds of applications. So what we're going to do from now on is to make progress towards the final project, which is a complete intercom system using both Arduino and Raspberry Pi. First, we will focus on adeno functionalities, then Raspberry Pi functionalities. And after that, we will be ready to start the final project. So let's begin this part on Avenue. There are actually two sections here. First, we will add all the components that we need for the project one by one. For each component, I will give you some basic explaination, okay, and I'm not going to dive into details here because knowing how to create basic circuits and code with Arduino is part of the prerequisites of the course. The goal of this section is just to do some recaps and make sure that you have the correct secrets. And also that we can reach the next practice section where you will create programs to interact with the Arduino functionalities from the Raspberry Pi. So for some components, if you already know how to use them, feel free to skip the coding part. If not, then watch the video to expand your Arduino knowledge. You can also see those following videos as a quick help to come to if you are stuck in some of the following activities of project. All right, let's get started. 25. RGB LED - Circuit: Let's start with the first component. We're going to add to the secret, the RGB LED. So basically what I'm going to do for the listens on creating the secret is I'm going to create a secret first on a simulation here, on Tinkercad. And then I'm going to show you the real circuit. Use this simulation here because it's simply easier to show you how to connect to the components. Can not going to use the simulation for the course just to create a circuit. And if you want to create an account on Tinkercad, so that's free, that free tool. You go on Tinkercad.com. You just create your account and then you go to secret and you can do create new secret here. And that is where I am right now. The first thing I'm gonna do is to add an adrenal, both here. So Arduino Uno, and I'm going to add a breadboard. So let's use a breadboard, let's use a full side breadboard. So the components, you can see here you have all the components you just search and you have the different compounds. I'm going to put the Arduino here like that. And before we actually add the RGB, I'm going to connect those two lines here. So the 90 line, I'm going to connect here like that to the ground. So let's first connect the ground. So let's make a line for ground and the line for a five volt. Okay, between the Arduino and the breadboard, okay, So the ground line will be black and five volt line will be red. Okay, so that's important. First you connect the ground. Here are the breadboard to the ground of the Arduino, and five volt of the breadboard to five volt of the Arduino. So we can just add a black wire here and a red wire here. Okay? Now that we have this down, I'm going to search for the B allele and just take it he and plug it. Let's play it here. Tree, Let's play it here. So one thing is that if you don't have an RGB LED, you can also use three different entities of you. Probably know how to create a secret with one LED. So you could create a secret with three LEDs if you don't have an RGB, RGB, well, you can see that you have four legs. And here we see red cathode, blue, green. So here the order you can see is red, blue, and green. But maybe the other for the colors on your algebra allele. For my RGB LED for the physical one. And I'm going to show you just after is not red, blue, green, but red, green, and blue. Okay? So that you can, of course, experiment with colors and then find out. And then the pin here, the LED cathode, which is actually the longer leg of the LED. Okay, so you just check what is the longer leg and this is the cathode or the anode depends. If this is a cathode, what you're gonna do is you're going to simply link this to the ground. So let's use a black wire. Can use black wire if possible for the ground and write four VCC of five volt. So now that I have this ground line, I can just link the cathode to the ground. If this is an anode, you would need to link to 50, ten. How to know that? Well, you have to check the manual of description of what you bought because you can just find out from the component. And if you're not sure, then try with the CAFO mode like this and then make the code make to experimentation. And if it doesn't work, then try to plug these 25 volt and see if that works. All right, and now for each of the columns here, I'm going to add a resistor. A resistor. Let's add a resistor here between, for example, the red and a digital pin here. And we're going to use a 220 ohm resistor. Ok, you can see here the color is red, red, and brown. For a four band resistor, if you have a five band resistor That's going to be red, red and black, and black. And I'm going to connect this to digital pin number 11 on the Arduino Uno. So let's change the color actually. Let's use whether it's used arrange, okay, let's avoid black and red. And I'm going to copy this and copies here. And we're going to do blue line to pin ten. This well, let's put it blue, so it's easier to recognize it. And green to nine. And let's put it in green also. Alright, and the secret is done. So one thing that is very important is to make sure that you connect the three LEDs are the three legs of the RGB LED pins where you have a TLD here as you can see, so that you can use the PWM functionality because we are going to use the analogWrite function. So here I have chosen 11, 10, and 9, which are all PWM compatible, which can see for example, on the Arduino Uno, we have also the 65 and 3, and it may also depend on the board you have. So make sure you have PWM functionality. And this is the real secret with the RGB LED. So very important. First of all, before you do anything with the Arduino, just make sure to unplug your Arduino from the Raspberry Pi. The Raspberry Pi is still pulled on. As you can see, the ID is here, but we have pulled off the audio. Okay. And then you can see we have, so here we have the red and the black wires, okay, to make the common ground and common five volt on a breadboard, I have plugged my RGB here. The longer leg is actually the second one on the left. So I connect this to a ground line here with this small black wire. And then for each of the other legs, I have a 220 ohm resistor. And then this other part of the resistor goes to a digital pin. Okay, this one is actually outskirts. Pull it in. And you can see I have collected from pin 111091 difference from the Tinkercad simulation is that here my colors are actually read green and blue. Okay, How to know that? What I have just made some experimentations. Okay, So I have used wire which is white, green, and blue. Okay, just for the three colors, it doesn't really matter the color of the wire, the case just to give you some, maybe some indication. So you don't make more mistakes in the future. Alright? So you can see that this egret. 26. RGB LED - Code: Let's quickly see how to control the RGB LED with your Arduino. Once again, in this section, I'm going to go quite fast. The goal is not to go into details about each arduino functionality, but to reach the next activity part where you will practice on arduino functionality is controlled by the Raspberry Pi using cellular communication. So let's start and let's directly write some basic code for the RGB LED. So what you can do here already is to connect your audience wants the secret is correctly done. You can connect your arduino back to the Raspberry Pi with the USB cable and then check that you have the Arduino connected here. Corey book, of course. And now we're basically how to control an RGB LED is very simple. If you already know and you probably already know how to control an LED. While an RGB LED is nothing more than three different LEDs when we speak about the code. So we are going to control each leg of each color separately. So I'm going to create some defined here. So defined RGB, red being with bin number 11. So make sure that you use the pins you have selected. If you have selected different pins, defined RGB. So green, ten. And define RGB, blue been nine. And if you have some doubt about the other of those pins for you, well, just start with this and then you will be able to change that after you experiment with some code. So we have the defines, then what we do is we simply do pinMode on each leg, just like we were controlling three different LEDs. So pinMode for the red pin, pin mode, for the green pin mode for the okay, everything is correctly set up. And then we'll, I'm not going to do anything in the loop. I'm just going to show you how to just power on this RGB. So if you do digital rights here, you can do digital rights. And you can also do another right because we are using beings net up PWM compatible. I'm going to start with digital rights. Okay? So you can put, for example, high and then the digital That's just use the same here. So for the red and then for the green, and then for the blue. So let's say we want only to bore on, load only the green and the blue, okay, to make a combination of green and blue color. So those are going to be powered on with full intensity and this one with 0 intensity. So let's bodies connected. Let's load. Alright? And you can see we have these color, which is that exactly Blue, not exactly green, but in the middle. So with this you can do different combinations, okay, up to seven different combinations. And then I'm going to comment this. And now let's see an example with analog, right? Okay. So I'm going to use also the pin like this. And then you need to give a number between 0 and 255. So here you have more control over the poles, okay? So if you put 0, that's going to be pulled off. If you put 255, it's just like you use digital, right, with high. And then if you any of, for example, the one or 180, this is going to be a percentage. The brightness basically between 0 and 255, that's going to be applied to the red color of the RGB LED. So let's do this. I'm going to use the same for the green, but I'm going to put 0 here. I'm going to try to make a purple color. So I'm going to do blue and let's say 200. Okay? So we're gonna do a mix of red and blue, which should total. And let's see that. So let's run the code. Upload, uploading and don't applauding. Okay, and we have a column that is purple. So that's pretty much it for the RGB LED. You first create some define const int. And then you do pinMode for each bin, and then you can use either digital rights are analog, right? So this only if you have the PWM functionality, of course. And you can create any combination of column and this you can use in the setup in the loop, while you can use that anywhere you want. 27. Push Button - Circuit: Now that we have our RGB LED undersecretary correctly working, Let's add a push button. So here you will see that it's going to be very simple. So you find a push button here on the right. You take the package button and you are going to put it somewhere here in the middle of the breadboard. That's what I usually do. And then what we will need to add some connections to that push button. So first we will need to add a connection to the ground. And for that I'm going to add also a resistor. Resistor here that's directly connect, for example, the leg on the right here to the ground. And let's choose a 10 kilo ohm resistor. So that's going to be brown, black, and orange for a forbidden resistor. And that's going to be brown, black, black, and red for a five band resistor. So this register will act as a pull down resistor. So basically with this, we can make sure that the push button state will be low, or basically 0 volt by default. And when we price on the package button, the state is going to be high or five volt, okay, so that's very important to either use a external register or use the internal pull-up register of the Arduino, which is going to do the opposite, which means that the state is going to be high by default and low when you press on the page. But all right, so we have the connection to the ground now. So if the connection to the ground is on the right side here, the left side is going to be connected to five volt. And I'm going to use right here because that's 50 connection. Okay, So the push button is now correctly connected to five volts and ground. And now we are going to add a connection to a digital piece. So you take the side, okay, which is also connected to the ground. And from the same line here, you take a wire and that's connected to pin number seven here. Double-click here. Just going to do this to make it nicer. Okay, like this. And let's choose OK, think for example. So from the same side as where you plugged the resistor here, you're going to put a wire to pin number seven, okay, so we won't use interrupt here. You can choose any pin you want. It doesn't really matter. All right, and this circuit is done. Here, is the physical circuit with the push button that I've added also alongside with the RGB LED. So here you can see we have the plus button on the middle of the breadboard. And then on the left I have this wire, this red wire to the five volt line here. And then on the right side I have this restore, this 10 kilo ohm resistor connected to the ground, and then a wire from the same line here connected to digital pin number seven. 28. Push Button - Code (With Debounce): Let's now see how to read data from the push-button and more specifically, how to know when we have priced on the button. So the exact moment when we have pressed on the button, this is something you should already know how to do as per the prerequisites of the course, but I'm still going to do it quickly as a recap. Also, we will rebound the butter. So first of all, what you're going to do is to create a defined period. But Pete so defined our constant int. And so the penis number seven. And then if you want to initialize the push button, you do pin mode with button being in the inputs. So the first button is initialized. Now if you want to read from the push button, you're going to do digital real button, pin. And this is going to return you a bite, Okay, That is high, output is low. So for example, bytes state is equal to digital radio button. Now, if we want to check if we have pressed on the push button, what we need to do is we need to save the previous state. So we need to read the state, saved the previous state, and then read again and check if we have something different. Okay, So I'm going to add here vide, previous button state. And I'm simply going to initialize that to the current state of the button just after we initialize it. So that would be the first state of the particle. And so now what we need to do in the void loop is to read the package button again with digital rate and then compare to the previous button state and see if we have something different. But what I'm also going to do is I'm going to debouncing the push button. Because if we just compare the previous and the current state, what's going to happen is we're going to have the bounce mechanisms of the physical bumps of the button. And this will give us some wrong measurements. Okay, so we need to denounce the button to make sure that when we actually price, we get just one price on the code and not multiple processes because the bottom is bouncing. So to do that, I'm going to create two new variables here. Unsigned, long, last time. Buttonpressed is equal to, so let's initialize to out to 0, doesn't matter. And then unsigned, long, let's say button debouncing is equal to endless but 50 milliseconds. So what we're gonna do here, basically, we're going to first in the void loop, get the time. So unsigned, long time now is equal to MRT. And then we're going to do if time now minus last time the button has been priced. If this is greater or equal than the button dividends duration here, then we can enter this. If. So, we will see that when the state of the button changes, we're going to update the last time the burden was priced to the current time. And so that the next time we enter the while loop, we're going to check the condition. So time now minus last time we boron has been priced in, This is lower than the delay. So basically, if this is lower than 50 milliseconds, then we are not going to read the state against. So basically we are going to ignore the burden for 15 milliseconds every time the button has been pressed. So we don't read all do bounces of a button. So now if enough time has passed, which we can do is do bite button state to the current one is equal to digital. Read. With button pin. Like this. We read the button state and then we do If button state is different from the previous button state. In that case, we know that the state has changed. And what we can do is to now update the last time the button pressed. So actually let's name it different. Last time Boudin changed, okay. Because it's not pressed here is just changed. Because if we are here it could be pressed or released. So I'm just going to use changed, which is for both. Okay. So last time the button has changed is actually time, no. Okay. Because that what just happened. And also the previous button state, which is now the current button state. And so the next time we enter the while loop, we're going to first check if enough time has passed since the last time, but unchanged. And we only going to enter this if again after 50 milliseconds. And then when, when we are here in the code, we know that the burden state has changed. So to know if the bone has actually been priced are released. We do if burden state is equal to hi In our case because we have a pull down resistor. This means that the button has been priced, and if it's low, it means the button has been released. So here we could do. So let's actually do serial.begin. Here we use sale to debug and Syrian dot print LN, let's say button has been pressed. So when we enter this, if we know that the button has been pressed, so just priced and we'll have any bounce because the next time we're going to read the button is 50 milliseconds later. So let's upload the code. And actually we have an error. Why do I have this arrow? You can see can't open device because I have simply not plugged buy Adreno to the Raspberry Pi r to the computer if you're using your normal computer? No, I checked tools. Okay. Upload. And done uploading. I'm going to open the cyan monotone, make sure the border-radius correct. And I'm going to press on the push button. Button has been priced. I press again. And you can see every time I press, I press again. Button has been pressed. One more time and well, the code is working. So this is the structure that you are going to use. Often do know when you have pressed or released on push-button and with the debunks mechanism. 29. Buzzer - Circuit: We now have the RGB LED and the package button in our secret. Let's now add a buzzer so we can make some sound. So the mother can also be named sometime Piazza. Okay. So you take your brother and you just plug it, for example, here. And when you have two pins, a positive and a negative. So you just put it there, just plugged in negative two, the ground like that. And you plug the positive directly to a digital pin. Let's use HE digital pin number eight, which is available. And let's take, for example, yellow color. Okay, you don't need any resistor here. And so that's very basic, okay, you just plug in a negative to the ground and the positive to a digital pin. It doesn't matter which one. Okay, you don't need to have PWM for the Basel. And here is the secret with the buzzers. Of course material just connect and you pour of the Arduino. You can see I have the buzzer here. And so one important thing you can see, you should have a passive buzzer. How to know this is a passive. Well, if you look at the back, you should see the PCB like this should be gray or something similar. If it's completely black, it means you have an active, but okay, so here I'm using a passive and I see the PCB. You can see I have a minus and a plus. You have the plus here. So the plus is the positive side. So I'm just going to plug it back here. And so you have the negative side connected to the line here, the ground line. And positive, which is connected to digital pin number eight on the adrenal. 30. Buzzer - Code: And now let's quickly check how to control the buzzer. So that's going to be the easiest component to control actually. Okay, so first you do a defined here versus a pin, which is pin number eight here. And then you are going to use pinMode. Pinmode with buzzer pin. And this is output because the system thing with control from the ru. And then well, you have one function that is super useful is the function tone. So the tone function will basically create a sound on the buzzer. And so you first need to put the pin, so the pin, and then the frequency, and then a duration. So the frequency will be enhanced. So for example, if I play 440, That's the a sound I'm going to play that sound for, let's say, 500 milliseconds, okay? So frequency and the duration in milliseconds, I'm just going to try this program like this. Let's upload the program. Okay? And as you can here, we have a sound for 500 milliseconds. Now let's change, for example, a lower frequency and let's see what we get. Okay, you can see the sound is different. Now if I want to play that for two seconds, I'm going to put 2 thousand. Alright, and that's pretty much it. Now what you can do is you can use, of course, the tone in the setup in the loop well, anywhere you want. So you first provide the pin, the frequency, and the duration. And so this component is going to be useful for our project to inform the user for something went good or something went bad, we can play different kinds of sounds. 31. LCD Screen - Circuit: Let's now add an LCD screen to our circuit. So the LCD screen will be super useful. So we can display some text directly from the Arduino. So LCD we are going to use a 16 by two, okay, and where I'm going to put it, the Tinkercad and then you will see that I'm going to directly plug it here on the breadboard when I do the real secret. But for the, just for the simulation and to show you the connections for the pins, that's going to be easier like this funnel. So you have many connectors here and let's go step-by-step, one-by-one. First you have GND ground. You're going to connect to the ground line. Let's make sure that it's ground line, okay. That's actually particularly this. So ground is going to be black. Then you have VCC. And this is a, you connect to the VCC line here, five volt line. Then we have Visio. Visio is going to be used to calibrate the luminosity of the screen, okay, using a potentiometer. So basically here we need to also have a potentiometer. So I'm going to put a potentiometer here. And so the potentiometer, you have three legs. You're going to put one of the leg, for example, the left one connected to the ground, the black wire, and then the opposite one connected to by volt with the red wire. And then you're going to connect this Vizio here to the middle pin of the potentiometer. And let's put it to the next pin is the pin. And while this one, you're going to plug it in to a digital pin. But because we will not have enough digital pin, I'm going to plug it to an analog pin instead. So I'm going to plug it to a four because well, you can use actually analog pins as digital pins. So if you ever ran out of digital pins, just use an analog pin instead. And in the coat, just use it as a digital pin. Let's change the color. Let's put green. And then the RW, RW is going to be connected to the ground. We were black wire and the four enable he is going to a five. So same reason as for the spin that use a different color, for example, purple. We're going to use this other digital pin because we won't have enough digital pins here on the secret for all the components. Then you have d, 0, 1, 2, and 3. You have not gone up at anything for those bins here. And then D4 default, we're going to plug it to the digital pin number two. So let's use another color, yellow. Okay, D5 is the next one to digital pin number three. Let's use the class and then the 6 to digital P4. So we just follow with pink, the 7 to digital pin five. Let's use orange. Okay? And then we have LED, so we have to, the first one is going to be connected to five volt, but with a resistor. So that's going to be the LD of the LCD screen, okay? Because when you use actually LEDs here you can see we use 200 ohm resistor also. That's the same reason here. I'm going to add areas TO here, 220 on, and I'm just going to connect it between here, this connection on the LCD with red wire and then connect it to the line, five volt line. And then we have that one connected to the ground. All right. One thing to make sure that you don't do is so don't connect anything to those pins here. Digital pins 0 and 1. Why is that? You can see we have RX and TX. So those pins are also actually used for cellular communication. So when, later on when you sail communication with the USB cable, if you have anything plugged here, you're going to have some weird behavior, okay? So you're going to have some errors on the silo communication and maybe also on the components that you use on those pins to leave those spins empty. And here is the circuit with the LCD screen. So this can look a bit messy. But well actually if you just follow all the steps that I did on Tinkercad, you should not have any problem. So I have plugged as you see, the LCD screen directly on one of the line here. So each, actually each pin, each leg of the ACP is on a different line, okay? This is not connected on the blue or the red line is connected here and separated lines on the breadboard. And then you can see each connection here. So I have the first one. Maybe you will have also different names on the LC. So you have, for example, VSS, Vdd. At the end I have a and k. So the name can be different, but the other is going to be the same as what I did before. So the first one is connected to the ground here. The second one, see this red wire is connected to the SEC. And then the third one is v. So v is here connected. So this blue connected to the output pin of the potentiometer. And the potentiometer also has, so for the two extreme legs, a black wire to the ground and the right wire to this disease. After that, we have the R S, R S, which is connectivity to a four. And then we have the RW, RW connected to the ground here with this black wire. The enabled connected to a five. And then we have nothing for the 0 until d3. And then you can see D4. D4 is connected to digital pin number two, and then d5 to digital P3, the six to digital pin four AND seven to digital pin number 5. At the end we have, so for the a here, which is basically the positive side of the LED screen, I have 1220 ohm resistor connected from C to a different line. And then this red wire connected to the plus key on the breadboard. And the last connection, he is a black wire to the ground. Alright, and that's it for the LCD screen. 32. LCD Screen - Code: Let's now write the code to just print something simple on the LCD screen. So first of all, we're going to use a defined for each of the pin that we need. Okay, so define, I'm going to put LCD. So we will need the pin which has been a four, and then define LCD bean, which is pin number A5. Then we need the pins from LC 4 pin, which is two, and we will need the four, D5, D6, and D7. So let's change that by 6, 7, which are being number 2, 3, 4, and 5. Okay, so we have six pins that we need in our program. Then to initialize the LCD screen, we will need first to include the liquid crystal libraries. So liquid crystal dot h, like this with angle brackets. Then we can add so not in the void setup but in the global scope here, liquid crystal, let's name it LCD. And we're going to initialize it by providing the different pins here in the other. So our S ending and then default. And then I'm going to go to the new line here. Let's put D5. Let's align this here. Six, the seven. So we have our LC object. And now to finish initialization in the void setup, we do LCD dot, begin with 16 and 2. So we're going to use 16 columns and two lines, which is basically what the screen has. And then, well, if you want to print something on the screen, you just do LCD dot print. And let's say starting with dot, dot, dot. Let's say we want to print that at the beginning. So let's make sure that the board is connected. Let's upload the code. I forgot here. So well, they arise here, but I forward here a comma. Let's run again. Okay, and this is what you can see here. So maybe you will not have anything is because you need to move the potentiometer until you find, you can see here until you find the correct angle for the potentiometer to correctly calibrate the screen. So it looks good. Okay, we have starting correctly displayed. So make sure that for the first time you actually move the potentiometer so that you can see something. Once you see something that we can go further. Okay? And now for example, if you want to clear the screen, let's say we do delay 2000, so we want to print for two seconds and then we do LCD dot clear. This is going to print starting and after two seconds, it's going to clear the entire screen. Let's upload this. So you can see it's starting. And after two seconds, the text is gone. All right, so let's comment that. And one other thing is that you can actually play with the cursor. So by default the Kusto is going to be at 00. So you can do LCD dot set, Crusoe, and by default is going to be 00. Here you can see you have 16 by two. So if you put, for example, 3, That's going to be under third color. If you put one here, That's going to be on the second line, okay, so we start to count at 0. So you can go from 0 to 15 here and 0 to one here. And so here we're going to print starting, but let's see where. So upload. And you can see we print starting on the second line, starting from the fourth column, okay, because this is the 31, so that's signal line for color. And so every time we want to change the cursor, you just do Ltd said Crusoe. And then you start to print something and that's going to start to print from where the cursor is. Okay, so basically that's pretty much it. First you initialize the LCD, you do LCD dot, begin with 16 by two, and then we set q. So you can set the cursor to a different place with print, you print some text. And LCD clear is very useful. So you can also clear the entire screen before you write something. 33. Servo Motor - Circuit: All right, and let's finish this section with a several motto. And just after that, we are going to practice with the amino component, the second communication and the Raspberry Pi. So I'm going to add a server here. So a microbe. That's actually flip it like that. Okay? So on a classic hobby, several motto you will have here you will see three different pins and three wires. So you will have a brown one, the red one, and an orange one or something that is similar. So the brown one corresponds to the ground. So we are going to first connect the ground to the ground of the Arduino. So let's connect to Common Ground here. Let's put that in black, and then we have power. So power we can give five volt here. Let's go here. That's going to be right. And then we have the signal. So the signal is basically to be connected to a digital pin. I'm going to use 12. So for this digital pin, you don't need any PWM pin or any special pin, just any digital pin will do. And let's use a different color. Let's use brown. Okay, and that's it for the seven motto, but I'm going to explain you something a bit extra here. So here what you're doing is you're going to control the settlement or we've been number 12. So that's going to give the command to the server, but the several is pulled from the university. The thing is that settlement also, if you use a hobby, one standard one is completely fine. It's going to be pulled on from the Arduino, okay, That's obvious here, but the server may draw more power than other components. So if you want to add multiple servers or give more strength to the several, you may also want to add an external power supply. So I'm just going to show you. So I'm just going to use these nine volt battery here just as an example. So yours is not going to be that one that can be any other power supply. But basically what you can do is you can see when you have an external power supply, you have a negative and a positive. So let's actually connect this to this side of the breadboard. Is I'm going to put that in black and red. So now we have this side of the breadboard with the ground connected to the amino here, the line with five volt, and now the other side here. So those kids though he is not in this case just on top, that is just a visual. So you have another line with another ground and another line, which is this time the external power supply. And what you can do is that instead, you can just pour on the several, some Jews gonna do this. So, but the ground here. And then the power supply is going to come from the external power supply. Okay? And one very important thing to do is to actually connect the grounds of everything, okay? Because if you don't connect the ground of everything, then the command you're going to stand here on pin number 12 is not going to work, is going to be some garbage commands. So you're going to connect the ground of this to the ground of the Arduino. Make sure you connect the ground, okay. And not the ground to the Vcc because you may have some problems. Okay? And now you have the same servomotor. You can leave the same comment, but it's externally powered by another battery or another power supply. So all the grounds are connected. Everything here is powered by the Arduino. The several motto is powered by this. Okay? And then we still have the comment from pin number 12 that goes to the settlement. And just one last thing is don't use PIN number 13 here on the Arduino Uno because PIN number 13 is also connected to the built-in LED. And if you connect anything to that, well, you may have some unexpected behavior because this beam is going to be triggered automatically at some point. So just don't use the pin number 13. So you can use pin two to 12 on Arduino Uno because those ones are reserved. Better to reserve them for sale communication. And these for the built-in LED. Alright, that's it for the secret. Then here is the real secret with the servomotor. You can see he uses very small one and the gear is in plastic here. Okay, so that's a very low-quality one, but that's going to do for this course. That's basically the standard one, the cheapest you can find and maybe the one you have on your Arduino kits. And you can see I have some kinds of stuff like this, but I'm going to use on the several for the final project. So you can see we have this wire, this cable with a brown, red and orange. Brown and red are collecting here to ground and five volt or the Arduino. I'm not going to use an external power supply. I'm just going to cover the seven from the amino. This is completely fine, okay, for this circuit is just that if you want more cerebral or if you want to use that in a real application where you need to put a lot of talk on the service or a lot of force on the several. Maybe use an external possibly. So here I've just moved the bits, the ground and the 50 line here on the right. So I can put the several here on the left. And then this is connected to pin number 12. Okay, just to show you, I have another several. So you can see that's completely different. Here, are bought another one and the gear here is a metal. And you can see when you buy the circle, usually you have stuff like this that comes with it. Okay, So you can just plug this to the server. So you can basically plug the server to something mechanical, for example, a door mechanism or anything like a camera, anything that you want. So you can plug the several here with the mechanical part with those tools here. And you can see any hobby several, well has the same same connector, so I'm maybe going to use that one for the rest of the course, but that's the exact same. Several. 34. Servo Motor - Code: And let's now write the Arduino code to control the several motto. So first of all, I am going to of course add a defined several bin, which is number 12. Then to initialize the several, I will need to include the several libraries. So several dot h, Okay, so we needed to include a library for the LCD screen as well for the cell. And then we're going to create a circle here in the global scope. So several, we use the name of the library and then let's name it several weeks as lowercase. Okay? So we just create it like this. No parentheses, nothing. In the void setup. I'm going to do several dot attach. So we're going to basically attach a pin to control the seven motto. And of course that's the server. And now the server is initialized. So if I want to give a command to several, several dots, right? And what can I give? I can give a number, so an integer number between 0180. So what is that? Basically, a servomotor is not unlimited, okay, in what it can do, it can go from 0 to 180 degrees. So basically it can make half per rotation. You can do more than that with a seven motto. Okay? So for example, if you want to control a wheel with your amino, well that's not the kind of motto you want. You want a model that can make more than one term, okay? So here basically 0 is going to be one extreme position and 180 is going to be the other extreme position. Let's put 50. Okay, so we're going to put the servomotor at 50. Well actually let's go to the middle, Let's go to 90. So that's going to be, that should be the middle position of the servomotor. And let's upload that code to the Arduino connected upload and let's see what the server is doing. Compiling, uploading. Okay, and you can see here the Sibyl has moved. This is the middle position. So maybe if I want to middle position here, I can change that plastic parts to be around something like around the middle position. Okay. So it's going to be like this on the extreme position and he is going to be like that on the lowest position. And maybe you several can make a little bit of noise is just that. Well, the plastic gear is not a great quality. And the several might force a bit, but that's not really a problem here. Again, if you want to have a better quality, you can buy another several, but for now, that's all we need if you have that from your starter kit. And then when I want to show you something more, just because we're going to use that in the following, is how to make the several sweep. So basically the sweep example. So we're gonna go from 0 to 180 and from one or a 100, 8200 and that indefinitely. So you can see the several moves from one side to the other. So I'm going to remove that. And in the void loop I'm going to add a for loop. So for int I is equal to 0, I lower than 180, and I plus plus. So basically we go from 0 to 180 and we do several dots, right? I, and we add a delay of, let's say, 10 milliseconds. So every 10 milliseconds, we increment the position from 0 to 180. And once we reach 180 degrees, we do for int I is equal to 180. If I is greater than, lead to greater or equal than 0, I minus, minus. So with this, we go from 180, 200, okay? Because we decrease, we decrement the counter every time several experiments, correctly spelled, right? I. And delay tends to what this is going to make. The cell will go from 0 to 180 degrees every 10 milliseconds. This is going to make the several go from 180 to 0. And then we come back here and we start at 0 again. And let's see what it does. So let's upload. And let's check the several motto. Okay? And you can see the several is going from the minimum to the maximum position and coming back. So now if you want to stop this, what you can do is you can upload another program that doesn't use. The several are what I usually do is quite safe to do. I just disconnect the wire on the pin number 12 here you can see the comment wire. Okay, that's it. I don't disconnect the ground item is connected, possibly just disconnect the command. And I make sure that this doesn't touch anything else. Okay, so I can continue with my experiment and I don't need to upload an empty code. All right, and that's the end of the section where we built the Arduino circuit for the following him the course. Let's now get some practice with those components, which we are going to control from the Raspberry Pi. 35. PART 2 - Practice : You now have all the components you need for the final project of this course. And before we go there, let's get some practice first, in the following activities, you will create some programs to control some Arduino components from the Raspberry Pi. You will see some examples that are going to make you understand better what you can achieve using cereal and how to put what you want to do into real code. That works. And let's start. 36. Activity 04 - Debug Serial Using LCD Screen: In this activity number 4, we are going to start from the code actually of the activity number three. So we have the Python code