This project demonstrates a synchronous communication handshake between a computer (running Python) and an Arduino microcontroller. The Python script sends a command, and the Arduino processes it and returns a response.
synchronous.py: The Python script that acts as the "Master". It sends input commands and waits for a reply.synchronous.ino: The Arduino firmware that acts as the "Slave". It listens for commands and echoes a status back.
- Arduino Board (Uno, Nano, Mega, etc.)
- USB Cable
You need the pyserial library to access the USB ports.
pip install pyserial- Open
synchronous.inoin the Arduino IDE. - Connect your Arduino to the computer.
- Select your Board and Port in Tools > Board and Tools > Port.
- Upload the code.
- Important: Note the Port name (e.g.,
COM6or/dev/ttyUSB0) shown in the IDE.
- Open
synchronous.pyin your code editor. - Locate the configuration section at the top:
# Configuration ARDUINO_PORT = "COM6" # <-- Change this to your Port BAUD_RATE = 9600
- Update
ARDUINO_PORTto match the port you found in the Arduino IDE.
- Close the Arduino IDE Serial Monitor.
- Note: The serial port can only be used by one program at a time. If the IDE is connected, Python cannot connect.
- Run the Python script via terminal:
python synchronous.py
- Wait for the initialization message:
Connected to COM6 at 9600 baud. Waiting for Arduino to reset... Ready to send commands. - Type a command (e.g., "Hello") and press Enter.
| Error | Solution |
|---|---|
SerialException: Access is denied |
Another program (like Cura or Arduino Serial Monitor) is using the port. Close them. |
No response from Arduino |
Check if the BAUD_RATE in Python (9600) matches Serial.begin(9600) in the Arduino code. |
Garbage characters (?) |
Baud rate mismatch. Ensure both are set to 9600. |
This project is open-source. Feel free to modify it for your own needs.
Author: Rubens Braz.