Lesson 14 Ambient Light


In one of our previous lessons “Lesson 04: Photocell”, we connected a photocell externally to sense the light. Actually, the micro:bit has an integrated light detection device internally. Today we are going to use that internal light sensor to do an experiment. We will light the rainbow LED ring when the light in the outer environment is comparatively dark.

Component List

  • micro:bit board
  • Micro USB Cable
  • micro:bit breadboard adaptor
  • Breadboard
  • 8 RGB Rainbow Ring
  • Male to male jumper wires

Light Sensing

You might be curious how micro:bit senses light without any light sensor. Let’s see a paragraph about the display from the micro:bit website:

The display is a 5×5 array of LEDs. It is connected to the micro:bit as a 3×9 matrix. Runtime software repeatedly refreshes this matrix at a high speed, such that it is within the user persistence of vision range, and no flicker is detected. This LED matrix is also used to sense ambient light, by repeatedly switching some of the LED drive pins into inputs and sampling the voltage decay time, which is roughly proportional to ambient light levels.

That’s really amazing! Except for its light emitting ability, micro:bit can sense light!Because LEDs are so commonly used as light emitters it is easy to forget that they are fundamentally photodiodes, and as such are light detectors as well.The function may be implemented with no additional hardware cost if incorporated in the LED driver circuit.

Hardware Connection

Please complete the hardware connection according to the breadboard diagram below.

After connection, you will see:


Open Microsoft Makecode, write your code in the edit area.

Of course, you can download the whole program from the link below.


Code Explanation

Find the light level (how bright or dark it is) where you are. The light level 0 means darkness and 255 means bright light. The micro:bit measures the light around it by using some of the LEDs on the LED screen.

Set the color value of all RGB LED beads to be 0.

Use LED as a light sensor is not sensitive. It requires comparatively brighter light so that light level can change.


When in darkness, the LED ring will be turned on; while in brightness, the LED ring will be turned off.

Taking it further

If we want to rotate the rainbow color after the ring was illuminated, then how would you design the circuit and program it?

micro:bit Starter Kit Lessons

  • Lesson 01 – LED
  • Lesson 02 – Button
  • Lesson 03 – Trimpot
  • Lesson 04 – Photocell
  • Lesson 05 – RGB LED
  • Lesson 06 – Self-lock Switch
  • Lesson 07 – Temperature Sensor
  • Lesson 08 – Servo
  • Lesson 09 – Buzzer
  • Lesson 10 – Motor
  • Lesson 11 – Rainbow LED
  • Lesson 12 – Accelerometer
  • Lesson 13 – Compass
  • Lesson 14 – Ambient Light
First published at 4:59pm on July 9, 2019
Last updated at 4:59pm on July 9, 2019