Light Sensing Codebug

Introduction

Have you ever noticed that street lamps and security lights turn on automatically at night? How do they do that? The answer is surprisingly simple, they have light sensors in them to work out how dark it is. If the sensor doesn’t detect enough light, it will turn the lamp on.

Now you can make your own automatic light with CodeBug and a light dependant resistor (LDR).

You will need
CodeBug CodeBug
Micro USB cable
Computer
Crocclips
Light dependant resistor
10k ohm resistor

How do LDRs work

An LDR works by changing the amount of resistance it produces depending on how much light is hitting it. The more light that hits an LDR, the less resistance it will have. We can use this change in resistance to our advantage by creating a voltage divider.

An LDR has two legs which are connected to separate pieces of metal on its surface.These pieces of metal are separated by a substance called a semiconductor which conducts more electricity when its electrons are excited by the light photons. This substance is usually orange on LDRs, shown in the image below.


Wiring up the LDR

Using croc clips, connect the 10kohm resistor to the PWR leg on CodeBug. Then connect one of the legs of the LDR to leg 0 on CodeBug and to the other end of the 10kohm resistor. Finally connect the other leg of the LDR to the GND leg on CodeBug.


Writing your program

To show the value that we are reading from the LDR, we need to read the analogue value of leg 0 and scroll the value on the LED display. Follow the tutorial below to write your program.


Testing your project

Click the play button on the emulator. It will scroll "255". If you click and hold leg 0, the emulator will start scrolling 0 instead. This shows us the highest and lowest value our analogue leg can read.

Load your program onto your CodeBug and shine a bright light on the LDR, the value scrolling will change to numbers within the 0-255 range. When the light is shone on LDR the value will be lower.


What next

Try controlling external LEDs or GlowBugs to light them up when the light level falls.


Back to top