This is only a preview of the May 2020 issue of Practical Electronics. You can view 0 of the 80 pages in the full issue. Articles in this series:
|
Max’s Cool Beans
By Max the Magnificent
Flashing LEDs and drooling engineers – Part 3
T
he problem with things like
switches and LEDs is that there’s
always something more to wrap your
brain around. In the first column in this
miniseries (PE March 2020), for example,
we started by looking at a single-pole,
single-throw (SPST) switch directly driving
O n/ A ctiv e
O f f / I nactiv e
O n/ A ctiv e
O f f / I nactiv e
O n/ A ctiv e
O f f / I nactiv e
a single unicolour LED (Fig.1a). In this
case, the results were fairly rudimentary:
(a) S ingle unicolor LED
(b) T wo unicolor LEDs
(c) S ingle bicolor LED
Switch is On (Closed), LED is On; Switch
is Off (Open), LED is Off. One possibility Fig.2. SPST switch driving one unicolour LED, two unicolour LEDs, and one bicolour LED.
that we didn’t consider would have been
5V
to use a single-pole, double-throw (SPDT) switch to
A rd uino
directly drive two unicolour LEDs (Fig.1b).
LS 18
Our next step was to use our SPST switch to drive
1
8
VS S
VDD
D7
N O
2
S W 0 O UT 0 7
D8
D4
the input of a microcontroller unit (MCU), and to use
3
S W 1 O UT 1 6
D9
one of the MCU’s outputs to drive our solitary unicN C
4
S W 2 O UT 2 5
D5
S W 1
R1
R2
R3
olour LED (Fig.2a). Later, in our second column (PE
April 2020), we used our MCU to take the signal from
D1
D2
D3
the SPST switch and use it to drive a pair of unicolour
S ingle p ole, center of f
LEDs (Fig.2b).
(S P CO ) switch )
The great thing about using an MCU is that we don’t
need an SPDT switch, along with two input pins on Fig.3. Using an SPCO switch and MCU to drive three LEDs.
our microcontroller, to drive our two LEDs. The MCU
can use a single pin to determine the state of an SPST switch
switch as performing an On-On operation, while an SPCO
and use this information to control the LEDs. (It probably goes
switch behaves in an On-Off-On fashion.
without saying – but I’ll say it anyway – that we can always
Purely for the sake of discussion, let’s assume that we have
use an SPDT switch in the role of an SPST switch by simply
the NC and NO contacts of an SPCO switch connected to two
ignoring one of its normally closed (NC) or normally open
inputs of an MCU, and that we are using the MCU to control
(NO) contacts.)
three LEDs (Fig.3). Observe that, in this particular example,
we don’t need to use pull-up resistors on the switch’s NC and
NO contacts because these are provided in the LS18 IC that we
Eeek! Three states!
are using for debouncing (we discussed this IC, which comes
As I mentioned in my column on electrical switch terminolfrom LogiSwitch.net, in PE March 2020).
ogy (https://bit.ly/30ZpToT), it’s also possible to obtain singleThe idea is, if the switch is toggled one way, the NC terminal
pole, centre-off (SPCO) switches, which are also known as
(and hence pin D5 on the MCU) will be connected to 0V while
single-pole, changeover switches. These are toggle or rocker
the NO terminal (and hence pin D4 on the MCU) will be pulled
switches that look like SPDT switches – including their scheup to 5V. By comparison, if the switch is toggled the other way,
matic symbols – except that they have three positions for the
the NO terminal will be connected to 0V while the NC terminal
actuator (lever or rocker). If we think of an SPST switch as
will be pulled up to 5V. Finally, if the switch is in its centre poacting in an On-Off manner, then we might consider an SPDT
sition, both the NC and NO terminals will be pulled
up to 5V. Our MCU program can use this information
5V
to determine which of the three states the switch is in
Dx
5V
R2
R3
T op v iew
and to light one of the three LEDs accordingly.
a
a
R1
But what if we have only a single unicolour LED,
D2
D3
S id e v iew
a = anod e
k
k
as illustrated in Fig.2a, but we are using an SPCO
k = cath od e
a
a
k
switch? If we refer to the three states of our switch as
N O
D1
k
being ‘Up’, ‘Centre’ and ‘Down’, then one possibility
T o someth ing
lik e relays or a
would be as follows (where ‘long’ could be 800ms,
microcontroller
N C
S W 1
T o someth ing
‘short’ could be 200ms, and ‘cycles’ means we repeat
S W 2
lik e a relay or a
the actions over and over again):
microcontroller
0V (GN D)
(a) S P S T switch with one LED
0V (GN D)
(b) S P DT switch with two LEDs
Fig.1. SPST and SPDT switches driving LEDs directly.
66
Switch -> Up: LED cycles On short and Off long
Switch -> Centre: LED is Off
Switch -> Down: LED cycles On long and Off short
Practical Electronics | May | 2020
underestimated your man. But
this means we have to modify our
1 = Red anod e
circuit. Even though both of the
S id e v iew
2 = Common cath od e
Rg
Rr
forward-voltage drops and for3 = Green anod e
3 2 1
ward currents are the same, the
2
fact that we intend to occasionally have both LEDs illuminated
Rgr
Fig.4. Bicolour LED with three terminals
at the same time means that each
0V (GN D)
0V (GN D)
(common cathode).
LED will require its own currenta) I d entical Vf and I f and
b) Dif f erent Vf and / or I f and / or
limiting resistor, as illustrated in
only one LED on at a time
both LEDs on togeth er
Fig.5b. In this particular example,
Another possibility for the Centre posiboth of the resistors will have the
tion would be to have the LED ‘breathing’ Fig.5. Alternative bicolour LED current-limiting
same value, but we would have
(ie, repeatedly fading up and fading down). resistor configurations.
to determine the values individuHow about if we have two unicolour LEDs
ally if the forward voltage drops and/or the forward current
called GLED (green) and RLED (red), as illustrated in Fig.2b,
values were different for the LEDs.
but – once again – being used in conjunction with an SPCO
Just for giggles and grins (and because I just rooted one out
switch? In this case, we could implement something like the
of my treasure chest of salvaged parts) let’s assume we have
following:
an SPCO switch and MCU controlling a single bicolour LED.
One possibility would be as follows:
Switch -> Up: GLED -> Off; RLED -> On
Switch -> Centre: GLED -> Off; RLED -> Off
Switch -> Up: GLED -> Off; RLED -> On (red)
Switch -> Down: GLED -> On; RLED -> Off
Switch -> Centre: GLED -> On; RLED -> On (yellow)
Switch -> Down: GLED -> On; RLED -> Off (green)
Once again, another possibility for the Centre position would
be to have both of the LEDs breathing, or maybe have them
Actually, by varying the relative intensity of the red and green
both present a periodic flash (say turning On for 100ms once
LEDs we should be able to achieve at least four colours: red,
every second).
green, yellow, and orange. To be honest, using the pulsewidth modulation (PWM) technique we discussed in an earWe want more!
lier column, I would have assumed that values of red = 255
Now sit down, take a deep breath, and try to curb your natural
and green = 255 would have resulted in yellow, while red =
enthusiasm because we’re about to dip our toes into the bico255 and green = 128 would have resulted in orange. In reallour waters (remember to take your socks off first). There are
ity, however, I had to mess around with the values a bit to get
a variety of different types of bicolour LEDs around. The first
anything approaching the orange and yellow I was looking for.
component we will be playing with, which contains red and
The point is, if we do have four colours, then we can make
green LEDs, is part number 160-1939-ND from Digikey.com
things even more spiffy as follows (Fig.6):
(https://bit.ly/2uPq8Hh). This little rascal sports three pins: a
red anode, a green anode, and a common cathode (Fig.4). Other
Switch -> Up: LEDs -> Red
colour combinations are available, as are common anode conSwitch -> Down: LEDs -> Green
figurations with one anode and two cathodes.
Switch was Up (red) -> Centre: LEDs -> Orange
Looking at the data sheet for this little scamp (https://bit.
Switch was Down (green) -> Centre: LEDs -> Yellow
ly/2TvpEhW), we see that the forward current (If) is 30mA for
both diodes. Meanwhile, the forward voltage drop (Vf) is 2.0V
If you wish, you can download a sketch (CB-LED-Pt3-May20.
for the red diode and 2.1V for the green diode. Let’s assume
txt) for this scenario to see how I implemented it from the May
they are both 2.0V, which is ‘close enough for government
2020 page of the PE website. Furthermore, I’ve created a short
work,’ as they say.
video to show all of this in action (https://bit.ly/2vV7oXk).
At this point we have a choice. If both of the forward-voltIf we wanted to make things even more spiffy (and why
age drops are the same, and both of the forward currents are
wouldn’t we?), we could implement a breathing effect for the
the same, and if we only ever intend to have one diode on at
yellow and orange centre positions.
a time, then we can get by with a single current-limiting reNow, suppose we had something like Fig.2b but with two
sistor, as illustrated in Fig.5a. Using Ohm’s law (V = IR), and
bicolour LEDs? How about you reflect on some of the lightremembering we’re assuming a power supply of 5V, a forward
ing effect possibilities you could use with SPST, SPDT, and
voltage drop of 2V, and a forward current of 30mA (0.03A),
SPCO switches, and then email me to tell me what you’ve
then Rgr = (5 – 2)/0.03 = 100Ω.
come up with?
Having said this, do you seriously expect me to not try
turning both LEDs on at the same time to see what the resulting yellow colour looks like? If so, you have seriously
What? More?
There is another type of bicolour LED that has only two pins.
For the purpose of these discussions, the component we will
be playing with is part number 160-2245-ND from Digikey.com
(https://bit.ly/38nFTmo). This
T op v iew
little scamp contains a yellow
2
1
LED and a green LED (Fig.7),
S id e v iew
but other colour combinations
are available.
2 1
1 = Yellow cath od e
Looking at the data sheet
2 = Green cath od e
Up / O f f / I nactiv e
Center (f rom Up )
Down/ O n/ A ctiv e Center (f rom Down) Up / O f f / I nactiv e
for this little chap (https://bit.
Fig.7. Bicolour LED with two ly/3cyH7i3), we see that we’re
Fig.6. Using a bicolour LED with an SPCO switch and with two
going to have a bit of a problem.
terminals.
colours for the centre position.
T op v iew
3
1
Practical Electronics | May | 2020
From
MCU
From
MCU
67
From
MCU
2
D2
D3
Rg
Ry
D1
D2
5V
0V
0V
5V
(a) Using two currentlimiting resistors
1
D3
2
1
Rg
(b) Yellow LED on
Ry
D1
D2
D3
Rg
Ry
2
1
(c) Green LED on
D1
Fig.8. Using two 1N5817 Schottky diodes in parallel with two
current-limiting resistors.
On the bright side, the forward voltage drop (Vf) is 2.1V for
both LEDs. On the downside, the forward current (If) is 20mA
for the yellow LED and 30mA for the green LED.
Isn’t it ironic? When we were working with the three-terminal bicolour LED, the forward voltage and current values were
the same for both LEDs, which means we could have gotten
by with a single resistor so long as we didn’t illuminate both
LEDs at the same time. In the case of our two terminal bicolour LED, we can only ever have one of the LEDs on at any
one time, but – since the forward currents are different – we
need a different current-limiting resistor for each LED. The
problem is that we can have only one current-limiting resistor (it doesn’t matter to which side of the bicolour device the
resistor is connected), or can we?
This is where we roll up our sleeves and reveal just how
cunning we can be (shut the doors and close the curtains because we don’t want the unwashed masses to learn all our
secrets). What we’re going to do is use two current-limiting
resistors, each in parallel with a 1N5817 Schottky diode, as
illustrated in Fig.8a.
In reality, we could use any general-purpose diode, like a
1N4001. The main reason for using these Schottky diodes is
their low forward-voltage drop, which will be 0.32V according to the data sheet (https://bit.ly/32QgRLA).
Now, this can take a little time to wrap your brain around,
but we’ll take it step by step. Remember, we’re using an Arduino Uno as our MCU, which mean its digital outputs switch
between 0V and 5V. What we are going to do is to use two of
our digital outputs to control our bicolour LED.
If we set the MCU output connected to pin 2 of the bicolour
LED to HIGH (5V) and the other MCU output to LOW (0V),
then the yellow LED will be forward biased and will turn on,
while the green LED will be reversed biased and will turn
off (Fig.8b). Also, Schottky diode D2 will be forward biased,
which means it will turn on, thereby shorting out resistor Rg,
which means the circuit acts like Rg doesn’t exist. Meanwhile,
Schottky diode D3 will be reverse biased, which means it will
be turned off, which means the circuit acts like D3 doesn’t exist.
Remembering that the Vf of the yellow diode is 2.1V, the Vf
of Schottky diode D2 is 0.32V, and the If of the yellow diode
is 20mA (0.02A), then using V = IR, Ry = (5 − 2.1 − 0.32)/0.02
= 129Ω. Since the closest E24 series resistor is 130Ω, we’re
right on the nose.
By comparison, if we set the MCU output
connected to pin 2 of the bicolour LED to
LOW (0V) and the other MCU output to
HIGH (5V), then the green LED will be
forward-biased and will turn on, while
the yellow LED will be reversed-biased
68
and will turn off (Fig.8c). Also, Schottky diode D3 will be
forward-biased, which means it will turn on, thereby shorting
out resistor Ry, which means the circuit acts like Ry doesn’t
exist. Meanwhile, Schottky diode D2 will be reverse-biased,
which means it will be turned off, which means the circuit
acts like D2 doesn’t exist.
Remembering that the Vf of the green diode is 2.1V, the Vf
of Schottky diode D3 is 0.32V, and the If of the green diode is
30mA (0.03A), then using V = IR, Rg = (5 − 2.1 − 0.32)/0.03 =
86Ω. In this case, the closest E24 series resistor is 82Ω, which
– once again – is ‘close enough for government work.’
What’s that you say? ‘Why do the three circuit incarnations in Fig.8 differ from each other?’ Actually, I’m just messing with your head. Since this is a series circuit (if we regard
each Schottky-resistor combo to be a series element), then as
far as the forward/reverse biasing of the Schottky diodes or
the turning On and Off of the yellow and green LEDs, all of
these circuits are functionally equivalent. The only reason I
drew them in different ways here was to emphasise that fact.
What happens if we switch back and forth between the
two colours really quickly? If this was a red-green bicolour
diode, I’d expect to see yellow. Since all I have is a yellowgreen bicolour diode, I have no idea, but I’m looking forward
to finding out when I get a spare minute, after which I’ll create
a video and make the code available for you to download in
my next column.
Next time
In my next column, we will start to consider tricolour LEDs,
which are beasts of a different colour (pun intended). In the
meantime, I welcome your comments, questions, and suggestions.
Extra! – Awesome clock using 7-segment displays
Since we are currently submerging ourselves in all things LEDrelated, I thought I’d share a rather awesome clock project I ran
across recently on Hackaday.io (https://bit.ly/39ra4uC). This
bodacious beauty, which was created by a Hackaday member
who goes by the username of Frugha, is based on the humble
7-segment LED display.
Frugha used 6 rows × 12 columns = 144 of these little rascals to create a display matrix. Frugha also used an Arduino
Nano, a real-time clock (RTC), and 18 MAX7219 7-segment
display driver ICs to implement a device that is awesome in
every way I can think of (Fig.9).
There are so many subtleties about this, such as the way
Frugha uses individual segments to ‘smooth’ the edges of his
characters. Also, the way in which adjacent digits ‘share’ a
column, with the upper-right corner of one digit sharing the
column with the lower-left corner of the digit to its right. All
I can say is I love this little scamp (the clock, not Frugha, although I’m sure he or she is a very lovable person).
Fig.9. Awesome clock made out of 7-segment displays.
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
Practical Electronics | May | 2020
Max’s Cool Beans cunning coding tips and tricks
S
ince I’m providing downloadable Arduino
sketches (programs) to accompany the current miniseries
of Cool Beans columns, I thought it might be a good
idea to provide a rational for the coding style I use. In my
first coding column (PE March 2020) we talked about cases,
spaces, comments, #define statements, and why using ‘magic
numbers’ is a bad idea. In my second column (PE April 2020)
we considered naming conventions for our variables and
functions, the use of curly brackets { }, and using four-space
indentation. Now, let’s take a slightly deeper dive into some
of the more nitty-gritty details.
I can see you!
For the purpose of these discussions, we will consider a program
that is contained in a single file. The term ‘scope’ refers to the
accessibility (visibility) of variables in a program.
A global variable (a variable that is declared outside of any
function) is said to have a global scope because it’s visible to
any and all of the functions in the program. Consider the following two declarations and initialisations of global variables:
int
StartCountA = 0;
const int StartCountB = 0;
Both of these variables can be read by any of the functions in
the program, but only StartCountA can be overwritten with
a new value. The use of the const (‘constant’) keyword with
the declaration of StartCountB means that its value cannot be
changed after its original initialisation.
By comparison, a local variable (a variable that is declared
inside a function) has scope only within that function and is not
visible to any of the other functions in the program. This means
that it’s possible to declare local variables with identical names
in multiple functions, but each of these variables will be seen
only by the functions in which they are declared.
Remember that we are using upper and lower camel case for
our global and local variables, respectively.
Newbie programmers often make all of their variables global,
which isn’t a problem in small programs, but it’s not a good
habit to adopt because it makes larger programs difficult to understand and maintain.
Foxy for() loops
When using a for() loop, many programmers declare the controlling variable as a local variable. It’s also common to use the
letters i and j to name these variables, where i typically stands
for ‘index’ and j is just the next letter after i; for example:
int i, j; // Declare local variables
for (i = 0; i < MAX_ROW; i++)
{
for (j = 0; j < MAX_COL; j++)
{
// Do something useful
}
}
There are several things I would do differently here. First, I
wouldn’t declare i and j as local variables to the function, but
I would instead declare them as part of the for() statements
as shown below:
Practical Electronics | May | 2020
for (int i = 0; i < MAX_ROW; i++)
{
for (int j = 0; j < MAX_COL; j++)
{
// Do something useful
}
}
Next, over the years I’ve come to realise that having singleletter variables can cause more trouble than they are worth,
not the least that they don’t actually give any indication as to
what they do. I now believe that three letters is a rule-of-thumb
minimum. Also, on the basis that – in this example – we are
using both of our variables to index across rows and columns,
I would write this snippet of code as follows:
for (int iRow = 0; iRow < MAX_ROW; iRow++)
{
for (int iCol = 0; iCol < MAX_COL; iCol++)
{
// Do something useful
}
}
When we subsequently use the iRow and iCol variables in
the body of our nested for() loops to implement some form
of algorithm, having meaningful names makes it much easier
to understand and maintain the code.
More regarding for()
There’s actually something rather clever about for() statements
that professional programmers don’t even think to mention,
which may explain why non-professional programmers don’t
know anything about it. Consider the following:
for (initialization; condition; modification)
{
}
As we see, there are three semicolon-separated parts to this statement: initialisation (where we initialise our control variable);
condition (where we test our control variable); and modification (where we increment or decrement) our control variable.
Because we typically employ increments and decrements of
one, using things like iRow++ and iRow-- respectively, beginners often think that’s all we can do. But if we remember that
iRow++ is the same as saying iRow = iRow + 1, we quickly
realise that our modification could be by any positive or negative integer amount such as iRow = iRow + 3.
But wait, there’s more, because both the initialisation and
modification sections can comprise multiple comma-separated
elements; for example:
for (i = 0, j = 10; i < 10; i++, j--)
{
}
I know, I know… this example breaks all my rules (single-character variables and magic numbers), but we’re only doing this to
illustrate a point, so I’m going to let myself off with a warning.
In my next column, among other things, we’ll talk about the
way in which the << and >> operators work.
69
|