Lesson 04 Photocell


A photocell is a kind of special resistor based on internal photoelectric effect. Its resistant value is opposite to the brightness of light. Brighter light leads to lower resistant value and in turn varies the voltage that passes through. Usually a photocell is the core component of a photoswitch. In the following experiment, we are going to use photocell to control the brightness of 5*5 LED matrix display on the BBC micro:bit.

What you need

  • micro:bit board
  • Micro USB Cable
  • micro:bit breadboard adaptor
  • Breadboard
  • Photocell
  • 10K resistor
  • Male to male jumper wires


Photocell is a kind of special resistor made of a semi-conductor material. It is based on internal photoelectric effect. Brighter light, lower resistant value. As with the increase intensity of light, its resistant value swiftly decreased. The minimum light resistance value can reach under 1KΩ and can be very sensitive to light. When there is no light, its resistant value arrives at its maximum value. Its dark resistant value usually can reach 1.5MΩ at max.

Hardware Connection

Please complete hardware connection according to the breadboard diagram below:

After connecting the circuit, you can see the below:


Open Microsoft Makecode, and write the code in the editor window, try programming by yourself first.

Of course, you can see the whole program in the link below. Just click “Edit” on the right top corner, then click “download” to download the whole code into micro:bit.


Link of the whole program: https://makecode.microbit.org/_iqDLctEkHFj1

Code Explanation


This is an assignment operation. set a to 1 equals to “a=1”. We use an equals sign to make a variable store the number or string. When you use the equals sign to store something in a variable, the equals sign is called an assignment operator, and what you store is called a value.

analog read

Read an analog signal (0 through 1023) from the pin you assign.

show icon

Shows the selected icon on the LED matrix on the micro:bit.

Clear Screen

Turn off all the LED lights on the LED matrix.

If else

In Lesson 2, we have talked about the usage of “if” sentence. In sentence of “ if else”, when the format value is false(or 0), then it will implement “else” block before running program in sequence. Here is the flow chart:

In this experiment, micro:bit reads the analog voltage of pin 0 as the reference value of brightness.

In the programming block “forever” , scan analog voltage of pin 0 in a loop. Once the voltage is lower than the reference value minus 2( it shows intensity of light increase, resistant value of photocell decrease), then it detects the light has been turned off.

In the program, calibrationVal-2 is for adjusting sensitivity. Lower value, higher sensitivity. However, it is not suitable for all kinds of light environment. You have to adjust this parameter until it can adapt to the present light condition.


Turn on a light, you will see the 5*5 LED screen on micro:bit become empty. Turn off light, LED screen displays a sign of heart.

Note: After resetting the micro:bit, it will calibrate the reference value according to the present brightness. So, in order to make the program run properly, you must start micro:bit program on the condition that the light is turned on .

Taking it further

If we want to use the photocell to control an LED, then how would you design 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 1:38pm on July 9, 2019
Last updated at 1:38pm on July 9, 2019