The challenge we have set for you is to create an automated light switch.
A good example is a street lamp which uses a light sensor to control its on/off switch.
The light, in this case an LED, must automatically switch on as it gets dark and switch off as it gets light.
It must use a sensor to measure the light level.
It must constantly compare the light level with a default value so that it knows when to switch on and when to switch off.
There are two circuits operating in this sensing and control task.
You are building an input > process > output control system.
light sensor computer (micro:bit) LED
This is the input circuit.
You will use a light dependent resistor or LDR as the sensor.
The LDR monitors the light level and constantly feeds the data into the micro:bit.
The darker it becomes, the greater the input value from the LDR.
The lighter it becomes, the lower the input value from the LDR.
LDR
The LDR is wired to the micro:bit as shown in the diagram below:
The green wire is connected to micro:bit Pin 3V.
The white wire from the LDR is connected to the Pin GND.
A third, yellow wire is joined to the connection between the LDR and the resistor. The other end of the yellow wire is connected to micro:bit Pin 1.
That completes the input circuit.
We have a decision to make.
When do we want the LED to come on?
How dark must it get before the light comes on?
Finding the correct input value from the LDR is known as calibration.
To calibrate the system, we must create blocks to display the input value on the micro:bit at the press of a button.
Experiment by recording the input data using a range of light levels (see the table below).
The calibration code is quite simple.
Pin 1 is continuously collecting analogue input data from the LDR. So you must create code that will display this analogue value when you press button A.
You will need the red, Advanced Pins, blocks. Here is the algorithm to help you.
When button A is pressed
Display the number analogue read from Pin 1
Experiment to complete the table below by gradually covering the LDR to simulate fading light levels and recording the input values.
Choose an input value somewhere between dim amd very dim as your default or switching value.
Note, the value will vary from circut to circuit so do not copy from another group.
The output circuit provides power to the light. In this case a red LED.
This is connected to micro:bit as follows:
The red, positive wire from the LED is connected to micro:bit Pin 0.
The black, negative wire from the LED is connected to micro:bit Pin GND.
You do not have to know the theory but the science is interesting.
A resistor reduces the current flowing in a circuit. This change in the current can be measured as an analogue value by the micro:bit.
The LDR's resistance decreases as the light level falling on the component increases.
So the brighter the light, the lower the resistance and the higher the electric current.
When using an LDR as a light sensor it is important to know that the resistance increases as the light level decreases.
For these reasons, the LDR can be used as an analogue sensor because it inputs a range of values to the micro:bit.
It is important for the coder to be well practised in the skill of designing algorithms in preparation to writing computer code.
There are a number of different ways of representing algorithms. We can use text, pictures, diagrams and flow charts.
The algorithm below is in the form of a flow chart.
By following the algorithm you will have to create the control code for the micro:bit using the MakeCode editing environment.
The decision symbol which compares the current light level with the default value is coded using a combination of a conditional statement (if else) and a comparator (input > default).
If it is true that the current light level, input by analogue read pin P1 is greater than the default value of say 800, it will turn the LED off.
If it is false that the current light level, input by analogue read pin P1 is greater than the default value of 800, it will turn the LED on.
The screen shot below shows the code being simulated to test it.
Using the simulator to test code before downloading it to the micro:bit
When testing their code, the learners used a default value of 800 to switch on the lamp.
They are using the simulator to compare the analogue read pin P1 value with the default.
In their test, the learners have set the analogue read pin P1 value to 868.
This is greater than 800, so the simulator has changed digital write pin0 to 1 (which turns the lamp on).
N.B. The concepts taught in this challenge are fairly universal and are applied to an enormous number of very different sensing and control systems.
A conditional statement with a comparator
Simulator testing the code
Input is > 800 so output Pin 1 is on