Build it step by step
0 of 7 doneTest the tilt switch
Print its state.
Test the OLED
Show a word.
Detect a shake
Four changes in 0.6 seconds.
Pick an answer
Random from a list.
Fade it in
Contrast ramp.
Add sleep
Off after 10 seconds.
Write your own answers
Make it yours.
How do you build a shake-to-answer magic ball?
Shake the toy and an OLED shows a random answer to your question. You need Seeed XIAO RP2040, Tilt sensor (ball switch, SW-520D), 0.96 inch OLED SPI (SSD1306, 7-pin) and USB A to micro-USB cable (data). It takes 7 steps and about 45 minutes.
- Level
- Beginner
- Time
- About 45 min
- Parts
- 4
Parts list
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Tilt switch | XIAO D0 and GND (internal pull-up) | Shake sensor |
| OLED CS, DC, RES | XIAO D3, D1, D2 | Control lines |
| OLED D0 (SCK) and D1 (MOSI) | XIAO D8 and D10 | SPI |
| OLED VCC and GND | XIAO 3V3 and GND | Power |
| XIAO USB | A small USB power bank | Power |
Code
A tilt switch is a tiny ball that opens and closes a contact as it rolls, so a quick shake gives a burst of changes; four changes within 0.6 seconds counts as a shake, which a slow tilt does not. Each shake picks a random answer from the list and fades it in on the OLED, and after 10 seconds the display switches off to save power. Write your own answers for a party or a classroom. The XIAO RP2040 has no battery charger, so power it from a small USB power bank rather than wiring a LiPo to it. It needs the Raspberry Pi Pico core by Earle Philhower in the Arduino IDE, and board "Seeed XIAO RP2040", which names the pins D0 to D10. For a hand-held toy, put it in a rounded case, or a clear sphere, and keep the cable tidy.
Runs on a virtual Seeed XIAO RP2040 in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
// Magic 8-ball: shake it and the OLED shows an answer. Library: Adafruit SSD1306 / GFX. Board: Seeed XIAO RP2040 (arduino-pico core).
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 oled(128, 64, &SPI, D1, D2, D3); // DC, RESET, CS
const int TILT = D0;
const char* ANSWERS[] = {"Yes", "No", "Maybe", "Ask again", "Definitely", "Not today", "Looks good", "Doubtful",
"Go for it", "Wait a bit", "Surely not", "Try it", "Signs say yes", "Cannot say", "Very likely", "Unlikely"};
const int N = sizeof(ANSWERS) / sizeof(ANSWERS[0]);
unsigned long lastShow = 0;
bool awake = false;
void showAnswer() {
const char* a = ANSWERS[random(N)];
oled.ssd1306_command(SSD1306_DISPLAYON);
oled.clearDisplay(); oled.setTextColor(WHITE); oled.setTextSize(2);
oled.setCursor(4, 24); oled.print(a);
for (int c = 0; c <= 255; c += 15) { oled.ssd1306_command(SSD1306_SETCONTRAST); oled.ssd1306_command(c); oled.display(); delay(30); } // fade in
lastShow = millis(); awake = true;
}
void setup() {
pinMode(TILT, INPUT_PULLUP);
oled.begin(SSD1306_SWITCHCAPVCC);
oled.clearDisplay(); oled.display();
randomSeed(micros());
}
void loop() {
static bool last = false; static int flips = 0; static unsigned long t0 = 0;
bool now = digitalRead(TILT);
if (now != last) { last = now; if (millis() - t0 > 600) { t0 = millis(); flips = 0; } flips++; }
if (flips >= 4 && millis() - t0 < 600) { flips = 0; showAnswer(); } // four changes in 0.6 s counts as a shake
if (awake && millis() - lastShow > 10000) { // sleep after 10 seconds
oled.ssd1306_command(SSD1306_DISPLAYOFF); awake = false;
}
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Gentle tilt | Nothing happens |
| Shake | A random answer fades in |
| Wait 10 seconds | Display turns off |
| Shake again | A new answer |
If it does not work
Nothing on screen
Check the SPI wiring and reset pin.
Answers on every tilt
Raise the shake count.
No shake detected
Lower the count or shake harder.
Wrong pins
Select the XIAO RP2040 board so D0 to D10 are defined.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the shake-to-answer magic ball?
Seeed XIAO RP2040, Tilt sensor (ball switch, SW-520D), 0.96 inch OLED SPI (SSD1306, 7-pin) and USB A to micro-USB cable (data).
How long does the shake-to-answer magic ball take to build?
About 45 minutes for a beginner maker, in 7 steps.
How do I know the shake-to-answer magic ball works?
Gentle tilt: Nothing happens. Shake: A random answer fades in. Wait 10 seconds: Display turns off.
Nothing on screen: what should I check?
Check the SPI wiring and reset pin.
Answers on every tilt: what should I check?
Raise the shake count.
No shake detected: what should I check?
Lower the count or shake harder.
Wrong pins: what should I check?
Select the XIAO RP2040 board so D0 to D10 are defined.
