This is only a preview of the May 2024 issue of Practical Electronics. You can view 0 of the 72 pages in the full issue. Articles in this series:
Articles in this series:
Articles in this series:
Articles in this series:
Articles in this series:
Articles in this series:
|
Max’s Cool Beans
By Max the Magnificent
Arduino Bootcamp – Part 17
A
few moments ago, as I pen
these words, I was conversing
with my son about – let’s say an
occurrence that had just occurred – when
I started to make the comment, ‘If there’s
one good thing to be said for any of
this…’ and he finished my sentence by
saying ‘…it’s not our fault and it’s not
our problem.’ I felt so proud. Despite
his habitual look of disinterest when
I’m discoursing, it turns out he has been
listening to me after all.
That’s interesting
I’m a digital logic designer by trade.
Although we haven’t discussed any of
this thus far in our bootcamp columns,
there are several mathematical tools
available to make the lives of digital logic
designers simpler, including Boolean
algebra, Karnaugh maps, and De Morgan
transformations. Don’t panic. We aren’t
going to go into any of these tools here.
However, if you do plan on taking a
deeper dive into electronics, it would
be a good idea to gain some familiarity
with these topics. In which case, may I
make so bold as to note that they are all
introduced in excruciating exhilarating
detail in my book, Bebop to the Boolean
Boogie – see: https://bit.ly/3u9XIWV
I often use Bebop as a reference myself.
The scary thing is that I regularly read
something in there and think, ‘Wow,
that’s interesting, I never knew that,’
before remembering that I wrote it in
the first place. Another book I keep
for reference is Logic Machines and
Diagrams by Martin Gardner (https://
bit.ly/3v7b8DJ). Martin was one of
my heroes. His Mathematical Games
That’s interesting
It’s not my problem
That’s interesting AND it’s not my problem
Fig.1. Venn diagram.
50
Fig.2. Dual breadboard setup (Source: Mike Tooley / PE).
columns appeared in Scientific American
magazine for a quarter of a century.
One of the topics covered in Martin’s
book is Venn diagrams, which typically
involve the use of circles to illustrate
simple set relationships in areas like
probability, logic, statistics and computer
science. The reason I mention this here is
that I’m bubbling over with glee because
I just spotted something that (a) relates to
what we’ve been talking about in recent
columns and (b) falls neatly into a Venn
diagram intersection of ‘that’s interesting’
and ‘it’s not my problem’ (Fig.1).
It’s not my problem
A couple of columns ago (PE, March
2024), we migrated to using a dualbreadboard setup. At that time, we noted
that it is possible to remove one or both
power and ground rail assemblies from
the top and bottom of the breadboard. Did
you pause to say to yourself, ‘I wonder
why anyone would ever want to do such
a thing?’ Well, as soon as the full March
issue of PE landed on my desk, I read it
cover-to-cover. One item of particular
interest to me was Part 1 of Mike Tooley’s
Teach-In 2024 – Learn electronics with
the ESP32. This is going to be awesome.
I’ve never found the time to play with an
ESP32, so I’m going to follow this series
with great interest.
One of the things that immediately
caught my eye was the first picture of
Mike’s breadboard setup, which was
accompanied by the caption: ‘Two
(slightly modified) breadboards provide
a convenient space for a 30-pin ESP32
development board.’ Look and see for
yourself (Fig.2). Do you spot anything
interesting and/or unusual? Jot down
a list and then we can compare notes.
The first thing I observe is the gaps in
the middle of the red and blue horizontal
lines reflecting the power and ground
rails, respectively. This indicates that the
conducting rails inside the breadboard
are themselves split. As we discussed in
my Arduinos and Solderless Breadboards
blog (https://bit.ly/3PdnqBk), these ‘splitrail’ boards make it easier to work with
circuits that require a mix of power
supplies, like 5V and 3.3V, for example.
If Mike is intending to use only a single
supply, then he will add jumpers to link
both sides of the power rails and both
sides of the ground rails.
Now look at the middle of Mike’s dualboard setup. The ESP32 is so wide that if
we tried plugging it into just one board,
there would be no free terminal holes
on one side and only one free hole next
to each of the pins on the other side. As
we see, Mike has removed the upper
power and ground assembly from the
Practical Electronics | May | 2024
H G F
I
I
J
J
60
55
5
1
5
55
60
A B C D E
F G H
I
J
0
J
I
H G F
E D C B A
(a) My breadboards
60
55
5
0
(b) Mike’s breadboards
Fig.3. Can you spot the difference?
lower board before connecting the boards
together. This allows his ESP32 to span
the two boards while still leaving two
free breadboard terminal holes next to
each of its pins.
Thus far, everything reflects what we’ve
discussed in our earlier columns. I’ve
saved the best (or worst, depending on
your point of view) for last. Suppose you
are playing a remote game of chess. If it’s
your opponent’s turn and they send you
a message saying ‘Qf3,’ then you know
they’ve just moved their queen to the
square located on file (column) f and row
3. The reason you know this is because
everyone who plays chess has adopted
a standard notation for describing and
recording the moves.
Similarly, there are occasions when
it may be advantageous to be able to
document instructions for populating a
breadboard with components and wires
using some form of standard notation
along the lines, ‘Connect a 150Ω resistor
between locations (x1,y1) and (x2,y2)’
or ‘Connect a wire between locations
(x3,y3) and (x4,y4).’ But what form of
notation should we use to identify our
(x,y) values?
Suppose you were the inventor of
the original breadboard. One approach
might be to use the letters A through
E and F through J to identify the rows
while also employing numbers to
identify the columns. Well, that seems
to be simple enough. I mean, seriously,
how complicated could it be? More
complicated than you might think.
Let’s start with the breadboards I’ve
been using (Fig.3a). As we see, the letters
A through J are associated with the rows
Practical Electronics | May | 2024
Preparing to rock and roll
Just to set the scene, let’s remind ourselves
that we now have two common-cathode
7-segment displays in play. In addition
to their ground pins (their common
cathodes), each display has eight segment
(anode) pins (the decimal point counts
as a segment), so 16 anode pins in all.
As we noted in our previous column
(PE, April 2024), the Arduino Uno has
14 digital I/O pins numbered 0 to 13.
We are reserving pins 0 and 1 for UART
communications with our host computer,
which leaves 12 pins for us to work with.
The problem is that our dual displays
require 2 x 8 = 16 pins. This is a bit of a
poser. A conundrum, some might say.
The solution we decided to adopt (at
D1
D0
11
10
Upper Breadboard
1
From Current-Limiting Resistors
BC377
E3
1C
Lower Breadboard
5
E D C B A
55
H G F
E D C B A
60
at each end. Assuming we have the red
power rail on the top (which is how I
prefer to work), then the columns are
numbered from 1 on the right to 63 on
the left.
Personally, I would have preferred
the numbers to start with 1 on the left
and increment as we move to the right.
Of course, they do this if we rotate the
board 180° clockwise (or anticlockwise,
for that matter), but then we would have
the ground rail on top, which would cause
me to wear my frowny face.
Now look at Mike’s breadboards (Fig.2
and Fig.3b). In this case, the count starts
at 0, which actually makes good sense
on several levels. However, for reasons
unknown (unless it’s to vexeth us greatly),
rather than associating the 0 with the
leftmost or rightmost column, all the
boards I’ve seen like this have the 0
positioned halfway between the second
and third columns, thereby requiring us
to count back from column 5 to determine
with which column the 0 is related. Also,
this leads us to the realisation that the
leftmost column is numbered –1. Color
me confused.
It gets worse. If you look at row A in
the upper left of one of Mike’s boards
and follow it horizontally to the right, it
ends up as row J. Similarly, if you look at
column 0 in the upper left of the board
and follow it vertically to the bottom, it
ends up as column 61.
Other than having rotational symmetry,
which benefits us not at all, this annotation
scheme has nothing whatsoever to
recommend it. For example, if you are
using a board as shown in Fig.3a, there
is no ambiguity if you are instructed to
‘Connect a wire between B3 and G60.’ By
comparison, following this instruction is
more than problematic if your board is
as shown in Fig.3b because there are two
locations that can be addressed as B3 and
two more that can be referenced as G60.
Happily, if there’s one good thing to
be said for any of this… it’s not my fault
and it’s not my problem!
2B
From Arduino
Fig.4. Controlling the displays with transistors.
least, for the moment) is to use eight
of the Arduino’s pins (pins 2 to 9) to
drive both displays in parallel (sideby-side). Also, for each display, we’ve
added a BC377 transistor to control its
ground pin. Arduino pins 10 and 11
are used to control displays D0 and D1,
respectively (Fig.4).
You can remind yourself of our entire
current setup by downloading, perusing,
and pondering of our latest and greatest
incarnation (file CB-May24-01.pdf).
As usual, all of the files that are mentioned
in this month’s column are available from
the May 2024 page of the PE website:
https://bit.ly/pe-downloads
As one final reminder, our BC377
transistors are of a type known as NPN.
The way we are using these transistors
in this circuit, we can think of them as
acting like switches. In the case of an
NPN transistor, applying 0V (LOW) to
its control terminal (the base, B) will
turn the transistor off, which means it
will act like an open switch. In turn, this
means the ground pin on the display can’t
activate any of the light-emitting diode
(LED) segments, even if the Arduino is
trying to light them up.
By comparison, applying 5V (HIGH)
to the transistor’s base will turn it on,
which means it will act like a closed
switch between its collector (C) and
emitter (E) terminals. This will effectively
connect the display’s ground pin to the
breadboard’s ground (0V) rail, which
means any of its segments being powered
by the Arduino will light up.
Firing up our two displays
This bit is very important. Since our two
displays are wired in parallel, we must
51
Listing 2a. The setup() function for first test.
ensure that only one of them is active at
any time. If both displays are active at
the same time, our Arduino will be very
unhappy. In a crunchy nutshell, this
means we must never drive the Arduino’s
10 and 11 pins HIGH at the same time.
As I’ve noted on many previous
occasions, it’s almost invariably a good
idea to take things one step at a time,
verifying that any new additions to our
system work in isolation before bringing
everything together. It’s a lot easier to
debug things when there are only a
limited number of things that can go
wrong (as I just found to my cost).
Let’s start by creating a little program
that commences with both our D1 and D0
displays being turned off. The program
will then repeatedly count from 0 to
9, first presenting the value on D1 for
one second, then turning D1 off and
presenting the same value on D0 for
one second, then turning D0 off and
proceeding to the next value.
Listing 2b. The loop() function for first test.
52
Take a few moments to think about
how we might implement this. Maybe
you could create your own test program
and then compare your approach to my
solution (file CB-May24-02.txt).
As usual, we won’t go through the entire
program line-by-line here. Instead, we
will focus only on the interesting bits.
Note that, from our earlier programs,
NUM_SEGS (‘number of segments’) and
NUM_DIGITS (‘number of digits’) are
defined as 8 and 10, respectively. Also,
ON_OFF_TIME is defined as 1000 (one
thousand milliseconds, or one second).
Last, but not least, PinTrD0 and PinTrD1
are integer variables that we’ve associated
with the Arduino pins we’re using to
drive our two transistors.
Lert’s start with the setup() function
as seen in Listing 2a. The first thing we do
on Lines 39 through 42 is tell the Arduino
that the two pins driving our transistors
are of type OUTPUT and set them driving
LOW values. Next, on lines 45 through 48
we tell our Arduino that the pins we are
using to drive the segments in our displays
are of type OUTPUT. Finally, on Line 49
we call our trusty old DisplaySegs()
function to turn all the segments off.
To be honest, line 49 is superfluous
to requirements because we already
know that the transistors controlling
the displays are turned off, but it never
hurts to make sure.
The interesting part of this program takes
place in the loop() function, as seen in
Listing 2b. We start on Line 56 with a for()
loop that counts from 0 to 9. On line 58
we use our DisplaySegs() function to
apply power to the segments associated
with the current digit on both displays.
The important thing to note here is
that, even though we are applying power
to the segments, nothing will light up
because the transistors associated with
both displays are currently off.
On Lines 61 through 63, we turn display
D1 on, wait one second, and turn it off
again. On Lines 66 through 68, we turn
display D0 on, wait one second, and turn
it off again. Then we move onto the next
count in our for() loop and do it all
again. Once we’ve displayed all the digits
0 through 9, the for() loop terminates,
at which point we proceed to the next
iteration of the main loop() function.
So, there you have it. Easy peasy lemon
squeezy. What could possibly go wrong?
I’m so glad you asked…
Arrrggghh
When I set my program running, it
appeared to be functioning 98% (give
or take) as planned. How did I come up
with this particular percentage? Well, as
Homer Simpson famously said, ‘People
can come up with statistics to prove
anything; 40% of all people know that.’
The bottom line is that everything
worked as expected except… whenever
Fig.5. Welcome to the real world (we hope you’ll enjoy your stay).
Practical Electronics | May | 2024
To Display D1
To Display D0
From Arduino
Fig.6. Ah! There’s the little scamp!
display D1 was activated, segment A was on for every digit.
Even more puzzling, it was noticeably dimmer than any of the
other segments on either display. ‘Oh dear,’ I said to myself
(or words to that effect).
Why am I telling you about this? Well, it’s not uncommon for
things to fail to function as planned, so you’ll probably run into
something like this yourself at some stage in your experiments.
Hopefully, my problem, and the investigation thereof, will
provide a ‘teachable moment,’ as they say. You’re welcome.
The first thing I did was to swap the two 7-segment displays.
The problem remained with segment A on the new D1 display.
Next, I unplugged the end of the blue wire driving pin 7
(segment A) of D1, I unplugged the end of the purple wire
driving pin 7 (segment A) of D0, and I connected them into
the other’s display (any references to the colours of wires
relate to my illustration in file CB-May24-01.pdf). Now, the
always-on, low-intensity segment A appeared on D0. ‘Hmm,’
I thought to myself.
I tried swapping out the jumper wire driving the errant
segment. No change. Next, I swapped out the resistor driving
the wire connected to the misbehaving segment (plus I used
my multimeter to confirm that the original resistor’s value was
as it should be). No change.
To Display D0
To Displays D0 & D1
Good grief!
I replied that each segment in our 7-segment displays requires
a current-limiting resistor. So… eight segments a display… two
displays… sixteen resistors. Joe responded that he agreed… in
principle. However, he then pointed out that since only one
of our displays is going to be on at any time, we can therefore
make do with only a single set of resistors.
O-M-G. I feel like an old fool (but where are we going to
find one at this time of the day?). What this means is that we
can remove one set of resistors and simplify our wiring from
its current layout (Fig.7a) to a more minimalist interpretation
(Fig.7b). Just to make sure we’re all tap-dancing to the same
skirl of the bagpipes; I’ve updated our master drawing (file
CB-May24-03.pdf).
Hang on. Maybe I should have mentioned all this at the
beginning of this column before you implemented our original
wiring scheme yourself. Oh well, it’s too late now. Once again,
we can consider this to be a teachable moment. And, once
again, you’re welcome.
Both displays together
When we eventually get to present the time on our clock,
we don’t want to display the most-significant digit (MSD)
on display D1 followed by the least-significant digit (LSD)
on display D0 with only one digit visible at a time. What we
really want is to have both of our displays presenting values
at the same time.
Lower Breadboard
Upper Breadboard
To Display D1
I also checked that none of my resistor leads were touching
each other, and I rechecked all my connections and wiring.
Everything appeared to be OK. Eventually I did what I always
do in these circumstances – I set up a Zoom call with my chum,
Joe Farr, in the UK, and we talked our way through things.
It wasn’t long until we decided that there just had to be a
problem with my wiring or – possibly – the internals of the
breadboard. The thing is that the real world (see Fig.5) looks very
different to my artistic interpretation (file CB-May24-01.pdf).
So, I went through everything step-by-step one more time. Do
you recall when I said that my wiring appeared to be correct?
It turned out I hadn’t looked closely enough. I’d plugged one
of my flying leads one hole to the right (Fig.6). Everything
looked great when viewed from every angle, except the angle
I hadn’t viewed things from. Give me strength!
As soon as I repositioned the errant wire, everything started
to function as expected. I was a happy chappy. The radiance of
my smile once more lightened the world. I was basking in the
belief that I hadn’t made any more mistakes when… Joe inquired
why I had decided to use two sets of current-limiting resistors.
98765432
(a) Original wiring
From Arduino
9 8 7 6 5 4 3 2
(b) Simplified wiring
Fig.7. One set of current-limiting resistors will suffice.
Practical Electronics | May | 2024
Listing 4a. The loop() function for second test.
53
We know we can’t actually achieve
this because – with our current setup –
only one of our displays can physically
be active at any time. However, due to
persistence of vision (the optical illusion
that occurs when the visual perception
of an object does not cease for some
time after the rays of light proceeding
from it have ceased to enter the eye),
if we swap back and forth between the
displays fast enough, we can make it
appear that both displays are presenting
values simultaneously.
As a starting point, I’ve forked our
earlier test program and made a few
teensy-weensy modifications (file
CB-May24-04.txt). In fact, the only
changes occur in our loop() function,
as illustrated in Listing 4a.
Just for giggles and grins, I’ve decided
to present the number 42, with the 4
and 2 appearing on displays D1 and D0,
respectively. As before, our ON_OFF_TIME
definition is still set to 1000, which
equates to 1000 milliseconds (ms).
So, we start by seeing the ‘4’ for one
second, then the ‘2’ for one second, and
then we do it all again. Now, let’s change
the value associated with ON_OFF_TIME
to be 100 (that’s 1/10 of a second) and
reload the program into the Arduino. In
this case, we can sort of see both digits at
the same time, but accompanied by a lot of
annoying flickering. How about we change
the value associated with ON_OFF_TIME
to be 10 (that’s 1/100 of a second).
Wow! Both digits appear to be on at
the same time without any observable
flickering (Fig.8). ‘I love it when a plan
comes together,’ as fearless frontrunner
Hannibal Smith of A-Team fame
famously said.
’42?’ I hear you say… well, just take
a look here: https://bit.ly/pe-nov21-42
Blinded by the light
Although the song Blinded by the Light
was originally written and recorded by
Bruce Springsteen, my favorite version
is the one performed by Manfred Mann’s
Earth Band, but we digress…
As we shall soon see, there are more
factors at play here than at first meet
the eye (no pun intended). Depending
on who you are talking to, our eyes and
brains retain a visual impression for
somewhere between 1/30 and 1/60 of
a second. Let’s say 1/50 second, which
equates to 20ms. Known as ‘persistence
of vision,’ this explains why we no longer
perceive any flickering in our displays,
each of which is now on for 10ms and
off for 10ms.
But wait, there’s more, because if we
look at our displays, which are currently
presenting 42, they appear to be running
at full brightness to our eyes, even though
we know that each display is active only
half of the time. How can this be?
Well, one reason is that the human
eye is incredibly sensitive over a wide
dynamic range from starlight to daylight.
Even within a single scene, the eye can
perceive a dynamic range of up to 100dB,
which equates to a brightness ratio of
100,000:1. Thus, one reason our displays
appear to be so bright is that we have
nothing to compare them to. I’m sure we
would be able to detect some difference
if we compared them to a third display
running at full brightness 100% of the
time, but that’s not an issue here.
The technical term for what we’ve been
doing with our displays is ‘multiplexing,’
which basically means that not all
displays are on at the same time. Suppose
we wished to add two more displays,
thereby allowing us to present both hours
and minutes (HH:MM) at the same time.
If we were to extend our multiplexing
technique, then each display would be
active for only 1/4 of the time.
At this point, we would probably
become aware that our displays are not
as bright as we would prefer. Also, since
each display would now be on for 10ms
and off for 30ms (assuming we stick with
our ON_OFF_TIME definition set to 10)
we may start to see some flickering.
One solution to the flickering is to
increase the refresh rate, perhaps changing
the value associated with ON_OFF_TIME
to 5ms (1/200 of a second) or even 1ms
(1/1000 of a second).
With respect to the dimming of our
multiplexed displays, one possibility is
to increase the current being fed to each
of the display segments. As a reminder,
an Arduino Uno’s pins prefer to sink or
source up to 20mA. They can handle
up to 40mA, but only for short periods
of time. Thus, if we wanted to supply
more than 20mA for sustained periods
of time, we would use the Arduino’s
Components from Part 1
LEDs (assorted colours)
https://amzn.to/3E7VAQE
Resistors (assorted values)
https://amzn.to/3O4RvBt
Solderless breadboard
https://amzn.to/3O2L3e8
Multicore jumper wires (male-male) https://amzn.to/3O4hnxk
Components from Part 2
7-segment display(s)
https://amzn.to/3Afm8yu
Components from Part 5
Fig.8. I think we’ve found the answer.
Online resources
For the purposes of this series, I’m going to assume
that you are already familiar with fundamental concepts like voltage, current and resistance. If not, you
might want to start by perusing and pondering a short
series of articles I penned on these very topics – see:
https://bit.ly/3EguiJh
Similarly, I’ll assume you are no stranger to solderless breadboards. Having said this, even if you’ve used
these little scamps before, there are some aspects to
them that can trap the unwary, so may I suggest you
feast your orbs on a column I wrote just for you – see:
https://bit.ly/3NZ70uF
Last, but not least, you will find a treasure trove of
resources at the Arduino.cc website, including example programs and reference documentation.
54
Momentary pushbutton switches
https://amzn.to/3Tk7Q87
Components from Part 6
Passive piezoelectric buzzer
https://amzn.to/3KmxjcX
Components for Part 9
SW-18010P vibration switch
https://bit.ly/46SfDA4
Components for Part 10
Breadboard mounting trimpots
https://bit.ly/3QAuz04
Components for Part 12
Light-Dependent Resistor
https://bit.ly/3S2430m
Components for Part 13
BC337 NPN Transistor
https://bit.ly/40xAgyS
Components for Part 14
HC-SR04 Ultrasonic Sensor
https://bit.ly/49AMBq4
Components for Part 15
Real-Time Clock (RTC)
https://bit.ly/3S9OjHl
Practical Electronics | May | 2024
pins to control transistors that could
handle higher current values.
When it comes to LEDs, the
relationship between current and
luminous intensity is reasonably linear;
that is, 2x the current results in 2x the
luminous intensity. So, does this mean
that 2x the current results in 2x the
brightness? That depends on whether
we are talking about actual brightness as
measured by an electronic instrument,
in which case the answer is ‘Yes,’ or
perceived brightness as seen by the
human eye, in which case the answer is
‘No.’ This is because the eye’s response
to signal intensity is non-linear.
At this point, I can imagine you saying,
‘Hang on a minute. We are using 7-segment
LEDs whose data sheets specify a ‘forward
voltage drop’ of 2V and a ‘maximum
continuous (sustained) forward current’ of
20mA. Also, we are using 150Ω currentlimiting resistors to provide our 20mA
maximum. So, how can we increase the
current?’ I’m glad you asked.
If we look more closely at the data
sheet, we may also see a ‘maximum
pulse current’ specification of 100mA, for
example (this value will have associated
qualifiers, like a maximum active duration
and a maximum duty ratio).
Let’s perform a thought experiment.
Suppose we take two segments on one
of our displays. Assume we connect a
150Ω resistor to one segment, thereby
limiting its current to 20mA, and we set
this segment running 100% of the time.
Also assume we connect a 75Ω resistor
to the second segment, thereby allowing
a current of 40mA, and we turn this
segment on and off very quickly such
that it’s active only 50% of the time.
Since the two segments’ average
currents are identical, will their perceived
brightnesses also be identical? Let’s say
‘Yes,’ because it’s almost true, and we
really don’t want to wander any further
into the weeds.
Is it a good idea to boost the current
in this way? It depends. On the one
hand, it provides a solution to a problem.
On the other hand, using high current
pulses ages LEDs and causes them to
dim faster, which may or may not be an
issue depending on the expected life of
the product.
More problematic is what will happen
if the microcontroller crashes or freezes
(locks up) with one or more of the LEDs
on. This may also occur when the user
performs some action like activating
a menu subsystem, which means the
programmer must account for all such
possibilities. In this case, the LEDs won’t
last long at all. This is the sort of thing
professional electronic engineers must
consider and design around.
Happily, this technique – boosting the
current – provides one more example
of ‘that’s interesting’ and ‘it’s not our
problem’ because we aren’t going to use it.
It’s almost time
Finally, nearly, it’s almost time for us
to start displaying the time, but first…
You may feel free to call me an old
fussy-fuddy-duddy if you will but –
before we proceed – since we’ve made
so many modifications to our prototype,
I think it’s a good idea to make sure
we haven’t &%**$#$<at> anything up
in the process (forgive my use of the
engineering vernacular).
I just loaded one of the programs we
created earlier (PE, March 2024). This
is the one that loops around reading the
date and time from our real-time clock
(RTC) and displaying these values in our
Serial Monitor (file CB-May24-05.txt).
It still works! Hurray!
The time has come!
To be honest, after all we’ve been through,
the next part may seem somewhat
anticlimactic… except for the fact that
it works, of course.
What we are going to do is fork the
final program we created in the previous
column (PE, April 2024). This was the
one that looped around reading the time
from the RTC and then displaying the
2-digit hour and 2-digit minute values
as a series of four digits on our single
7-segment display.
We are going to augment this program
with the ability to present pairs of
digits on our dual displays using the
technique we developed above. This
means we will be able to display the
two hour digits followed by the two
minute digits. The result is available
for your delectation and delight (file
CB-May24-06.txt).
All we need to know for the purpose
of these discussions is that, in addition
to ON_OFF_TIME being defined as 10,
we’ve also defined ON_TIME as 360,
Listing 6a. (above) Our new DisplayPair() function.
Listing 6b. (right) Our modified loop() function.
Practical Electronics | May | 2024
55
SML_PAUSE as 180, and BIG_PAUSE as
720. Also, we’ve added our PinTrD0 and
PinTrD1 declarations so we can control
our transistors.
The first change we’ve made is to add
a new function called DisplayPair()
as illustrated in Listing 6a. The role of
this function is to display a pair of digits
for a specified amount of time (this is
our ON_TIME). It doesn’t take long to
realise that the body of our new function
is almost identical to the contents of the
loop() function in our previous test
(Listing 4a). In fact, there are only two
significant differences.
The first difference is seen in Lines 93
through 103. As opposed to displaying
fixed values of 4 and 2 on D1 and D0,
respectively, we are instead displaying
integer variables called msd and lsd,
whose values are passed into the function.
The second difference is that, as
opposed to the original loop() function
running forever, we are using the for()
loop that starts on Line 91 and ends on
Line 104 to limit the duration of the
display to a specific number of cycles,
as defined by the integer variable
numCycles.
The value assigned to numCycles
is calculated on Line 89. From
our previous experiments, we
know we can achieve a flickerfree presentation by swapping
back and forth between displays, with
each display being on for ON_OFF_TIME
(which we’ve set to 10ms, or 1/100 of
a second). This means that a complete
display cycle (D1 then D0) will take (ON_
OFF_TIME * 2) = 20ms.
Since the total time we wish to display
this pair of digits is ON_TIME (which
we’ve defined to be 360ms), then our
numCycles will equate to 360 / 20 = 18.
The reason for doing things this way, as
opposed to simply plonking a value of
18 in our for() loop, is that it makes it
easy for us to experiment with different
values.
Next, let’s turn our attention to our
new loop() function, as illustrated
in Listing 6b. Lines 63 through 74 are
identical to the program we created in
our previous column. This is where we
read the current date and time from our
RTC and boil things down to the hour
and minute we wish to display.
As you may recall, we are using the
RTC in its default 24-hour mode, in
which the hours are numbered from 0
to 23, and then converting this into a
12-hour format, in which the hours are
numbered 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
and 11. Meanwhile, the minutes are
numbered from 0 to 59.
On Line 77 we call our new
DisplayPair() function, passing
in two arguments in the form of the
most-significant and least-significant
hours digits. On Line 78 we pause for
a short delay. On Line 81 we call our
DisplayPair() function again, this
time passing in the most-significant and
least-significant minutes digits. Finally,
on Line 82 we pause for a longer delay.
I’m sitting here watching my dual
display presenting the time in the form:
HH… MM… … … HH… MM… … …
HH… MM… … … and it looks great!
Next time…
We are getting closer and closer to creating
a 4-digit display device. As the next step
along the way, we are going to refresh our
minds as to the concept of binary coded
decimal (BCD), and then investigate
various deployments of BCD-to-7-segment
Cool bean Max Maxfield (Hawaiian shirt, on the right) is emperor of all he
surveys at CliveMaxfield.com – the go-to site for the latest and greatest
in technological geekdom.
Comments or questions? Email Max at: max<at>CliveMaxfield.com
Teach-In 8 CD-ROM
Exploring the Arduino
EE
FR -ROM
CD
ELECTRONICS
TEACH-IN 8
FREE
CD-ROM
SOFTWARE
FOR
THE TEACH-IN
8
SERIES
FROM THE PUBLISHERS OF
This CD-ROM version of the exciting and popular Teach-In 8 series
has been designed for electronics enthusiasts who want to get to
grips with the inexpensive, immensely popular Arduino microcontroller,
as well as coding enthusiasts who want to explore hardware and
interfacing. Teach-In 8 provides a one-stop source of ideas and
practical information.
The Arduino offers a remarkably effective platform for developing a
huge variety of projects; from operating a set of Christmas tree lights
to remotely controlling a robotic vehicle wirelessly or via the Internet.
Teach-In 8 is based around a series of practical projects with plenty of
information for customisation. The projects can be combined together
in many different ways in order to build more complex systems that can
be used to solve a wide variety of home automation and environmental
monitoring problems. The series includes topics such as RF technology,
wireless networking and remote web access.
PLUS: PICs and the PICkit 3 – A beginners guide
The CD-ROM also includes a bonus – an extra 12-part series based around the popular
PIC microcontroller, explaining how to build PIC-based systems.
£8.99
INTRODUCING THE ARDUINO
• Hardware – learn about components and circuits
• Programming – powerful integrated development system
• Microcontrollers – understand control operations
• Communications – connect to PCs and other Arduinos
PLUS...
PIC n’MIX
PICs and the PICkit 3 - A beginners
guide. The why and how to build
PIC-based projects
Teach In 8 Cover.indd 1
04/04/2017 12:24
PRICE
£8.99
Includes P&P to UK if
ordered direct from us
SOFTWARE
The CD-ROM contains the software for both the Teach-In 8 and PICkit 3 series.
ORDER YOUR COPY TODAY!
JUST CALL 01202 880299 OR VISIT www.epemag.com
56
Practical Electronics | May | 2024
|