Python is one of the programming languages that has experienced the greatest boom in recent times. Its ease of use allows for quickly creating small programs and scripts, with very short development times.
This simplicity has earned Python a place in the Internet of Things (IoT), where it stands out for the ease of communicating with different devices (computers, tablets, smartphones), whether by cable, Bluetooth, or the Internet.
Of course, the world of Arduino is no exception and it is very easy to connect Arduino with Python, using the serial port and the PySerial library.
In this post, we will see how to connect Arduino with Python and the PySerial library, to use it in our electronics, robotics, and IoT projects.
Serial communication can be done either by cable or wirelessly via Bluetooth, as we saw in the post Conectar Arduino por Bluetooth con los módulos HC-05 ó HC-06
For this post, we will use Arduino’s serial port. If you are not yet familiar with the serial port, you can read the post Comunicación de Arduino con puerto serie
Install Python and PySerial
The first thing we need is to have Python installed on our device. If you haven’t started with Python yet, you can check the post Nuestro primer programa en Python where we saw how to install Python on Windows and Linux, and some basic examples to introduce its use.
Once we have Python installed, to communicate with Arduino we need the PySerial library, which allows us to easily use the serial port. The PySerial library is available at this link https://github.com/pyserial/pyserial
Download and run the installer, to add the PySerial library to our Python installation.
We can also install the PySerial library directly from Python, by typing the following command from a console.
python -m pip install PySerial
With either method, we will finally have the PySerial library installed and ready to be used in our projects.
Code Examples
Next, we are going to see a couple of simple examples to start testing and using the PySerial library together with Arduino.
Receive Information from Arduino
In this first example, we are going to read information sent by Arduino, captured, and displayed on the screen by Python.
To do this, we start by loading the following sketch into Arduino, which simply continuously sends the text “Hello World” once per second.
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("Hola mundo");
delay(1000);
}
We leave the sketch running on Arduino, and we are going to create the Python script. We create a new empty text file, which we save with the name “read.py”. Inside it, we copy the following code.
import serial, time
arduino = serial.Serial('COM4', 9600)
time.sleep(2)
rawString = arduino.readline()
print(rawString)
arduino.close()
What we do is import the Serial library (PySerial) and instantiate a PySerial object, which we have called “arduino”. In the Serial object constructor, we pass the parameters of the serial port we are using.
Remember to replace the serial port in the code, in the example “COM4”, with the serial port where you have Arduino connected.
Next, we use the “readline()” command of the Serial object to read a line sent by Arduino. We display the line on the screen using the “Print()” command.
Finally, we close the serial port using the “close()” command.
As we can see, using the serial port with PySerial is really simple. The only thing that might seem strange is why we had to import the “time” library.
The reason is that from the moment we create the Serial object until it is actually ready to be used, a certain amount of time is needed to open the serial port. Therefore, we have to introduce a wait using the “Sleep” function, which belongs to the “time” library.
Send Information to Arduino
In this second example, we are going to send data to Arduino from Python. For this, we will use the following sketch that we saw in the post Comunicación de Arduino con puerto serie
This sketch receives a number from 1 to 9 and blinks the integrated LED, connected to PIN13, the number of times received. We load the sketch into Arduino, and as before, we leave it running.
const int pinLED = 13;
void setup()
{
Serial.begin(9600);
pinMode(pinLED, OUTPUT);
}
void loop()
{
if (Serial.available()>0)
{
char option = Serial.read();
if (option >= '1' && option <= '9')
{
option -= '0';
for (int i = 0;i<option;i++)
{
digitalWrite(pinLED, HIGH);
delay(100);
digitalWrite(pinLED, LOW);
delay(200);
}
}
}
}
On the Python side, we will need a new script that we will call, for example, “write.py”. Inside it, we paste the following code.
import serial, time
arduino = serial.Serial("COM4", 9600)
time.sleep(2)
arduino.write(b'9')
arduino.close()
As we can see, writing is very similar to reading. First, we import the PySerial library and create a new object of type Serial, indicating the values of the serial port we are using.
This time, we write the value using the “write” function (in the example, 9). The “Write” function sends bytes, so it is necessary to convert the value to bytes by preceding it with a b (in the example b’9’).
Finally, we close the serial port with the “close()” function.
Again, we had to import the time library, to be able to use the Sleep function, and give some time between starting the serial port connection and sending data.
With this, we now have the basic functions to send and receive information to Arduino from Python, using the PySerial library to control the serial port. It is easy to integrate these functions into our programs.
Download the Code
All the code from this post is available for download on Github.

