12 Machine building¶
Presentation Machine 1¶

Presentation Machine 2¶

Presentation Machine 3¶

π₯ Mariam’s Individual Contribution β Self-Balancing Robot¶
Full page: Mariam’s Week 12 documentation
π€ Main Robot¶
This week the group assignment required us to design a machine that includes mechanism + actuation + automation + function + user interface, build the mechanical parts and operate it manually, and document both the group project and individual contributions.
After discussing many ideas, we decided to build a self-balancing robot. Mariam also proposed adding a camera that detects a smile β and when a smile is detected, the robot deliberately loses balance π. This made the individual task significantly more complex, but led to exploring several interesting technologies.
π Sketches of Electronics Part and Control Logic¶
Here are some example sketches created during research β this approach helps build stronger understanding, functioning as a visual database from first idea to final result.
π Main Robot β Electronics¶
After finishing the research, work began on the part of the robot responsible for maintaining balance. The following components are needed for the main functionality:
1. MCU β Seeed Studio XIAO RP2040
Pins D0βD7 are used as follows: D0/D1 for Motor A direction, D2/D3 for Motor B direction, D4/D5 for SDA/SCL (LCD + MPU6050), D6 for LED, and D7 (RX) to receive the signal from the face detection PCB.
2. Sensor β MPU6050
The MPU6050 is the "gold standard" for balancing projects β it combines a 3-axis accelerometer (tells the robot which way is down) and a 3-axis gyroscope (tells how fast it is falling), with 16-bit ADC for detecting very small movements.
3. Motors β 2Γ TT DC Gear Motor (3β6V)
This is a compact and convenient option
4. Motor Drivers β 2Γ A4953
The only motor driver available in the lab; its minimum voltage is 8V, which is not ideal for lower voltages.
5. Battery β 3Γ 1200mAh 4.2V Li-Po
Compact, easy to use, and enough to power both motors and the MCU for the camera.
6. Voltage Regulator β LM2940
Sensitive to noise, so a 22Β΅F capacitor is used to keep the 5V rail stable.
7. Display β OLED SSD1306 (IΒ²C)
Compact size and multiple colors allow nice animations.
8. Diode β Schottky Diode SOD123
Placed near the 5V line so that when powering from battery, USB current won't damage the MCU.
9. Resistors & Capacitors
For proper configuration and to avoid short circuits.
πΊοΈ Main Robot β Schematic and PCB Design¶
With all components and their purposes defined, the KiCad schematic and PCB designs were assembled.


3D views of the PCB:

Once the files were ready, .nc files were generated on modsproject.org for F.Cu (1/64” bit) and Edge.Cuts (1/32” bit).
π§ Main Robot β Soldering¶
All components laid out and workspace prepared:


Final soldered result (top and back):

π§ Main Robot β Programming & PID Control¶
The first test code initialized the MPU6050 and OLED and drove the motors forward β but without a closed-loop system the robot cannot balance. A balancing robot is like keeping a stick on your finger: without continuous correction it falls.
The robot needs PID control β a smarter brain that runs every few milliseconds:
| Term | Formula | Role |
|---|---|---|
| π’ P (Proportional) | Kp Γ error |
Reacts to current tilt |
| π‘ I (Integral) | Ki Γ Ξ£ error |
Fixes long-term drift |
| π΅ D (Derivative) | Kd Γ Ξerror/Ξt |
Reacts to how fast it is falling |
motor speed = P + I + D β recalculated every few milliseconds.
Video of the first test (MPU6050 + OLED + motors, open loop):
With Hrach’s help a temporary body was built for testing. Here are the first videos of the robot falling π€ͺ:
After several tests, the SDA pin of the RP2040 burned out. The MCU was replaced with an ESP32-C3 (same pin functions), and Onik helped further stabilize the system.

π Smile Detection Module¶
Research¶
The XIAO ESP32-S3 Sense was chosen as a vision co-processor β it focuses entirely on the camera stream and smile detection, and communicates to the main MCU via a single GPIO pin (PIN 43: HIGH = smile detected, LOW = no smile).
Electronics β Smile Detection PCB¶
Components: ESP32-S3, LM2940 voltage regulator, capacitors and resistors. The PCB was also designed in KiCad.

3D views:
Soldering β Smile Detection¶


When attaching the ESP32-S3, double-sided tape was used under the chip to prevent its back copper layer from shorting with the PCB GND.
Final result:
Programming β Smile Detection & Web Interface¶
The smile detection algorithm is brightness-based (grayscale, no face detector). It compares a forehead zone (top 10β25% of frame) against a mouth zone (62β85%). If the mouth is significantly brighter (teeth visible) and local contrast is high enough, a smile is counted. A web dashboard served from the ESP32-S3 shows the live brightness chart, contrast meter, total smiles, and GPIO 43 status in real time.
Web interface screenshot:

Smile detection demo video:
Limitation: zones are hardcoded as fixed frame percentages, so detection only works when the person is centered at a specific distance. Any position change breaks the detection β the algorithm doesn’t know where the face actually is.
π§ͺ Person Classification β Edge Impulse Experiment¶
After the brightness-based approach proved unreliable, a custom ML model was built using Edge Impulse to classify between Mariam and Hrach.
A Python + OpenCV script auto-captured 100 face photos per person via webcam, saved into dataset/Mariam/ and dataset/Hrach/.
The dataset was uploaded to Edge Impulse β an Impulse was created (Image input β Classification) β features generated β model trained.




Model testing showed 91.36% accuracy on the test set:
The model was deployed as an Arduino Library (.zip) and installed via Sketch β Include Library β Add .ZIP Library.


Real-world result: the model recognized Hrach as Mariam, and almost any object as Hrach. The dataset of 100 images per person was far too small. π
π€ Face Detection β Final Approach¶
Returning to basics: the camera only needs to detect that a person is present, not recognize who. Two built-in ESP-DL models run directly on the ESP32-S3:
HumanFaceDetectMSR01β fast first-pass candidate detectionHumanFaceDetectMNP01β refines candidates with a confidence threshold
Logic: Face detected β PIN 43 HIGH β ESP32-C3 stops balancing motors. No face for 1.5 s β PIN 43 LOW β robot resumes. Green bounding boxes are drawn around detected faces and the live web interface shows camera feed + detection stats.
π Final Robot with Interface¶
After many PID coefficient adjustments and code modifications, here is the final result. The robot balances and stops when a face is detected. The balancing is not yet perfect, but the full pipeline β PCB design, milling, soldering, PID control, and computer vision β was completed in one week.
π Files for Download¶
| File | Description |
|---|---|
| PCB Design β Face Detection | KiCad PCB β ESP32-S3 face detection board |
| Schematic β Face Detection | KiCad schematic β ESP32-S3 board |
| PCB Design β Self Balancing Robot | KiCad PCB β ESP32-C3 main robot board |
| Schematic β Self Balancing Robot | KiCad schematic β ESP32-C3 board |