How to Connect Arduino to Python via Serial Communication
In this tutorial, you’ll learn how to connect an Arduino board (e.g., Uno, Nano, Mega) to your Python environment using the pyserial library, enabling two-way communication between hardware and software.
This is one of the most important foundations in robotics and embedded systems, as it allows your computer to send and receive commands, sensor data, and control signals in real time.

What You’ll Learn
- How serial communication works between Arduino and a computer
- How to install and configure
pyserial - How to send and receive data between Python and Arduino
- Common debugging issues and how to fix them
Requirements
| Component | Description |
|---|---|
| Arduino Board | Uno, Nano, or Mega |
| USB Cable | For connection |
| Computer | With Python installed |
| Software | Arduino IDE, Python IDLE/VS Code |
| Python Library | pyserial |
Step 1: Install Python and PySerial
If Python isn’t installed, download it from:
👉 https://www.python.org/downloads
Once installed, open your terminal or command prompt and run:
pip install pyserial
To confirm the installation:
import serial
print("PySerial installed successfully!")
If you get no error, you’re good to go.
Step 2: Upload Arduino Code
This Arduino sketch listens for data from Python, processes it, and sends a response back.
Arduino Code (arduino_serial.ino):
int x;
void setup() {
Serial.begin(115200);
Serial.setTimeout(1);
}
void loop() {
if (Serial.available() > 0) {
x = Serial.readString().toInt(); // Read integer from Python
Serial.println(x + 1); // Send back result
}
}
👉 Upload this code to your Arduino using the Arduino IDE.
Note the COM port your Arduino is connected to (e.g., COM4 on Windows or /dev/ttyACM0 on Linux/macOS).
Step 3: Write the Python Code
This Python script opens the serial port and allows you to send numbers to the Arduino and receive responses.
Python Code (serial_comm.py):
import serial
import time
# Set up serial connection
arduino = serial.Serial(port='COM4', baudrate=115200, timeout=1)
time.sleep(2) # wait for the connection to establish
def write_read(x):
"""Send data to Arduino and read response."""
arduino.write(bytes(x, 'utf-8'))
time.sleep(0.05)
data = arduino.readline().decode('utf-8').rstrip()
return data
while True:
num = input("Enter a number: ")
value = write_read(num)
print(f"Arduino replied: {value}")
✅ Output Example:
Enter a number: 4
Arduino replied: 5
Step 4: Common Errors & Fixes
| Issue | Cause | Fix |
|---|---|---|
SerialException: could not open port | Arduino IDE Serial Monitor is open | Close Serial Monitor before running Python |
COM port busy | Port used by another process | Restart Arduino or unplug/replug |
Output appears as b'5\r\n' | Raw byte format | Use .decode('utf-8').rstrip() |
| Arduino not responding | Wrong COM port or baud rate | Check Device Manager or Tools → Port |
Step 5: Real-World Applications
Once this link is working, you can extend it to:
- Stream sensor data (e.g., temperature, ultrasonic sensors) to Python for visualization
- Send control commands from Python (e.g., turn LEDs on/off, move motors)
- Build data-driven robotics systems where AI models in Python command embedded hardware
- Integrate with frameworks like ROS (Robot Operating System) for real robot control
Example: Control an LED from Python
You can easily modify your Arduino code to toggle a digital pin via Python input.
Arduino Code Snippet:
int led = 13;
void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
char command = Serial.read();
if (command == '1') digitalWrite(led, HIGH);
if (command == '0') digitalWrite(led, LOW);
}
}
Python Code Snippet:
arduino = serial.Serial('COM4', 9600)
while True:
cmd = input("Enter 1 to ON or 0 to OFF: ")
arduino.write(cmd.encode())
In Summary
- Python acts as the controller, Arduino as the executor.
- Communication happens over Serial (USB) using the PySerial library.
- Once mastered, this setup becomes the foundation for advanced IoT, automation, and robotics projects.
Softmatic Academy Note
At Softmatic Academy, we teach robotics, AI and embedded systems from a practical lens combining software intelligence with hardware control.
This simple link between Arduino and Python is your first step toward understanding how AI can drive physical actions in real-world robots.
