In this guide, you will learn how to sense the light level using a micro:bit. 

By use of a clever bit of coding the micro:bit's display LEDs can be used to measure the light level, without the need for any extra hardware. However, this feature is only available when using the JavaScript blocks editor. 

You can however attach a resistor and photoresistor to the micro:bit and use an analog input of the micro:bit to measure the light level. This has the advantage that once you know how to do this, you can attach other analog sensors to your micro:bit and make measurements with them.


If you just want to measure light using the built-in facility then you just need a micro:bit.

When it comes to the choice of resistor to use, I have used a 2.2kΩ resistor here, but a 1kΩ or 10kΩ resistor will also work, but will change the range of readings.

BBC micro:bit

The British Invasion is here! No, not music...microcontrollers! New to the USA is the newest and easiest way to learn programming and electronics - the BBC...

Small Alligator Clip to Male Jumper Wire Bundle - 12 Pieces

For bread-boarding with unusual non-header-friendly surfaces, these cables will be your best friends! No longer will you have long strands of alligator clips that are grabbing little...

Half-size breadboard

This is a cute half size breadboard, good for small projects. It's 2.2" x 3.4" (5.5 cm x 8.5 cm) with a standard double-strip in the middle and two power rails on both...

Photo cell (CdS photoresistor)

CdS cells are little light sensors. As the squiggly face is exposed to more light, the resistance goes down. When its light, the resistance is about 5-10KΩ, when dark it goes up...

Through-Hole Resistors - 2.2K ohm 5% 1/4W - Pack of 25

ΩMG! You're not going to be able to resist these handy resistor packs! Well, axially, they do all of the resisting for you!This is a 25 Pack of 2.2K...

Built-in Light Sensor

To try out the built-in light sensing capability of the micro:bit, run the JavaScript Blocks code below.

From the editor, you can click on Download button (bottom right) and then copy the downloaded file onto your micro:bit. Alternatively, you can Click here to open the editor in a separate browser tab.

The light level block returns a value between 0 and 255 where 0 is dark and 255 is very bright. To fit this into a single digit range between 0 and 9 this number needs to be divided by 28.

You may be looking on the front of the micro:bit for a light sensor, but you won't find one. The micro:bit actually uses the LEDs of the display as a light sensor.

The technique for this relies on the fact that when an LED is reverse-biased (the wrong polarity for producing light) it can be filled with a small amount of charge. If you then let one of the pins for the LED act as an input and the other as a low output, the charge on the LED will leak away until the digital input stops being HIGH and becomes LOW. The time taken for this to happen depends on the amount of light falling on the LED. You can read more about this technique here

Breadboard Layout

Push the wires of the photoresistor and resistor into the breadboard in the positions shown below. Note, it doesn't matter which way around the leads go for either the resistor or photoresistor.

Then connect the breadboard to your micro:bit using the alligator clip to male header pin leads.

The photoresistor and fixed value resistor (2.2kΩ) are in an arrangement called a voltage divider. The point where they are connected (called the voltage divider's output) together will have a voltage that depends on the ratio of the two resistors. Since the fixed resistor never changes value, then the photoresistor (whose resistance varies with the amount of light falling on it) will control the voltage at this point. This voltage is connected by the yellow lead to pin 2 of the micro:bit which is used as an analog input to measure this voltage.

If the photoresistor is very brightly lit then it will have a low resistance and the output voltage will be 'pulled up' towards 3V. If on the other hand the photoresistor is in the dark, it will have a very high resistance and the fixed value resistor will dominate, pulling the voltage down towards 0V.


Whatever language you use to measure the light level, the program will follow the same basic pattern. The voltage will be read at pin 2 and then scaled to produce a number between 0 and 9 to be displayed on the micro:bit's LED display.

JavaScript Block Code

The JavaScript Blocks Code editor is embedded directly on this page below. From the editor, you can click on Download button (bottom right) and then copy the downloaded file onto your micro:bit. Alternatively, you can Click here to open the editor in a separate browser tab.

The result of the analog read pin block will be a number between 0 and 1023 where 0 represents 0V and 1023 represents 3V. This value is stored in the variable reading. This is then scaled to be a single digit number by dividing by 50. You can change the number 50 to make the light readings more or less sensitive depending on the range of light you are interested in. For instance the light level indoors is considerably less then outdoors on a sunny day. 


To run the MicroPython version of the code, open up the online Python editor here and paste the following code into the editor window.

Download: file
from microbit import *

while True:
    reading = pin2.read_analog()
    number = int(reading / 50)

So that the display only shows a single digit, the number is converted to the integer part of the number (no decimal point) using the int function.

The str function must then be used inside the show method, to convert the integer value into the string that show expects.


Make sure that you have your Arduino environment set up for micro:bit by following this guide.

Now start a new Sketch by clicking on the File menu and New. Then paste the following code into the editor window.

Download: file
#include <Adafruit_Microbit.h>

const int readingPin = 2;

Adafruit_Microbit_Matrix microbit;

void setup() {

void loop() {
  int reading = analogRead(readingPin);
  int number = reading / 50;

Other Things to Do

You might like to try and turn your micro:bit into an automatic night-light that sets the screen to all LEDs on when the light level falls below a certain level.

About the Author

If you have found this lesson useful, and want to learn Python, then you might like my book Programming the BBC micro:bit: Getting Started with MicroPython. 

This guide was first published on Mar 09, 2018. It was last updated on Mar 09, 2018.