PySerial and Arduino – communication with software

Digi-Key shipmentMy first order of stuff from DigiKey arrived today – yay!  Unfortunately it is so tightly packed inside the box that I am unable to dig in more here at work.  More on that later…

Yesterday i began exploring making the arduino communicate with the computer it is attached to.  Not having a breadboard left me with few options for what I could connect to the board, so I decided to work more on the software end of things.

I chose to work in Python at this time because I have been meaning to give it a try, and it is probably going to be pretty easy to web-enable python stuff using django.  Plus I am a little bit wary of using Processing for various reasons.

So after choosing Python for the computer side of things, I had a look at the arduino documentation for computer interfacing with python.  I started with trying out Firmata, which looked quite nice – a standardized way of communicating with the arduino from any language!  After getting python firmata installed on my computer I went looking for documentation, but unfortunately they only had a couple pieces of example code, which I tried to use and did not get working.  I think I need to dig around a bit more in the core firmata documentation to figure out what to do next.

So the next step was to try straight up serial com with pySerial.  My first goal was to get a light turning on and off at sending 1 or 0 over to the duino from the Arduino IDE itself.  This worked just fine after I realized that the serial interface is working with bytes, meaning ascii characters.  So in other words, 1 = 48, etc etc.  

My next step was to make the arduino respond differently to different keystrokes – my plan was simply to make the LED on board flash a number of times according to a number sent from the keyboard.  IE – send 1, light flashes 1 time.  Here is a copy of my arduino sketch used to work with this:

#define LED 13
#define BUTTON 7
int state = LOW;
int switching = -1;
void setup(){
pinMode(LED, OUTPUT);
Serial.begin(14400);
}
void loop(){
if (Serial.available() > 0) {
switching = int( Serial.read());
if( switching > -1){
Serial.println("Blinking");
while(switching > -1){
digitalWrite(LED, HIGH);
delay(50);
digitalWrite(LED, LOW);
delay(50);
switching = switching -1;
}
}
}else{
Serial.println("nothing to report");
}
delay(500);
}

I then installed pyserial on my mac and loaded IDLE to try it out. As per the pySerial Documentation, I issued the following commands in IDLE:

ser = serial.Serial('/dev/tty.usbserial-A9007KDb', 14400)
ser.readline()

which resulted in failure. I was getting a “port not open” error from python/pyserial. I checked again with my Arduino IDE and found that this was working fine. After a bit more troubleshooting, I decided to switch the baudrate of the serial connection to 9600 instead of 14400, which was what was in the arduino serial tutorial to begin with.

##New Arduino Sketch Code:
Serial.begin(9600);
##New Python Code:
ser = serial.Serial('/dev/tty.usbserial-A9007KDb', 9600)

And this worked!  After this I set about making the two talk consistently, which is harder than I anticipated.  When in python I call serial.readline() for example, it should echo out “Nothing to report” most of the time, where instead I am getting something like “Notng to report”.  I do not yet know what it is doing, but hopefully I will figure it out.  I am also having a problem with the Arduino reading incoming things.  Python’s serial.write() function will only accept text strings, not integers, and then the arduino interperts these as ascii characters.  This is all fine and good for a lot of things, and indeed welcome, but for now I just want to make 1 be… 1, not 49.  We will see what I can figure out.

Tags: , ,

2 Responses to “PySerial and Arduino – communication with software”

  1. stonechild April 6, 2009 at 9:59 pm #

    Well, I see you have gotten some goodies in the mail and have even done something with your Arduino. I have gotten most of the stuff I need for the first go round, but have done nothing except turn the Arduino on. It seems to work.

    Thanks for the idea of doing something very basic. It didn’t occur to me to just send some data to it and see if it comes back. I’m going to use C or C++ for now. Would prefer to use Java..

    I have a bunch of 74HC595s coming from Thailand. I plan to use them with the 8×8 LED matrix if anyone can tell me what pins do.

    ==

  2. jafoca April 6, 2009 at 10:08 pm #

    Hey there!

    If you want to use Java then by all means do! My background is actually in java, but recently I have been doing a lot of work in Ruby, so I am compromising between the two and going with Python for my arduino work! Hehe!

    Anyway, it looks like there is a LOT of info on using java with the arduino:
    Java interfacing info here
    And the general interfacing page (for all languages)

Leave a Reply