Software apps and online services
Hand tools and fabrication machines
Having previously created an tutorial (Binary DVM), that makes use of the limited display area using binary.
It was only a small step having previously created the main code module for Decimal to Binary conversion to creating a Binary Clock but the only thing missing was a RTC (Real Time Clock).
However, the Microbit does not have a build in RTC.
The RTC enables the realisation of clock projects with a battery backup.
As such the following project uses a Microbit and a Kitronik RTC to create a 24H clock with a Binary display and additionally has an alarm option.
The project software which will be to run on the Microbit will be created in Makecode Blocks.Supplies
The supplies are available from a number of outlets and you may have your own preferred supplier.Step 1: Defining the Display Area
Although the display area is limited in the amount of data that can be shown at any one time, it lends itself ideally to the display of bit data.
As such there is sufficient area to display 4 x 4 bit binary words to represent time with notifications and selection modes.
The display is split into 3 main areas; Time, Selection and Modes.
Sixteen LED's assigned to Time, each column of 4 LED's is assigned to a time interval, the intervals being in the form H, H, M & M.
Each bit of the Binary word has a weighting of 1, 2, 4 & 8 with the LSB on row 4 and the MSB on row 1.
Each Binary 4 bit word allows a count of 0 to 15, which is more than sufficient for the 24H time format, requiring a maximum count per column of 2, 9, 5 & 9.
One row of 4 LED's at row 0 are used to identify the time column selected when entering time.
One column of 5 LED's at column 4 are used to identify Modes, Functions and Operation.
Tick - LED 4, 0 flashing on & off is used to indicate Seconds and operation.
Time - LED 4, 1 indicates Time mode when on. (Default mode at switch on)
Alarm - LED 4, 2 indicated Alarm mode when on.
Alarm Notification - LED 4, 3 & LED 4, 4 flash when the Alarm is triggered.
.Step 2: RTC (Real Time Clock)
The RTC is the beating heart of the application, allowing the setting & keeping of accurate time.
Further details of the RTC can be found at Kitronik.
The RTC provides a regulated supply negating the need to power the Microbit by its own USB or JST connector and battery backup is provided to retain the time in the event of power loss.
Before using the RTC you will need to load the Extension package.
Using Makecode from the Settings icon, select Extensions and type Kitronik RTC in the search.
Select the package to install it and it will be added to the other extensions.
There are a number of code blocks to read from and write to the RTC.
We will only require 4 of these code blocks for the Binary Clock.
These will be utilised to write the set time to the RTC and to read the time back to update the clock display.Step 3: Coding the Clock
The first part of the code is program initialisation of variables, arrays and informative text.
Bclk – Binary Clock
<Sel - A button selects the column that will be adjusted for time setting.
>Inc – B button increments the time.
Pressing both A & B buttons together changes the mode between Time & Alarm.
Strval – is the string value containing the time in the form “HH:MM:SS” returned from the RTC
Only HH & MM are used to display or set the time.
Mode – retains the mode value for Time = 1 and Alarm = 2 selected with the A+B button combination.
Period – is the value for the time column, selected with the A button.
0 = column 0 (H), 1 = column 1 (H), 2 = column 2 (M), 3 = column 3 (M)
Tick_en – Enables = 1 or Disables = 0 the tick (seconds), indicator.
Inc – Intermediate storage of the incremental time setting value.
Tm_list – stores the value of each time column during setting.
Alarm – Enables or Disables the Alarm indicator.
The forever continually calls the tick function.
The tick function which is normally enabled, displays an alternating on/off LED in the top right hand corner to indicate operation and seconds.
Additionally, it calls the showtm function which reads the RTC and processes this to be displayed in binary, whilst also calling alarm_mode, if this is enabled displays the alarm notification LED’s in the bottom right hand corner.
Function showtm, calls rdtime and the value used from this is strval containing the time string.
A loop is created which increments through strval extracting each single number and ignoring the separator “:”.
Each single number is then converted into its binary equivalent with function dec2bin and assigned to the correct column.
Function rdtime, reads the first 5 characters in the string returned from the RTC (ignoring the seconds portion), and passes it to strval.
If the alarm was set (Mode = 2), then the alarm set values are compared to the values returned by the RTC, if there is a match then alarm = 1 if there is no match alarm = 0.
Alarm_mode when enabled displays two on/off alternating LED’s on the lower right-hand corner of column 4.
Function dec2bin converts a decimal number to binary and displays it in the correct column.
The number to be converted is passed in via value and the display column is passed in via col.
List2 is the array into which the 4-bit binary word from the binary conversion process is stored.
A loop is initiated that proceeds to divide the value by 2 the remainder is stored in the array element the integer value is divided by 2, this is repeated until the integer value is <=1 and this last value is placed in the array.
The largest single digit decimal value is 9 and in binary this is 1001 as a 4-bit word.
The array then needs to be processed in reverse order for the correct result.
A loop is then initiated to switch on the correct LED in the appropriate column, this is accomplished for each occurrence of a one in the 4-bit binary word.
The human interface is accomplished with buttons.
The A button.
This selects the column into which the time value will be entered and displays an illuminated LED over the chosen column on row 4.
Once all the time columns have been updated, incrementing the selection to the 5th column updates the time variable.
If mode = 1 the RTC is updated otherwise the Alarm time is updated.
The B button
This is the increment button and increments the selected time column.
In order to reduce errors and save time in getting to the correct value the maximum value that can be entered per column is capped based on the time value for a 24-hour time system.
These maximum values are stored in tm_max, one per time column and are automatically selected based on the time column.
The maximum values are H = 2, H = 9, M = 5, M = 9
The increment value is converted to binary in dec2bin and the display updated.
Button A+B Mode selection.
Pressing both buttons together allows selection between the Time mode or the Alarm mode the appropriate mode is displayed on the display.
Depending upon which mode is selected the display is updated to show either the Time or the Alarm set time.Step 4: Operation
Download the Hex file to the Microbit, insert a CR2032 battery into the RTC.
Plug the Microbit into the RTC and power the RTC board via USB or the screw terminals.
The Tick LED will start to flash and shortly after this the time will be displayed.
If this is the first time of use the displayed time is very likely to be wrong and will need to be set to the correct time.
Pressing the Selection (A) & Increment (B) buttons together will allow cycling of the Mode options between Time & Alarm.
Setting the Time
The time setting is in 24H mode.
Use the Selection button (A), to move the LED across the top row, this indicates the column where the time can be changed. The selection columns correspond to H, H, M & M.
Where H = Hours and M = Minutes.
Having selected a column press the Increment button (b), repeatedly to increment the count by one on each press. The counts are indicated in binary, after all its a Binary Clock.
The increment button only increases the count and once the maximum is reached resets to zero, further presses will again increase the count.
Once the first column time is set, press the Selection button for the next column then use the Increment button to set the column time.
Note: *** When you set the Time or Alarm you will need to enter a time in the selected column even if the time in the column is to remain unchanged as skipping a column sets that column time to zero ****
Repeat the process until the time has been set using all 4 columns.
Press the Selection button for the fifth time to move it to the fifth column and the time is set.
Setting the Alarm
Setting the Alarm time is done in exactly the same way as for the Time.
In order for the Alarm to be triggered on the required time leave the Mode set to Alarm.
To switch the Alarm off set the Mode to Time.
To display the Alarm time set, cycle the Mode between Time and Alarm and the Alarm time will be displayed for a short period of time before reverting to displaying the current time.
The Alarm time is not stored in the RTC, therefore if power is removed it will require resetting.Step 5: Box It
The project will sit at the appropriate angle to view the clock but a box adds a sense of permanence.
You could buy a suitably sized box and cut and drill out the appropriate areas to allow the Microbit to fit in the socket.
However; additionally I wanted to duplicate the Microbit buttons along with other controls and indicators.
Typically, legends will need to be applied to the box to identify buttons.
These could be applied by hand; painted, engraved or applying labels.
A method to realise all these options would be to 3D print the box but first we would need to create a CAD file with which to create the printer file.
The file creation options are hand drawn or drawn with code.
I opted for "drawn with code" using Tinkercad CodeBlocksStep 6: Block Code Design
The box is a two part design consisting of a base and a lid.
Four corner screw holes will be used to secure the lid with a cut-out on the left hand side to allow entry for the USB plug.
The lid will have a cut-out for the Microbit connector and the required switches, in addition any text will be printed directly on the lid and screw holes will align with the base pillars.
The RTC board will be attached to the underside of the lid by 4 pillars and 4 screws.
The size of the box plus lid is 70 x 105 x 31 mm
The code for the Lid and the Base is available in TinkerCad CodeBlocks.Step 7: 3D Printing
Load the files into Cura and apply your preferred slicer settings.
Infill: 80%, Tri-Hexagon
Save your files then print.
With Cura you can load both files together on the same print area and print in one go.Step 8: Infill
The box lid is printed with recessed text which will be filled with coloured 2 part epoxy resin.
The resin is mixed with a ratio of 2 parts resin to 1 part hardener then an opaque colour pigment is mixed in.
The colour chosen was Yellow to contrast with the background. White would have been another choice.
Once mixed the resin is dripped into the recess by using a cocktail stick to transfer small blobs of resin which are used to gradually fill the recess in the letters.
Resist putting in too much resin in one go as you will likely end up with air bubbles and or creating too much overflow to the surrounding surface meaning you will have more to remove cleaning up and sanding once cured.
So fill slowly ensuring the bottom of the letter is covered and build it up finishing with a slight raised surface.
Once the resin has cured some light sanding will be required to level the surface, start with P240 grade progressing to finer grades as required to get a smooth finish finally finishing with a polish.
Don't apply too much pressure and too much speed or you will overheat the PLA and the resin resulting to a dull surface due to grit pickup, a little water applied during the sanding process will act as a lubricant and coolant.Step 9: Assembly
The Microbit will fit into the socket in two orientations, facing the main part of the RTC of facing away.
If facing the main part of the RTC the link connections cannot be used however, if the Microbit is facing away from the main part of the board then we can make use of these connections.
The assembly starts by soldering a right angle SIL pin header onto the RTC, this is to enable the connections to be made with push fit connectors.
The RTC is fitted by 4 * M3 (10+6mm), M/F standoffs with M3 nuts which are secured to the lid with 4 * M3 (8mm), screws in the prefabricated holes.
The switches are fitted in the prefabricated holes in the lid.
The connections required are:
GND, 3V, P1 (set), P2 (alert), P5 (sel) & P11 (inc)
Note *** Resistors (1R), on the diagram for P5 & P11 are simply reference connection points as direct connection to these pins on the Microbit in Code blocks is not available at this time. ***
P5 is the external connection for Button A which is connected by a SPST momentary switch. One connection to P5 and the other pin is connected to GND this button is for column selection during time setting.
P11 is the external connection for Button B which is connected by a SPST momentary switch. One connection to P11 and the other pin is connected to GND his button is for number increment during time setting.
P1 is a SPDT (on-on) switch which is used to enable or disable the setting options. The centre pin goes to P1 whilst one pin to connected to GND and the other to 3V via a 10k resistor. This enables a H (3V) and L (0V) to be applied to this pin. When P1 is connected to 3V this enables the time setting options and when 0V disables time setting. Thereby controlling whether the A & B buttons have any effect.
P2 is a SPDT (on-off-on) switch which is used to enable or disable the alert sounder and optional external lights.
The alert sounder is a Piezo Buzzer (simply affixed with a double sides sticky pad), requiring a pulse drive which is provided by the Microbit.Step 10: Finally
You've assembled the elements in the box, programmed the Microbit and fitted it into the socket on the box.
Next apply the power and set the time.