Create a digital spirit level with the ESP32

Leveling the Game

© Lead Image © Uladzimir Kazhamiakin,

© Lead Image © Uladzimir Kazhamiakin,

Article from Issue 260/2022

The small MPU6050 sensor contains a gyroscope and an accelerometer, which means that you can build a digital spirit level with it.

To build a digital spirit level, you first need a sensor such as the MPU6050, which determines the position of an object in space. It has an accelerometer and a gyroscope for each axis in space and measures just 4x4 millimeters. For our test setup, we used a module to hold the semiconductor. You can get it from AZ-Delivery for EUR4.79 [1]. If it is sold-out there, you can also purchase the module on Amazon or from Reichelt.

The MPU6050 communicates with the Raspberry Pi via the I2C bus. The AD0 connector defines whether the sensor resides on bus address 0x68 (AD0 to GND) or 0x69 (AD0 to VSS). The operating voltage for the module is in the range of 3.3 to 5 volts. If you need more information about the MPU6050, take a look at the datasheet [2]. To access the sensor, we will use a library with simple functions for accessing the sensor's readings.

I went for an ESP32, a highly integrated microcontroller with an unbeatable price/performance ratio, to process the data from the sensor. I will be using an ESP32 development kit [3], which you can pick up fairly cheaply for EUR9.49. The Arduino IDE provides the development environment.

Last but not least, the project needs a display to show the measured values of the sensor graphically. An inexpensive 1.8-inch TFT color display for EUR7.99 [4], which can be addressed via a Serial Peripheral Interface (SPI), is a good choice here. Because controlling the display is quite tricky, we will be using the GFX library from Adafruit [5]. The library's source code is available on GitHub [6].

Test Setup

The circuit diagram for our spirit level (Figure 1) shows you how to connect all the components for the project. Let's take a closer look at the two capacitors at the very top of the schematic. The 10-µF capacitor between the EN pin and ground lets you to load the program into the ESP32 without pressing the boot button. Theoretically this should work without an additional capacitor, but practice shows that this is often not the case. The basic problem is the USB driver in combination with certain hardware components of the ESP32 development board.

Figure 1: The circuit diagram for the test setup for our digital spirit level.

The second capacitor (1000 µF) is an option. If the backlight of the display flickers, the capacitor will eliminate this issue. It stabilizes the operating voltage, preventing the flickering. If sporadic problems occur in other digital circuits that are difficult to isolate, it never hurts to build a large capacitor into the power supply. It often helps to eliminate problems of this type.

The remaining connections in the schematic connect the sensor to the ESP32 via I2C. The display uses the SPI and also comes with an additional reset connection. Be sure to connect all the power supplies present in the schematic. As Figure 2 shows, the experimental setup consists of two breadboards with the electronics. On each of the two boards, I removed one of the power supply strips to achieve a setup where the development board fits neatly, and you can easily access all the connections.

Figure 2: The complete test setup on the specially adapted breadboard.

First Test

First of all, you need to start up the MPU6050 sensor. To do this, install the appropriate library in the Arduino IDE. In the menu, navigate to Sketch | Integrate library | Manage library. Once you get there, look for the MPU6050_light library and install it (Figure 3). If you are prompted to do so, install all the dependent libraries, too.

Figure 3: Finding and installing the MPU6050 library.

After the install you will find a sample program for reading the sensor values in the File | Examples | MPU6050_light | GetAllData menu (Figure 4). The software outputs the results of the measurements to the serial console. If you move the sensor, you can easily see how the individual values change. In addition to the data that the sensor inherently provides – acceleration in the direction of the spatial axes, angular acceleration about the axes, and temperature – it also outputs the angle of the axis as a computed value. We will be using this as the basis for our spirit level.

Figure 4: Typical output from the sample program.

Testing the Display

The next step is to fire up the display. To run the display you need three libraries: TFT LCD library [7], Adafruit ST7735 library [8], and Adafruit GFX library [6]. Install all three in the Arduino IDE as described above. Again, you may be prompted for the libraries.

After the install, go to File | Example | Adafruit ST7735 and ST7789 Library | graphicstest to find a program to test the display. To adapt it to our setup, you need to assign different values to TFT_CS, FT_RST, and TFT_DC. The code needed to do this is shown in Listing 1. Simply copy this into the program at the appropriate place. After uploading it to ESP32, a sequence of test graphics will run across the display.

Listing 1


// For 1.44" and 1.8" TFT with ST7735 use:
#define TFT_CS 17
#define TFT_RST 14
#define TFT_DC 2
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Adafruit IO API

    The Adafruit IO API offers a convenient means for network-ready sensors and other components.

  • Adafruit PyPortal

    Unlike other displays for the Raspberry Pi, Adafruit's PyPortal touchscreen provides an autonomous environment, including a microprocessor, sound output, and a WiFi connection.

  • Bluetooth LE

    Bluetooth Low Energy is ideal for networking battery-powered sensors. We show you how to use it on the Raspberry Pi.

  • CircuitPython

    The CircuitPython run-time environment runs on almost all microcomputers and microcontrollers, making it perfect for cross-platform programming.

  • Whiptail

    Whiptail interfaces add menus and information pages to your Raspberry Pi projects.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More