Build it step by step
0 of 8 doneScan the I2C bus
You should see 0x68 and 0x3C.
Wire the sensor and OLED
Same SDA and SCL wires.
Upload the sketch
The screen shows pitch and roll.
Lay it flat and press Zero
Both values become 0.0.
Tilt one edge
The matching angle changes.
Check against a phone level app
Compare a few angles.
Fix it in a box
Keep the sensor flat to the box base.
Test on a shelf
Re-zero on a surface you trust.
How do you build a digital spirit level?
An accelerometer shows pitch and roll in degrees on an OLED, with a beep when the board is level. You need Arduino Uno R3, MPU6050 IMU, SSD1306 OLED Display, Passive buzzer, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 8 steps and about 150 minutes.
- Level
- Beginner
- Time
- About 150 min
- Parts
- 7
Parts list
- Development BoardsBuyArduino Uno R3 × 1
- SensorsBuyMPU6050 IMU × 1
- DisplaysBuy
- Motors & ActuatorsBuyPassive buzzer × 1
- SwitchesBuy
- PrototypingBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| MPU-6050 VCC and GND | Uno 5V and GND | The common GY-521 board has its own regulator |
| MPU-6050 SDA and SCL | Uno A4 and A5 | I2C, address 0x68 |
| OLED SDA and SCL | Uno A4 and A5 | Same I2C bus, address 0x3C |
| OLED VCC and GND | Uno 5V and GND | Power |
| Passive buzzer plus and minus | Uno D3 and GND | Level beep |
| Zero button | Uno D2 to GND | Sets the current angle as zero |
Code
The MPU-6050 measures acceleration, and when the board is still the only acceleration is gravity. The angle of the board against gravity comes from atan2: pitch from the x reading against the combined y and z, and roll from y against z. The sketch wakes the sensor, reads six bytes (x, y and z, two bytes each), converts them to g using the default 16384 counts per g, and shows the angles to one decimal place. A button stores the current angle as zero, so you can level against a surface that is not perfectly flat or calibrate the sensor mounting. When both angles fall within half a degree a short beep sounds once, rather than continuously. An accelerometer cannot separate gravity from motion, so hold the board still while reading: shaking or accelerating will move the numbers. It is a good learning tool and a handy level for shelves and pictures, but its accuracy is about a degree, so it is not a surveying instrument. The OLED and sensor share the same two I2C wires because they have different addresses. Setup: Arduino IDE, board "Arduino Uno". Install the Adafruit SSD1306 and Adafruit GFX libraries.
Runs on a virtual Arduino Uno 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 <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
const uint8_t MPU = 0x68, BTN = 2, BUZ = 3;
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
float pitchOff = 0, rollOff = 0;
bool wasLevel = false;
bool readAccel(float &ax, float &ay, float &az) {
Wire.beginTransmission(MPU);
Wire.write(0x3B); // first accelerometer register
if (Wire.endTransmission(false) != 0) return false;
if (Wire.requestFrom(MPU, (uint8_t)6) != 6) return false;
int16_t x = Wire.read() << 8 | Wire.read();
int16_t y = Wire.read() << 8 | Wire.read();
int16_t z = Wire.read() << 8 | Wire.read();
ax = x / 16384.0; // default range is plus or minus 2 g
ay = y / 16384.0;
az = z / 16384.0;
return true;
}
void setup() {
pinMode(BTN, INPUT_PULLUP);
pinMode(BUZ, OUTPUT);
Wire.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B); // power management: wake the sensor
Wire.write(0);
Wire.endTransmission();
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
}
void loop() {
float ax, ay, az;
if (!readAccel(ax, ay, az)) {
oled.clearDisplay();
oled.setCursor(0, 0);
oled.setTextColor(SSD1306_WHITE);
oled.print("No sensor");
oled.display();
delay(500);
return;
}
float pitch = atan2(ax, sqrt(ay * ay + az * az)) * 57.2958;
float roll = atan2(ay, az) * 57.2958;
if (digitalRead(BTN) == LOW) { // zero here
pitchOff = pitch;
rollOff = roll;
delay(300);
}
float p = pitch - pitchOff, r = roll - rollOff;
bool level = fabs(p) < 0.5 && fabs(r) < 0.5;
if (level && !wasLevel) tone(BUZ, 2000, 80); // one short beep as you reach level
wasLevel = level;
oled.clearDisplay();
oled.setTextColor(SSD1306_WHITE);
oled.setTextSize(1);
oled.setCursor(0, 0); oled.print("PITCH");
oled.setCursor(0, 34); oled.print("ROLL");
oled.setTextSize(2);
oled.setCursor(0, 12); oled.print(p, 1);
oled.setCursor(0, 46); oled.print(r, 1);
if (level) { oled.setCursor(96, 24); oled.print("OK"); }
oled.display();
delay(100);
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Flat on a table, then Zero | Both near 0.0 |
| Lift one edge by about 5 degrees | One angle reads about 5 |
| Return to flat | Beep and OK appear |
| Tilt on both axes | Both values change |
| Shake the board | Numbers jump, settle when still |
If it does not work
No sensor message
Check VCC, GND, SDA and SCL, and the 0x68 address.
OLED blank
Check 0x3C, or change to 0x3D for some screens.
Angles drift
The board is moving, or the sensor is warming up.
Wrong axis
The module is mounted rotated, swap the formula or the mounting.
No beep
Check the buzzer is passive and on D3.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the digital spirit level?
Arduino Uno R3, MPU6050 IMU, SSD1306 OLED Display, Passive buzzer, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).
How long does the digital spirit level take to build?
About 150 minutes for a beginner maker, in 8 steps.
How do I know the digital spirit level works?
Flat on a table, then Zero: Both near 0.0. Lift one edge by about 5 degrees: One angle reads about 5. Return to flat: Beep and OK appear.
No sensor message: what should I check?
Check VCC, GND, SDA and SCL, and the 0x68 address.
OLED blank: what should I check?
Check 0x3C, or change to 0x3D for some screens.
Angles drift: what should I check?
The board is moving, or the sensor is warming up.
Wrong axis: what should I check?
The module is mounted rotated, swap the formula or the mounting.
