arahan2108coder
Published

Link Python to Arduino UNO

You need Arduino UNO and Python 3. Even you can use Python 3.10 (Beta). It is a quite simple project.

BeginnerFull instructions provided646
Link Python to Arduino UNO

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1

Software apps and online services

Python 3
You can use any version of Python even 3.10. (Beta)
Arduino IDE
Arduino IDE

Story

Read more

Code

Arduino

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

Python
import serial
import time

arduino=serial.Serial('COM1', 9600)
time.sleep(2)

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

while 1:
    
    datafromUser=input()

    if datafromUser == '1':
        arduino.write(b'1')
        print("LED  turned ON")
    elif datafromUser == '0':
        arduino.write(b'0')
        print("LED turned OFF")

Credits

arahan2108coder
2 projects • 0 followers
Contact

Comments

Please log in or sign up to comment.