Phase 1 was a scenic tour through the world of robotics — high mileage, low spending, maximum borrowing from things we already owned. Laptop, old phone, RC car from the garage, a few skipped Starbucks runs. Ten parts, under $40, and a surprisingly complete picture of how robots actually think.
But a tour is just a tour. At some point you have to move in.
Welcome to Phase 2.
One Truth We Keep Coming Back To
👉 Robots need senses and a brain. The senses are sensors. The brain is a program — specifically, our Python program, which for now lives on a laptop. We say "for now" because a robot that needs a laptop sitting next to it isn’t really autonomous. Eventually, that brain needs to move into the robot itself. But we’ll get there.
Based on everything the sensors collect — sight, sound, touch — the Python brain decides how to respond.
Back when we were just getting started in Phase 1:
Python gives orders → Motors execute
Simple enough. But motors need to spin both directions, speed up, slow down, stop instantly. That requires a middleman — a driver module — to handle the actual electrical work. So the real flow became:
Python gives orders → Driver module → Motors execute
And then we realized that Python and hardware don’t always speak the same language. Different components from different manufacturers don’t always play nicely together. We needed one more layer — a microcontroller — to translate and distribute commands to the right place:
CEO (Python) → Manager (Microcontroller) → Team Leader (Driver module) → Workers (Motors)
Now here’s an interesting twist: what if the Workers aren’t motors, but sensors? Sensors don’t execute commands — they report information back up the chain. Same structure, reversed direction, one less middleman needed:
Workers (Sensors) report → Manager (Microcontroller) → CEO (Python)
That’s exactly the setup we’re building today.
Phase 2 Shopping List
Before we dive in, here’s the full component list for everything ahead. Buy it all at once or piece by piece — either way works:
| Component | Notes | Est. Cost |
|---|---|---|
| C101 4WD kit | ~$20 | |
| Raspberry Pi 3B | ~$50 | |
| Pi Camera Module v2 | ~$13 | |
| L298N Motor Driver | Already have from Phase 1 | — |
| Arduino Nano | Already have from Phase 1 | — |
| HC-SR04 Ultrasonic Sensor | ~$4 | |
| Portable USB Power Bank (5V) | Power source for Pi | ~$15 |
| Li-ion battery pack 7.4V | Power source for motors | ~$12 |
For this part, the only new purchase is the HC-SR04 — about $4 each, or $6 for two on Amazon. Everything else we already own.
Alright. Let’s build something.
Wiring It Up
Step 1 — Seat the Arduino Nano on the breadboard. Make sure the two rows of pins straddle the center divider — one row on each side. If both rows land on the same side, the pins are electrically connected to each other, which is not what we want.
Step 2 — Connect the HC-SR04 to the Arduino Nano using four Male-to-Male jumper wires:
HC-SR04 VCC → Arduino 5V HC-SR04 TRIG → Arduino D5 HC-SR04 ECHO → Arduino D4 HC-SR04 GND → Arduino GND
Step 3 — Connect the Arduino Nano to your laptop via USB-Mini-B cable. The power LED on the Nano should light up.
That’s the hardware done. Four wires and a USB cable.
Upload the Arduino Code
If you’ve been through Phase 1, this process is familiar. Quick recap for completeness:
1. Software and Driver
Download and install the latest Arduino IDE from the official Arduino website. Most Arduino Nanos sold today use a CH340 USB chip — if your computer doesn’t recognize the board, search for "CH340 driver" and install it.
2. Configure the Board
Connect the Nano via USB, then in Arduino IDE:
- Tools → Board → Arduino AVR Boards → Arduino Nano
- Tools → Processor → ATmega328P (Old Bootloader) if your board is older; ATmega328P if newer
- Tools → Port → select the COM port that appeared when you plugged in the USB
3. Upload
Paste the code below, click Verify (✔), then Upload (→). Wait for "Done uploading."
const int trigPin = 5;
const int echoPin = 4;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// Send ultrasonic pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure how long the echo takes to return
long duration = pulseIn(echoPin, HIGH);
// Convert time to distance in cm
int distance = duration * 0.034 / 2;
// Send the reading to Python via Serial
Serial.println(distance);
delay(100); // Wait 100ms before next reading
}
This code fires an ultrasonic pulse, listens for the echo, converts the round-trip time into centimeters, and sends that number to Python over Serial — 10 times per second.
The Python Side
Install the serial library if you haven’t already:
pip install pyserial
Create a new file called Ultrasonic.py:
import serial
import time
# Change 'COM3' to your actual port (check Arduino IDE → Tools → Port)
# On Mac/Linux it looks like '/dev/ttyUSB0' or '/dev/cu.usbserial-...'
arduino_port = 'COM3'
baud_rate = 9600
try:
ser = serial.Serial(arduino_port, baud_rate, timeout=1)
time.sleep(2) # Wait for Arduino to reboot after connection
print(f"Connected to {arduino_port}")
except Exception as e:
print(f"Connection error: {e}")
exit()
try:
while True:
if ser.in_waiting > 0:
line = ser.readline().decode('utf-8').rstrip()
if line:
print(f"Distance: {line} cm")
except KeyboardInterrupt:
print("\nStopped.")
finally:
ser.close()
print("Connection closed.")
Hit Run. You’ll see "Connected to COM3" followed by a continuous stream of distance readings. Wave your hand in front of the HC-SR04 and watch the numbers change in real time.
Distance: 42 cm
Distance: 38 cm
Distance: 21 cm
Distance: 9 cm
Distance: 12 cm
The Manager (Arduino) is measuring, and reporting back to the CEO (Python). Exactly as planned.
How It Works
The HC-SR04 fires a burst of ultrasonic sound — too high-pitched for human ears — and listens for the echo. The math is straightforward:
distance = (travel time × speed of sound) ÷ 2
Divide by 2 because the sound makes a round trip: out to the object, back to the sensor. That’s it. No AI, no training data, no GPU required. Just physics.
Ultrasonic vs. Camera: The Quick Version
We spent a lot of Phase 1 with camera-based detection. Now we have a $4 hardware sensor. Which one wins?
Neither. They solve different problems.
| Camera | HC-SR04 | |
|---|---|---|
| Works in the dark? | No | Yes |
| Knows what the object is? | Yes | No |
| Precise distance in cm? | Estimates | Yes |
| CPU cost | High | Near zero |
| Price | Already owned | ~$4 |
The camera sees the world in rich detail but needs light and processing power. The ultrasonic sensor is blind to everything except "something is X centimeters away" — but it works in pitch darkness, costs almost nothing, and barely uses any CPU. In a real robot, you’d use both. Different tools, different jobs.
What’s Next
We have a sensor that measures distance and a Python program that reads it. What’s missing? A brain that lives inside the robot — not on a laptop sitting nearby.
That’s Part 12. The Raspberry Pi 3B comes out of the box.
Next up: Part 12 — The robot stops borrowing our laptop and gets a brain of its own.