This is only a preview of the September 2022 issue of Practical Electronics. You can view 0 of the 72 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 39: Using infrared to synchronise time
L
ast month, we showed how easy it is to set
up a PicoMite BackPack. We focussed on the 2.8-inch
320x240 touch TFT module, as this has the same-sized
screen which was used in the original Micromite BackPack
(and thus in many PE projects). In addition, we showed how
to use the bigger 3.5-inch touch TFT with a higher resolution
of 480x320. However, we also mentioned that there are many
smaller (and cheaper) screens that can be used, which are
handy if you only want to show a small amount of data, for
example, the time. Since last month, we have received numerous
emails about using these smaller screens; in particular, asking
for help with the OPTION settings. Therefore, we will begin
this month with a useful guide covering some of the more
popular smaller screens.
While setting up the hardware for this task, I simply connected
a PicoMite to a display, wrote a short program to show the time,
and left the unit on test while setting up the next PicoMite.
Fortunately, I had recently received a delivery of new RPi Pico
modules and display modules; so I was able to set up multiple
‘clocks’ and position them side by side. Note that no RTC
(real-time clock) was used in any of these clocks. Instead, each
clock was configured using just MMBASIC’s built-in TIME$
variable. All I did was set TIME$ at the command prompt to
a value as close to the exact time as possible. Everything was
going well, but then my 10-year-old son made a comment; he
noticed that the clocks were all showing a slightly different
time and asked, ‘which clock is actually correct?’.
I gave the ‘time is relative’ answer, and then went on to tell
him that probably none of the clocks were showing exactly
the correct time. However, it gave me an idea for a topic that I
had planned for a future article – infrared (IR) communication.
So, the second part of this month’s article will demonstrate
how to synchronise several clocks to show the same time by
using MMBASIC’s built-in IR SEND command. Essentially,
we will show you how to add an IR transmitter to one clock
(the ‘master clock’), and then add an IR receiver to each of the
other clocks so that they can all be set to show the same time.
For this article you will need at least two PicoMite BackPacks
(ideally three or more), but do remember that a PicoMite costs
less than £4, and displays start from only around £5 (and
much less from the likes of eBay if you’re prepared to wait
Micromite code
The code in this article is available for download from
the September 2022 page of the PE website.
48
Fig.1. The Waveshare Pico-LCD-0.96 display module makes for a
compact PicoMite BackPack. It has a really nice IPS screen with a
resolution of 160x80.
for delivery from the Far East). The only other parts you will
need are probably already in your spare parts draw: an NPN
transistor, a couple of resistors, a single IR transmitter LED,
and as many IR receivers as you have PicoMite BackPacks.
Let’s begin by showing the OPTION settings to create some
PicoMite BackPacks with smaller screens.
0.96-inch (160x80) IPS screen
This is one of my favourite small screens. It produces a very
clear image with vivid colours, and a great viewing angle. It is
available from many suppliers as a standalone screen (and is
the same screen we used back in MIWM, Part 9, PE, Oct 2019).
It is also available as a Pico plug-in module (see Fig.1 from last
month – top-left image). If you search online for ‘Waveshare
Pico-LCD-0.96’ you will see that this plug-in display module
costs around £7.
With the display module and PicoMite connected to each
other, you will need to enter these two configuration settings
at the command prompt:
OPTION SYSTEM SPI GP10,GP11,GP28
OPTION LCDPANEL ST7735S,L,GP8,GP12,GP9,GP13
Once these are set, you will find all the usual MMBASIC
graphical commands will work with this screen. To test this,
try it with something like: CLS RGB(CYAN). If you do not
see the screen change to the appropriate colour, type OPTION
LIST and check the parameter values are entered as shown
above. Any errors will need to be fixed – you do this by first
typing OPTION LCDPANEL DISABLE and then either OPTION
SYSTEM SPI DISABLE (if you made an error with one of those
Practical Electronics | September | 2022
Fig.3. The Waveshare Pico-LCD-1.3 display module running the
clock code, as listed in the text.
Fig.2. Ensure your OPTIONs are set with these parameter values
to make the Pico-LCD-0.96-inch screen operate correctly. Shown
here, the program listing for displaying the clock in Fig.1.
parameters), or by re-entering the OPTION LCDPANEL setting,
this time with the correct parameters.
Next, set the TIME$ variable as close as possible to the correct
time with the command TIME$=”hh:mm:ss” (replacing hh,
mm and ss with the appropriate values). Finally, enter the
following five-line program code:
BOX 0,0,160,80,5,RGB(green),RGB(blue)
DO
TEXT 80,40,TIME$,cm,3,1,RGB(yellow),RGB(blue)
PAUSE 10
LOOP
RUN the above program to see the time displayed in the centre
of the screen; the result is shown in Fig.1. It is also worth
making the program automatically start up on power-up, so
stop the program (Ctrl-C), type OPTION AUTORUN ON at the
command prompt, and then RUN the program again. Note that
if power is removed, then the TIME$ variable will be reset to
’00:00:00’ at power-up. This is useful because it will better
demonstrate the IR time-synchronising that we will cover
shortly. Fig.2 is a screen grab of the OPTION settings, and also
shows the program listing (with a commented line at the start
showing which screen it is).
1.3-inch (240x240) square TFT
Another useful Pico plug-in display module is the Waveshare
PICO-LCD-1.3 (top-right, Fig.1 in last month’s article). In
addition to the 240x240 screen, there are four pushbuttons,
and a digital joystick (up, down, left, right and select). The
cost of this display module is around £8. To configure this
display, enter the following configuration settings at the
command prompt:
OPTION SYSTEM SPI GP10,GP11,GP28
OPTION LCDPANEL ST7789,L,GP8,GP12,GP9,GP13
The program is
identical to the one above
except for the differences
highlighted in bold.
Because these two
screens have different
pixel resolutions the code Fig.4. A standalone Waveshare 0.91needs to be altered so inch OLED module requires just four
that items are displayed connections to the PicoMite – see text
in the correct positions for details.
(and in a suitably sized
font). With the program entered, set OPTION AUTORUN ON
and then RUN the program. You will once again see the time
displayed in the centre of the screen (see Fig.3), and that
completes the second clock.
0.91-inch (128x32) OLED
OLED (organic light-emitting diode) displays are made up of
individually controlled LEDs, so they don’t need a backlight.
Hence images on an OLED are generally superior to those on
an LCD. They have high contrast, making them easy to read,
even compared to the older-style LCD character modules, such
as the 16x2, 20x2 and 40x4 displays.
Several Pico plug-in OLED display modules are available, but the
current ones are not directly supported by the PicoMite because
the relevant display drivers are not built into MMBASIC. However,
there is nothing to stop us from using a standalone OLED module
that is supported by MMBASIC. For example, if you search for
‘Waveshare 0.91-inch OLED’ you will see a compatible OLED
module that costs around £5. Note that this OLED module is not
a colour OLED; instead, it is mono (single colour), typically white
or blue. This display is easy to interface to a PicoMite since there
are just four wires to connect (refer to Fig.4):
OLED PicoMite
VCC 3V3
GND GND
SDA GP0
SCL GP1
To configure this OLED module, enter the following configuration
settings at the command prompt:
You will notice that the settings are almost identical to
the 0.96-inch screen above – the only difference being the
display driver parameter: ST7789 instead of ST7735S. This
is something I like about the Waveshare display modules;
they try to use consistent GP pin numbers for identical pin
functions across their range.
With the 1.3-inch display configured properly, quickly test
it: set TIME$ and enter the following five-line program:
As before, set the TIME$, set OPTION AUTORUN ON and enter
the following program code:
BOX 0,0,240,240,5,RGB(green),RGB(blue)
DO
TEXT 120,120,TIME$,cm,5,1,RGB(yellow),RGB(blue)
PAUSE 10
LOOP
CLS
DO
TEXT 64,16,TIME$,cm,3,1
PAUSE 10
LOOP
Practical Electronics | September | 2022
OPTION SYSTEM I2C GP0,GP1
OPTION LCDPANEL SSD1306I2C32,L
49
Once again, the time is displayed in the centre of
the screen; or more realistically, due to the lower
pixel-count (and appropriate font size), the time
essentially fills the entire screen space – see Fig.5.
1.28-inch (240x240) round LCD
This screen provides a nice alternative to the
usual square/rectangular displays. It is available
as a Pico plug-in module – see Fig.1 from last
month – top centre image. That one is advertised
as an LCD screen; whereas the standalone round
display is advertised as being an IPS screen (IPS is
generally regarded as giving a much better display
Fig.6. The Waveshare 1.28-inch
than an LCD). I ordered a standalone IPS display,
round LCD standalone module
but having seen it in operation, I must question
needs eight connections to the
Fig.5.
The
Waveshare
0.91-inch
OLED
whether it is really is an IPS display because the
viewing angles are more like a standard LCD. PicoMite clock – here it is using a white display. PicoMite – see text for details.
Anyway, since I received several emails asking
about the specific configuration setup for this display we’ll
the clocks to show the same time. Depending on how many
continue. Bearing in mind I have a standalone display rather
PicoMite clocks you have access to, you will need to modify
than a Pico plug-in module, I had to first make the following
the hardware of one (and only one) of them to act as the
connections (refer to Fig.6):
master clock by adding a simple IR transmitter (Tx) circuit.
The remaining clocks are modified to act as slave clocks by
LCD PicoMite
adding an IR receiver (Rx) to each one.
VCC 3V3
You will also need to make suitable changes to the software.
GND GND
The master clock program code needs to be able to transmit
DIN GP11
it’s time data via the IR Tx; and the slave clock program code
CLK GP10
needs to be able to receive this time information (via IR) so
CS
GP9
that it can update it’s TIME$ variable to the value received.
DC
GP8
Using this simple concept means that all clocks will display
RST GP12
the same time.
BL
GP13
Master clock
The above pins were selected to match the pin-out of the
equivalent Pico plug-in display module, so the following
should work for either type of round display module. If ypu
use a Pico plug-in module, you will notice that once again
Waveshare have used consistent GP pin numbers for identical
pin functions and hence it is just the display driver reference
(GC9A01) that is the main difference from what was used on
the previous (non-OLED) screens above.
By the way, the typical cost of the standalone display module
is around £13, whereas the Pico plug-in module (complete
with digital joystick) costs around £23.
To configure this display, enter the following configuration
settings at the command prompt:
OPTION SYSTEM SPI GP10,GP11,GP28
OPTION LCDPANEL GC9A01,L,GP8,GP12,GP9,GP13
After configuring it, quickly test it with something like CLS
RGB(red), then set the TIME$, and also set OPTION AUTORUN
ON. Last, enter the following program code:
The master clock IR transmitter circuit is shown in Fig.8 – it
can be connected to any spare GP output pin – in this case it is
connected to GP22. The component values used do not need to
be exactly the same – the circuit is provided more for guidance,
but remember that a Pico output pin has very limited current
drive capability, so an IR Tx LED cannot simply be connected
to a GP output pin. With this circuit in place, the Pico remains
within operational specification limits, and it also means that
the Pico can drive the IR Tx at 5V (resulting in better range –
not that much range is really required for this application).
Now build the IR Tx circuit and connect it to one of your PicoMite
clocks. As a reference, we built the circuit on a breadboard, then
used three male-female DuPont leads to connect it to the PicoLCD-0.96 PicoMite Clock (that was moved onto an Expander
module – see Fig.9). By using this display module for the master
clock, we were able to use the bottom right-hand button (referred
to as ‘User Key B’) as a trigger to transmit the time data. All
that was required to achieve this was the command SETPIN
GP17,INTL,btn,PULLUP at the start of the program code,
meaning that the program will jump to a subroutine (called btn)
whenever the button is pressed. We then put the IR transmitting
CLS RGB(blue)
DO
TEXT 120,120,TIME$,cm,2,2,RGB(yellow),RGB(blue)
PAUSE 10
LOOP
The program is identical to that above except for the differences
highlighted in bold. With everything entered correctly, you
will once again see the time displayed in the centre of the
round screen (see Fig.7); and that completes the fourth clock.
Adding IR
We are now going to discuss how to add IR functionality to
the clocks so that we can demonstrate how to synchronise
50
Fig.7. The Waveshare 1.28-inch round LCD PicoMite clock.
Practical Electronics | September | 2022
VSYS
7
Infrared (IR)
L ED
1
GP22
code in this subroutine. Transmitting
IR data is made simple thanks to
the MMBASIC command IR SEND
– please refer to the PicoMite User
Manual for more details about the
parameters used with this command.
Slave clocks
Each slave clock just needs a
standard TSOP IR receiver connected
GND
to any spare GP input pin – in this
case we will use GP22. Fig.10 shows
the connections but note that we
Fig.8. This simple IR
must connect the TSOP IR Rx to a
transmitter circuit uses
3.3V supply rather than the more
an NPN transistor, a
usual 5V supply. The reason for this
couple of resistors, and
is that the maximum input voltage
an IR Tx LED.
on any input pin (here GP22) is
3.6V. So, by supplying 3.3V to the TSOP, we will keep the input
voltage on GP22 below the 3.6V limit and not damage the Pico.
Connect a TSOP IR receiver to GP22, GND and 3V3, as
shown in Fig.10. We used three female-female DuPont leads
with the TSOP inserted at one end, and the other end used to
connect to a 1.3-inch PicoMite clock (that was moved onto an
Expander module – see Fig.11). The program code can then
use the standard IR interrupt (set up with the two commands:
SETPIN GP22, IR and IR DevCode, KeyCode, myIrInt).
This will result in the program code jumping to a subroutine
(here called myIrInt) whenever an IR signal is detected. We
then put the IR receiving code in this subroutine to process,
check, and if necessary, update the TIME$ variable.
Software
The two required program listings are a bit too long to print
line-for-line here, so we have made two downloads available
at the September 2022 page of the PE website. Download the
MasterClockIR.txt and SlaveClockIR.txt files and load them
into the relevant PicoMite clocks. RUN both programs, and if
using a PICO-LCD-0.96 as the master clock, press the ‘B’ button
to sync and update any slave clock that is within range of
the master clock. If you are not using a PICO-LCD-0.96 as the
master, you will need to modify the start of the MasterClockIR
program to allow for a suitable trigger (see comments within
the code for how to do this).
Fig.9. A Pico-LCD-0.96 master clock built on an expander
module allows easy connection via three DuPont leads to the IR
Tx circuit (which here has been assembled on a breadboard).
Button B is used to send the time data via IR.
that it is actually the Epoch time that is transmitted via IR, and
the SlaveClockIR code converts it back into a recognised date/
time. We do this because it means we only need to transmit
five 7-bit key code values to transfer both date and time (refer
to code comments for more details).
I hope this has shown you how easy it is to transmit a few
bytes of data between PicoMites. Why not have a think about
how you can use IR in your project other than to decode
button presses from an IR remote control. If you come up with
anything interesting, then do drop us an email and maybe we
can mention it in a future article.
Next time
Next month, we will continue our exploration of the PicoMite
and show you how to use a GPS receiver (another low-cost Pico
plug-in module) to
create a simple and
Questions? Please email Phil at:
useful tracker.
contactus<at>micromite.org
Until then, stay
safe, and have FUN!
Epoch time
If you take a closer look at the program code, you may spot
two new commands: EPOCH(now) and DATETIME$(n). The
first command converts the current date/time into ‘Epoch
time’ (a number which represents how many seconds have
elapsed since midnight GMT on 1 January 1970). The second
command converts an Epoch time value into the usual date/
time format. Why use these? Epoch time makes it much easier
to do date/time calculations. For example, to add an hour onto
the current time, convert the current
IC1
time into Epoch time, add 3600
TSO P 4xx
and convert back to normal date/
IR receiver
time. This way, we don’t have to
worry about roll-over for something
GP22
3V3
awkward like 31 December 1999
GND
at 23:34:56 (which would mean
rolling over date, month and year,
as well as the hour to ‘00’) all very
Fig.10. The IR receiver
tricky when you start looking into
comprises just a single
it – but very easy with conversion
component – a TSOP
to an Epoch-time value.
IR Rx. Ensure it is
Further examination of the
connected to the 3.3V
MasterClockIR code will reveal
supply – not 5V.
Practical Electronics | September | 2022
Fig.11. A Pico-LCD-1.3 slave clock built
on an expander module allows easy
connection to the TSOP IR Rx via three
DuPont leads.
51
|