This is only a preview of the February 2020 issue of Practical Electronics. You can view 0 of the 80 pages in the full issue. Articles in this series:
|
Make it with Micromite
Phil Boyce – hands on with the mighty PIC-powered, BASIC microcontroller
Part 13: Controlling RGB LEDs and building a Mood Light
which in turn is connected to (and
drives) the red, green, and blue LEDs.
Smart RGB LEDs typically have a serialcommunication link to the outside world
to receive appropriate data to control
the overall colour and brightness of
the LED. Smart RGB LEDs are usually,
but not always, packaged as a surfacemount device (SMD, see Fig.2b). Types of
smart RGB LEDs that you have possibly
heard of include the WS28xx, NeoPixel,
DotStar and APA102.
Fig.1. The Micromite Mood Light has many features and is controlled via an IR remote.
It is shown here inside a modified Ikea FADO table lampshade.
O
ver the last couple of months
we have explored serial data
communication techniques. This
included writing code to interpret key
presses from a capacitive keypad, and
also how to use bit-banging to display
characters on an LED matrix. This month,
we will show you how to use MMBASIC’s
SPI commands to perform the function
of bit-banging, and we’ll use this method
to control RGB LEDs. Once we have
learnt how easy it is to set the colour
and brightness of an LED, we will put
this knowledge into practice and build
a new mini-project – a fully featured,
IR-controlled Mood Light (see Fig.1).
Colourful LEDs
adjusting the voltage, and hence the current
supplied to each of the three LEDs, the
human eye is tricked into seeing a solid
colour. This is exactly the technique used in
colour TV for illuminating each individual
RGB pixel on the screen.
There are many different RGB LEDs
available today, but they typically fall
into one of two main types:
1. Standard 4-pin RGB LED – these have
a red, green, and blue pin connected
to the ‘outside world’, along with a
common pin (see Fig.2a). By applying
various voltages directly to the RGB
pins, the LED emits a range of colours.
2. Smart RGB LED – these integrate a
controller chip inside their package,
A nice analogy is that the ‘standard’
type can be considered as an analogue
LED (requiring voltages), and the ‘smart’
type viewed as a digital LED (requiring
data). We will be using the smart digital
type in our discussions this month, and
use several to build the Mood Light.
A useful point to understand is that a
smart RGB LED (which is based on a
serial communication link) includes
four pins: Clk In, Data In, Clk Out and
Data Out. This allows multiple smart
LEDs to be ‘daisy-chained’ (the output
of one feeding into the input of the next)
to create a string of LEDs, while retaining
the ability to individually control each
LED in the string (see Fig.3).
The Blinkt! module
From the many RGB LEDs and modules
available to choose from, we’ve selected
RGB LEDs differ from normal LEDs – they
can to create virtually any colour by mixing
the appropriate amount of red, green, and
blue light. They are able to do this because
an RGB LED (as its name implies) contains a
red LED, a green LED and a blue LED. These
three LEDs are positioned very closely
to each other inside a single package. By
Micromite code
The code in this article is available
for download from the PE website.
66
Fig.2. a) (left) Typical through-hole 4-pin RGB LED and b) (right) Smart RGB LED –
here, two views of a single APA102 SMD package.
Practical Electronics | February | 2020
S D I
S D O
C K I
C K O
G N D
V C C
L E D 1
D A T A
C L O C K
V C C
L E D 2
S D I
C K I
S D O
C K O
G N D
V C C
D a ta in p u t
C l o ck i n p u t
D a ta o u tp u t
C l o ck o u t p u t
0 V
+ 5 V
L E D 3
IC 1
T S O P
I R r e ce
L E D 8
16
Fig.3. Smart RGB LEDs can be cascaded
to create a string (pinout shown here is
for the APA102).
the Blinkt!, manufactured by Pimoroni.
It is one of the lowest-cost smart RGB
LED modules available, and it comprises
of not one, but eight ultra bright, and
individually controllable APA102 LEDs
(connected together as shown in Fig.3).
The Blinkt! is actually designed to be
plugged directly into a Raspberry Pi’s
40-way GPIO connector. However, as with
any Raspberry Pi add-on (or any Arduino
shield), there is nothing preventing us
from connecting it to a Micromite. Even
though the Blinkt! has a 40-way female
socket, only four pins are used; two
for 5V power (5V/0V), and two for the
synchronous serial link (Clock and Data)
– see Fig.4. So we need just four wires
to connect the Blinkt! to the Micromite’s
Development Module, as shown in the
schematic in Fig.5. Also shown are the
connections to the IR receiver that we will
use later for the Mood Light. However,
before we begin writing any code, we
first need to understand what data the
APA requires to function.
L E D
( t o p ) vi e w
D A T A
3
5 V
3
9
8
9
8
3
0 V
4 0 - w a y so
cke
2
t ( b o t t o m ) vi e w
Fig.4. The Blinkt! is a low-cost
Raspberry Pi module containing eight
ultra-bright APA102 RGB LEDs. Each
RGB LED can individually be set to any
colour and brightness via the single twowire serial interface.
Practical Electronics | February | 2020
We’ll now discuss how to set all eight
LEDs on the Blinkt! to full red intensity,
and at maximum LED brightness.
Referring to Fig.6, you will see that the
bytes we need to send are:
0V
3
2
(START frame [4 bytes]) 0,0,0,0
(LED 1 frame [4 bytes]) 255 (the 8-bits:
111 11111), 0 (no blue), 0 (no green),
255 (full red intensity)
(LED 2 frame [4 bytes]) 255,0,0,255
(LED 3 frame [4 bytes]) 255,0,0,255
(LED 4 frame [4 bytes]) 255,0,0,255
(LED 5 frame [4 bytes]) 255,0,0,255
(LED 6 frame [4 bytes]) 255,0,0,255
(LED 7 frame [4 bytes]) 255,0,0,255
(LED 8 frame [4 bytes]) 255,0,0,255
(END frame [4+1 bytes]) 0,0,0,0,0
The APA102 data format
From the APA102 datasheet (http://bit.
ly/pe-feb20-APA) we can summarise
the following requirements to set the
overall brightness of the smart RGB
LED, as well as the red, green and blue
colour intensities:
1. Send 32 zeroes (0) for the ‘start-ofmessage’ frame
2. Send 4-byte LED frame (data for
brightness, and the RGB intensities):
n byte 1: 111xxxxx where xxxxx =
brightness value between 0 (off) and
31 (max)
n byte 2: B value between 0 (blue
intensity off) and 255 (max blue
intensity)
n byte 3: G value (0 to 255)
n byte 4: R value (0 to 255)
3. Repeat step 2 for each LED in the strip
(eight times for the Blinkt!)
4. 32 zeroes (0) to begin the ‘end-ofmessage’ frame
5. Send an additional eight zeroes (0) to
complete the ‘end-of-message’ frame
(for 8 LEDs).
S T A R T
fra m e
3 2 b its
(4 b y te s )
L E D
fra m e
Bit-banging with the SPI command
Last month we sent data to the 8×8 LED
matrix module by bit-banging it with raw
code. In summary, the Data signal was
set as required to either a high (1), or a
low (0) logic level, and then the Clock
signal was pulsed to load the data bit
into the LED matrix driver chip. This was
repeated for each bit sent. In last months
code we used SUB MAXwrite(value) to
bit-bang a 16-bit value. We could modify
SUB MAXwrite(value) to clock out 8
bits (instead of 16 bits), and then call it
D a ta fo r L E D
E n d
fra m e
s tr in g
00000000
00000000
00000000
00000000
8 b its
8 b its
8 b its
8 b its
B lu e
bbbbbbbb
G re e n
gggggggg
R e d
rrrrrrrr
8 b its
8 b its
8 b its
00000000
00000000
00000000
00000000
00000000
8 b its
8 b its
8 b its
8 b its
8 b its
B r ig h tn e s s
L E D
111 xxxxx
fra m e
3 2 b its
( 4 b y te s ) 3 b its 5 b its
E N D
fra m e
3 2 + 8 b its
So if we send the above 41 bytes to the
Blinkt!, then we will see all LEDs set to
red at full intensity (‘redness’), and at
maximum brightness. We will demonstrate
this shortly, but first we’ll discuss how to
send this data to the Blinkt!
32*0 LED1 LED2 LED3 LED4 –––– LED8 (32+8)*0
S ta rt
fra m e
25
Seeing red!
Fig.5. The Micromite Mood Light
requires just three components: an IR
remote control and receiver, and the
Blinkt! module – (plus some Micromite
code!). The connections to the Blinkt!
and the IR receiver are shown here.
S D I
C L O C K
r
5V
25
(5 V )
G N D
i ve
The above is represented diagrammatically
in Fig.6. If you look closely, you will see
that we need to send multiple 8-bit chunks
of data (multiple bytes). Let’s look at this
in more details.
Fig.6. The serial-data format required to control a string of APA102 RGB LEDs.
67
41 times, passing each byte shown above,
one at a time, in the correct sequence.
However, all this can be greatly simplified
by replacing the SUB code with a single
SPI command, which in effect automates
the bit-banging for us.
The Micromite has three pins dedicated
to SPI:
n Pin 25 SPI Clock
n Pin 3
SPI Data Out
n Pin 14 SPI Data In (not used here)
To use the SPI pins, they first need to be
configured at the start of our code with
the single command: SPI OPEN speed,
mode, bits. Please refer to the User
Manual (geoffg.net/Micromite.html – see
Appendix D) for detailed information
regarding the SPI parameters. In the
limited space here we will define the exact
line of code we need to use to configure
the SPI pins: SPI OPEN 1000000,0,8
(the 8 refers to 8 bits at a time; ie, one
byte at a time).
Once the SPI pins have been
configured, we can send bytes of data
with the following single command:
SPI WRITE nbr, data1, data2,...
where nbr represents the number of
data-bytes we are sending, and dataX
are the individual bytes.
Using the above information we can
begin to write some simple test code. First,
make the four connections between the
Blinkt! and the Micromite, as shown in
Fig.5 (there is no need to connect the IR
receiver just yet). Next, enter the following
six lines of code (remember to save any
existing program code in the Micromite,
should you need to!).
SPI OPEN 1000000,0,8
SPI WRITE 4, 0,0,0,0
FOR i = 1 to 8
SPI WRITE 4, &b11111111,0,0,255
NEXT i
SPI WRITE 5, 0,0,0,0,0
On running the code you should see the
Blinkt! glow bright red. If not, check the
four connections are made correctly, as
shown in Fig.5, and also check your
code is typed exactly as shown above.
To ensure you fully understand what is
going on, here is an explanation of each
line of code:
Line 1 Configures the SPI pins at a clock
speed of 1MHz, and with 8 bits
per data element
Line 2 Sends the start-of-message frame
(four bytes of 00000000)
Line 3 Forms a loop that is repeated eight
times (once for each LED)
Line 4 Sets the 5-bit LED brightness to a
maximum, and red intensity to full
Line 5 Repeats Line 4 eight times
68
Line 6 Sends the end-of-message frame
(five bytes of 00000000)
Changing colour and brightness
Assuming you have a glowing red Blinkt!,
we can now explore how to change the
colour, and brightness, of the LEDs. Have
a go at each of the following tasks; each
will require a change to Line 4 (apart
from the final task):
1. Reduce the LED brightness (hint:
use &b111xxxxx where xxxxx is
replaced with a 5-bit binary value such
as &b11100001). Note: a brightness
value of 0 (ie, &b11100000) will turn
the LED off
2. Change the LED colour from red to
green, and set at full brightness again
(remember that Line 4 sends four bytes
representing: &b111(5-bit brightness),
blue intensity, green intensity, red
intensity)
3. For completeness, change the LED
colour to blue
4. Now create a colour based on a
mix of the RGB values – start with
yellow, cyan, or magenta (hint: set any
combination of two out of the three
RGB intensities to 255)
5. Set colour to white (hint: requires all
three RGB intensities)
6. Set to a colour of your own choice
such as light purple (hint: you may
want to use an online colour selector
tool; eg, www.rgbtool.com
7. Finally, set the eight LEDs from left to
right as: red, green, blue, red, green,
blue, red, green (hint: comment out lines
3 and 5 to remove the loop, then add
seven lines identical to Line 4 – one for
each LED. Then set the first, fourth and
seventh line to red, the second, fifth,
and eighth to green, and the third and
sixth to blue)
The above tasks will give you a
clear understanding of using the
SPI command to easily control RGB
LEDs. Now let’s put our learning
into practice by building a fully
featured Mood Light, complete
with an IR remote control to change
colours and functionality.
The Micromite Mood Light
Having already connected the
Blinkt! module to the Micromite
(via the Development Module), all
we need to do to create the Mood
Light is add an IR receiver.
Make the three connections as
shown in Fig.5 and then download
the file MKC_MoodLight.txt from
the February 2020 page of the PE
website. Load the program into your
Micromite, and before you run it,
take a quick look at the program
code. I want to draw your attention
to the SUB IR_Int (at the bottom of the
program), which is called whenever the IR
receiver detects an IR signal. On pressing
a button on the 44-key IR transmitter,
the variable KeyCode will be set, and
its value immediately passed to SUB
IR_Int (refer to Fig.7 to see the unique
KeyCode value returned for each button).
The main function of SUB IR_Int is
to check which button is pressed (with
SELECT CASE KeyCode) and to then
alter the appropriate program variable(s)
accordingly. Any change to any variable(s)
is then immediately picked up in the
main program, which will result in the
required visual change to the Blinkt! LEDs.
Now run the program and have a play.
Below is a summary of the Mood Light’s
features, and opposite a useful reference
table of the operations of each of the IR
transmitter buttons.
Mood Light features
n Three modes: static (on), fade (up/
down) and flash (on/off)
n Quick set a colour: red, green, blue,
white, orange, pink, magenta, yellow
n Fine tune to any custom colour
n Store/recall three custom colours
n Adjust overall brightness
n Adjust fading/flashing speed
n Switch power on/off
Please feel free to add and/or change the
functionality to suit your own needs! Now
that you have the code and electronics
working, it can be finished off by placing
the Blinkt! module in an appropriate
housing. You will see that the LEDs are
very bright, and the ideal thing to do is
add a diffuser of some kind. This can
be as simple as a piece of white paper,
or if you want something a little more
permanent, then grab yourself an Ikea
58
186
130
2
26
154
162
34
42
170
146
18
10
138
178
50
56
184
120
248
24
152
88
216
40
168
104
232
8
136
72
200
48
176
112
240
16
144
80
208
32
160
96
224
Fig.7. The 44-key IR remote control used to
control the Mood Light. Shown on the right are the
KeyCode numbers for each button, as used in the IR
interrupt to determine which key has been pressed.
Practical Electronics | February | 2020
IR (infrared) remote control button operations
n Increase brightness: when in static mode, each press
increases the brightness variable up to a maximum
value of 31
n Decrease brightness: when in static mode, each press
decreases the brightness variable down to a minimum
value of 1
n Play/pause: when in static mode, toggles between
minimum and maximum brightness
n Power: toggles between on and off (sets brightness
variable to 0 to turn off)
n R: immediately switch colour variables to: rr = 255,
gg = 0, bb = 0
n G: immediately switch colour variables to: rr = 0, gg
= 255, bb = 0
n B: immediately switch colour variables to: rr = 0, gg
= 0, bb = 255
n W: immediately switch colour variables to: rr = 255,
gg = 255, bb = 255
n Coloured buttons: set appropriate values for colour
variables: rr, gg, and bb
n Increase red: increases the value of the red (rr) variable
by 5 (up to a maximum value of 255)
n Decrease red: decreases the value of the red (rr) variable
by 5 (down to a minimum value of 0)
n Increase green: increases the value of the green (gg)
variable by 5 (up to a maximum value of 255)
n Decrease green: decreases the value of the green (gg)
variable by 5 (down to a minimum value of 0)
n Increase blue: increases the value of the blue (bb)
variable by 5 (up to a maximum value of 255)
n Decrease blue: decreases the value of the blue (bb)
variable by 5 (down to a minimum value of 0)
n Quick: increases the speed variable
n Slow: decreases the speed variable down to a minimum
value of 0
n DIY1: recalls the three stored custom RGB values for
custom colour 1 and loads them into variables rr, gg,
and bb
n DIY2/DIY3: as DIY1 (but for recalling the second or
third stored custom colour)
n DIY4: stores the current rr, bb, and gg variable values
into custom colour 1 memory (ie, into DIY1)
n DIY5/DIY6: as DIY4 (stores into either the second
(DIY2), or third (DIY3) custom colour memory
n AUTO: not used
n FLASH: select flash mode
n JUMP3/JUMP7: select static mode
n FADE3/FADE7: select fade mode.
FADO table lamp (around £15) and place the ‘globe’ part over
the Blinkt! module. This makes for a very professional looking
end product, as can be seen see Fig.1.
Final thoughts for the Mood Light
This mini project has resulted in a practical gadget that you may
want to keep using. To avoid tying up your MKC and DM, you
could replicate the simple Micromite Mood Light circuit onto a
Micromite Mood Light kit
A kit of parts to make the Mood Light is available from
micromite.org – it contains a Blinkt! module, 44-key IR
remote and IR receiver, as featured in this month’s article.
Practical Electronics | February | 2020
Fig.8. The Explore28 module – an SMD version of the MKC/DM
– is ideal for building a standalone Mood Light. It is available fully
assembled from micromite.org (shown here actual size in the
lower two photos).
piece of stripboard. All you need is a 28-pin PIC (loaded with
the Mood Light program), a 5V PSU, a 3.3V voltage regulator,
and a tantulum capacitor with a value between 10µF and 47µF
(refer to the Micromite User Manual – Quick Start Tutorial,
Basic Circuit, for connections).
Add the IR receiver to pin 16 (and 5V power), and the Blinkt!
to 5V power, and to pins 3 and 25 as shown in Fig.5. This will
totally free up you MKC and DM ready for next month’s article.
An alternative to building a dedicated Micromite Mood Light
circuit is to consider using a Micromite Explore 28 module
instead. This is effectively a complete MKC and DM preassembled onto a single, compact PCB measuring just 40mm ×
20mm (see Fig.8). This tiny size is achievable because all parts
are SMD. All I/O (and power) pins are brought out onto two
rows of 0.1-inch header pins, making it easy to mount onto a
scrap piece of strip-board and allowing you to add the Blinkt!
and IR receiver. If you need any help with any of this then
simply drop me an email outlining what you need to know.
Next month
Whenever you work through the practical topics covered in this
series, or indeed whenever you use your Micromite to work on
any of your own projects, you first have to position your MKC/
DM close to your computer in order to connect them together
with a USB lead. Next, you launch your preferred terminal
application, point it to the relevant virtual COM port, and then
you’re able to interact with your ‘tethered’ Micromite. This is
something that has hopefully become second nature to you.
Now imagine that you have a Micromite positioned somewhere
remotely in your house; possibly in a hard-to-access position
such as in the loft. Wouldn’t it be fantastic to be able to work
from your usual location, and yet somehow connect your
computer to the ‘remotely located’ Micromite without having
to relocate it? This would then allow you to interact with an
un-tethered Micromite as if it were positioned right next to
you (something we will eventually need for our future robot
buggy project).
Next month, we’ll show you how to achieve wireless (untethered) interaction
with a remote
Questions? Please email Phil at:
Micromite by using
contactus<at>micromite.org
a low-cost Bluetooth
module.
69
|