Digital spirit level with MPU6050
Build it step by step
0 of 4 doneWire the bus
MPU6050 and OLED share 5V, GND, SDA A4 and SCL A5.
Fix the sensor flat
Glue or screw the GY-521 flat to the base of your level, so its board is parallel to the bottom edge.
Upload
The bubble appears on the left and angles on the right.
Zero it
Put it on a surface you know is level (check with a real spirit level). If it shows a small angle, subtract it in the code.
How do you build a digital spirit level with MPU6050?
An MPU6050 accelerometer measures tilt in two axes and an OLED shows a bubble and the angles, like a spirit level. You need Arduino Nano, MPU6050 IMU, SSD1306 OLED Display, Breadboard 400 points (half size), Jumper wires male to male (40 pcs, 20 cm) and USB A to micro-USB cable (data). It takes 4 steps and about 60 minutes.
- Level
- Intermediate
- Time
- About 60 min
- Parts
- 6
Parts list
- Development BoardsBuyArduino Nano × 1
- SensorsBuyMPU6050 IMU × 1
- DisplaysBuy
- PrototypingBuy
- PrototypingBuy
- Cables & WiresBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| MPU6050 VCC / GND | Nano 5V / GND | The GY-521 board has a 3.3 V regulator |
| MPU6050 SDA / SCL | Nano A4 / A5 | I2C (address 0x68) |
| OLED VCC / GND / SDA / SCL | Nano 5V / GND / A4 / A5 | Same bus (0x3C) |
Code
Arduino IDE, board "Arduino Nano". Install "Adafruit SSD1306". The MPU6050 is read directly, so no IMU library is needed.
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 <Adafruit_SSD1306.h>
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
const int MPU = 0x68;
float pitch = 0, roll = 0;
void setup() {
Wire.begin();
Wire.beginTransmission(MPU); Wire.write(0x6B); Wire.write(0); Wire.endTransmission(); // wake up
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
oled.setTextColor(SSD1306_WHITE);
}
void loop() {
Wire.beginTransmission(MPU); Wire.write(0x3B); Wire.endTransmission(false);
Wire.requestFrom(MPU, 6);
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 p = atan2(-ax, sqrt((float)ay * ay + (float)az * az)) * 180 / PI;
float r = atan2(ay, az) * 180 / PI;
pitch = pitch * 0.8 + p * 0.2; roll = roll * 0.8 + r * 0.2; // smooth the jitter
oled.clearDisplay();
oled.drawCircle(32, 32, 28, SSD1306_WHITE); oled.drawCircle(32, 32, 6, SSD1306_WHITE);
int bx = 32 + constrain(roll, -20, 20) * 1.1, by = 32 + constrain(pitch, -20, 20) * 1.1;
oled.fillCircle(bx, by, 5, SSD1306_WHITE); // the bubble
oled.setCursor(70, 14); oled.print("X "); oled.print(roll, 1);
oled.setCursor(70, 34); oled.print("Y "); oled.print(pitch, 1);
if (fabs(roll) < 0.5 && fabs(pitch) < 0.5) { oled.setCursor(70, 52); oled.print("LEVEL"); }
oled.display();
delay(30);
}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 level table | X and Y within ±0.5°, "LEVEL" |
| Tilted 10° by a protractor or a 10° wedge | Shows 9.5 to 10.5° |
| Turned upside down | Angles jump: the level is meant to be used face up |
If it does not work
Screen blank
Check the OLED address (0x3C or 0x3D) and the shared bus wires.
All readings 0
The MPU6050 did not wake. Check its address with an I2C scanner (0x68, or 0x69 if AD0 is high).
Not zero on a level surface
Normal sensor offset. Store the reading you get on a level surface and subtract it.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the digital spirit level with MPU6050?
Arduino Nano, MPU6050 IMU, SSD1306 OLED Display, Breadboard 400 points (half size), Jumper wires male to male (40 pcs, 20 cm) and USB A to micro-USB cable (data).
How long does the digital spirit level with MPU6050 take to build?
About 60 minutes for a intermediate maker, in 4 steps.
How do I know the digital spirit level with MPU6050 works?
Flat on a level table: X and Y within ±0.5°, "LEVEL". Tilted 10° by a protractor or a 10° wedge: Shows 9.5 to 10.5°. Turned upside down: Angles jump: the level is meant to be used face up.
Screen blank: what should I check?
Check the OLED address (0x3C or 0x3D) and the shared bus wires.
All readings 0: what should I check?
The MPU6050 did not wake. Check its address with an I2C scanner (0x68, or 0x69 if AD0 is high).
Not zero on a level surface: what should I check?
Normal sensor offset. Store the reading you get on a level surface and subtract it.
