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

Measuring light and turning continuous rotation servos

I decided to use light measuring sensor and continuous rotation servo to turn servo based on the light ammount. I have released full code for free use.

Basic idea

Measure ammount of light and divide it with 5 to get speed for servo and then turn servo. This could be used to to automatically lower curtains when night comes maybe

//Petri Mäki
// 19.4.2013
// http://www.petrimaki.wordpress.com

#include <Servo.h>
Servo myServo;

float lightValue;
int lightPin = 1;
int servoPin = 2;
int delayTime = 1000; // ms

void setup(){
Serial.begin(9600);
myServo.attach(servoPin);
}

void loop()
{

lightValue = analogRead(lightPin);
Serial.print(”Light value: ”);
Serial.println(lightValue); //Print light value, not sure what the scales are. 1000 seems to be realy dark and close 0 means there is much light

/* Turn light value to speed. I decide to divide light value
with 5 since i think it gives good accurate. e.g 1000 / 5 = 200
where 1000 would be realy dark light value and 200 is result
for speed*/
int newSpeed = lightValue / 5;
Serial.print(”Speed: ”);
Serial.println(newSpeed);
if(newSpeed > 90) {
if (newSpeed > 180) { // Speed can only be 180 MAX
newSpeed = 180;
}

myServo.write(newSpeed);

} else {
if (newSpeed < 0) {
newSpeed = 0; //Speed cannot be -1
}
myServo.write(newSpeed);
}
delay(delayTime); //ms

}

// Continuous rotation servos
// Controlling continuous rotation servos with servo.h
// 90 = servo wont move
// 0 = Full speed to left
// 180 = Full speed to right

void servoRight(int rotatingSpeed) { // Turn right based on value
myServo.write(rotatingSpeed);
}

void servoLeft(int rotatingSpeed) { //Turn left based on value
myServo.write(rotatingSpeed);
}

What i used:

  • Arduino UNO
  • Breadboard
  • Jumper Wires
  • Light sensor (Unknown model)
  • SpringRC SM-S4303R Continuous Rotation Servo

Source:

This page is licensed under a GNU General Public License v2

Arduino with blinking lights

Simple blinking light code with Arduino Uno. See code at the end.

Basic idea:

Light on the left side(alphaLed) blink first and after this light next to it starts and will be on until end of the loop. Then alpaLed blinks again and new light starts. This will go over and over until all lights are up. Then all light will be shutdown and loop starts over.

Working code

int alphaLed = 13;
int led1 = 12;
int led2 = 11;
int led3 = 10;
int led4 = 9;
int led5 = 8;

void setup() {
// setup leds
// alphaLed should be the one most left and its the one that blinks in every turn.
// See the video.

pinMode(alphaLed, OUTPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
}

void loop() {

// Blinking leds. AlphaLed is the first led in line that blinks. AlphaLed blink every time when new led lights up.

digitalWrite(alphaLed, HIGH);
delay(1000);
digitalWrite(alphaLed, LOW);
delay(1000);

// leave led1 on High mode
digitalWrite(led1, HIGH);
delay(1000);

digitalWrite(alphaLed, HIGH);
delay(1000);
digitalWrite(alphaLed, LOW);
delay(1000);

// leave led2 on High mode
digitalWrite(led2, HIGH);
delay(1000);

digitalWrite(alphaLed, HIGH);
delay(1000);
digitalWrite(alphaLed, LOW);
delay(1000);

// leave led3 on High mode
digitalWrite(led3, HIGH);
delay(1000);

digitalWrite(alphaLed, HIGH);
delay(1000);
digitalWrite(alphaLed, LOW);
delay(1000);

// leave led4 on High mode
digitalWrite(led4, HIGH);
delay(1000);

digitalWrite(alphaLed, HIGH);
delay(1000);
digitalWrite(alphaLed, LOW);
delay(1000);

// leave led5 on High mode
digitalWrite(led5, HIGH);
delay(1000);

// Turn off all leds and start all over

digitalWrite(alphaLed, LOW);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
delay(2000);

}

What i used:

  • Arduino
  • 6 x Leds
  • Breadboard
  • Jumper Wires

This page is licensed under a GNU General Public License v2