Sample Code for MakeCode - Light
Last revision 2026/01/26
The article offers sample code for using a micro:bit to measure ambient light, exploring the principles of light sensing and practical applications such as automatic street lighting and dawn wake-up service. It covers hardware setup and programming techniques to effectively utilize light sensors.
Principle
1.Light
First let's share some interesting little things about "light":
The speed of light in a vacuum is about 300,000 kilometers per second, which is the largest speed in the universe.

People will transplant bacteria that cause fireflies luminescent, infect tobacco, tobacco can grow to glow. Scientists have boldly envisioned to plant luminescent plants on both sides of the road, so that could they be used as streetlights?

Some of the bacteria on land emit colorful light at night, and so far, the mystery of their glow has not been completely figured out.

Shadow less lamp, is an advanced light source, its shape is a large light plate, which contains a lot of fluorescent lamps that shoot into all directions, and all the shadows of the operating table are illuminated, so there is no shadow under the shadow less lamp.

2.How do we see light?
Living in this world, we can see the sunlight, moonlight, lamp light, movies, television, the fundamental reason is the light.
Eyes, a sophisticated camera. The eye, like the camera, can also be divided into two parts according to its function, the front cornea, lens and glass equivalent to the camera lens, which belong to the refractive system.

The retina at the back equals to the film of the camera and belongs to the photosensitive system. The refractive system is actually like a lens that brings images of the vast world to converge into a tiny retina.
3.How to measure the light strength?
Light has different strength, strong or weak. Light strength is not the same in the morning, noon, evening and night. So, how do we measure the intensity of light?
Typically, we use something called a "light sensor" to measure the intensity of light. A light sensor is a device that uses the size of the light value to change the output resister or output voltage. The following image is one of the light sensors:

Preparation
The device in the red circle below is the on-board light sensor: it is a mini high-sensitivity optics hardware that not only can detect the intensity of the light, but also distinguish colors.

For more information, refer to the Getting Started guide.
Sample Code 1
Let's write a program to detect the ambient light values, as follows:

Result 1
This is a simple program that detects the intensity of light and displays the light intensity data obtained on the OLED screen.
Sample Code 2
We get the intensity of the light, but how could we use this data? In real life, where might light sensors be used? For example, street lamps does not light in the daytime, but at night, the street lights on both sides of the road will automatically light. Here it may use two principles. One is to light ON and extinguish automatically at the fixed time. The other is to use light sensors to detect ambient light, when the light is dim, turn on the street lights automatically. Let's use this principle to make a funny day-lighting auto wake-up service program.

Result 2
The program simulates a scenario. When the light from the window hits the sensor in the morning, the horn makes a musical sound to remind you that it's time to get up. So, you press the A key, the music stops playing, and the bedside light lights up. When you are dressed, press the B key, the bedside lamp goes out and the program enters the next waiting state.
Conclusion
Through the learning above, we have learned the following:
- Learned knowledge of light.
- Learned how people see light.
- Learned the measurement method of light.
- Through the programming of dawn wake-up service device, we understand the principles and applications of light sensors more deeply.
Was this article helpful?
