Main Content

Electronic Spirit Level

An electronic spirit level using ‘Motion Gestures’ to get things horizontal OR to the slope of your choice!

This project uses an MPU6050 motion sensor to act as a Spirit Level using some RGB LEDs to indicate offset from level. The real fun comes with the addition of ‘Motion Gesture’ detection to allow the Spirit Level to be re-levelled about an arbitrary slope. This then becomes the ‘Null’ level which the Spirit Level measures against. This allows you to check that different surfaces are at the same slope rather than simply horizontal.

This is a SweetMaker StrawberryString project building on the StrawberryString hardware configuration and the SweetMaker framework.

Level Detection
The SweetMaker MotionSensor library provides the current orientation of the sensor and spirit level allowing the tilt in the x and y directions to be determined. It also allows an offset to be provided allowing for a more accurate ‘zero’ position to be used. As the sensor moves the x and y tilt are read and used to indicate the current orientation.

The MotionSensor includes routines to correctly calibrate the MPU6050 which is essential for accurate operation. These are stored in EEPROM by the StrawberryString library for use when next started.

Level Indication
The RGB LEDs are used to indicate the current level in the following way. Four of the LEDs are arranged in two pairs indicating the tilt in the X and the Y direction.

When approximately level the LED hue converges on green, but as the tilt increases the hue changes proportional to the tilt, one of each pair increasing, one decreasing. This gives a visual indication using Hue.

When level the LED brightness is the same on the LEDs. As tilt increases the brightness quickly changes, one of each pair increasing and one decreasing. This gives a visual indication using brightness.

When precisely level the LEDs change to pink. This gives a very clear indication when you have found your target slope.

Most spirit levels work for horizontal or vertical surfaces. This spirit level can work for arbitrary slopes by using it’s releveling feature. When the level is waved back and forth it triggers a releveling routine which takes the current slope as the target slope. It then continues to operate measuring against this new target slope.

Releveling Gesture Detection
The releveling request gesture is detected from the angular speed of the level following these steps:

- Starting from low speed
- Rising to high speed
- Falling to low speed
- Returning to high speed
- Falling to low speed
- Completing within 2000ms

The MotionSensor provides the rotation delta between samples which allows the rotational speed to be indicated. Thresholds for low and high speed were selected by experimentation. A state machine was implemented to manage the detection process using the timer functionality in the SweetMaker framework.

Releveling Process
Once a relevel request gesture has been detected the releveling process starts. This comprises of the following steps:

- Flash the LEDs PURPLE for 10 seconds to allow time for the spirit level to be held in position.
- If the spirit level is at rest record the current orientation and use this as an offset and then flash the LEDs green to indicate success.
- If the spirit level is not at rest flash the LEDs red to indicate failure.
- Flash the LEDs PURPLE to indicate completion.

The StrawberryString and MotionSensor support the use of offset rotations and auto-levelling. The StrawberryString also stores this in EEPROM so it is used next time the device starts.”

Link to article