4-Digit Counter with micro:bit

Difficulty
Easy
Steps
13

Besides having the onboard LED display on the micro:bit, you can also add an external display to it.

In this guide, we will use the micro:bit and an external 4 digit display to create a counter. We will use the TMP1637 package in the MakeCode editor to do this.

After completing this guide, you will have gained a basic understanding of how to use a 4 digit display module with the micro:bit.

Parts Used in This Guide

Buy

Step 1  The Module

Before we begin, let's take a closer look at the 4-Digit LED Module!

There are four pins:

  • Clock Input (CLK) 
  • Input/Output Serial Data (DIO)
  • Voltage Current Collector (VCC)
  • Ground (GND)

Step 2  Connect module to breadboard

Step 3  Connect GND to GND

Step 4  Connect 5V to VCC

Step 5  Connect P2 to DIO

Step 6  Connect P1 to CLK

Step 7  Add the extension

It's now time to work on the code! For this module, we will need to add an extension or package.

  1. First, open up the MakeCode editor and start a new project
  2. Click on Advanced > Extensions
  3. In the search box, paste or type :  TM1637 
  4. Click on the TM1637 package and it will be added to the MakeCode Editor

Step 8  Create 'on start' code

  • Now, we will begin by adding code to the 'on start' block.
  • Here, we will create an item for a counter with a starting value of 0. 
    • So, click on the 'Variables' tab.
    • Click on 'Make a variable'. 
    • Name it 'item'. 
    • Give it a value of 0
  • We will then create another variable, 'tm'.
  • From Variables add 'set....to...'  
  • Click on the TM1637 tab. Drag and drop a 'CLK DIO intensity LED count' block.
  • In this guide, we have wired:
    • CLK to pin P1 
    • DIO to pin P2

Step 9  The counter!

  • Click on TMP1637 and drag a ' ... show number ... ' block into the editor.
  • 'tm' will be the first parameter here, and 'item' will be the second parameter. 
  • Under variables, drag and drop a 'change ... by ... " block.  We will increase the value of item by 1.
  • After each count, there is a pause of 1 second. So, from the 'Basic' tab, drag and drop a 'pause (ms) ... ' block and change the value to 1000.

Step 10  Reset the counter!

Let's add a reset button to the counter. We will use a button on the micro:bit to do this. 

  • From the 'Input' tab, drag and drop a 'on button A pressed' block into the editor.
  • Click on the 'set item to 0' block above and copy it. You can do this by right-clicking on it and selecting 'Duplicate'.
  • Drag the copied block into 'on button A pressed'
  • Click on 'A' and change it to 'A+B'

Step 11  Turn the counter off!

Great! Next, let's make it so that when button B is pressed, the counter turns off.

  • Click on the 'Input' tab. Drag and drop a 'on button A pressed' block into the editor.
  • Click and right click on the 'set item to 0' block. 
  • Select 'Duplicate'.
  • Drag the copied block into 'on button A pressed'
  • Click on 'A' and change it to 'B'
  • From the TM1637 tab, drag and drop a 'turn off item' block
  • Change 'item' to 'tm'

Step 12  Turn the counter on!

So you can now turn the counter off as well as reset it. But what about turning it on?

  • Click on the  on button B pressed' block above, right click on it and select 'Duplicate'
  • Click on 'B' and change it to 'A'
  • From the TM1637 tab, drag and drop a 'turn on item' block
  • Change 'item' to 'tm'

Step 13  Upload the code

Finally, it's time to upload the code onto your micro:bit!

  1. Connect the micro:bit to your computer using a microUSB cable
  2. In MakeCode editor, click on the Download button on the bottom left-hand corner 
  3. Find the hex file in your Downloads folder or where you have saved it to
  4.  Open up Finder on the MacOS or Explorer on Windows, and drag the hex file into MICROBIT under 'Devices' on the macOS.
  5. The micro:bit will flash for a few seconds and then you will see the counter display start up!

Congratulations you have made your first mirco:bit counter, in summary you can start the timer by pressing 'A', stop the timer from counting by pressing 'B' and finally the counter can be reset by pressing 'A+B' at the same time.