Posts Tagged ‘Python’

I’m not going to be providing any ground breaking knowledge in this post as I’m not the first or even the 10,000th person to use a Python script to control an Arduino. My aim is simply to save you a little time if you are trying to do this for the first time.

The first order of business is to set up your Arduino board and take a few notes.

Here’s a cheesy sketch that toggles a few outputs depending on the character it sees on the Arduino’s RX pin. After you get this to work check out the “Firmata” library.

int firstOutput = 9;
int secondOutput = 10;
int inByte = 0;
void setup()
{
Serial.begin(9600);
pinMode(firstOutput, OUTPUT);
pinMode(secondOutput, OUTPUT);
digitalWrite (firstOutput, LOW);
digitalWrite (secondOutput, LOW);
}
void loop()
{
if (Serial.available() > 0) {
inByte = Serial.read();
if (inByte = 1) {
digitalWrite(firstOutput, HIGH);
digitalWrite(secondOutput, LOW);}
if (inByte = 2) {
digitalWrite(secondOutput, HIGH);
digitalWrite(firstOutput, LOW);}
}
}

After you download the sketch connect an LED and current limiting resistor in series from pin 9 to GND and from pin 10 to GND.

Click on the “Serial Monitor” button in your Arduino IDE. Set the baud to 9600. Make note of the serial port that’s being used (top of screen). Mine says “/dev/ttyUSB0”. You’ll need this info later.

Type “1” in the input line if the serial terminal (no quotes) and hit the “Send” button. One of your LEDs should turn on. Sending a “2” should toggle the LEDs. If this works move on. If not regroup and check your wiring.

Once you are able to toggle the LEDs it’s time to move on to the Python side of the equation. I’m not going to cover the install of Python or the pySerial plug in. This is covered elsewhere on line. Here’s a good place to start for the pySerial install: http://www.psychicorigami.com/2010/06/26/chumby-to-arduino-communication-using-pyserial/

Once you have Python and pySerial installed talking to Arduino is fairly straight forward. Open Python in your terminal. Just type “python” and hit Enter.

>>> import serial
>>> ser = serial.Serial(‘/dev/ttyUSB0’, 9600, timeout=1)
>>> ser.write(‘1’)
Hit Enter
>>> ser.write(‘2’)
Hit Enter

This should toggle the LEDs just like the Arduino Serial Monitor connection did.

What’s the next step? Let’s turn this into a simple script that toggles the outputs. Put the following in a text file and save it as “pythonArduino.py”. Note* your path to Python may be different…

#!/usr/local/bin/python

import serial
import time
ser = serial.Serial(‘/dev/ttyUSB0’, 9600, timeout=1)

ser.write(‘1’)
time.sleep(5)
ser.write(‘2’)
time.sleep(5)
ser.write(‘1’)
time.sleep(5)
ser.write(‘2’)

That should be it. In your terminal move into the directory that you just save the script in. Now just type “python pythonArduino.py” and something magical should happen to your LEDs. I’m crossing my fingers!

Here’s a little more info from Arduino.cc.

I just wanted to share a few tools that I use to connect to my LAMP server to check status and occasionally modify settings or files from the couch. I run Ubuntu Server 10.10 on the LAMP server and Ubuntu 10.10 on my other machines. I have to admit that I have a Windows XP Pro machine downstairs to run Processing and for the wife and kids to manage their ITunes, etc.

CONNECTING TO YOUR SERVER
ConnectBot – This is a secure shell (SSH) client for the Android platform. It works as advertised and can get you to your server’s command line from your Android device. In the Android Market search for “ConnectBot”.

TRANSFERRING FILES
AndFTP – This is a simple FTP client for the Android Platform. I haven’t had any real issues with it. If you need to get a few files from your tablet or phone to your server this app will get the job done. In the Android Market search for “AndFTP”.

DATA ENTRY
Hacker’s Keyboard – This is a pretty nice keyboard that will add CTRL key access to your Android device. In the Android Market search for “Hacker’s Keyboard”.
Full Keyboard – This keyboard adds a D-Pad and quite a few more keys to your arsenal. There are times when it comes in handy but for most applications the Hacker’s Keyboard in landscape mode will get the job done a little more efficiently. In the Android Market search for “Full Keyboard”.