5. Bedside light

This is a very simple project to introduce the children to sensing and control which makes use of an LDR as a light sensor and an LED for the bedside light.

This also introduces the concept of an analogue input. A digital input is either ON or OFF. There are only two possible conditions. An analogue input is one from a range of possible values. In the case of the microbit, from 0 to

Once these principles are understood, the tasks can be re-imagined to fit hundreds of different scenarios. The bedside light could become a street light, or emergency lighting on the inside of an aircraft etc. etc.. The aim for every teacher should be to find a relevant context that links to the wider curriculum.

Task:

To make a child's bedside light that will come on automatically when it gets dark and turn off when it gets light.


Algorithm:

Repeat forever

If light sensor is high turn light off

if light sensor is low turn light on

Model:

A light dependent resistor (LDR) can be used as the light sensor. This is a cheap component that can be purchased for less than £1.

An LDR does not operate like a simple on/off switch.

As the light level is reduced, the LDR's resistance increases and the voltage flowing through it to pin 2 decreases.

The script must read the analogue value between pin 2 and GND. For reasons that will be explained in the Science section of this website, a fixed resistor is connected between 3 volts and pin 2 on the micro:bit. The resistance of R2 should be similar to the resistance of the LDR.


But do not worry if you don't understand all of that, just follow the diagram on the right and pictures below.

By experimentation, it was discovered that an analogue input of greater than 950 is a good setting to turn the bedside light on.

This experimentation or calibration activity is an excellent way for children to develop an understanding of analogue control systems. The optimum values will vary depending on the resistance of the LDR used and the light level required to switch the LED on and off.

An LED (with a limiting resistor R1) connected between pin 1 and GND is used for the light.


MakeCode Editor script:


Micro Python script:

from microbit import *

while True:

sleep(100)

if pin2.read_analog() > 950:

pin1.write_digital(1)

else:

pin1.write_digital(0)