Python talks to the Arduino…
Sort of a followup to my previous post ‘Arduino talks to Processing, Python…‘, I thought I’d try going the other way: Send commands to the Arduino from Python over the serial port.
My son and I recently soldered together a lolshield (‘lots of LEDs’) for the Arduino. The ultimate goal for this is to have Python, via Pygame (most likely) run graphics on the lolshield as if it was a mini-flatscreen. But first I need to get something sent from Python to Arduino, and this was surprisingly hard.
First, the internets came to my aid and I got some sample code allowing the Arduino to read a single character from the serial port, and blink an LED that number of times. The source was by Tod E. Kurt, and I got it off his page here. My modified version is below (modified mainly with docs so I could understand what’s going on…)
// Arduino Code: // Below, 'character' types are defined: They hold 1 byte of data, 256 values. // A char can be interpreted as a small number (0-255) or as a member of the // ASCII set (which is what we deal with below). Characters expressed as // ASCII are surrounded in single-quotes, like '5'. // Thus each char has a corresponding numeric value can thus be tested against. int ledPin = 13; // select the pin for the LED int val = 0; // variable to store the data from the serial port void setup() { pinMode(ledPin,OUTPUT); // declare the LED's pin as output Serial.begin(9600); // connect to the serial port } void loop () { if (Serial.available()) { // For the below examples, let's pretend that the passed-in serial // value is character '5'. // Since the declared variable val is an int, it converts the char // value passed in into an int. // If char val = '5', the numeric representation is 53. val = Serial.read(); // read the serial port // If the stored value is a single-digit number, blink the LED // that number of times. // Here we compare the int value of val against the int values // of the string. // Characters '0' and '9' are equivalent to integer 48 and 57. if (val > '0' && val <= '9' ) { Serial.println(val); // Convert from char to int: // From above, int conversion of val, which is char '5', is 53. // int conversion of char '0' is 48. // 53-48 = 5 : blink that # of times val = val - '0'; for(int i=0; i<val; i++) { Serial.println("blink!"); digitalWrite(ledPin,HIGH); delay(150); digitalWrite(ledPin, LOW); delay(150); } } } }
Now for the Python stuff. On the ‘Arduino: Playground‘ site, I found this example Python code using the pySerial libary:
>>> import serial # if you have not already done so >>> ser = serial.Serial('/dev/tty.usbserial', 9600) >>> ser.write('5')
And it works, presuming you’re in the interactive Python shell. If however you try to make a module out of the same code and execute it, it will fail: No multi-blinking LED. After researching several posts of other people that had similar issue (here, here, here, and here) I tracked down the main culprit: When you make a connection to the serial port via Python, it sends a reset command to the Arduino. If you immediately send your data to the port immediately after, the Arduino won’t be initialized and ready to receive it. To solve this, we simply pause the module for a bit while the Arduino does its thing:
# simpleSerialSend.py import sys import serial import time PORT = 'COM4' # The port my Arduino is on, on my WinXP box. def main(val=5): # Open a connection to the serial port. This will reset the Arduino, and # make the LED flash once: ser = serial.Serial(PORT) # Must given Arduino time to rest. # Any time less than this does not seem to work... time.sleep(1.5) # Now we can start sending data to it: written = ser.write(val) ser.close() print "Bytes Written to port:", written print "Value written to port: '%s'"%val if __name__ == '__main__': args = sys.argv try: main(args[1]) except IndexError: main()
This module can now be ran from the command prompt, and you can optionally pass in (from 0->9) the number of times you want the LED to blink:
c:\pyModules\simpleSerialSend.py 5
When this runs, you’ll see the LED blink once when the reset happens, a 1.5 second pause, and then the LED will blink 5 times.
What’s interesting is I tried this same thing with Processing, and failed, and I presume for the same reason: The Processing sketch was executing too fast to allow the Arduino time to warm up. And, there doesn’t seem to be a ‘pauses \ sleep \ wait’ function in Processing. I tried using java.lang.Thread, but it didn’t seem to pause the sketch until it was done running. So I need to do some more research there. Had I got that working, then the title of this blog post would probably be different 😉