Skip to content

Instantly share code, notes, and snippets.

@brilliant-ember
Last active December 10, 2020 16:40
Show Gist options
  • Save brilliant-ember/8186773f15c96f8a0076387f846b49c6 to your computer and use it in GitHub Desktop.
Save brilliant-ember/8186773f15c96f8a0076387f846b49c6 to your computer and use it in GitHub Desktop.
Python and Arduino Serial communication
import serial
from time import sleep
COM = '/dev/ttyUSB0' #This is for linuxm Will be COM# for windows copmuters so check ur usb port names
BAUD = 9600
ser = serial.Serial(COM, BAUD, timeout = .1)
print('Waiting for device');
sleep(1)
print(ser.name)
while True:
try:
val = str(ser.readline().decode().strip('\r\n'))
print(val, end="\n")
except(UnicodeDecodeError):
pass
except:
print(" \n closing")
ser.close()
exit()
# arduio file
void setup() {
// initialize serial communication at 9600 bits per second
Serial.begin(9600);
}
void loop() {
Serial.println(10);
delay(1); // delay between serial prints for stability
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment