Build it step by step
0 of 7 doneWire the sensor
VCC to 3V3, GND to GND, SDA to A4, SCL to A5.
Wire the display
5V or 3V3, GND, SDA to A4, SCL to A5.
Wire the button
D2 to GND.
Install the libraries
Adafruit ADXL345, Unified Sensor, SSD1306 and GFX.
Upload the sketch
The display shows an angle near zero on a flat table.
Tilt the board
The number follows it.
Press the button
The present angle becomes zero.
How do you build a digital protractor?
An ADXL345 accelerometer measures its own tilt from the pull of gravity and shows the angle in degrees on an OLED, with a button to zero it on any surface. You need Arduino Nano, ADXL345 accelerometer, SSD1306 OLED Display, Tactile push button 6 mm, Jumper wires male to male (40 pcs, 20 cm) and Breadboard 830 Tie Points. It takes 7 steps and about 120 minutes.
- Level
- Beginner
- Time
- About 120 min
- Parts
- 6
Parts list
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| ADXL345 VCC | Nano 3V3 | Power at 3.3 V (use the 3.3 V pin for most breakouts) |
| ADXL345 GND | Nano GND | Common ground |
| ADXL345 SDA and SCL | Nano A4 and A5 | I2C, address 0x53 |
| OLED VCC | Nano 5V | Power (check your OLED accepts 5 V, otherwise use 3V3) |
| OLED GND | Nano GND | Common ground |
| OLED SDA and SCL | Nano A4 and A5 | Shared I2C bus, address 0x3C |
| Zero button | Nano D2 and GND | Sets the current angle to zero |
Code
An accelerometer measures acceleration, and when it is at rest the only acceleration it feels is gravity, which points down. If the board is tilted, gravity is split between the axes, and the angle follows from the ratio of two of them. The program uses the X and Z readings and the atan2 function, which gives the angle in degrees from flat, between minus 180 and plus 180. It averages sixteen readings to steady the number. A button sets the present angle as zero, so you can lay the board on a surface and then measure how far another surface is from it. The tilt is measured in one plane only, the one that turns about the Y axis, so line the board's long side up with the slope you are measuring. The ADXL345 has small errors that differ between chips, so treat the reading as good to about a degree and not as a surveying tool. Do not use it for anything safety-critical, such as checking a ladder or a load. Keep the ADXL345 on 3.3 volts.
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_Sensor.h>
#include <Adafruit_ADXL345_U.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
const int BUTTON = 2;
float offsetDeg = 0;
float readAngle() {
float sx = 0, sz = 0;
for (int i = 0; i < 16; i++) { // average 16 readings to steady the number
sensors_event_t e;
accel.getEvent(&e);
sx += e.acceleration.x;
sz += e.acceleration.z;
delay(5);
}
return atan2(sx, sz) * 180.0 / PI; // tilt in the X to Z plane, 0 when flat
}
void setup() {
pinMode(BUTTON, INPUT_PULLUP);
Wire.begin();
if (!accel.begin()) { while (true) {} }
accel.setRange(ADXL345_RANGE_2_G);
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { while (true) {} }
}
void loop() {
float angle = readAngle();
if (digitalRead(BUTTON) == LOW) {
offsetDeg = angle; // this surface is now zero
delay(250);
}
float shown = angle - offsetDeg;
if (shown > 180) shown -= 360;
if (shown < -180) shown += 360;
oled.clearDisplay();
oled.setTextColor(SSD1306_WHITE);
oled.setTextSize(1);
oled.setCursor(0, 0);
oled.print("Angle");
oled.setTextSize(3);
oled.setCursor(0, 20);
oled.print(shown, 1);
oled.setTextSize(2);
oled.print(" deg");
oled.setTextSize(1);
oled.setCursor(0, 56);
oled.print("Press button to zero");
oled.display();
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Board flat | Close to 0 |
| Tilt it up by hand | Number rises smoothly |
| Tilt it the other way | Number goes negative |
| Press the button on a slope | Shows 0 |
| Lay it on a book against a wall | Close to a right angle with a steady reading |
If it does not work
Does not start
Check the sensor wires and the address 0x53.
Angle is always 0
Check SDA and SCL are not swapped and the library is installed.
Number jumps
Hold the board still, and keep wires from pulling on it.
Wrong by a few degrees
Use the zero button on a surface you trust.
Reads 90 flat
Mount the board so the X to Z plane is the one you want to measure.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the digital protractor?
Arduino Nano, ADXL345 accelerometer, SSD1306 OLED Display, Tactile push button 6 mm, Jumper wires male to male (40 pcs, 20 cm) and Breadboard 830 Tie Points.
How long does the digital protractor take to build?
About 120 minutes for a beginner maker, in 7 steps.
How do I know the digital protractor works?
Board flat: Close to 0. Tilt it up by hand: Number rises smoothly. Tilt it the other way: Number goes negative.
Does not start: what should I check?
Check the sensor wires and the address 0x53.
Angle is always 0: what should I check?
Check SDA and SCL are not swapped and the library is installed.
Number jumps: what should I check?
Hold the board still, and keep wires from pulling on it.
Wrong by a few degrees: what should I check?
Use the zero button on a surface you trust.
