The inspiration for this instructable is the DIY Digital Spirit Level found here by GreatScottLab. I liked this design, but wanted a bigger display with a more graphical interface. I also wanted better mounting options for the electronics in the case. Ultimately, I used this project to improve on my 3D design skills (using Fusion 360) and to explore new electronic componentry.
DigiLevel will provide feedback on whether a surface is level - both along the x-axis (horizontal) and y-axis (vertical). Degrees from level are shown, as well as a graphical representation on a 2 axes chart. In addition, the battery level is shown, and the current temperature in Fahrenheit or Celsius is shown (as reported by the accelerometer chip). This is minimal audible feedback - an initial tone to verify power, and then a double tone any time the level is moved from a non-level position to a level position.
I've provided detailed instructions on how you can make this digital level, but feel free to extend and modify on my design, much as I did on the DIY Digital Spirit Level.
Step 1: Materials
Following are the materials used in putting together this Digital Level. Most of the buying links are for multiple pieces, which are typically cheaper than buying the individual components. As an example, the TP4056 chip comes with 10 pieces for $9 (less than $1/TP4056), or it can be bought individually for $5.
- TP4056 Li-Po battery charger (Amazon - http://a.co/d/eKVM6PE)
- LSM9DS1 accelerometer (Amazon - http://a.co/d/9rqVIgh)
- Arduino Nano (Amazon - http://a.co/d/fTfFmSA)
- 128x64 OLED LCD Display (Amazon - http://a.co/d/cPbYohJ)
- Piezo speaker (Amazon - http://a.co/d/bHIjlQO)
- 3.7V Li-Po battery (Amazon -
- M2 pan head self-tapping screws - 4 M2x4, 6 M2x6, and 6 M2x8 screws are needed (eBay - https://ebay.us/elpD0A)
- Slide switch (Amazon - http://a.co/d/9JGSyHE)
With the exception of the screws, the links provided will take you to Amazon. Nearly all of these items, however, can be purchased on eBay or direct from China at a significant discount. Just keep in mind that ordering from China can result in long lead times (3-4 weeks is not unusual).
Note also that there are alternatives for many of these components. For instance, you could substitute a different accelerometer for the LSM9DS1 (such as the MPU-9205). You could replace the Arduino Nano by using any Arduino-compatible processor with the proper GPIO pins.
In particular, the LSM9DS1 is one I bought on sale at Sparkfun for less than $10, but it is normally higher priced; the MPU-9025 (http://a.co/d/g1yu2r1) provides similar functionality at a lower price.
If you make a substitution, you will likely need to modify the case (or at least how you mount the component in the case) and you will likely need to modify the software to connect to the alternative component. I do not have those modifications - you will need to research and update as appropriate.
Step 2: Wiring Diagram
The wiring schematic details how the various electronic components are wired to each other. Red lines represent positive voltage while black lines represent ground. Yellow and green lines are used for data signals from the accelerometer and to the OLED LCD display. You'll see how these components are wired together in the following steps.
Step 3: Make the Case
If you have a 3D printer, the case can be printed fairly easily. The STL files included in this Instructable. If you don't have a 3D printer, you can upload the STL files to a 3D printer bureau (such as this one) and have them printed for you.
I printed mine with no brim or raft (and no supports) and 20% infill, but you can print yours however you're used to printing. Each piece should be printed separately, laying flat. You may need to rotate it 45 degrees to get it to fit the printer bed. Mine was printed using a Monoprice Maker Select Plus with a bed size of 200 mm x 200 mm - each piece took about 12 hours to print. If you have a smaller bed, it may not fit. Scaling is not recommended as the mounts for the electronic components will then not be scaled appropriately.
Step 4: Wire the Components to a Breadboard to Verify Connectivity (optional)
I strongly recommend wiring the primary components to a breadboard to verify connectivity before proceeding with the mounting of the components inside the case. You can download the software to the Arduino Nano (see the next step), and to verify that the OLED LCD display is correctly wired and is operational, and that the accelerometer has been correctly wired and that it is reporting its data to the Arduino Nano. Also, this can be used to verify the operation of the optional piezo speaker.
I did not connect the battery and charger to the breadboard at this stage - connecting the switch to control the battery is done after you mount the switch to the case. The last picture shows how this looks prior to wiring.
Step 5: Download the Software to the Arduino Nano
The software is loaded to the Arduino Nano using the Arduino IDE. This can be done at any time during the process of building the DigiLevel, but is best done when the components have been wired using a breadboard (see the previous step) to verify correct wiring and operation of the electrical components.
The software requires that 2 libraries be installed. The first is the U8g2 library (by oliver) - you can install this by clicking on 'Sketch -> Include Library -> Manage Libraries...' in the Arduino IDE. Search for U8g2 and then click on Install. The second library is the Sparkfun LSM9DS1 library. You can get instructions on how to install that library here.
After the library specifications, the software has a setup section and a main processing loop. The setup section initializes the accelerometer and the OLED LCD display, and then displays a startup screen prior to showing the main display. If a speaker is connected, it will play one beep on the speaker to signify the power on status.
The main processing loop is responsible for reading the accelerometer, obtaining the x and y angles and then displaying the values as a set of absolute numbers and also pictorially on a graph. The temperature reading from the accelerometer is also displayed (in either Fahrenheit or Celsius). If the level was previously non-level, when it returns to level it will generate two beeps on the speaker (if connected).
Finally, the voltage from the battery is obtained to determine and display the current battery level. I don't know how accurate this code is, but it is accurate enough to show a full battery and the gradual draw down of the battery level during use.
Step 6: Mount and Wire the OLED Display and Piezo Speaker
The 1.3" OLED display (128x64) mounts to the top half of the case using 4 M2x4 pan head self-tapping screws. I suggest you connect your wires to the the display prior to mounting. This ensures that you can see how the pins are labeled as you are connecting the wires. Once the display is mounted, you won't be able to see the labels for the pins. You'll notice that I added a label to the back side of the display so that I could remember the pin values (since I didn't do this the first time and I wired it incorrectly...).
The speaker is used to emit a brief tone when the Digital Level is turned on to verify that the battery is good and that it's operational. It also emits a double tone whenever the level is moved from a non-level position to a level position. This is to provide an audible feedback as you're positioning the level or whatever the level is on. It is mounted to the top half of the case using 2 M2x4 pan head self-tapping screws. You do not need a speaker - the DigiLevel will operate just fine without it, however you will be missing any audible feedback.
Step 7: Mount and Wire the Battery, Battery Charger, and Switch
The switch needs to be mounted to the case prior to connecting it to the battery. This is because if you wire it first, you won't be able to mount the switch without disconnecting it. So mount the switch first, then mount the pre-wired TP4056 and Li-Po battery, then complete the wiring to the switch.
The TP4056 has 4 wiring pads: B+, B-, Out+, Out-. You will want to wire the battery to the B+ (positive voltage) and B- (ground) connections. The Out- connection is used for the ground that will go to the Arduino Nano, and the Out+ is connected to one pin of the switch. The second pin of the switch is then wired to the VIN of the Arduino Nano.
My soldering job is not the best - I like to use heat-shrink tubing to cover and insulate the soldered joint. You'll notice that on one of the soldered connections here, the heat-shrink tubing was impacted by the heat of the soldering and it shrunk before I was able to move it.
Step 8: Mount and Wire the Accelerometer
The accelerometer (LSM9DS1) is mounted in the middle of the bottom half of the case. There are 4 pins to be wired: VCC goes to the V5 pin on the Arduino Nano; GND goes to ground; SDA goes to the A5 pin on the Arduino Nano; and SCL goes to the A4 pin on the Arduino Nano.
I've used jumper wires with Dupont connectors for wiring, however you can solder the wire directly to the pins if you prefer. If you solder the wires directly to the pins, you will probably want to do this before mounting the accelerometer chip to make it easier.
Step 9: Complete the Electronics by Wiring the Arduino Nano
Final wiring is done by connecting all of the electrical components to the Arduino Nano. This is best done prior to mounting the Arduino Nano so that the USB port is accessible for calibration and any other last minute software changes.
Start by connecting the switch to the Nano. The positive lead (red) goes from the switch to the VIN pin of the Nano. The negative lead (black) from the battery will go the GND pin on the Nano. There are two GND pins on the Nano and all four electrical components have a ground wire. I chose to combine the two grounds on the bottom of the case into one lead wired to one of the GND pins. The two grounds from the top of the case I combined into one lead wired to the other GND pins.
The accelerometer (LSM9DS1) can be connected to the Nano by connecting the VDD pin on the accelerometer to the 3V3 pin on the Nano. Do NOT connect this to the 5V pin or you will damage the accelerometer chip. Connect SDA to the A4 pin on the Nano, and SCL to the A5 pin on the Nano. The GND pin goes to the GND pin on the Nano (combined with the negative lead from the battery).
The OLED LCD display can next be connected to the Nano by connecting the VCC pin on the display to the 5V pin on the Nano. Connect SDA to the D2 pin on the Nano, and SCL to the D5 pin on the Nano.
Finally, the speaker can be connected by connect the red wire (positive) to the D7 pin on the Nano. The black wire goes to GND along with the GND of the OLED LCD display.
Step 10: Calibration
Once the software is downloaded, and prior to mounting the Arduino Nano, you may need to calibrate your level. Ensure that the accelerometer board has been mounted. Mounting it with the screws should result in a level board, however if it is slightly off for any reason, calibration will ensure a correct display.
Put the bottom case on a surface that is known to be level (using a bubble level or some other means). Read the displayed values for X and Y. If either is non-zero, you will need to update the software with the calibration amount. This is done by setting either the xCalibration variable or the yCalibration variable to the appropriate amount (what's displayed).
// Set these variables with initial values as appropriate
bool displayF = true; // true for Fahrenheit, false for Celsius
int xCalibration = 0; // calibration amount for leveling out the x-axis
int yCalibration = 0; // calibration amount for leveling out the y-axis
long irvCalibration = 1457; // calibration amount for internal reference voltage
At this time, you should also set the value of displayF to the appropriate setting depending on whether you want the temperature displayed in Fahrenheit or Celsius.
Reloading the software onto the Nano should now result in a 0/0 reading on a known-level surface.
Step 11: Mount the Arduino Nano and Assemble the Case
Once calibration is completed, you can mount the Arduino Nano in the case by applying hot glue to the rails and placing the Arduino Nano on these rails, with the pins facing up and the USB port facing the interior of the case.
The case containing all of the electronics can now be assembled by putting the two halves together and using 4 M2x8 pan head self-tapping screws.
Step 12: Verify the Operation of Your New Digital Level
Make sure the Li-Po battery is charged. If the case is assembled, you won't be able to see the charging LED indicators directly. If you want to verify the charging operation by viewing the charging lights directly you will need to open the case, however you should be able to see the red glow indicating that charging is occurring with the case closed.
Once charged and assembled, turn on the Digital Level and verify its operation. If it is not working, the two likely trouble points are the wiring for the OLED LCD display and the wiring for the accelerometer. If the display is not showing anything, then start with the OLED LCD wiring. If the display is working, but the H and V labels both show 0 and the temperature is 0 (C) or 32 (F), then the accelerometer is probably not wired correctly.
Step 13: Final Thoughts...
I put together this digital level (and the Instructable) primarily as a learning experience. It was less important to me to make a functioning level as it was to explore the various components and their capabilities, and then put them together in a way that adds value.
What improvements would I make? There are several I'm considering for a future update:
- Expose the USB port of the Arduino Nano through the case by modifying the way it's mounted. This would allow for easier updates to the software (which in any case should be rare).
- 3D print the case using a wood filament. I've been experimenting with Hatchbox Wood filament and I'm very pleased with the results I've gotten. I think this would provide a better overall look to the DigiLevel.
- Update the design to use the MPU-9250 accelerometer to lower the cost while not impacting the function.
This is my first instructable and I welcome feedback. While I have tried to avoid it, I'm sure that this still has a more US-centric perspective - so apologies for those outside of the US.
If you found it interesting, please vote for me in the First Time Author Contest. Thanks for reading through to the end!
Runner Up in the
First Time Author
RetiredG made it!