Before we move on to the next project, let's take a breath and look back at what we've actually built.
We started with a dusty old RC car and a laptop. We ended up with a wireless robot controlled by a Python program and a keyboard. Not bad. But more importantly — do we understand why it works? Let's walk through it.
The Command Chain (One More Time)
Everything comes back to this:
👉 CEO (Python) → uses Cellphone (HC-06) → Manager (Arduino Nano) → Team Leader (L298N) → Workers (Motors)
The CEO doesn't talk to the workers directly. Orders flow down the chain, each layer doing exactly its job and nothing more. Simple, clean, and scalable — which is exactly why real robots work the same way.
Part 1: The Arduino Code — Teaching the Manager
The Arduino Nano needs to understand what the CEO is saying. That's what the uploaded C code does — it's essentially an employee handbook for the Manager.
1. Hardware Setup
The code creates a virtual Bluetooth port on pins 10 (RX) and 11 (TX) — this lets the Arduino talk to the HC-06 module without interfering with the USB upload port. Communication speed: 9600 baud (the standard handshake rate).
Six pins are defined for the L298N motor driver:
- Direction pins: IN1, IN2 (left motor) and IN3, IN4 (right motor)
- Speed pins: ENA (left motor) and ENB (right motor)
2. Startup (void setup)
On power-up, the Arduino activates Bluetooth and all six motor control pins. Both motors are set to speed 200 out of 255 — fast enough to move, controlled enough not to immediately crash into something.
3. The Movement Functions
These are the core commands. Each one sets the IN1–IN4 pins HIGH or LOW to control which direction each motor spins:
| Function | What it does |
|---|---|
moveForward() | Both motors spin forward |
moveBackward() | Both motors spin backward |
turnLeft() | Left motor backward, right motor forward — spins in place |
turnRight() | Left motor forward, right motor backward — spins in place |
stopCar() | All pins LOW — everything stops |
4. The Main Loop (void loop)
The Arduino sits in a permanent loop, constantly checking: "Has the CEO sent anything?"
If data arrives over Bluetooth, it reads the character and acts:
| Key received | Action |
|---|---|
w | Move forward |
s | Move backward |
a | Turn left |
d | Turn right |
x | Stop |
No character? Do nothing. Wait for the next one.
Part 2: The Python Code — The CEO's Voice
The Python script is what you actually interact with. It watches the keyboard, translates keypresses into single-character commands, and fires them over Bluetooth to the Arduino.
1. Setup
Three tools are imported:
serial— opens the Bluetooth COM port and sends datakeyboard— detects which key is being pressedtime— handles delays
ser = serial.Serial('COM6', 9600, timeout=1)This opens COM6 at 9600 baud — matching the Arduino exactly. time.sleep(2) gives the connection two seconds to stabilize before anything is sent.
2. The Control Loop
The program runs in an infinite while True loop, constantly checking the keyboard:
- W, A, S, or D pressed → send the corresponding letter to the Arduino, print the current direction on screen
- No key pressed → automatically send
x(stop) — so the robot doesn't keep rolling when you lift your finger
3. Exiting
Press ESC: the program sends x to stop the motors, waits 0.2 seconds, then exits the loop cleanly.
4. Error Handling
If anything goes wrong mid-run (lost connection, port error), the except block catches it, prints the error message, and exits gracefully instead of crashing silently. On the way out, ser.close() releases the COM port so it's free for next time.
The Full Roster
| Role | Who |
|---|---|
| CEO | Python script on your laptop |
| Cellphone | HC-06 Bluetooth module |
| Manager | Arduino Nano |
| Team Leader | L298N H-Bridge Motor Driver |
| Workers | 2 DC motors |
Every component has one job. Every job connects to the next. Pull any one piece out and the whole chain breaks — which is also exactly how you debug it when something goes wrong.
That's the overview. Next up: we give our robot eyes it can take on the road — turning a phone into a mobile camera stream.