Magic 8-ball
Shake the box and an answer appears on a small display, like the toy that gives yes, no or maybe replies.
Build it step by step
0 of 8 doneInstall the library
Search for LiquidCrystal I2C in the Library Manager.
Wire both I2C devices
Display and sensor share A4 and A5.
Upload the sketch
Display shows Shake me.
Check both addresses with an I2C scanner
0x27 and 0x68.
Shake the board
Thinking appears for a moment.
Wait
An answer shows.
Shake several times
Different answers come up.
Mount it in a box
Use a power bank.
How do you build a magic 8-ball?
Shake the box and an answer appears on a small display, like the toy that gives yes, no or maybe replies. You need Arduino Nano, MPU6050 IMU, 16x2 LCD with I2C backpack (PCF8574), Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 8 steps and about 120 minutes.
- Level
- Beginner
- Time
- About 120 min
- Parts
- 5
Parts list
- Development BoardsBuyArduino Nano × 1
- SensorsBuyMPU6050 IMU × 1
- DisplaysBuy
- PrototypingBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| MPU6050 VCC and GND | Nano 5V and GND | Most breakout boards include a regulator |
| MPU6050 SDA and SCL | Nano A4 and A5 | I2C, address 0x68 |
| LCD I2C SDA and SCL | Nano A4 and A5 | Shared bus, address 0x27 |
| LCD I2C VCC and GND | Nano 5V and GND | Display power |
| Nano power | USB power bank | Lets you shake it freely |
Code
The MPU6050 measures acceleration on three axes. At rest the total is about 1 g, because gravity is always pulling, so the sketch computes the total length of the acceleration vector and looks for a sudden change from one reading to the next. A change of more than 0.8 g in 20 milliseconds is a firm shake. The sketch then shows Thinking and, after a pause that builds suspense, picks a random answer. The sensor is read directly over I2C, with no library: register 0x6B wakes it from sleep and 0x3B starts the six bytes of acceleration data. The raw numbers are divided by 16384 to get g for the default plus or minus 2 g range. The 12 answers are a mix of positive, neutral and negative replies like the classic toy. Put the Nano, sensor and display in a small box or a ball and power it from a USB power bank so you can shake it freely. Raise 0.8 to 1.2 if it triggers too easily.
Runs on a virtual Arduino Nano 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).
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // try 0x3F if the screen stays blank
const uint8_t MPU = 0x68;
// Each answer is split into two 16-character lines.
const char* LINE1[] = {"It is certain", "Without a doubt", "Yes, definitely", "Most likely", "Signs point", "Ask again", "Cannot predict", "Better not", "Don't count on", "Very doubtful", "Outlook good", "My reply is"};
const char* LINE2[] = {"", "", "", "", "to yes", "later", "now", "tell you now", "it", "", "", "no"};
const uint8_t N_ANSWERS = sizeof(LINE1) / sizeof(LINE1[0]);
float lastMag = 1.0;
bool waiting = true;
unsigned long shakeStart = 0;
void mpuWake() {
Wire.beginTransmission(MPU); Wire.write(0x6B); Wire.write(0); // wake from sleep
Wire.endTransmission(true);
}
float readAccelG() { // total acceleration in g
Wire.beginTransmission(MPU); Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU, (uint8_t)6, (uint8_t)true);
int16_t ax = (Wire.read() << 8) | Wire.read();
int16_t ay = (Wire.read() << 8) | Wire.read();
int16_t az = (Wire.read() << 8) | Wire.read();
float x = ax / 16384.0, y = ay / 16384.0, z = az / 16384.0; // default range is plus or minus 2 g
return sqrt(x * x + y * y + z * z);
}
void show(const char* a, const char* b) {
lcd.clear();
lcd.setCursor(0, 0); lcd.print(a);
lcd.setCursor(0, 1); lcd.print(b);
}
void setup() {
Wire.begin();
lcd.init(); lcd.backlight();
mpuWake();
randomSeed(analogRead(A0));
show("Shake me", "and ask a question");
}
void loop() {
float mag = readAccelG();
float change = fabs(mag - lastMag);
lastMag = mag;
if (change > 0.8 && waiting) { // a strong sudden change counts as a shake
waiting = false;
shakeStart = millis();
show("Thinking...", "");
}
if (!waiting && millis() - shakeStart > 1500) {
randomSeed(micros() + analogRead(A0));
uint8_t i = random(N_ANSWERS);
show(LINE1[i], LINE2[i]);
waiting = true;
delay(1500); // let the shaking settle before the next one
}
delay(20);
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Power on | Shake me |
| Gentle tilt | No reaction |
| Firm shake | Thinking, then an answer |
| Twenty shakes | At least eight different answers |
| Shake during an answer | Waits until the next shake |
If it does not work
No reaction to shaking
Check the MPU6050 on 0x68 and lower the 0.8 threshold.
Triggers by itself
Raise the threshold, or check for a loose wire.
Display blank
Try address 0x3F, or turn the contrast screw.
Same answer often
Leave A0 floating for the random seed.
Sensor not found
Use the I2C scanner project to check the bus.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the magic 8-ball?
Arduino Nano, MPU6050 IMU, 16x2 LCD with I2C backpack (PCF8574), Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).
How long does the magic 8-ball take to build?
About 120 minutes for a beginner maker, in 8 steps.
How do I know the magic 8-ball works?
Power on: Shake me. Gentle tilt: No reaction. Firm shake: Thinking, then an answer.
No reaction to shaking: what should I check?
Check the MPU6050 on 0x68 and lower the 0.8 threshold.
Triggers by itself: what should I check?
Raise the threshold, or check for a loose wire.
Display blank: what should I check?
Try address 0x3F, or turn the contrast screw.
Same answer often: what should I check?
Leave A0 floating for the random seed.
