Python_Geek
Published © GPL3+

Python + Arduino + Mu = projects better than ever before!

Adding Python to Arduino projects, plus an editor with pyserial pre-installed, equals amazing projects!

BeginnerProtip5 minutes9,513
Python + Arduino + Mu = projects better than ever before!

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
Or another Arduino- compatible device.
×1

Software apps and online services

Mu
Arduino IDE
Arduino IDE

Story

Read more

Code

Arduino code

Arduino
int datafromUser=0;
void setup() {
  // put your setup code here, to run once:
  pinMode( LED_BUILTIN , OUTPUT );
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(Serial.available() > 0)
  {
    datafromUser=Serial.read();
  }

  if(datafromUser == '1')
  {
    digitalWrite( LED_BUILTIN , HIGH );
  }
  else if(datafromUser == '0')
  {
    digitalWrite( LED_BUILTIN, LOW);
  }
  
}

Python code

Python
import serial
import time

arduino=serial.Serial('COM12', 9600)
time.sleep(2)                       #sleep 2 secs

print("Enter 1 to turn ON LED and 0 to turn OFF LED")       #print some output

while 1:                            #loop forever

    datafromUser=input()

    if datafromUser == '1':         #datafromUser equals 1?
        arduino.write(b'1')         #send '1' over serial port
        print("LED  turned ON")     #print some output

    elif datafromUser == '0':       #datafromUser equals 0?
        arduino.write(b'0')         #send '0' over serial port
        print("LED turned OFF")     #print some output

Credits

Python_Geek

Python_Geek

3 projects • 7 followers

Comments