In this blog post I will show you how to read and write data from Arduino with Python over a serial interface. I will use a library called
"pySerial". Although I'm using Ubuntu the library is said to work well across all operating systems.
Prerequisites
- Python
- Arduino IDE
- Some of the Arduino boards (Uno, Mega, etc.)
- USB cable between the Arduino board and your PC
Installing pySerial library
You have two options for installing
pySerial - using
pip (a package manager for Python) or downloading the library and installing it manually.
1. Using pip.
I assume that you are familiar with
pip . If you have Python 2 >= 2.7.9 or Python 3 >= 3.4 then you already have
pip by default. If you do not have
pip then you have to install it. Detailed information on how to do that could be found
here. The following steps are executed in Linux environment so Windows users should skip the 'sudo' part in the commands.
Open a terminal and type the following command:
That should be enough to start using the library for most of the users but in my case I had
pySerial already installed so I upgraded it with the following command:
You can check the version of the library with
You should see
pySerial and its version in the list.
2. Installing pySerial manually.
You have to download the library from
here. Windows users can make use of the .exe installer but I'll show you how to install the library from archive. Get the archive named '
pyserial-x.x.tar.gz'. It should be uncompressed into a folder. Windows users may need to install an additional archiver like 7-zip for working with
tar.gz. For Linux just open a terminal and type the following:
The last step is to navigate to the folder (on terminal/command prompt) you have just uncompressed the archive into and type the following:
Whichever of the methods you chose that should be enough to start using the library.
Reading data
Connect your Arduino with the PC and open Arduino IDE. Now you can see the device name in 'Tools -> Serial Port' menu. In my case the name is '/dev/ttyACM0'. See the screenshot below.
Remember the name as we will make use of it later. Now copy the code below, paste it in the Arduino IDE sketch worksheet, verify it and upload it on the Arduino board.
Now open IDLE and copy&paste the code below.
Now is the time to replace {device_name_placeholder} with your device name that you remembered (in my case '/dev/ttyACM0'). You can run the script and you should see the word "Arduino!" printed continuously in the console.
If you use Serial.println() in the Arduino code then you should use ser.readline(). Using ser.read() won't read anything because you are printing lines.
Writing data
Writing data to Arduino is easy too. See the code below.
Replace {device_name_placeholder} with your device name again. Note the 'b' before 'Arduino'. It is only used in Python 3 because strings are Unicode by default and they should be converted to bytes. For Python 2 you can skip it.
Conclusion
That was a simple example on how to read and write data from and to Arduino in Python. I will show you soon how to use that to make a simple cyclecar that is controlled by a GUI application.