Create a digital spirit level with the ESP32
Spirit Level Program
After testing and performing a trial run with the components of our spirit level without any errors, it's time to take a closer look at the program (Listing 2) for the spirit level. You will find it, along with some other files, in the download section for this article [9]. The first block imports the required libraries (lines 1 to 5). To make the code more readable, the program then defines some constants (lines 6 to 8).
Listing 2
spiritlevel.ino
01 #include "Wire.h" 02 #include <Adafruit_GFX.h> 03 #include <Adafruit_ST7735.h> 04 #include <SPI.h> 05 #include <MPU6050_light.h> 06 #define TFT_CS 17 07 #define TFT_RST 14 08 #define TFT_DC 2 09 10 Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST); 11 MPU6050 mpu(Wire); 12 13 void setup(void) { 14 Wire.begin(); 15 mpu.begin(); 16 mpu.calcOffsets(); 17 tft.initR(INITR_BLACKTAB); 18 tft.fillScreen(ST77XX_BLACK); 19 } 20 21 int x, y, xold, yold; 22 23 void loop() { 24 mpu.update(); 25 x=floor(mpu.getAngleX()); 26 y=floor(mpu.getAngleY()); 27 if ((x!=xold) or (y!=yold)) { 28 tft.setTextSize(1, 3); 29 tft.setCursor(20, 0); 30 tft.setTextColor(ST77XX_BLUE); 31 tft.print("2D-Wasserwaage"); 32 tft.fillCircle(64-x, 80+y, 12, ST77XX_BLACK); 33 tft.fillCircle(64-x, 80+y, 5, ST77XX_YELLOW); 34 tft.drawCircle(64, 80, 7, ST77XX_RED); 35 tft.drawCircle(64, 80, 27, ST77XX_RED); 36 tft.drawCircle(64, 80, 47, ST77XX_RED); 37 tft.drawLine(64, 30, 64, 130, ST77XX_RED); 38 tft.drawLine(14, 80, 114, 80, ST77XX_RED); 39 tft.fillRect(0, 142, 160, 30, ST77XX_BLACK); 40 tft.setTextSize(2); 41 tft.setTextColor(ST77XX_GREEN); 42 tft.setCursor(0, 142); 43 tft.print("x="); 44 tft.println(x); 45 tft.setCursor(64, 142); 46 tft.print("y="); 47 tft.println(y); 48 } 49 xold=x; 50 yold=y; 51 delay(50); 52 }
Lines 10 and 11 define the objects that we will use to address the display (tft
) and the sensor (mpu
). The setup()
function starting in line 13 initializes all of the objects, with the calcOffsets()
method to calibrate the sensor. After starting the program, it will take a while for the values to settle. The variable definitions for the measured value of the X and Y axis then follow (line 21). The xold
and yold
variables are used to determine whether the values changed since the last measurement. If so, the program updates the contents of the display. This procedure reduces the volume of data sent to the display to the required minimum.
The loop()
function calls the mpu.update()
method to start a measurement (line 24). Then the mpu.getAngleX()
and mpu.getAngleY()
methods read the measurement values. The library returns floating-point values, which the floor()
function converts to integers. Some sensor accuracy is lost in the process, but as a positive side effect, the values will not fluctuate as much. At this point, there are certainly many solutions that offer more accurate results. But for our spirit level, this simple procedure is perfectly OK. Just bear in mind that with more overhead you can tickle more accurate values out of the sensor.
The commands in the if
statement starting in line 27 update the display. What is important to note here is that the program always drops new display information on top of the existing information. This means that you can't read older display information after some time because everything is filled up. Erasing the entire display before each write isn't a good idea either because it causes annoying flickering.
The solution is to selectively edit the areas that change. The command
<C>tft.fillRect(0,142,160,30,ST77XX_<C><C> BLACK)<C>
in line 39 erases just the part where the measured values are shown. Parts that remain constant, such as the heading, are overwritten by the program. Apart from this, the code in this section is pretty much self-explanatory. If in doubt, consult the library documentation [5].
At the end of the loop()
function starting in line 40, the program copies the values of the x
and y
variables to xold
and yold
. This is followed by a delay()
to the flow before the program starts the next round of the loop. A YouTube video [10] shows you how the program works.
Conclusions
This article shows how to build a digital spirit level with simple materials. All in all, there are still many options for increasing the sensor's accuracy. A 3D printed housing for the technology would massively improve the stability of the setup.
The sample program, which is fairly simple, makes it easy to implement quite a bit more accuracy and functionality. The combination of ESP32 and the display allows for many more interesting projects to be built. All told, this is a neat project that you can learn a great deal from. I hope you makers out there enjoy building your personal spirit level.
Infos
- MPU6050 module: https://www.az-delivery.de/en/products/gy-521-6-achsen-gyroskop-und-beschleunigungssensor
- Data sheet for MPU6050: https://invensense.tdk.com/wp-content/uploads/2015/02/MPU-6000-Datasheet1.pdf
- NodeMCU ESP32 development kit: https://www.az-delivery.de/en/products/esp-32-dev-kit-c-v4
- 1.8 inch TFT color display: https://www.az-delivery.de/en/products/1-8-zoll-spi-tft-display
- Adafruit GFX library: https://learn.adafruit.com/adafruit-gfx-graphics-library
- Adafruit GFX library (Quellen): https://github.com/adafruit/Adafruit-GFX-Library
- TFT LCD library: https://www.arduino.cc/en/Reference/TFTLibrary
- Adafruit ST7735 library: https://github.com/adafruit/Adafruit-ST7735-Library
- Source code for this article: ftp://ftp.linux-magazine.com/pub/listings/MakerSpace/
- Spirit level in action [in German]: https://youtu.be/ib4KpretMa8:
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
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.
News
-
PipeWire 1.0 Officially Released
PipeWire was created to take the place of the oft-troubled PulseAudio and has finally reached the 1.0 status as a major update with plenty of improvements and the usual bug fixes.
-
Rocky Linux 9.3 Available for Download
The latest version of the RHEL alternative is now available and brings back cloud and container images for ppc64le along with plenty of new features and fixes.
-
Ubuntu Budgie Shifts How to Tackle Wayland
Ubuntu Budgie has yet to make the switch to Wayland but with a change in approaches, they're finally on track to making it happen.
-
TUXEDO's New Ultraportable Linux Workstation Released
The TUXEDO Pulse 14 blends portability with power, thanks to the AMD Ryzen 7 7840HS CPU.
-
AlmaLinux Will No Longer Be "Just Another RHEL Clone"
With the release of AlmaLinux 9.3, the distribution will be built entirely from upstream sources.
-
elementary OS 8 Has a Big Surprise in Store
When elementary OS 8 finally arrives, it will not only be based on Ubuntu 24.04 but it will also default to Wayland for better performance and security.
-
OpenELA Releases Enterprise Linux Source Code
With Red Hat restricting the source for RHEL, it was only a matter of time before those who depended on that source struck out on their own.
-
StripedFly Malware Hiding in Plain Sight as a Cryptocurrency Miner
A rather deceptive piece of malware has infected 1 million Windows and Linux hosts since 2017.
-
Experimental Wayland Support Planned for Linux Mint 21.3
As with most Linux distributions, the migration to Wayland is in full force. While some distributions have already made the move, Linux Mint has been a bit slower to do so.
-
Window Maker Live 0.96.0-0 Released
If you're a fan of the Window Maker window manager, there's a new official release of the Linux distribution that champions the old-school user interface.