Built-in project · Arduino Uno R3

Tilt level on an Arduino Uno

An ADXL345 accelerometer read with the Adafruit library: the three axes in m/s^2 and a spirit level that says which way to tip. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate Runs in your browser. Free, and no account needed.

The parts retail for about $33. On Mokxi they're free.

Tilt level · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Tilt level: an ADXL345 accelerometer on I2C, read with the Adafruit
// unified-sensor library, as a spirit level that says which way to tip.
//
//   ADXL345 (GY-291)   SDA -> A4   SCL -> A5   VCC -> 5 V   GND -> GND
//                      CS, SDO, INT1 and INT2 left as the board pulls them
//
// An accelerometer at rest measures gravity: 9.8 m/s^2 straight down. Lying
// flat that is all on Z; tipped, some of it moves onto X or Y, and the share
// says how far. The sketch prints the three axes in m/s^2 and the raw counts
// on X (256 a g at full resolution), and calls it level when X and Y are both
// under half a meter per second squared, about three degrees.
//
// The chip starts in standby and does not measure until it is told to:
// begin() does that, and setRange() picks 16 g at full resolution.
//
// Drag the board to tip it.

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>

Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);

void setup() {
  Serial.begin(9600);
  if (!accel.begin()) {
    Serial.println("No ADXL345 detected");
    while (1) delay(10);
  }
  accel.setRange(ADXL345_RANGE_16_G);
  Serial.println("Tilt level: ADXL345 ready");
}

void loop() {
  sensors_event_t event;
  accel.getEvent(&event);
  int16_t rawX = accel.getX();

  Serial.print("X: ");
  Serial.print(event.acceleration.x);
  Serial.print("  Y: ");
  Serial.print(event.acceleration.y);
  Serial.print("  Z: ");
  Serial.print(event.acceleration.z);
  Serial.print(" m/s^2  raw X ");
  Serial.print(rawX);

  // Half a m/s^2 is 13 counts at 3.9 mg a count.
  int16_t rawY = accel.getY();
  if (rawX > 13) {
    Serial.println("  tip it nose up");
  } else if (rawX < -13) {
    Serial.println("  tip it nose down");
  } else if (rawY > 13 || rawY < -13) {
    Serial.println("  roll it level");
  } else {
    Serial.println("  level");
  }
  delay(300);
}

Parts list

3 parts, plus the jumper wires. They retail for about $33; on Mokxi they're free.

How it is wired

4 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Arduino Uno R3 pin 5V; Accelerometer, ADXL345 pin VCC
  • Ground: Arduino Uno R3 pin GND; Accelerometer, ADXL345 pin GND
  • Arduino Uno R3 pin A4; Accelerometer, ADXL345 pin SDA
  • Arduino Uno R3 pin A5; Accelerometer, ADXL345 pin SCL

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.