How to Send Data from Arduino to Python via Serial Communication
To send data from
Arduino to Python, use the Arduino's Serial.print() to send data over USB and read it in Python with the pyserial library. Open the serial port in Python, read the incoming data, and process it as needed.Syntax
On the Arduino side, use Serial.begin(baud_rate) to start serial communication and Serial.print() or Serial.println() to send data. In Python, use the serial.Serial(port, baud_rate) to open the port and readline() or read() to receive data.
- Arduino:
Serial.begin(9600);starts communication at 9600 bits per second. - Arduino:
Serial.println(data);sends data with a newline. - Python:
serial.Serial('COM3', 9600)opens the port (replace 'COM3' with your port). - Python:
ser.readline()reads a line of data sent from Arduino.
arduino
void setup() { Serial.begin(9600); // Start serial communication at 9600 baud } void loop() { int sensorValue = analogRead(A0); // Read sensor Serial.println(sensorValue); // Send value to serial delay(1000); // Wait 1 second }
Example
This example shows Arduino sending analog sensor values every second and Python reading and printing those values.
arduino and python
// Arduino code void setup() { Serial.begin(9600); } void loop() { int sensorValue = analogRead(A0); Serial.println(sensorValue); delay(1000); } # Python code import serial import time ser = serial.Serial('COM3', 9600) # Replace 'COM3' with your Arduino port while True: if ser.in_waiting > 0: line = ser.readline().decode('utf-8').rstrip() print(f"Received from Arduino: {line}") time.sleep(0.1)
Output
Received from Arduino: 523
Received from Arduino: 520
Received from Arduino: 518
...
Common Pitfalls
- Wrong port: Using the wrong serial port name in Python causes connection failure.
- Baud rate mismatch: Arduino and Python must use the same baud rate.
- Not waiting for data: Reading before Arduino sends data can cause empty reads.
- Encoding errors: Always decode bytes to string in Python using
decode('utf-8'). - Serial buffer overflow: Reading data too slowly can overflow Arduino's serial buffer.
arduino and python
/* Wrong way: baud rates differ */ void setup() { Serial.begin(115200); // Arduino at 115200 } // Python opens at 9600 - mismatch causes no data // Correct way: match baud rates void setup() { Serial.begin(9600); } // Python uses 9600 too
Quick Reference
| Step | Arduino Code | Python Code |
|---|---|---|
| Start Serial | Serial.begin(9600); | ser = serial.Serial('COM3', 9600) |
| Send Data | Serial.println(data); | line = ser.readline().decode('utf-8').rstrip() |
| Read Data | N/A | if ser.in_waiting > 0: |
| Close Port | N/A | ser.close() |
Key Takeaways
Use matching baud rates on Arduino and Python for serial communication.
Send data from Arduino with Serial.print or Serial.println.
Read and decode serial data in Python using pyserial's readline and decode methods.
Ensure you use the correct serial port name in Python.
Add delays in Arduino to avoid flooding the serial buffer.