Arduino and Raspberry Pi, working together over a serial communication. Step-by-step tutorial (UART)

preview_player
Показать описание


This video shows you how you can connect a Raspberry Pi to an Arduino, both to program the Arduino and then to communicate with the Arduino. In this case the Raspberry Pi takes on the role of a primary controller with the Arduino acting as a secondary device responding to the instructions of the Raspberry Pi.
#raspberrypi #arduino

This is achieved using serial communications over USB, although could also be applied to direct serial from the Raspberry Pi GPIO to the Arduino Serial pins (with appropriate voltage level-shifting).

This example uses the USB port which uses UART serial communications. These are generally asynchronous communications sent half-duplex with one side sending at a time. This is similar to RS-232 serial communications that you may have used on older computers, but this uses different voltages and doesn't meet the full RS-232 specification.

#uart

The video explains the circumstances this can be useful, before working step-by-step through a practical exercise with the Arduino measuring an analog input voltage and reporting that to the Raspberry Pi, and the Raspberry Pi telling the Arduino when to turn an LED on and off.

The communication between the Raspberry Pi and the Arduino is over a custom text based protocol, making it easy for humans to understand the communication. First the code is created on the Arduino using C++ which can be tested using the Arduino IDC serial monitor. Then the serial code for the Raspberry Pi is added in Python which sends instructions to the Arduino.

This is going to form the basis of more advanced projects that I'm hoping to work on in future. So if you want to see more please subscribe to my channel and click on the notification icon to be notified of my future videos.

Chapters:
00:00 Introduction
00:34 Advantages of Raspberry Pi
01:53 Advantages of the Arduino
03:36 Taking the best of each platform
04:28 Using GPIO serial pins
06:05 Creating the circuit
06:46 Installing the Arduino on the Raspberry Pi
07:19 Launching the IDE and programming the Arduino
08:35 Basic electronics code
10:05 Adding serial code to the Arduino
33:27 Adding Python serial code to the Raspberry Pi
37:43 Running the Raspberry Pi and Arduino code together

Raspberry Pi UART Information

Arduino Serial Information
Рекомендации по теме
Комментарии
Автор

I've now created a further video using SPI to communicate between the Raspberry Pi and Arduino.

PenguinTutor
Автор

Thks I'll give them a try next year. But in-general my raspberry piV5 will be for everyday/routine computer stuff & the arduino is for geek electronics projects.

tombouie
Автор

This application is an example where you could use Red Code to write code for both devices.

BartdeBoisblanc
Автор

Wow, thanks. I spend two days trying to do this. Readline() hangs the rpi. the following doesn't block.
if arduino.in_waiting > 0:
inChar =
inputString = inputString + inChar
if inChar == "\n":
stringComplete = True
# arduino.reset_input_buffer() # not needed, read must pull from serial buffer
iS to save
inputString = ""

motojaknow
Автор

Thank you so much for this video, it was incredibly helpful

simon
Автор

The library used here "pyserial" does not work with threading or multiprocessing. Just a heads up for anyone watching this

donkeypoopdragon
Автор

Thanks for the info. I suppose if you wanted to blink a light on the Arduino/etc, you would use some sort of if serial input = x, then do y, correct?

That'd be a good followup if you haven't made it yet.

JeremyCook
Автор

Great vid, can u make one where arudino and raspberry pi are using spi ?

garipoter
Автор

I am curious as to why you said you'd be switching to an older version raspberry pi for the final version of your battery-powered project. Is the gain in battery life so big that it's worth the downgrade in processor, ram, GPU, etc? Another way to ask this is, is the model 4 that much power hungry than the 1?

IncipiensNovus
Автор

Thank you for the video, however I'm wondering how could you reset the Arduino using the RPI?

chamseddinebedhiaf
Автор

hi thanks for your video but i have some question
i want to use readable() in python(raspberry pi) but readable() always return 'True'
if there is nothing to read from arduino the code readable() have to return 'false' i dont know why.... can you help me?

b
Автор

great lecture!! pls tell me whether Arduino Uno can send images to raspberry pi over serial communication ??

kovidhaswapnapriyak
Автор

Hey sir, I have some problems, may you help me?

busraakargol
Автор

When using various Arduino boards attached to Pi400 via USB, I notice that the Arduino serial plotter performs poorly when I am using a potentiometer or other analog device. Specifically, the serial plotter seems unable to keep up with analog input. I wonder if interfacing the Arduino with Pi400 via the Pi's TX and RX pins might give faster performance than USB provides. Would appreciate your thoughts.

floridian
Автор

When using an Arduino Nano attached to Pi400 via USB, I find that the Arduino Serial Plotter lags by comparison to the Plotter on my Windows computer. Have you had the same experience? Do you think that one Arduino is better for use with the Pi than another? For example, I notice that the Leonardo is often recommended and that its processor is often touted as optimal for interfacing with the Pi, but I haven't seen a systematic comparison.

mike
Автор

How to activate arduino sketch only when prompted or activated in node red not when connected? Pls advise. Thx

terryterry
Автор

I made an ultrasonic height and weight measuring device. I wrote the codes with Arduino and arduino UNO. Normally everything works fine. But sometimes after waiting for an hour or two, I take a measurement, the device is measuring, it shows the numbers on the screen, but it does not print the result to the thermal printer. The numbers stay on the screen, then the device is reset with whatchdog. This is starting to get annoying. can you help me with this

vangeziyorum
Автор

hi i bought a few Arduinos and they turned out to be fake or cloned with ch340 chip and i cannot do what i wanted i read that i cannot reprogram the ch340 chip to be recognized as a hid joypad is there a way that i can make the rpi and arduino bypass the ch340 chip and get recognised as joypad like the Arduino Leonardo as one thing im trying to make a force feed back simulator steering wheel and because of the ch340 chip cannot be reprogramed to be seen as a hid when plugged in so could i use rpi as hid and use it to controll the firmware on an arduino so use rpi as usb interface and brains use arduino as a slave for pots and motor drivers im a new to rpi and arduino i have rpi zero 2w arduino mega 2560 ch340 chip and uno with ch340 chip i also read that rpi zero can be detected as a hid with a few changes

sketflamez
Автор

Can they communicate with each other without the cable wire?

MountReal
Автор

With the dislike button being removed I am not sure whether I should give this guy a chance… Let’s see after 40 min…

ferdinandvanvuren