Skip to content

Instantly share code, notes, and snippets.

@oampo
Created December 4, 2013 10:09
Show Gist options
  • Save oampo/7785192 to your computer and use it in GitHub Desktop.
Save oampo/7785192 to your computer and use it in GitHub Desktop.
Python->Arduino communication example
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
}
void loop() {
String serialString;
while (Serial.available()) {
serialString += Serial.read();
}
if (serialString == "119") {
digitalWrite(13, 1);
}
else if (serialString == "115") {
digitalWrite(13, 0);
}
}
import serial
print('Starting USB')
# Change /dev/ttyACM0 for COMX if on Windows
s = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
while (1):
# Change raw_input for input if using Python 3
key=raw_input('Enter key:\n').strip()
if key=='w':
s.write("w".encode("ascii"))
if key=='s':
s.write("s".encode("ascii"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment