Python can read data from an Arduino board through the serial port that connects them

When your Arduino sends data — sensor readings, button presses, temperature values — Python can capture that information on your computer. The connection happens through a USB cable, which creates a serial port that both devices can write to and read from. Python uses a library called PySerial to open that port and pull in whatever your Arduino is sending.

The process has three parts: your Arduino must send the data, Python must open the right serial port, and Python must read and store what arrives. Most of the confusion happens because you need to know which port your Arduino is using and what speed (baud rate) both devices are set to. Once those match, the data flows.

Key Takeaways

  • Your Arduino connects to your computer through a USB port that appears as a serial device — on Windows it looks like COM3 or COM4, on Mac and Linux it looks like /dev/ttyUSB0 or /dev/ttyACM0.
  • You must install PySerial on your computer before Python can read from the Arduino: run pip install pyserial in your terminal or command prompt.
  • Your Arduino code and your Python code must use the same baud rate (usually 9600) or the data will arrive as garbage characters.
  • The Arduino sends data as bytes, so Python receives it as text — you will need to decode it and strip away line breaks before you can use the numbers.
  • Test your connection with a simple script that just prints what arrives before you try to process or store the data.

Install PySerial and find your Arduino's port

Open your terminal (Mac and Linux) or command prompt (Windows) and type pip install pyserial. This downloads and installs the library that lets Python talk to serial devices. Wait for the installation to finish — you will see a message saying it succeeded.

Next, find out which port your Arduino is using. Plug the Arduino into your computer with the USB cable. On Windows, open Device Manager (right-click the Start button and select it), then look under Ports. You will see something like COM3 or COM5 — that is your port. On Mac, open Terminal and type ls /dev/tty.* — look for something like /dev/tty.usbserial or /dev/tty.usbmodem. On Linux, type ls /dev/tty* and look for /dev/ttyUSB0 or /dev/ttyACM0. Write down the exact name.

Check your Arduino's baud rate and upload a test sketch

Open the Arduino IDE and look at your sketch — find the line that says Serial.begin() with a number inside the parentheses. That number is your baud rate. The most common is 9600. If you do not see this line, add it inside the setup() function: Serial.begin(9600);

Then add a line to send data. The simplest test is to send a number every second. Inside your loop() function, type:

Serial.println(analogRead(A0)); delay(1000);

This reads the value from analog pin A0 and sends it to the serial port once per second. If you do not have a sensor connected, the value will just be noise, but that is fine for testing. Upload this sketch to your Arduino.

Write a Python script to read the serial data

Create a new Python file (or open your text editor and save a blank file as read_arduino.py). Type this code:

import serial import time ser = serial.Serial('COM3', 9600, timeout=1) time.sleep(2) while True:     if ser.in_waiting > 0:         line = ser.readline().decode('utf-8').rstrip()         print(line)

Replace 'COM3' with the port you found earlier (on Mac or Linux, use the full path like '/dev/ttyUSB0'). Replace 9600 if your Arduino uses a different baud rate. The timeout=1 tells Python to wait up to 1 second for data before moving on. The time.sleep(2) waits 2 seconds after opening the port — this gives the Arduino time to reset when the connection opens.

The while True loop runs forever. Each time data arrives, ser.readline() grabs one line, .decode('utf-8') converts it from bytes to text, and .rstrip() removes the line break at the end. Then print(line) shows it on your screen.

Run the script and watch for data

Save your Python file. Open your terminal or command prompt, navigate to the folder where you saved it, and type python read_arduino.py (or python3 read_arduino.py on Mac and Linux). You should see numbers appearing on your screen — one per second if you used the test sketch above.

If you see nothing, the most common problems are: the port name is wrong, the baud rate does not match, or the Arduino sketch is not actually running. Check Device Manager or the terminal again to confirm the port. Look at your Arduino sketch and make sure Serial.begin() has the same number as your Python script. If the Arduino IDE shows an error when you uploaded, the sketch did not load.

If you see garbage characters instead of numbers, your baud rates do not match. Change the 9600 in your Python script to 115200 or whatever your Arduino sketch uses, save, and run again.

Store the data in a file or variable

Once you see clean data arriving, you can save it instead of just printing it. Add these lines to your Python script:

with open('arduino_data.txt', 'a') as f:     f.write(line + '\n')

Put these lines right after the print(line) statement, with the same indentation. Now every reading gets written to a file called arduino_data.txt in the same folder. The 'a' means append — it adds to the file instead of overwriting it.

If you want to convert the text to a number so you can do math with it, add this line before you print or save:

value = int(line)

Now value is a number, not text. If your Arduino sends multiple values separated by commas (like 23,45,67), split them first:

values = line.split(',') temp = int(values[0]) humidity = int(values[1])

Stop the script and close the port cleanly

Press Ctrl+C in your terminal to stop the script. Python will exit the loop and close the serial port automatically. If you want to close it in your code instead, add this at the end:

ser.close()

This is useful if you want your script to read for a set amount of time and then stop. For example, you could replace while True with while time.time() - start_time < 60 to read for 60 seconds, then the script would reach ser.close() and exit.

Frequently Asked Questions

What if Python says "port does not exist" or "permission denied"?

On Windows, make sure the port name matches exactly what Device Manager shows — it is case-sensitive. On Mac and Linux, you may need to run the script with sudo (type sudo python3 read_arduino.py). If permission is still denied, your user account may not have access to the serial port. On Linux, add your user to the dialout group by typing sudo usermod -a -G dialout $USER, then log out and back in.

Can I read from multiple Arduinos at once?

Yes. Create a separate serial object for each port: ser1 = serial.Serial('COM3', 9600) and ser2 = serial.Serial('COM4', 9600). Then check both in your loop: if ser1.in_waiting > 0 and if ser2.in_waiting > 0. You can read from both in the same script.

Why does my data have weird characters or seem corrupted?

The baud rate is almost always the problem. Check your Arduino sketch for Serial.begin() and make sure the number matches exactly in your Python script. Also check that your USB cable is a data cable, not a charge-only cable — some cables only carry power and will not transmit data.

How do I send data from Python back to the Arduino?

Use ser.write(b'your message'). The b before the quotes tells Python to send it as bytes. Your Arduino can read it with Serial.read() or Serial.readStringUntil('\n'). Make sure your Arduino sketch has code listening for incoming data.

What if the script reads old data from the buffer?

Add ser.reset_input_buffer() right after you open the port. This clears out any data that arrived before your script started reading. This is useful if your Arduino has been sending data while Python was not listening.