arduino.py 466 B

1234567891011121314151617
  1. import serial
  2. import time
  3. def connect_to_arduino(serial_port, baud_rate):
  4. try:
  5. ser = serial.Serial(serial_port, baud_rate)
  6. print(f"Connected to Arduino on {serial_port} at {baud_rate} baud.")
  7. time.sleep(2) # Wait for the serial connection to establish
  8. return ser
  9. except serial.SerialException as e:
  10. print(f"Failed to connect to Arduino: {e}")
  11. return None
  12. def send_toggle_command(ser):
  13. ser.write(b't')