BBC micro:bit
Light Dependent Resistor

Introduction

A light dependent resistor is a resistor which varies depending on the amount of light that is illuminating it. The one used on this page costs about 50p to buy and looks like this.

LDR

Circuit

The circuit needs you to use a 1-10KOhm resistor something like this.

micro:bit Code

This configuration gives a low number when the LDR is lit up, a high one when it is covered.

You get the reverse of this if you change the configuration slightly. Connect one pin of the LDR to the 3V pin. Connect pin 1 to the other pin of the LDR. On that same pin, connect the resistor to GND.

For me, I got no difference between the range or consistency of the readings for each method.

Programming

To make use of the LDR, you need to get a sense of the numbers it is working with and how they are affected by the amount of light. It takes a while for a large number to scroll across the matrix. Having to press the A button makes it easier for you to know what was happening to the LDR when the reading was taken. You need to know the range of readings that you get. Start by testing when nothing is covering the LDR and note down the reading. Then cover it and note down the reading. Finally, try placing your hand at different distances from the LDR and taking some readings.

micro:bit Code

The readings you get will differ depending on the light conditions of the room where you use the circuit. You need to get the range of values to be able to do something in your program when the value is at a particular level. My readings were from 250 to 750 and varied in quite large chunks with the distance my hand was from the sensor.

Challenges

  1. An LDR can be used like a touch button. Write a program that checks the light readings and responds like a button when the LDR is touched. If you have 3 of these, you could connect one on each of the large pins.
  2. Add an LED to your circuit or light up the matrix when the lDR indicates that it is dark.
  3. Use analog write blocks or vary the brightness of the LED matrix depending on the amount of light hitting the sensor.
  4. Take the LDR reading and use it to make a buzzer sound on pin 0 at a tone that depends somehow on the amount of light hitting it. You could use the raw LDR reading as the frequency.
  5. Use 2 LDRs and make it so that a sprite can be moved around the matrix by varying the distance of your hands to each of the LDRs. Have one of them control the x of the sprite, the other its y.