Introduction
Building a humanoid robot on wheels is an exciting and challenging experience. This project aimed to create a robot with face detection, recognition, speech processing and obstacle avoidance — all running on a Raspberry Pi 4B.
The robot stands at 4'5" and uses a combination of Python, C++ and Java to handle its various subsystems efficiently.
Phase 1 — Planning and Hardware
Core Features Designed
- Face detection and recognition via Pi Camera and OpenCV
- Obstacle avoidance using ultrasonic sensors
- Speech synthesis for human interaction
- Automatic wheel-based locomotion
Power System Challenges
We initially planned to power the robot using a 20000mAh power bank (18W output). However, this was insufficient for the Raspberry Pi 4B under load, causing glitches and instability.
We switched to a 12V 2.5Ah lead-acid scooter battery, using a buck converter to step down to 5V for the Pi. This solved the instability completely.
Python — High-Level Development
Python handles the high-level tasks: face recognition, speech and sensor coordination. Its extensive library ecosystem makes rapid prototyping fast.
Motor Control with RPi.GPIO
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
# Define and set motor pins
GPIO.setup(MOTOR_PIN, GPIO.OUT)
GPIO.output(MOTOR_PIN, GPIO.HIGH)
Ultrasonic Sensor
import time
import RPi.GPIO as GPIO
GPIO.setup(TRIG_PIN, GPIO.OUT)
GPIO.setup(ECHO_PIN, GPIO.IN)
GPIO.output(TRIG_PIN, True)
time.sleep(0.00001)
GPIO.output(TRIG_PIN, False)
while GPIO.input(ECHO_PIN) == 0:
pulse_start = time.time()
while GPIO.input(ECHO_PIN) == 1:
pulse_end = time.time()
distance = (pulse_end - pulse_start) * 17150
Face Detection with OpenCV + Dlib
import cv2
import dlib
detector = dlib.get_frontal_face_detector()
camera = cv2.VideoCapture(0)
while True:
ret, frame = camera.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = detector(gray)
for face in faces:
print(f"Face detected at: {face}")
Speech Synthesis
import pyttsx3
engine = pyttsx3.init()
engine.say("Hello human, I am online.")
engine.runAndWait()
Multi-Threading
import threading
def control_motors():
# Motor control loop
pass
def face_recognition():
# Vision processing loop
pass
t1 = threading.Thread(target=control_motors)
t2 = threading.Thread(target=face_recognition)
t1.start()
t2.start()
C++ — Performance-Critical Control
C++ handles motor control and real-time sensor reads where Python's latency would cause problems.
Motor Control with WiringPi
#include <wiringPi.h>
pinMode(MOTOR_PIN, OUTPUT);
digitalWrite(MOTOR_PIN, HIGH); // Motor ON
delay(1000);
digitalWrite(MOTOR_PIN, LOW); // Motor OFF
Ultrasonic Sensor in C++
long duration, distance;
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance = duration * 0.034 / 2;
Multi-threading in C++
#include <thread>
std::thread motorThread(controlMotors);
std::thread faceThread(detectFace);
motorThread.join();
faceThread.join();
Java — Structured Application Layer
Java manages the structured application logic, communication between modules and GPIO via Pi4J.
Motor Control with Pi4J
import com.pi4j.io.gpio.*;
GpioController gpio = GpioFactory.getInstance();
GpioPinDigitalOutput motorPin =
gpio.provisionDigitalOutputPin(
RaspiPin.GPIO_01, "Motor", PinState.LOW
);
motorPin.high(); // Motor ON
Thread.sleep(1000);
motorPin.low(); // Motor OFF
Face Detection with OpenCV Java Bindings
CascadeClassifier face_cascade = new CascadeClassifier(
"haarcascade_frontalface_default.xml"
);
VideoCapture capture = new VideoCapture(0);
Mat frame = new Mat();
while (capture.read(frame)) {
Mat gray = new Mat();
Imgproc.cvtColor(frame, gray, Imgproc.COLOR_BGR2GRAY);
Rect[] faces = face_cascade.detectMultiScale(
gray, 1.1, 3, 0,
new Size(30, 30), new Size(500, 500)
);
for (Rect face : faces) {
Imgproc.rectangle(frame, face.tl(), face.br(),
new Scalar(0, 255, 0));
}
}
Architecture — Which Language Does What
- Python — Face recognition, speech synthesis, high-level coordination
- C++ — Real-time motor control, sensor reads, time-critical loops
- Java — Application structure, GPIO abstraction via Pi4J, networking
Conclusion
Building this robot taught more about hardware constraints, power management and software architecture than any tutorial ever could. The combination of Python, C++ and Java gives you coverage across the full spectrum — from rapid prototyping to production-grade real-time control.