Skip to content

Instantly share code, notes, and snippets.

@jreisstudio
Created January 11, 2013 01:28
  • Star 8 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jreisstudio/4507236 to your computer and use it in GitHub Desktop.
Python + Arduino on/off the LED.
#define LED 13
// Using http://slides.justen.eng.br/python-e-arduino as refference
void setup() {
pinMode(LED, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
char serialListener = Serial.read();
if (serialListener == 'H') {
digitalWrite(LED, HIGH);
}
else if (serialListener == 'L') {
digitalWrite(LED, LOW);
}
}
}
import serial # you need to install the pySerial :pyserial.sourceforge.net
import time
# your Serial port should be different!
arduino = serial.Serial('/dev/tty.usbmodem1411', 9600)
def onOffFunction():
command = raw_input("Type something..: (on/ off / bye )");
if command =="on":
print "The LED is on..."
time.sleep(1)
arduino.write('H')
onOffFunction()
elif command =="off":
print "The LED is off..."
time.sleep(1)
arduino.write('L')
onOffFunction()
elif command =="bye":
print "See You!..."
time.sleep(1)
arduino.close()
else:
print "Sorry..type another thing..!"
onOffFunction()
time.sleep(2) #waiting the initialization...
onOffFunction()
@BenHamm
Copy link

BenHamm commented Feb 18, 2019

Thanks so much! This git got me unstuck in my project.

@gatopadre
Copy link

It leave me this error:
Captura de pantalla de 2019-03-11 00-32-49

any idea?

@gatopadre
Copy link

It leave me this error:
Captura de pantalla de 2019-03-11 00-32-49

any idea?

It was a problem with the identation in the code, sorry.

@remitoudic
Copy link

Hey ,
Thank you for the code
if you use python 3 at least you will get also a problem with
arduino.write('L')
change it to
arduino.write(b'YourStringText ')

@chaima-elm
Copy link

THANK YOU !

@maffin743
Copy link

как запустить его?!??!?!?!
откуда его запускать

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment