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

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.

LESSON LEARNED: Always ensure your power source meets the peak requirements of the Raspberry Pi, not just idle consumption. The Pi 4B can draw 3A+ under heavy AI processing.

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.

LESSON LEARNED: Lead-acid batteries are heavy and not ideal for portable projects. Lithium-ion batteries are better where weight and portability matter. Always match charger current to battery specifications.

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()
PYTHON CHALLENGE: Python's Global Interpreter Lock (GIL) limits true parallelism. For real-time motor control, Python threading is not enough — use C++ for time-sensitive subsystems.

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

FINAL LESSON: Split your robot's software by real-time requirements. Use Python for prototyping and vision, C++ for anything touching hardware in real-time, and Java for the application framework that ties it all together.

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.

CONTACT TERMINAL