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.
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:
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.
7 Comments
Nice article.
Why do you address it via /dev/ttyamao and
not /dev/ttyUSB, how it was detected?
Thanks,
Ton
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?
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.
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!
nice but
where is the picture to connect rpi and arduino?
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?
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′))