This is only a preview of the July 2020 issue of Practical Electronics. You can view 0 of the 72 pages in the full issue. Articles in this series:
|
Max’s Cool Beans
By Max the Magnificent
Flashing LEDs and drooling engineers – Part 5
I
n my previous column (PE, June
2020), we considered a traditional
tricolour LED containing three RGB
LEDs with a common cathode. For the
purpose of these discussions, we will
refer to these as ‘sub-LEDs’ to remind
ourselves that they are inside the main
device. When I was younger, brightereyed, and bushier-tailed, I remember
how I used to think these devices were
as ‘cool as cucumber.’ To be honest,
however, I’ve grown a little jaded over
the years to the extent that I was a tad
disappointed with the result.
First, each such LED requires three of
the digital input/output (I/O) pins on your
microcontroller unit (MCU) to drive it.
Second, if you wish to access more than
the eight basic colours provided by turning the three sub-LEDs on and off – red,
green, blue, yellow (red + green), cyan
(green + blue), hot pink (red + blue),
white (all on), and black (all off) – then
these pins have to support pulse-width
modulation (PWM) as described in Part
1 of this mini-series (PE, March 2020).
And third, I really wasn’t impressed with
the final effect.
Nifty NeoPixels
Now turn that frown upside down into
a smile, because a different sort of tricolour LED that never fails to delight is
the WS2812, which is also known as a
‘NeoPixel’ (a term originally coined by
the folks at Adafruit). This little beauty
is about 5 × 5mm square and 2mm thick
(Fig.1). In addition to three ultra-bright
RGB sub-LEDs, the WS2812 also contains a tiny WS2811 controller chip that
contains three 8-bit PWMs – one for each
Fig.1. WS2812 aka ‘NeoPixel’.
58
a strip of NeoPixels (Fig.2). There are a
couple of points to note on the physical
implementation front. Theoretically, each
NeoPixel can consume as much as 60mA
if all three sub-LEDs are fully on (they
are rated at 20mA each). In practice, I’ve
only ever measured them at 45mA fully
on, so this is the value I go by.
Now, working with only a handful of
NeoPixels is one thing, but if you are
driving a large number you have to start
paying attention to how much current you
are consuming. In the case of the project
we will be discussing in a moment, my
worst-case current consumption would be
50mA for an Arduino and (144 × 45mA)
for my NeoPixels if I were to drive them
all full on to give a brilliant white. This
gives us a grand total of 6,530mA, which
means I’ll need a power supply that
can provide at least 7A to give me some
head room. In practice, of course, I will
probably be lighting only a subset of the
NeoPixels, and I’ll typically be lighting
those with only one or two of the subLEDs, but it’s always best to design for
the worst-case scenario.
Personally, I generally use the same
supply to power my MCU and my NeoPixels, but different folks do things in different ways. If you decide to use your USB
cable to power your MCU and a separate
supply to power your NeoPixels, for example, then it’s very important to ensure
that both the MCU and NeoPixel 0V (GND)
signals are connected together.
It’s also a good idea to add a large electrolytic capacitor (1,000µF, 6.3V or higher)
across the 0V (GND) and 5V terminals of
your power supply. If you are using raw
WS2812s or NeoPixels in through-hole
packages, then it’s important to include a
100nF ceramic capacitor between the 0V
and 5V terminals of each package. This is
Nitty gritty details
another good reason for using Adafruit’s
Purely for the sake of giving us something
Floras or rings or strips, because they
to talk about, let’s assume we wish to drive
already have these
capacitors in place.
5V
Data-In
Data-Out
Data-In
Data-Out
Furthermore, it’s
390Ω
From
important to inMCU
To next
clude a serial resisNeoPixel
tor as close as posData-In
Data-Out
sible to the first ele0V
ment in the NeoPixel chain (Adafruit’s
Fig.2. Driving a chain of NeoPixels.
of the sub-LEDs – and supports a simple
serial communications protocol.
Each NeoPixel has four pins (some come
in 6-pin packages, but only four of the
pins do anything): 0V, 5V, Data-In, and
Data-Out, where the Data-Out from one
NeoPixel can drive the Data-In of another. This allows long strings of NeoPixels
to be daisy-chained together and for the
entire chain to be controlled by a single
digital MCU output.
NeoPixels are available in a variety of
packaging options, including raw chips
(https://bit.ly/2SOBe8b), individual Flora
breakout boards (https://bit.ly/3ck1ZZS),
and traditional 8mm through-hole packages (https://bit.ly/3bkJbs5). In the case
of the Floras, I like to buy them in sheets
of 20 (https://bit.ly/2LdFRVq).
You can also get these little scamps in
bars, rings, jewels and arrays (just go to
Adafruit.com, search for ‘NeoPixel,’ and
wend your way through their 28 glorious
pages of NeoPixel products). For the purpose of the project I’ll be discussing later
in this column, I’m using a 5m strip containing 30 NeoPixels per meter (https://
bit.ly/2SNHTzL). There are strips with 60
and 144 NeoPixels per meter, but I will
be chopping my strip into 144 individual
segments. From experience, I know that
chopping the 30-per-meter strips leaves
me with larger copper pads for soldering
than chopping the 60 or 144-per-meter
strips. Actually, for this particular project, I would have preferred to use Floras
as discussed above, but the strips give
me 30 NeoPixels for US$17, while the
Flora sheets give only 20 NeoPixels for
US$35. Since I require 144 NeoPixels for
this project, chopping strips provides a
much cheaper option.
Practical Electronics | July | 2020
NeoPixel rings already have such a resistor installed).
When I first started using NeoPixel strips, every now
and then the first one in the chain would die on me.
When I used an oscilloscope to look at the Data-In signal
coming from my Arduino Uno, I saw that the edge rate
was so fast it was overshooting the 5V and undershooting the 0V. After a bit of dabbling around, I determined
that a 390Ω resistor dampened things down nicely and
I haven’t had any problems since. Having said this, I
Up / Off/ Inactiv e
Center ( from Up )
Dow n/ On/ A ctiv e Center ( from Dow n)
Up / Off/ Inactiv e
should note that, in their NeoPixel Uberguide (https://
bit.ly/2SRl4eu), which is well worth perusing and pondering, the folks from Adafruit recommend using a 470Ω Fig.3. Using a tricolour NeoPixel with an SPCO switch and with two
resistor. I’ve also seen other people offering different colours for the centre position.
suggestions, but long ago I purchased a couple of hunHowever, the cunning rascals have also implemented things
dred 390Ω resistors for this very purpose, so that’s what I’m
in such a way that we can specify the combined RGB compogoing to use by golly!
nents as a single 24-bit value. It’s best to use hexadecimal for
this sort of thing, so we could achieve our electric violet colour
Luscious libraries
using: MyNeos.setPixelColor(6,0x8000FF), where 0x80
There are a number of libraries floating around that you can
equates to 128 in decimal, 0x00 equates to 0 in decimal, and
use to drive your NeoPixels. If I’m using an Arduino or related
0xFF equates to 255 in decimal.
MCU, I almost invariably use the library from Adafruit (https://
In many cases it is preferable to use this 24-bit value apbit.ly/2LdPxPK). Another easy-to-use Arduino library for proproach, because it lets us do things like pre-defining a colour
gramming NeoPixels (and other devices) that many of my
using something like #define COLOR_ELECTRIC_VIOLET
friends recommend is the FastLED Animation Library (http://
0x8000FFU, where the U is used to indicate that this is an unfastled.io/). Alternatively, if I’m using a Teensy MCU from
signed value. The use of the U (or u) is optional – the compiler
PJRC.com, then I will use their OctoWS2811 Library (https://
can usually figure things out for itself – but it rarely hurts to
bit.ly/2YLER2I) in conjunction with the OctoWS2811 Adapgive it the occasional hint, and it makes your intent clearer to
tor (https://bit.ly/2SNyJmJ).
someone else reading your code. Now, we could use MyNeos.
In the case of the Adafruit library, you start by instantiating
setPixelColor(6,COLOR_ELECTRIC_VIOLET) in our proyour string of NeoPixels, as part of which you specify how many
gram. If you download the sketches discussed later in this
pixels there will be in the chain and which of the Arduino’s
column, you’ll see this is just what we did.
pins you wish to drive them. For each of your NeoPixels, the
library will reserve three bytes in your Arduino’s SRAM, which
means an Arduino Uno with its 2KB of SRAM, for example, is
You turn me on
limited to driving a maximum of around 500 NeoPixels, while
Before we progress to my new project, we first need to tidy things
leaving 512 bytes of SRAM free for other things.
up with regard to our earlier switch experiments. Using a simiLet’s suppose we’ve instantiated a string of 10 NeoPixels,
lar setup to the one discussed in my previous column, I mountwhich will be numbered 0 to 9, and we’ve imaginatively
ed a single NeoPixel Flora on a breadboard and I wrote a little
called this string MyNeos. Now let’s suppose that we use a
sketch to control it using a single-pole, centre-off (SPCO) switch.
statement like MyNeos.setPixelColor(i,COLOR_HOT_
As with the standard tricolour LED in my previous column,
PINK), where i is the number of the NeoPixel we wish to
we’ll use red to indicate when the switch is Off/Inactive, green
change, and COLOR_HOT_PINK is a 24-bit hexadecimal value
to indicate when the switch is On/Active, and either orange or
representing the RGB components of our desired colour. It’s
yellow when the switch is in its centre position to provide an
important to note that this doesn’t actually change the value
indication as to its previous state (Fig.3). You can download a
of the NeoPixel in the string; instead, it changes the value in
sketch (file CB-Jul20-01.txt – available on the July 2020 page
the reserved area of the Arduino’s SRAM. It’s only when you
of the PE website) and watch a video (https://bit.ly/3cyCyUF)
use the command MyNeos.show() that all of the values in
to see all of this in action.
memory are uploaded into the physical string. Don’t worry,
we’ll be seeing simple examples of all of these things in the
A brace of balls
programs we create later.
And thus we arrive at my new hobby project. As I mentioned
in my previous column, I decided to build a magnificent matrix
based on ping pong balls illuminated by NeoPixels – someBits and bytes
thing like the ‘Video Wall’ you can see on YouTube (https://
It just struck me that there’s one thing that can prove to be conbit.ly/3aG1itl).
fusing if you are new to using NeoPixels – the fact that there
I also decided that my first pass would be a small, 12 × 12 =
are two ways for us to specify the colour we wish to use. Let’s
144 ping-pong prototype. It turns out that you can buy a bag
start by reminding ourselves that each NeoPixel contains red,
of 144 ping pong balls for only $11 in the US, but I knew I’d
green, and blue sub-LEDs. Also, it contains three 8-bit PWM
need some spares, so I purchased two bags, giving me 288 ping
functions, one for each of the sub-LEDs. This means that we
pong balls in all.
can assign each sub-LED a value ranging from 0 to 255.
Of course, I was immediately tempted to ‘go bigger and better’
Suppose we wanted to set the seventh NeoPixel in the chain
– say a 15 × 15 = 225 array – but I’d already ordered five meters
to a colour we might call ‘electric violet’ (remember that this
of 30 pixels per meter NeoPixel strip from Adafruit (https://bit.
pixel will actually be number 6 because we start counting at
ly/3dOa5v4), which will give me 150 NeoPixels, so I decided to
0). Let’s further suppose that, to achieve this colour, we want
stick with the original plan. Thank goodness I did, because evthe red component to be 128, the green component to be 0,
erything is requiring significantly more effort and taking much
and the blue component to be 255. In this case, the creators of
longer than I’d originally planned.
Adafruit’s NeoPixel library have implemented things in such
I started by considering how I was going to attach the segments
a way that we can use a statement like: MyNeos.setPixelof NeoPixel strip to my ping pong balls. I was working with a
Color(6,128,0,255).
Practical Electronics | July | 2020
59
one looked better (smoother) and
The next step was to attach the ping
brighter than the other. Although
pong balls to the wooden sheet. I won’t
they look the same in Fig.5, there
bore you with the problems I experienced
NeoPixel mounted
NeoPixel attached to
really is a difference when you are
aligning the balls and the jig I had to create.
inside ball
outside of ball
looking at them in the real world.
Suffice it to say that, if I ever build a wallOf course, the better option was the
sized ping-pong-ball display, I will do so
one that required me to cut 10mm
using 8 × 8 sub-arrays. So, let’s skip the
(3/8-inch) holes in 144 ping pong
gnashing of teeth and rending of garb, and
5/64" 1/4"
balls. ‘Oh dear,’ I said to myself (or
jump forward to the part where I say, ‘As
1 1/8"
1"
(2mm)
words to that effect).
always, my hot glue gun proved to be a
But then I looked in my tool
faithful friend.’
Fig.4. NeoPixel attachment alternatives (theoretical).
chest. Wouldn’t you know it – the
To be honest, I think the final result
largest drill I had was 25mm (1-inch) dilooks rather spiffy (Fig.6). I had hoped to
nice piece of 6mm (1/4-inch) thick plyameter. ‘Oh well,’ I said to myself, ‘the
have everything up and running in time
wood. I want the strips themselves to be
fates have made the decision for me, and
for this column, but it was not to be. I
flush with the surface of the wood. Know– after all – the difference between the two
still have to attach the 144 segments of
ing that the NeoPixels protrude from the
schemes is really very slight.’
NeoPixel strip, and then solder everything
surface of the strips by 2mm (5/64-inch),
So, I went away and drilled my 144 ×
together (144 × 3 = 432 soldered connecthere are two obvious alternatives (Fig.4).
25mm (1-inch) holes, sanded everything
tions – plus change), so we won’t be able
The first option is to cut a hole into the
down, and painted the board black. When
to see this little beauty in action until my
ping pong ball and mount the NeoPixel
I’d finished, I took one ping pong ball and
next column.
inside the ball. In this case, since the
popped it into a hole. What? The bottom
balls are 38mm (1 1/2-inch) in diameter
of the ball was flush with the lower sur(I’m also allowing 1.5mm (1/16-inch) beThe Seeeduino XIAO
face of the board! How could this be?
tween balls for ‘wriggle room’), the hole
In the past, I would have been tempted to
It turned out that what I’d assumed to
I drill through the board would need to
use an 8-bit Arduino Nano for this project
be a 6mm (1/4-inch) thick board when
be 28.6mm (1 1/8-inch) in diameter so as
on the basis that it’s relatively small and
I’d seen it lying around in the garage
to have the base of the ball flush with the
unobtrusive (https://bit.ly/2LfK2jy). On the
was in fact only 5mm (3/16-inch) thick.
bottom of the board. The second option
other hand, the Nano has only a 16MHz
So now I was back to having to cut the
would be to attach the NeoPixel to the
clock, 32KB of Flash memory, and 2KB
10mm (3/8-inch) holes in my 144 ping
outside of the ball. In this case, the hole
of SRAM, all of which is a tad limiting.
pong balls. Actually, if the truth be told,
I drill through the board would only need
But then the fates came into play once
I wasn’t too dismayed, because knowing
to be 25mm (1-inch) in diameter.
again, because the folks at Seeed Studio
I’d opted for the easier, but lower-qualiTo be honest, I didn’t think there would
told me about their Seeeduino XIAO
ty option had been niggling away at the
be much difference between these two
(https://bit.ly/3ckK31c) and they even
back of my mind.
schemes with regard to the way they
sent me one to play with. Costing only
looked, but I decided to build a prototype
US$4.90 and being the size of a small
using a piece of cardboard and a brace of
postage stamp (Fig.7), this little beauty
Building the board
balls. I created a little video showing the
boasts a 32-bit Arm Cortex-M0+ processor
I’m sure that, like me, over the years
same sequence of colours being displayed
running at 48MHz with 256KB of Flash
you’ve spent far more time than you’d
in both balls (https://bit.ly/2WZN6pq), and
memory and 32KB of SRAM. One thing
care to remember on the frustrating task
you can download the sketch I used (file
to note is that the programming connector
of drilling holes in ping pong balls. This
CB-Jul20-02.txt – available on the July
time I came up with
2020 page of the PE website).
something different.
I told myself they looked the same, but I
First, I took a ball and
also asked my wife (Gina the Gorgeous) and
held it up to the light
my 25-year-old son (Joseph the Commonto determine where the
sense Challenged), and they both said that
two hemispheres were
joined, then I used a
permanent marker to
make a dot in the centre
of one of the hemispheres (I don’t want
the join line to show).
Next, I used a stencil to
mark the 10mm (3/8inch) circles centred
on the dot. Finally, I
used some small, sharp,
curved nail scissors to
puncture a small hole in
the centre of the circle
and carefully cut the
ping-pong material
away. I then repeated
this process 143 more
times (that’s one long
evening I’m never going Fig.6. I’m convinced this is the best ping-pong-ball array on
Fig.5. NeoPixel attachment alternatives
to see again).
(prototype).
our street.
60
Practical Electronics | July | 2020
Having said this, I
only need to convert
the signal from a single
3.3V digital output on
the Seeeduino XIAO.
Furthermore, I only
require a unidirecYour best bet since
tional level conversion. For both these
Chock-a-Block with Stock
reasons, SparkFun’s
level-converter BOB
Visit: www.cricklewoodelectronics.com
would be overkill.
Or p h one our friend ly kn ow led g eab le st aff on 020 8452 0161
Fortunately, I ran
Components • Audio • Video • Connectors • Cables
across an awesome
Arduino • Test Equipment etc, etc
hack on Hackaday.com
(https://bit.ly/35LjlfL)
that provides a simple
solution requiring only
a single diode (a general-purpose IN4001
is perfect for this
task) and a ‘sacrificial’
NeoPixel (Fig.9).
The way this works
is that the NeoPixel’s
data sheet states a logic
1 value is considered
to be 0.7 × Vcc. So, if
we are powering the
Visit our Shop, Call or Buy online at:
NeoPixel with 5V, a
logic 1 will be 0.7 × 5
www.cricklewoodelectronics.com
Visit our shop at:
= 3.5 V. In reality, the
40-42 Cricklewood Broadway
3.3V signal from the
London NW2 3ET
Seeeduino XIAO might
work, but then again it might not.
The solution is to power the sacrificial
sacrificial NeoPixel to indicate something,
NeoPixel via the IN4001 diode. Since
because it will just be a little dimmer than
this diode has a forward voltage drop
the other NeoPixels. In the case of my
of 0.7 V, this means the first NeoPixel
12 × 12 ping pong ball array, however,
is being powered by a Vcc of 5 − 0.7 =
I’ll just include an extra NeoPixel before
4.3V. In turn this means that the first
the first ping pong ball and always drive
NeoPixel will see a signal of 0.7 × 4.3
it to be black (off).
= 3.01 V as being a logic 1, so the 3.3 V
output from the microcontroller more
Next time
than fits the bill.
By the time I reach my next column, I’ll
Meanwhile, the 4.3V Data-Out signal
have my 12 × 12 array up and running,
from the first NeoPixel is more than sufso we’ll be able to consider some of the
ficient to drive the Data-In signal to the
programs and effects we can run on it.
second NeoPixel in the chain. What this
Until that frabjous day, have a great time,
means is that we are using the first NeoPixand remember that – as always – I welel in the role of a voltage-level converter.
come any and all comments, questions,
In many systems, we could still use this
and suggestions.
MAPLIN
Fig.7. The Seeeduino: my new best friend.
Fig.8. SparkFun’s 4-bit bi-directional
logic-level converter.
is USB Type-C, which means you’re going
to need a USB-A to USB Type-C cable.
The Seeeduino XIAO has 11 digital/
analogue pins, 10 of which support PWM
and one of which can provide a true digital-to-analogue converter (DAC) output.
These pins can also be used to support a
UART interface, an SPI interface, and an
I2C interface. Apart from anything else,
this little beauty would be great for implementing wearable light effects. I think
it’s safe to say that the Seeeduino XIAO is
going to be making appearances in many
of my future projects.
Keeping things level
On the bright side, the Seeeduino XIAO
can be powered from the same 5V supply
I use for my NeoPixels. However, there
is a small fly in the soup or a large elephant in the room (I’m feeling in a generous mood, so I’ll let you employ the
metaphor of your choosing). The Seeeduino XIAO’s I/O pins use a 3.3V interface, but my NeoPixels require 5V data
signals, so we need some way to convert
between the two.
In the past, I’ve had a lot of success with SparkFun’s 4-bit bi-directional logic-level converter (https://bit.
ly/2WHnvRW). Costing only US$2.95,
this break-out board (BOB) can be used to
convert 3.3V signals to their 5V equivalents, and vice versa (Fig.8). It can even
be used with an I2C bus, which requires
pull-up resistors, because the BOB has
10kΩ pull-up resisters on both sides of
each channel.
Practical Electronics | July | 2020
020 8452 0161
IN4001
‘Sacrificial’ NeoPixel
5V
From
MCU
0V
Data-In
Data-Out
To next
NeoPixel
390Ω
Data-In
Data-Out
Fig.9. A cheap-and-cheerful voltage-level converter hack.
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
61
|