First Program with micro:bit: Beating Heart
Upload your first code to the micro:bit: a big and a small heart take turns on the LED display and beat with a double thump like a real heart. No wires or extra parts; just connect the board to a computer over USB. display.show shows an image and sleep makes the program wait.

Video
Circuit Diagram

Source Code
1# ─────────────────────────────────────────
2# BlueGrays · bluegrays.com
3# First Program with micro:bit: Beating Heart
4# ─────────────────────────────────────────
5# A big and a small heart take turns on the micro:bit's LED display. Like
6# a real heart it beats twice in a row, then rests for a moment. No extra
7# parts are needed.
8#
9# Pins:
10# LED -> Onboard 5x5 LED display
11
12from microbit import *
13
14# ============ SETTINGS ============
15BEAT_MS = 120 # Half of one beat (ms)
16REST_MS = 520 # Rest between two heartbeats (ms)
17
18
19# One beat: the heart grows, then shrinks.
20def beat():
21 display.show(Image.HEART)
22 sleep(BEAT_MS)
23 display.show(Image.HEART_SMALL)
24 sleep(BEAT_MS)
25
26
27while True:
28 beat()
29 beat()
30 sleep(REST_MS) # Rest
31FAQ
Can I make the heart beat faster or slower?
Yes. BEAT_MS is half of one beat and REST_MS is the rest between two heartbeats. With the values in the code one heartbeat takes 1 second, so 60 beats per minute. Set REST_MS to 270 and a heartbeat drops to 0.75 seconds, which is 80 beats per minute. Smaller numbers make the heart faster.
Can I draw my own shape?
Yes. You can write your own image as Image("09090:99999:99999:09990:00900"). The five groups split by colons are the five rows of the display, and each digit is the brightness of one LED: 0 is off, 9 is brightest. There are many built-in images too, such as Image.HAPPY, Image.DUCK and Image.GHOST.
How do I put this code on the micro:bit?
The code is MicroPython. Open the micro:bit Python Editor (python.microbit.org) on a computer, delete the code that is there and paste this one. Connect the board with a USB cable and press Send to micro:bit. If that button does not work, use Save to download the .hex file and copy it to the drive called MICROBIT. You can rebuild the same project with blocks in MakeCode, but this code cannot be pasted there.
Does it work on the older micro:bit (V1) too?
The code is written for the micro:bit V2. Projects that only use the LED display, buttons A and B and the accelerometer also run on a V1. The speaker, the microphone and the touch logo exist only on the V2, so code that uses them gives an error on a V1. If your board has a speaker on the back and a gold logo on the front, it is a V2.
