USB Serial Communication Between Arduino and Raspberry Pi

Let's try to communicate between Arduino and Raspberry Pi through USB cable using Serial communication.


Communication between 2 controllers is quite helpful. It can unlock more possibilities and also be able to do task simultaneously. Previously, I have shared how to do communication between Raspberry Pi and Arduino using I2C communication. Now, instead of using I2C, I will show you how to do it through USB cable and using serial communication. This setup also possible to program Arduino board using Raspberry Pi.


This video will show you how to do serial communication between Arduino and Raspberry Pi through USB cable.

Hardware Preparation

This is the list of items used in the video.

Sample Program

This is the sample code used in the video for Arduino (top) and Raspberry Pi (bottom).

#define BUTTON 2
#define LED1 3
#define LED2 4
#define LED3 5
#define PIEZO 8
#define NOTE_D4 294
#define NOTE_G4 392
#define NOTE_B4 494
void setup()
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
void loop()
if (digitalRead(BUTTON) == LOW) {
Serial.print("Button Pressed");
while (digitalRead(BUTTON) == LOW);
if (Serial.available()) {
String inString = "";
while (Serial.available()) {
char inChar = (char);
inString += inChar;
if (inString == "SW1 Pressed") {
digitalWrite(LED1, HIGH);
digitalWrite(LED1, LOW);
else if (inString == "SW2 Pressed") {
digitalWrite(LED2, HIGH);
digitalWrite(LED2, LOW);
else if (inString == "SW3 Pressed") {
digitalWrite(LED3, HIGH);
digitalWrite(LED3, LOW);
void playTone(int note)
tone(PIEZO, note, 200);

from gpiozero import LED, Button, Buzzer
import serial
led1 = LED(17)
sw1 = Button(21)
sw2 = Button(16)
sw3 = Button(20)
buzzer = Buzzer(26)
serialPort = serial.Serial("/dev/ttyUSB0", 9600, timeout=0.5)
def sw1Pressed():
serialPort.write("SW1 Pressed".encode('utf-8'))
def sw2Pressed():
serialPort.write("SW2 Pressed".encode('utf-8'))
def sw3Pressed():
serialPort.write("SW3 Pressed".encode('utf-8'))
sw1.when_pressed = sw1Pressed
sw2.when_pressed = sw2Pressed
sw3.when_pressed = sw3Pressed
while True:
command = serialPort.read_until('\0', size=None)
commandString = command.decode('utf-8')
if len(commandString) > 0:
if commandString == "Button Pressed":
buzzer.beep(0.1, 0.1, 2)
except KeyboardInterrupt:

Thank You


  1. Python Serial library – PySerial
  2. Library to control GPIO on Raspberry Pi – gpiozero

Thanks for reading this tutorial. If you have any technical inquiry, please post at Cytron Technical Forum.

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

IoT Push Notification Using Rain Sensor with MicroBit
Low Profile Aluminum Case and Icon Case Stress Test
DIY Digital Alarm Clock Using REKA:BIT With Micro:bit | Tutorial for Beginners
Send Temperature and Humidity Data To ThingSpeak Using micro:bit
DIY Interactive Robot using REKA:BIT with micro:bit | Tutorial for Beginners
Tutorials of Cytron Technologies Scroll to Top