Connected
Mood pebbles
Build two. Tap one to choose a mood and the same face appears on the other, in another room of the house.
- Hard
- about 70 min
- ESP32-C3
- 7 parts
- 10 wires
- 91 lines of code
ESP32 packs are built on real parts: the simulator runs Arduino Uno circuits only.
Idea
The two pebbles talk straight to each other by radio, without your Wi-Fi router and without the internet. There is no password to type; both just need the same pair number.
What you learn
Sending data between two boards with ESP-NOW, and keeping strangers' messages out.
How it works
input
Touch sensor
decides
ESP32-C3
output
Round screen
The other pebble (radio)
Powered with 5 V through the board's USB-C port.
A reference design. The sketch is compiled with the real ESP32 toolchain, and its pins are checked against the wiring table automatically. It has not yet been built and tested on real parts by us, so wire it on the bench and run the checks before you build it into a case. Real modules vary: follow the labels printed on yours, not their position in the picture.
Parts
0 of 7 ready
No prices here: they change by shop and by week. Buy each part by its name and the note beside it.
You also need
A computer with Arduino IDE 2 installed (free)
3D wiring
One jumper per step, with the USB cable unplugged. Several wires may share 3V3, 5V or GND: the expansion board or breadboard gives each its own pin.
Step 0 of 10
Lay out the parts
Seat the board on its expansion board or breadboard and lay the modules out round it. Nothing is connected yet.
All connections
mood-pebbles.ino
// Mood pebbles
// Build two. Tap one to pick a mood and the same face appears on the other,
// anywhere in the same house. No Wi-Fi password needed: they talk directly.
#include <WiFi.h>
#include <esp_now.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_GC9A01A.h>
const int PIN_TOUCH_IO = 3;
const int PIN_DIAL_SCL = 4;
const int PIN_DIAL_SDA = 6;
const int PIN_DIAL_DC = 1;
const int PIN_DIAL_CS = 7;
const int PIN_DIAL_RES = 0;
// Both pebbles must use the same number, so a neighbour's pair does not mix in.
const uint8_t PAIR_CODE = 42;
Adafruit_GC9A01A tft(PIN_DIAL_CS, PIN_DIAL_DC, PIN_DIAL_RES);
uint8_t EVERYONE[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
const int MOOD_COUNT = 4; // happy, calm, sad, love
const uint16_t MOOD_COLOUR[MOOD_COUNT] = {0xFFE0, 0x07FF, 0x5ADF, 0xF8B2};
volatile int incomingMood = -1;
int mood = 1;
bool wasTouched = false;
void drawMood(int m, bool fromFriend) {
uint16_t c = MOOD_COLOUR[m];
tft.fillScreen(GC9A01A_BLACK);
tft.fillCircle(120, 120, 100, c);
if (m == 3) {
// a heart: two discs and a triangle
tft.fillCircle(98, 105, 26, GC9A01A_RED);
tft.fillCircle(142, 105, 26, GC9A01A_RED);
tft.fillTriangle(74, 116, 166, 116, 120, 172, GC9A01A_RED);
} else {
tft.fillCircle(88, 100, 11, GC9A01A_BLACK);
tft.fillCircle(152, 100, 11, GC9A01A_BLACK);
if (m == 0) {
tft.fillCircle(120, 135, 34, GC9A01A_BLACK);
tft.fillRect(84, 98, 72, 38, c);
} else if (m == 1) {
tft.fillRect(96, 150, 48, 6, GC9A01A_BLACK);
} else {
tft.fillCircle(120, 178, 30, GC9A01A_BLACK);
tft.fillRect(88, 168, 64, 42, c);
}
}
// a ring round the edge shows the face came from the other pebble
if (fromFriend) {
for (int r = 112; r < 118; r++) tft.drawCircle(120, 120, r, GC9A01A_WHITE);
}
}
void onReceive(const esp_now_recv_info_t* info, const uint8_t* data, int length) {
if (length == 2 && data[0] == PAIR_CODE && data[1] < MOOD_COUNT) incomingMood = data[1];
}
void setup() {
pinMode(PIN_TOUCH_IO, INPUT);
SPI.begin(PIN_DIAL_SCL, -1, PIN_DIAL_SDA, PIN_DIAL_CS);
tft.begin();
drawMood(mood, false);
WiFi.mode(WIFI_STA);
esp_now_init();
esp_now_register_recv_cb(onReceive);
esp_now_peer_info_t peer = {};
memcpy(peer.peer_addr, EVERYONE, 6);
esp_now_add_peer(&peer);
}
void loop() {
bool touched = digitalRead(PIN_TOUCH_IO) == HIGH;
if (touched && !wasTouched) {
mood = (mood + 1) % MOOD_COUNT;
drawMood(mood, false);
uint8_t message[2] = {PAIR_CODE, (uint8_t)mood};
esp_now_send(EVERYONE, message, sizeof(message));
}
wasTouched = touched;
if (incomingMood >= 0) {
mood = incomingMood;
incomingMood = -1;
drawMood(mood, true);
}
delay(20);
}
Libraries
- Adafruit GFX Library: install it from Sketch > Include Library > Manage Libraries.
- Adafruit GC9A01A: install it from Sketch > Include Library > Manage Libraries.
Uploading
First time with an ESP32: in Arduino IDE open Boards Manager, search “esp32” and install “esp32 by Espressif Systems”.
- Open the sketch in Arduino IDE.
- Plug the board in with the USB-C cable.
- Tools > Board > ESP32C3 Dev Module (USB CDC On Boot: Enabled), then Tools > Port and choose the board's port.
- Press Upload (the arrow). Wait for “Done uploading”.