LED CONTROLLING USING ARDUINO AND PYTHON

Photo of author

By Jackson Taylor


PYTHON CODE
import serial
“””download library file from
https://pypi.python.org/pypi/pyserial “””
ser = serial.Serial(port=’com2′,baudrate=9600)
                            #enter comport name,baud rate
ser.close()              #closes previously open serial port
ser.open()                   # opens serial port
while True:
    x=raw_input(“ENTER LED STATE ? ON OR OFF n”)
    if x== ‘on’ or x==’ON’:
        ser.write(‘1′)
    elif x==’off’ or x==’OFF’:
        ser.write(‘0’)
#enter ctrl+c for closing this infinite loop
ARDUINO CODE
char data;
void setup()
{
pinMode(13, OUTPUT);
Serial.begin(9600);
delay(100);
}
void loop()
{
 if(Serial.available()>0)
{
  data=Serial.read();
  if(data==’1′)   digitalWrite(13, HIGH);
  else if (data==’0′) digitalWrite(13, LOW);
 }          
}