# Drive 4 digit 7-segment LED displays direct from a micro:bit

I bought a bag of random 7-segment LED displays to play around with, originally with the idea of adding them to the address and data busses on my 6502 breadboard computer. That became a bit tricky, but I got thinking about adding them to a BBC micro:bit. You can buy excellent displays for the micro:bit, like this one from Monk Makes, but I decided to to build and code my own from scratch, and like building and coding my own keypads, I learned a lot about an electronic component we take for granted.

It turns out that single-digit displays work pretty much as I expected. Each segment has a pin, and you send some volts to the pins and they light up, so a display that shows one digit and a decimal point will have nine pins, one for each segment and a common anode or cathode.

Multi-digit displays work rather differently, however. A 4 digit display does not use 4 x 8 pins. Instead it uses the same 8 pins to choose which segments to light plus 4 extra pins to chose which digit is going to light up at any given time. This means you have to multiplex the display: set the pins for the first digit, light it up by selecting it, change the segments and select the second digit, and so on. So each number is flashed on in turn. The video at the top of the page shows it working slowly. Normally you flash the digits so quickly, persistence of vision means that to the human eye it looks like they’re all on at the same time. This article on Raspi.TV explains it really well.

My display is a common cathode type. This means that you have to ground each digit selection pin in turn to make that digit light up.

If you want to try this, as well as the 4 digit LED display you’ll need a micro:bit, a breakout board, a breadboard, some jumper wires and around 7 or 8 resistors. I used 330 ohm resistors, but you may be able to use lower values depending on your display. I didn’t wire up the decimal point, just the 7 segments. I connected them like this:

```micro:bit pin   Resistor?   LED pin
--------------------------------------------------
0               yes         1
1               yes         2
8               yes         4
9               yes         7
12              yes         11
2		yes         10
13              yes         5
14              no          Digit 1 select (pin 12)
15              no          Digit 2 select (pin 9)
16              no          Digit 3 select (pin 8)
10              no          Digit 4 select (pin 6)
```

I decided to write the program to drive the display using MakeCode blocks. You can see the project here: https://makecode.microbit.org/_HxbLuUWob6Rs

It’s a simple counter that goes up and down when you press button B or button A. A forever loop keeps scanning the display, grounding each digit pin in turn to show the correct number. Timing is pretty crucial here, so I put the main program loop inside a ‘while true’ block inside the ‘forever’ block to remove the small delay that MakeCode adds to forever loops. It seems to work! I did occasionally see some ghosting artefacts, though I think it’s pretty amazing it works at all.

So, next time you look at a numerical LED display on a clock, cooker, or fridge, spare a thought for the design effort that went in to making it work.

This entry was posted in computers and tagged , , . Bookmark the permalink.

### One Response to Drive 4 digit 7-segment LED displays direct from a micro:bit

1. Johan Halmén says:

A 300 ohm resistor is way too big for the led, but probably very good for the Microbit. On this page:
https://tech.microbit.org/hardware/powersupply/
…it says:
The on-board current budget will vary depending on the use of the display, the Bluetooth, microphone, speaker and other peripherals. You should allow a worst case budget of 30mA for when all on-board peripherals are in use, leaving V1 90mA / V2 270mA for circuits plugged into the edge connector.

The optimal current for one led segment is 20 mA. That makes 140 mA for one digit showing all segments. Add another 20 mA for the decimal point. The V2 Microbit can handle that, but not the V1 Microbit. I’m about to build something out of my Microbit and this kind of 4*7 segment display, but I only have a V1 Microbit. So I’m afraid I have to use a set of transistors to make that happen.