Build it step by step
0 of 7 doneRead the three axes
Print the raw values.
Wire AREF
Use the 3.3 V reference.
Calibrate flat
Store the zero point.
Compute the angles
Roll and pitch with atan2.
Move the dot
Map angle to matrix position.
Add the level beep
Within 0.7 degrees.
Box it
Flat base and a window for the matrix.
How do you build a digital bubble level?
An IMU and LED matrix show a dot like a bubble level, with angle readout. You need Arduino Nano, ADXL335 analog accelerometer, MAX7219 8x8 LED matrix module, Tactile push button 6 mm and Active buzzer 5 V. It takes 7 steps and about 60 minutes.
- Level
- Beginner
- Time
- About 60 min
- Parts
- 5
Parts list
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| ADXL335 VCC and GND | Nano 3V3 and GND | Power (3.3 V) |
| ADXL335 X, Y, Z | Nano A0, A1, A2 | Analog outputs |
| Nano AREF | Nano 3V3 | 3.3 V reference |
| MAX7219 DIN, CS, CLK | Nano D11, D10, D13 | LED matrix |
| MAX7219 VCC and GND | Nano 5V and GND | Power |
| Calibrate button | Nano D2 to ground | Set flat |
| Buzzer | Nano D3 and GND | Level beep |
Code
The ADXL335 outputs a voltage for each axis that changes as gravity pulls on it; tilt shows up as one axis rising and another falling. It is a 3.3 V part, so power it from the Nano's 3V3 pin and tie the AREF pin to 3V3 so the ADC reads the output on the same scale; without this the readings are squashed. Lay the board flat on a surface you trust and press the calibrate button, which stores the flat reading, and then every angle is measured from that. The dot starts in the middle and moves one column for each 3 degrees of tilt, and a beep with a little centre dot says it is level within 0.7 degrees. Accuracy is about a degree, so for fine work check against a real spirit level. Arduino IDE, board "Arduino Nano" (try the Old Bootloader option if the upload fails).
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).
// Digital bubble level with ADXL335 and a MAX7219 matrix. Library: LedControl. Board: Arduino Nano.
#include <LedControl.h>
LedControl lc(11, 13, 10, 1); // DIN, CLK, CS
const int XP = A0, YP = A1, ZP = A2, BTN = 2, BUZ = 3;
float ox = 512, oy = 512, oz = 700; // calibration offsets in ADC counts
float avg(int pin) { long s = 0; for (int i = 0; i < 20; i++) s += analogRead(pin); return s / 20.0; }
void calibrate() { // lay it flat and press the button
ox = avg(XP); oy = avg(YP); oz = avg(ZP);
tone(BUZ, 2000, 100);
}
void setup() {
analogReference(EXTERNAL); // AREF wired to 3.3 V
pinMode(BTN, INPUT_PULLUP); pinMode(BUZ, OUTPUT);
lc.shutdown(0, false); lc.setIntensity(0, 4); lc.clearDisplay(0);
Serial.begin(9600);
}
void loop() {
if (digitalRead(BTN) == LOW) { calibrate(); delay(400); }
float x = avg(XP) - ox, y = avg(YP) - oy, z = avg(ZP);
float roll = atan2(x, z) * 57.2958; // degrees
float pitch = atan2(y, z) * 57.2958;
int col = constrain(3.5 + roll / 3.0, 0, 7); // 3 degrees per column
int row = constrain(3.5 - pitch / 3.0, 0, 7);
lc.clearDisplay(0);
lc.setLed(0, row, col, true);
if (abs(roll) < 0.7 && abs(pitch) < 0.7) { tone(BUZ, 1800, 60); lc.setLed(0, 3, 3, true); lc.setLed(0, 4, 4, true); }
Serial.print("roll "); Serial.print(roll, 1); Serial.print(" pitch "); Serial.println(pitch, 1);
delay(120);
}What the serial monitor should show
Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Nano with its sensors at their default values. Your numbers will follow your real readings, but the format should match.
roll -0.0 pitch 0.0 roll 0.0 pitch -0.0 roll 0.0 pitch 0.0 roll -0.0 pitch -0.0 roll -0.0 pitch 0.0 roll 0.0 pitch 0.0
Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Flat after calibration | Dot in the centre and a beep |
| Tilt one edge up | Dot moves to the other side |
| Serial angles | Close to a phone's level app |
| Level | Beeps within 0.7 degrees |
If it does not work
Dot jumps
Increase the averaging.
Angles squashed
Check AREF is wired to 3.3 V.
Wrong direction
Swap the signs for roll or pitch.
Matrix blank
Check DIN, CLK and CS order and the 5 V supply.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the digital bubble level?
Arduino Nano, ADXL335 analog accelerometer, MAX7219 8x8 LED matrix module, Tactile push button 6 mm and Active buzzer 5 V.
How long does the digital bubble level take to build?
About 60 minutes for a beginner maker, in 7 steps.
How do I know the digital bubble level works?
Flat after calibration: Dot in the centre and a beep. Tilt one edge up: Dot moves to the other side. Serial angles: Close to a phone's level app.
Dot jumps: what should I check?
Increase the averaging.
Angles squashed: what should I check?
Check AREF is wired to 3.3 V.
Wrong direction: what should I check?
Swap the signs for roll or pitch.
Matrix blank: what should I check?
Check DIN, CLK and CS order and the 5 V supply.
