BBC micro:bit – Lightning Distance Calculator

Lightning FlashHave you ever seen a lightning flash or heard the thunder of lightning and wondered how close you were from the lightning strike? Have you noticed that there often was a delay between the flash of light and the clap of thunder when a lightning occurs?

It is possible to calculate the distance to a lightning strike by counting the seconds between the lightning flash and the sound of thunder.

When lightning strikes, the first thing you see is the flash of light which you can see instantly. This is because light travels at a very high speed (Speed of light = 300,000 km/s). At the same time a clap of thunder is created. However, in the air a sound wave does not travel as fast as light, so it may take a few seconds for this clap of thunder to reach you. This depends on how far you are from the lightning.

Look at the following diagram and check the formula of speed. Bear in mind that the speed of a sound wave in the air is 340 m/s.

Your Challenge

Write a program for the BBC micro:bit that will allow the user to:

  1. Press the A button of the micro:bit when they spot a lightning flash.
  2. Press the B button of the micro:bit when they hear the thunderstorm.
  3. Display the distance (in meters) from the lightning strike using the LED screen.


We will complete this project in three main coding blocks.

First, let’s display an arrow pointing left to invite the user to press button A when they see a lightning strike.

Then we will record the exact time (in milliseconds) as to when button A was pressed and store this number in a variable called lightning. We will also display an arrow pointing towards button B.

Let’s now record the time (in milliseconds) when button B is pressed using a variable called thunder. We can then use these two timestamps to work out the time delay between the lightning strike and when the thunder was heard. Using the distance = speed x time formula, we can then workout and output the distance in meters from the lightning strike:



The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 3.9 / 5. Vote count: 9

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: