# 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

## 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)
• 3.3V  : 'VCC' stands for Voltage Common Collector, we'll connect the VCC pin to 3.3V on the micro:bit
• GND: In electronics, we define a point in a circuit to be a kind of zero volts or 0V reference point, on which to base all other voltage measurements. This point is called  ground or GND.
• Note: Voltage is the difference in potential between two points. As it is difficult to talk about voltage without a reference point, we need another point to compare it to.

## 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'.
• 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'