Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

BenHamm commented Feb 18, 2019

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

@gatopadre

This comment has been minimized.

Copy link

gatopadre commented Mar 11, 2019

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

any idea?

@gatopadre

This comment has been minimized.

Copy link

gatopadre commented Mar 11, 2019

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

This comment has been minimized.

Copy link

remitoudic commented Dec 6, 2019

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 ')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.