糖心TV

Skip to main content Skip to navigation

Sunlight Sensor

Sunlight Sensor & Extra Micro:bit Features

Activity Overview

Previously we鈥檝e used our micro:bit to record voltage data and store it on our computer to plot a characteristic curve. In this exercise we will begin testing some of the other functionalities of the micro:bit – in particular the in-built sunlight sensor. The LEDs on the front screen can emit light to form a pattern (as we鈥檝e seen before), but they can also harvest incoming light and convert it into an electrical signal.

 

You Will Need

  • The Fully Electric Challenge Pack, specifically:
    • BBC micro:bit GO Pack
  • A computer/laptop with internet access
  • MakeCode app (preferred) or MakeCode open in your browser
  • Your kit car from yesterday
Get Coding: Basic Example

To start, we are going to create a simple example where we detect the light level on the micro:bit鈥檚 LED screen and display two different patterns depending on how much light is detected.

Step 1: Create a new MakeCode project, delete the on start block and input an if/else block from the 鈥淟ogic鈥 module:

Basic Example Step 1

Step 2: Within the 鈥淟ogic鈥 module, drag a 鈥0 = 0鈥 block from the comparison section and drag it into the hexagonal 鈥渢rue鈥 box:

Basic Example Step 2

Step 3: Drag the round 鈥渓ight level鈥 block from the 鈥淚nput鈥 module to the left hand side 0. Then type in 100 into the right hand side 0 and change the operator from equals 鈥=鈥 to more than 鈥>鈥:

Basic Example Step 3

Step 4: Drag two 鈥渟how leds鈥 blocks from the 鈥淏asic鈥 module, one to be placed under the if statement and the other under the else statement. You can customise which LEDs light up however you want, I have chosen a sunny looking pattern for light levels above 100 and a moon pattern for when it drops below 100:

Basic Example Final Code

 

Our basic example is complete! If you press download as before with the micro:bit connected via USB to your computer, your program will be saved onto your micro:bit. If you are in a bright room and your micro:bit is showing a sunny pattern to begin with, try covering the LEDs and you should see the pattern change over. Alternatively, if you are in a darker room you can use your phone torch to illuminate the screen. It鈥檚 now time to add some extra features to our program!

 

Upgrading Our Program

By implementing a few extra blocks, we can upgrade our program to display animated patterns! Since the micro:bit has an on-board speaker, we can also play single notes or custom tunes depending on the incident light level.

 

Animating our screen

By including 鈥減ause (ms)鈥 blocks from the 鈥淏asic鈥 module and adding extra 鈥渟how leds鈥 blocks we can very simply create an animated gif for each of the two scenarios:

Sunlight Sensor Animated

Note: Feel free to add as many layers to the animation as you want!

Adding sound

After poking around in the 鈥淢usic鈥 module you will see you have the option to play a musical note, a custom melody and even change the tempo/volume of your sounds:

Sunlight Sensor with Sound

 

Final step: Once you are happy with your program and you have tested it works correctly – you may unplug your micro:bit from your computer and attach it to your kit car with the battery box provided. You have now created a mobile sunlight sensor which can annoy everyone in your household!


Bonus Tutorials

If you are interested in further examples of what your micro:bit has to offer, feel free to visit the link below in your spare time and browse an extensive list of tutorials. Some extra features of the micro:bit that weren鈥檛 covered today include a microphone, temperature sensor, compass, accelerometer, and many others!

Here is a sneak peek at some of the projects you will encounter on the website鈥

MakeCode Tutorials

Let us know you agree to cookies