Monday, May 9, 2011

Arduino Python Blink

Have been working on Arduino for sometime with reference to automating some aspects of my work.
Have been tinkering with Python just for the sake of it.
So now I want to talk to my Arduino using Python.
For the starters this is what I did.

Loaded this code on my Duemilanove.

void setup(){
Serial.begin(9600);
}

void loop(){
char i=Serial.read();
if (i=='Y'){
digitalWrite(13, HIGH);
} else if(i=='N'){
digitalWrite(13, LOW);
}
}

Dowloaded and installed pyserial.
On a mac, the pyserial tar file downloads to Download folder and unzips to the same folder.
From the terminal: cd /Downloads/pyserial-2.5
From the the terminal: sudo python setup.py install
Lo and behold pyserial is installed.
On terminal: python
That takes us to >>> python prompt.

At the python prompt, either the following can be entered and a py file can be created and run straight from the terminal user prompt.

import serial
arduino=serial.Serial('/dev/tty.usbserial-A9007RxF', 9600)
arduino.write('Y') //This should power the 13
arduino.write('N') //This should power down 13


So simple.
And now to bigger projects... PyRobots?

No comments: