DIY Robot Companion with ESP32-S3 and OLED Screen
Meet your very own pocket robot buddy! It blinks, looks around, smiles, gets surprised and even falls asleep when you ignore it. It runs on a rechargeable battery, so it can sit on your desk or ride in your bag.
- Level: Intermediate
- Time: about 2 hours
- Ages 11+ (with an adult helper)
What are we building?
A companion gadget is a little electronic friend with a personality. Ours has a tiny OLED screen for a face, an ESP32-S3 as its brain, and a lithium battery with a charging module for its energy. One button changes its mood.
Try it: pick a mood and watch the face change, just like the real screen will.
Mood: Happy
What you need
| Part | How many |
|---|---|
| ESP32-S3 development board | 1 |
| 0.96 inch OLED display, 128×64, SSD1306, I2C (4 pins) | 1 |
| TP4056 lithium charger module with protection (the one with 6 pads and two chips) | 1 |
| 3.7V lithium battery (LiPo pouch or 18650) with built-in protection | 1 |
| 5V boost converter module (such as MT3608) set to 5V | 1 |
| Small slide switch | 1 |
| Push button | 1 |
| Jumper wires, breadboard, double-sided tape | 1 set |
| Small box or 3D-printed case | 1 |
SDA_PIN and SCL_PIN in the code.How it works
- Charge: plug USB into the TP4056. It fills the battery safely and its LED shows when it is full.
- Boost: a battery gives 3.7V. The boost converter lifts it to a steady 5V for the ESP32-S3.
- Think: the ESP32-S3 runs the code. Every few seconds it picks a random place to look and a random time to blink.
- Draw: it paints the eyes and mouth on the OLED using simple shapes, many times every second, so they seem to move.
- Feel: each button press changes the mood. If nobody presses it for 60 seconds, the robot falls asleep.
Circuit diagram
Pin table
| From | To |
|---|---|
| ESP32-S3 3V3 | OLED VCC |
| ESP32-S3 GND | OLED GND |
| ESP32-S3 GPIO8 | OLED SDA |
| ESP32-S3 GPIO9 | OLED SCL |
| ESP32-S3 GPIO4 | Mood button, other leg to GND |
| Battery + / − | TP4056 B+ / B− |
| TP4056 OUT+ | Slide switch, then 5V boost IN+ |
| TP4056 OUT− | 5V boost IN− and the common GND |
| 5V boost OUT+ / OUT− | ESP32-S3 5V pin / GND |
Step-by-step instructions
Test the screen on a breadboard
With the battery not connected, wire only the OLED and the button to the ESP32-S3, powered by USB. In the Arduino IDE, install the esp32 by Espressif board package, then the libraries Adafruit SSD1306 and Adafruit GFX from Library Manager.
Upload the code
Choose Tools > Board > ESP32S3 Dev Module, pick your port, paste the code below and click Upload. You should see “Hi! I’m Bolt” and then the eyes appear. Press the button to change the mood.
Wire the battery power
With an adult, set the boost converter to 5.0V first. Then connect battery, TP4056, switch and boost converter as in the diagram, with the switch off. Connect the boost output to the ESP32-S3 5V and GND last.
Build the case
Place the OLED in a window cut into the lid. Fix the board, charger and battery inside with double-sided tape, leaving the TP4056 USB port reachable for charging. Mount the button and switch on the side. Make sure no bare wire touches the battery.
Play with your robot buddy
Slide the switch on. Watch it blink and look around. Press the button to change its mood, and leave it alone for a minute to see it fall asleep. To charge, plug a USB cable into the TP4056 and wait for its LED to show that it is full.
Arduino code
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SDA_PIN 8 // change if your board uses other pins
#define SCL_PIN 9
#define BTN_PIN 4
const char* NAME = "Bolt"; // give your robot a name!
Adafruit_SSD1306 display(128, 64, &Wire, -1);
enum Mood { HAPPY, NORMAL, SURPRISED, SLEEPY };
Mood mood = HAPPY;
float lookX = 0, lookY = 0;
int targetX = 0, targetY = 0;
unsigned long nextBlink = 0, blinkEnd = 0, nextLook = 0, lastPress = 0;
bool lastBtn = HIGH;
void drawEye(int cx, int h) {
int cy = 26;
if (mood == HAPPY && h > 10) { // happy "^" eye
display.fillCircle(cx, cy + 6, 18, WHITE);
display.fillCircle(cx, cy + 14, 18, BLACK);
} else if (mood == SLEEPY) { // closed eye
display.fillRoundRect(cx - 18, cy + 6, 36, 5, 2, WHITE);
} else { // open eye with a pupil
display.fillRoundRect(cx - 18, cy - h / 2, 36, h, 10, WHITE);
if (h > 14) {
display.fillCircle(cx + lookX, cy + lookY, mood == SURPRISED ? 4 : 8, BLACK);
}
}
}
void drawMouth() {
if (mood == HAPPY) { // smile
display.fillCircle(64, 50, 9, WHITE);
display.fillCircle(64, 46, 9, BLACK);
} else if (mood == SURPRISED) {
display.drawCircle(64, 54, 5, WHITE);
} else if (mood == SLEEPY) {
display.setTextSize(1);
display.setCursor(96, 4);
display.print("z z");
} else {
display.drawFastHLine(56, 54, 16, WHITE);
}
}
void setup() {
pinMode(BTN_PIN, INPUT_PULLUP);
Wire.begin(SDA_PIN, SCL_PIN);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
for (;;); // screen not found
}
display.setTextColor(WHITE);
display.clearDisplay();
display.setTextSize(2);
display.setCursor(10, 14);
display.print("Hi! I'm");
display.setCursor(10, 36);
display.print(NAME);
display.display();
delay(2000);
randomSeed(micros());
lastPress = millis();
}
void loop() {
unsigned long now = millis();
bool btn = digitalRead(BTN_PIN);
if (lastBtn == HIGH && btn == LOW) { // button pressed
mood = (Mood)((mood + 1) % 4);
lastPress = now;
delay(30); // debounce
}
lastBtn = btn;
if (now - lastPress > 60000UL) mood = SLEEPY; // falls asleep
if (now > nextLook) {
targetX = random(-8, 9);
targetY = random(-4, 5);
nextLook = now + random(1500, 3500);
}
if (now > nextBlink) {
blinkEnd = now + 150;
nextBlink = now + random(2000, 6000);
}
lookX += (targetX - lookX) * 0.2; // glide to the target
lookY += (targetY - lookY) * 0.2;
int h = (now < blinkEnd) ? 4 : (mood == SURPRISED ? 40 : 30);
display.clearDisplay();
drawEye(34, h);
drawEye(94, h);
drawMouth();
display.display();
delay(20);
}
Tips and troubleshooting
The screen stays black
Check VCC, GND, SDA and SCL. Some OLEDs use address 0x3F instead of 0x3C, so try changing it in display.begin(). Also check that your pins match your board.
It works on USB but turns off on battery
Check that the boost converter really gives 5V and that the battery is charged. A weak battery or a loose switch can make the board restart.
The TP4056 LED never changes
Check the battery polarity and the USB cable. Red usually means charging, and blue or green means full, but it depends on the module.
My module is an IP5306 and it switches itself off
Some IP5306 modules turn off when the load is very small. Press its button again, or add a small extra load as the module’s instructions suggest.
My screen is a different size
This code is for 128×64 screens. A smaller screen, like a 0.42 inch, needs a different library setup and smaller eyes.
What did we learn?
- I2C lets the OLED talk to the ESP32-S3 with just two wires.
- Animation is drawing, clearing and redrawing many times a second.
- Random numbers make a robot seem alive.
- Battery power needs a charger, protection and the right voltage.
Try these challenges
- Add a new mood, like “angry” or “love” with heart eyes.
- Show the time or battery level on the screen.
- Add a touch sensor or a buzzer so it chirps when it is happy.
- Use the ESP32-S3’s Wi-Fi to show the weather on its face.
Questions kids ask
Why not connect the battery straight to the board?
A battery gives 3.7V and changes as it drains. The 5V pin likes a steady 5V, so the boost converter keeps things stable.
Can I use my phone charger?
Yes, a normal 5V USB charger works for charging through the TP4056.
How long does the battery last?
It depends on the battery size. A 1000 mAh battery will usually run a small OLED gadget for several hours.
Comments
Post a Comment