Reading Arduino serial ports in Windows 7 with Python + Pyserial

Here i am going to show you 3 working examples on how to read Arduino serial ports with windows. You are going to need following programs / tools:

Installing Python + Pyserial to Windows

First you have ton install Python on your computer. This is just typical install with no big deal. When you have installed Python you may want to restart your computer. If you are not sure if your install was succesfull you can open your command prompt and type python or go to location where you installed python and run Python.exe. If everything works correctly your command prompt should look something like this:

python command prompt

After installing Python you can download Pyserial. Unzip pyserial to folder where you installed python and says lib. For me example to location was:

c:\Python27\Lib\pyserial-2.6\

After you have unzipped pyserial, you should start command prompt and go to location above. In there you can find setup.py named file, which you cn run by typing:

python setup.py install

Now you have installed Python + Pyserial

Reading  Arduino serial ports with Pyserial

First code is basic ”Hello world” with arduino and python
Code for Arduino:

Hello world with serial ports

Arduino code:

void setup(){
// Open serial connection.
Serial.begin(9600);

}

void loop(){
Serial.print("Hello world");
delay(10); // ms

}

Next code for python: (Note that WordPress might break Pythons code blocks)

import serial
import time
ser = serial.Serial('COM3', 9600, timeout=0)

while 1:
 try:
  print ser.readline()
  time.sleep(1)
 except ser.SerialTimeoutException:
  print('Data could not be read')
  time.sleep(1)

Arduino read data from Python

Arduino read data when user type something and prints it out byte by byte.

Arduino code:


int incomingByte = 0;

void setup(){
// Open serial connection.
Serial.begin(9600);

}

void loop(){
if (Serial.available() > 0) {
 // read the incoming byte:
 incomingByte = Serial.read();

 // say what you got:
 Serial.print("I got: "); // ASCII printable characters
 Serial.println(incomingByte, DEC);
}

}

Python code:

import serial
import time
ser = serial.Serial('COM3', 9600, timeout=0)
var = raw_input("Enter something: ")
ser.write(var)
while 1:
	try:
		print ser.readline()
		time.sleep(1)
	except ser.SerialTimeoutException:
		print('Data could not be read')

Arduino control led

Controlling 1 led light with arduino and pyserial by typing 0 or 1 to turn led on and off

Code for arduino:

int incomingByte = 0;
int ledPin = 13;

void setup(){
pinMode(ledPin, OUTPUT);
}

void loop(){

if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();

// say what you got:
if(incomingByte == 49) { // ASCII printable characters: 49 means number 1
 digitalWrite(ledPin, HIGH);
} else if(incomingByte == 48) { // ASCII printable characters: 48 means number 0
 digitalWrite(ledPin, LOW);
}
}

}

Code for python:

import serial
ser = serial.Serial('COM3', 9600, timeout=0)
while 1:
	var = raw_input("Enter 0 or 1 to control led: ")
	ser.write(var)

Reading data from sensor and printing that to computer

In this code we are going to use unknown light sensor for arduino. You can basically use any type of sensor you want since the principle is the same.

Code for arduino:


int lightPin = 1;
float lightValue;

void setup(){
  pinMode(ledPin, OUTPUT);
}

void loop(){
  lightValue = analogRead(lightPin);
  Serial.println(lightValue);
  delay(1000);
  }

Code for python:

import serial
import time
ser = serial.Serial('COM3', 9600, timeout=0)
while 1:
	try:
		print ser.readline()
		time.sleep(1)
	except ser.SerialTimeoutException:
		print('Data could not be read')
	time.sleep(1)

Sources:

This page is licensed under a GNU General Public License v2