import serial
import time
# Set the serial port and baud rate (Make sure the port matches your system's Arduino port)
# For Windows, it might look like 'COM3', 'COM4', etc.
# For Linux/macOS, it will likely be something like '/dev/ttyACM0' or '/dev/ttyUSB0'
arduino_port = '/dev/ttyACM0' # Change this to your Arduino's serial port
baud_rate = 9600
# Initialize serial connection
ser = serial.Serial(arduino_port, baud_rate, timeout=1)
time.sleep(2) # Give Arduino some time to reset
# Data to send to Arduino
data_to_send = 65 # ASCII value for the letter 'A'
# Send data to Arduino
print(f"Sending data: {data_to_send}")
ser.write(bytes([data_to_send])) # Send a single byte
# Wait for a response from the Arduino
response = ser.read() # Read the response from Arduino (1 byte)
# Print the response received from Arduino
print(f"Received data from Arduino: {response}")
# Close the serial connection
ser.close()