• First Steps
  • General
  • Projects
Facebook Twitter YouTube
Tutorials for Raspberry Pi Tutorials for Raspberry Pi
  • Hardware & GPIO
  • Sensors & Components
  • Scale
  • Cases
  • Fingerprint
  • Gas Sensor
  • Heartbeat
  • German
Tutorials for Raspberry Pi Tutorials for Raspberry Pi
Home»Arduino & ESP8266»How to let an Arduino and Raspberry Pi communicate with each other

How to let an Arduino and Raspberry Pi communicate with each other

Facebook Twitter LinkedIn Tumblr Email Reddit
arduino uno
Share
Facebook Twitter LinkedIn Email Tumblr Reddit Telegram WhatsApp

I have already described in a previous article how the Arduino works and which models exist. There are plenty of articles on the Arduino itself, but few in connection with the Raspberry Pi, but this combination offers many possibilities.

Since I want to show more about the Arduino in conjunction with the Raspberry Pi, I will begin by showing the direct communication of both.

You do not need anything more than a Raspberry Pi, a USB cable and an Arduino (e.g. Uno or Nano).

 

Preparation

In order for the Arduino to be able to communicate with the Raspberry Pi later, corresponding code must firstly be loaded on it. The easiest way to do it is via the PC/Mac.  I have already shown here how this works.

C++
1
2
3
4
5
6
7
8
9
10
11
void setup(){
    Serial.begin(9600);
}
 
void loop(){
    if (Serial.available()) {
        byte nr = Serial.read();
        Serial.print("The following char was received: ");
        Serial.println(nr, DEC);
    }
}

This is just a small example of how to use the serial component. Of course, you can also respond to the inputs by starting functions, setting or reading  I/O, etc.

 

Commissioning on the Raspberry Pi

On the Pi additional libraries are needed, we install them first:

sudo apt-get install minicom python-serial

You should disconnect the Arduino from the Pi if you connected it, as we need to find out the port name. To do this, do the following two times: once without and once with Arduino connected via USB.

ls /dev/tty*

You will notice that at the end of the list a new name has been added (for me that is/dev/ttyUSB0). With this name, you can address the Arduino via the serial port. Because the serial connection library was installed under Python, we can easily access it.

For this we create a new file

sudo nano serialTest.py

with the following content:

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import serial
import time
 
s = serial.Serial('/dev/ttyAMA0', 9600) # change name, if needed
s.open()
time.sleep(5) # the Arduino is reset after enabling the serial connectio, therefore we have to wait some seconds
 
s.write("test")
try:
    while True:
        response = s.readline()
        print(response)
except KeyboardInterrupt:
    s.close()

and execute:

sudo python serialTest.py

The output looks like this (Ctrl + C lets you stop it):

pi@raspberrypi ~ $ sudo python serialTest.py
The following char was received: 116
The following char was received: 101
The following char was received: 115
The following char was received: 116

The output of the read in characters takes place in ASCII format (DEC for decimal, HEX for the hexadecimal code). Of course, you can also issue the character directly or react to it.

 

How does this help me?

For one, the Arduino offers several features that may be useful, depending on the model. In addition, the Arduino also has many digital and analog I/O pins that can be easily controlled. It could also be used as a GPIO expander, which is much easier to control. Also, you can easily communicate with Arduino libraries/programs if you do not want to run them on the Pi.

python Serial Serielle Schnittstelle USB
Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
Previous ArticleRaspberry Pi GSM Module – Mobile Internet (LTE, 3G, UMTS)
Next Article Mit C# und .NET eigene GUI Apps für den Raspberry Pi entwickeln

Related Posts

Control all GPIOs with the Raspberry Pi REST API via Python

How to build a ESP8266 Scale (with Weight Sensor HX711)

Using TensorFlow Lite with Google Coral TPU on Raspberry Pi 4

How-To: Bluetooth Connection between ESP32’s and Raspberry Pi’s

7 Comments

  1. Ton Wiegman on 9. June 2019 23:36

    Nice article.
    Why do you address it via /dev/ttyamao and
    not /dev/ttyUSB, how it was detected?
    Thanks,
    Ton

    Reply
  2. Mike on 3. September 2019 23:48

    So I’ve followed all the instructions, but the python script fails because it says the port is already open. Did I make a mistake by loading the test code to the Arduino using the Arduino IDE on raspberry Pi? If so, is there any way I can get around this?

    Reply
    • Gyth on 14. September 2019 17:31

      Remove s.open() in the serialTest.py code line 5. When you excecute the s = serial.Serial(‘/dev/ttyAMA0’, 9600) code, the port is already opened hence no need to open it again.

      Reply
  3. Steve on 21. September 2019 0:30

    Can you please put publication dates on these articles? It would GREATLY help using them, and knowing what parts are likely to be outdated further down the line. Great site otherwise though!

    Reply
  4. dick olij on 15. October 2020 15:42

    nice but
    where is the picture to connect rpi and arduino?

    Reply
  5. Gary Bennett on 11. February 2022 8:22

    I cannot get past “TypeError: unicode strings are not supported, please encode to bytes: ‘test'” in the line
    s.write(“test”)
    in the raspberry pi program.
    How can I get the program to work?

    Reply
    • Nicolas on 14. November 2022 13:41

      According to stackoverflow you have to encode it yourself in newer versions of python. I don’t understand enough to explain what it means but their fix works

      you have to change line 8 to:

      s.write(bytes(“test”,’UTF-8′))

      Reply

Leave A Reply Cancel Reply

Raspberry Pi Bluetooth Data Transfer to the Smartphone

Transfer Raspberry Pi Raspbian OS to an SD Card (Windows)

How to establish a Raspberry Pi Remote Desktop Connection

Raspberry Pi Web Server Installation Part 6 – DNS Server via No-IP

Configure and read out the Raspberry Pi gas sensor (MQ-X)

Raspberry Pi LED Matrix Library – Installation for multiline m x n MAX7219 LED Matrices

Subscribe to Blog
Subscribe to Raspberry Pi Tutorials and don't miss any new Tutorial!
Subscribe

Thank you!

We will contact you soon.

Tutorials for Raspberry Pi
Facebook Twitter YouTube
  • Contact & Disclaimer

Type above and press Enter to search. Press Esc to cancel.