This is only a preview of the November 2024 issue of Silicon Chip. You can view 46 of the 112 pages in the full issue, including the advertisments. For full access, purchase the issue for $10.00 or subscribe for access to the latest issues. Articles in this series:
Items relevant to "Variable Speed Drive Mk2, Part 1":
Items relevant to "Surf Sound Simulator":
Items relevant to "JMP014 - Analog pace clock & stopwatch":
Items relevant to "JMP013 - Digital spirit level":
Items relevant to "FlexiDice":
Items relevant to "0.91-inch OLED Screen":
Articles in this series:
Items relevant to "3D Printer Filament Dryer, Part 2":
Purchase a printed copy of this issue for $13.00. |
Mini Projects #013 – by Tim Blythman
SILICON CHIP
Digital Spirit Level
Here’s an easy project that’s
really on the level! Using a
digital accelerometer module
and a bright LED display, it
indicates its tilt in either
degrees or percent. It’s
powered via a USB cable (eg,
from a USB battery bank).
› Tilt angle display: -99° to +99°
(1° resolution)
› Gradient display: -99% to +99% (1%
resolution; equivalent to -44.5° to +44.5°)
› Power: 5V DC via micro-USB socket
› Other features: an error message is shown if the Level is not
aligned correctly
A
device like a spirit level is a handy
tool for knowing whether things
are level or not. It can also tell you how
far something is from being level. For
example, we recently had to install
shelves on a sloping floor. We used a
spirit level to adjust the height of each
foot so that they were stable and round
items wouldn’t roll off them.
We thought a Duinotech Arduino
Compatible 8×16 LED Matrix display
(Jaycar XC3746) would make a great
base for attaching a few other parts
to build a Digital Spirit Level. It has
mounting holes in the corners, so you
attach this Level to a piece of equipment to check if it’s level or not (or
screw it to a long piece of straight timber to make it more accurate).
If you’re driving off-road, it’s important to know that your vehicle isn’t
tilting beyond its abilities. You could
mount this Level on the dash or near
the rear-view mirror to make it easy
to tell. Knowing that your caravan is
parked on level ground will help you
sleep at night, since you’re less likely
to fall out of bed!
In a vehicle, you could power it from
a car phone charger, while if you want
to make it portable, it can easily run
off a USB battery bank. You can see
a video of the Level in operation at
siliconchip.au/Videos/Digital+Level
Circuit details
The Level is made from three modules and one pushbutton, wired
Parts List – Digital Level (JMP013)
1 Leonardo Tiny main board [Jaycar XC4431]
1 I2C Tilt Sensor module [Jaycar XC3732]
1 16 × 8 LED Display module [Jaycar XC3746]
1 2-pin through-hole tactile pushbutton switch [Jaycar SP0611]
1 5cm length of double-sided foam tape [Jaycar NM2821]
1 micro-USB cable to suit Leonardo Tiny [Jaycar WC7724]
30cm of wire in various colours
(display module wiring harness can be cut up if required)
siliconchip.com.au
Australia's electronics magazine
together as shown in Fig.1. The Leonardo Tiny main board connects to a
tilt sensor module, which incorporates an MMA8452Q three-axis accelerometer. By sensing the acceleration
due to gravity, it can tell which way
is down and thus how far from level
the module is.
The accelerometer is a digital IC that
is controlled by and sends data over
an I2C serial bus. An I2C bus requires
two lines plus ground, so it connects
to the SDA (serial data) and SCL (serial
clock) pins of the Leonardo Tiny.
There is also a tactile switch connected
between the A1 pin and ground. The
main board applies a pullup to the pin
to sense when the button is pressed,
which shorts that pin to ground.
The LED matrix display module is
also marked with SDA and SCL, but
it doesn’t actually use the I2C protocol. Thus, we have connected it to
different pins, which we can directly
drive to produce its somewhat peculiar protocol. This sort of strategy is
often called ‘bit-banging’, where I/O
pins are driven manually high and
low as needed to generate the required
sequences.
November 2024 63
Fig.1: follow this wiring diagram to connect the
modules and switch. The SDA and SCL wires for the
tilt sensor must be soldered to the underside of the
Leonardo, while the remaining wires can be attached
from above.
In short, the AIP1640 chip in the
LED module expects 8-bit bytes with
their least significant bit first, while I2C
uses 9-bit bytes (as it adds an acknowledgment bit) and sends them with their
most significant bit first.
The first byte that is expected by the
AIP1640 chip is a command, while I2C
devices expect to see an address first
(allowing multiple devices to coexist on the same bus). Using the same
pair of pins to communicate with both
devices thus risks triggering unwanted
actions, so we have kept them separate.
Luckily, we had enough spare pins on
the Leonardo Tiny to do that.
Construction
We’ve used the large back surface of
the LED module as a convenient place
to mount the other modules. It’s a bit
fiddly to put together, but it makes for
a tidy final package.
Start by removing the header pins
from the tilt sensor module, if it came
with them fitted. We found it easiest
to start by bending the pins straight.
Apply heat to the underside of each
pin and pull it upwards; it should
slide right out of the plastic shroud.
Use some solder-wicking braid (with
a bit of extra flux, if you have some)
to remove any stray blobs or lumps
of solder.
For wiring, we used some coloured
solid-core wire we had on hand, but
the LED module also comes with a
harness we do not need, so you could
scavenge some wire from that.
Solder the tactile switch between
the GND (−) pad and the A1 pad of
the Tiny. Trim any excess lead length
so they do not protrude below the bottom of the PCB.
Then solder some insulated wires
to the SDA and SCL pins on the
underside of the Leonardo Tiny. These
are the white (SCL) and light blue
(SDA) wires in our photos. Attach the
Leonardo Tiny and tilt sensor to the
back of the LED Module using some
lengths of double-sided tape, being
sure to keep the tilt sensor’s edge parallel to the LED Module’s edge.
We placed them in the corners of the
module, but you might like to move
them down slightly to make the top
mounting holes more accessible. Be
sure to use foam-backed tape so there
is no chance of the boards shorting
together where they touch, particularly the wires on the back of the Leonardo Tiny.
Solder the other ends of the white
(SCL) and light blue (SDA) wires to
the corresponding pads on the tilt
sensor. Next, run the red wires seen
in the photo. They connect the Leonardo Tiny + pad to the Vcc pad on
both modules.
Similarly, the black wire in the photos connects the Leonardo Tiny – pad
to GND on both modules. Now solder
a wire (yellow in our photos) from D11
on the Leonardo Tiny to SCL on the
LED module. A dark blue wire then
goes from D10 on the Leonardo Tiny
to SDA on the LED module.
Software
The software is simple enough. A
library is used to communicate with
the tilt sensor and gather data from it.
It is processed to display a reading in
degrees, or a percentage gradient, on
the LED module. When the pushbutton is pressed, the display blanks and
changes modes.
The software is compiled and
uploaded using the Arduino IDE,
We found it
best to lay
it out in this
fashion, with
the switch and
the white and
light blue wires
attached to
the Leonardo
Tiny before it
is stuck to the
LED Module.
The remaining
wires can then
be soldered to
complete the
circuit.
64
Silicon Chip
Australia's electronics magazine
siliconchip.com.au
Silicon Chip
Binders
REAL
VALUE AT
$21.50*
PLUS
P&P
The display will show a percent symbol when a gradient is being shown. A
100% gradient is the same as a 45° angle.
An arrow is shown to the left of the numerical display to show whether the slope
is upwards or downwards. It is not shown when the displayed value is 0° or 0%.
which can be downloaded from www.
arduino.cc/en/software You will
also need the SparkFun MMA8452Q
library, which can be installed by
searching for “SparkFun MMA8452Q”
in the IDE’s Library Manager.
We’ve also included a copy of version 1.4.0 of the library in the software downloads (siliconchip.au/
Shop/6/462). The library file for the
LED module (XC3746.h) is included
in the sketch folder. You can use this
for your own projects by simply copying it to another sketch folder.
Connect the Leonardo Tiny to your
computer using a USB cable, then
choose the Leonardo board profile in
the IDE and set its serial port. After
that, you can upload the sketch. The
LED module should light up with a
splash screen and then display an
angle in degrees.
If you want to change the default
startup mode to percentage gradient, change the code to initialise the
dispMode variable as ‘true’ instead of
‘false’ and reupload the sketch.
If you see the message ERR flashing
on the LED display, the sketch cannot communicate with the tilt sensor.
Pressing the tactile switch (attached
siliconchip.com.au
to A1) when the message is flashing
should reset the Leonardo Tiny so that
it tries again.
If the ERR message is shown but not
flashing, ensure the Level is strictly
vertical. If that is not the case, errors
can creep into the calculations, so the
Level presents a warning instead of
producing erroneous values.
During regular use, pressing the
button will cause the display mode
to change between degrees and gradient. The up or down arrow indicates
whether the left-hand end of the Level
is higher or lower than the right.
Completion
Place the Level on a horizontal surface and carefully adjust the tilt sensor until the Level reads 0°. If you
like, cut a piece of card the same
size as the LED module and use double-sided tape to stick it to the back of
the Level. Remember to cut out a hole
for the switch.
You could also use some tape to
stick the Level to a vehicle or a length
of straight timber.
Be sure the vehicle or timber is level
and place the Level so it reads 0° or
0% before securing it.
SC
Australia's electronics magazine
Are your copies of
Silicon Chip getting
damaged or dog-eared
just lying around in a
cupboard or on a shelf?
Can you quickly find a
particular issue that you
need to refer to?
Keep your copies
safe, secure and
always available with
these handy binders
These binders will protect your
copies of S ilicon C hip . They
feature heavy-board covers, hold
12 issues & will look great on your
bookshelf.
H 80mm internal width
H Silicon Chip logo printed in goldcoloured lettering on spine &
cover
Silicon Chip Publications
PO Box 194
Matraville NSW 2036
Order online from www.
siliconchip.com.au/Shop/4
or call (02) 9939 3295
and quote your credit card
number.
*see website for delivery prices.
November 2024 65
|