[No.9] Lights On Automatically When It Gets Dark! Make a Rainbow Light

Programming and Making

With SPACEBLOCK, the tiny computer for making things, you can add all kinds of parts and get creative with programs to build many features. This time, let's use the "Ambient Light Sensor Module" (light sensor) to make a light that turns on automatically when it gets dark. You'll program it with "repeating," which you learned last time in [No.8], plus a new skill: "conditional branching."

Elementary School ~ Beginner 40 min

What You Need & Getting Ready

Top left: SPACEBLOCK Standard Package. Bottom left: light sensor. Top right: LED panel. Bottom right: jumper wires

Connect the Add-On Parts to SPACEBLOCK

First, let's connect the LED panel and the light sensor we're using this time to SPACEBLOCK.


How everything is connected

For the details, look at the next picture. The light sensor has 3 wires, so plug them into the yellow pin socket as shown.

Pay attention to the jumper wire colors and the pin socket positions when you connect them

What Can the Light Sensor Do?

The light sensor measures brightness and turns it into a number. Complete darkness is 0, and the brighter it gets, the bigger the number. Measuring how much of something there is, like brightness, and sending it to the program as a number is called analog input.

The light sensor raises or lowers the voltage on the pin plugged into SPACEBLOCK depending on the brightness it measures. That voltage is turned into a number from 0 to 1023 and passed to the program.

The light sensor measures brightness and turns it into a number

You get the light sensor's value with the [Read analog voltage of (A0)] block in the [Input] category. First, let's make a program that shows the value the light sensor measured. Combine it with the [Show number (0)] block in the [Basic] category, as in the next picture.

A program that shows the light sensor's value

Click the [Read analog voltage of (A0)] block to see the list of pin sockets, and choose "B0." Also make sure the light sensor is connected as shown in the picture.

Choose the name of the pin socket you plugged the light sensor's "S" wire into

Let's See What the Light Sensor Measures!

Run the program you made. The light sensor's value at the moment you run it appears in the console on the programming screen.
A program for checking the light sensor's value

When we ran it indoors with daylight coming in, the value was 300. If you change the light sensor's direction or position and run the program again, you'll see a different value.

The program in the picture above only tells you the value at the moment you run it. Let's change the program so it shows the value all the time. Add a [Repeat] block as in the next picture.

A program that keeps showing the light sensor's value

When you run the program in the picture above, it keeps showing the value over and over, so the values stream through the console one after another.

Try holding your hand over the light sensor or shining a light on it. You'll see the value change in real time. How bright does it have to be to get each number? If you cover the sensor completely, it becomes "0".

A Device That Lights Up the LED Panel When It Gets Dark

Now let's use the light sensor to make a device that turns on the LED panel when it gets dark and turns it off when it gets bright. Use the [if ( ) then ... else ...] block and the [(0) (=) (0)] block in the [Logic] category, and build the program as in the next picture.
A program that turns on the LED panel when it's dark and turns it off when it's bright

In a program, you can't give instructions like "when it gets kind of dark," "when it's about as dark as the evening," or "just use common sense". You have to use an exact number, like "if the light sensor's value is less than ○."

How small the light sensor's value has to be to count as "dark" is up to you, the programmer. The right value depends on where you want to put the device, so try different values and decide for yourself. This time, our goal was "turn on when the entrance light is switched off," and after testing we chose 50.

In your program, you can freely decide below which light sensor value it counts as "dark"
Key Point
With blocks in the "Logic" category, you can run different instructions depending on whether the condition you set is "yes" (true) or "no" (false). This is called "conditional branching." Depending on what you use as the condition, you can build all kinds of systems.
Comparing numbers is often used as a condition. Let's check what each symbol means.
Unless you tell it otherwise, a program runs each part once and moves on. When you want to keep checking something like the light sensor and run a different instruction every time the condition changes between "yes" and "no," as in this example, use the [Repeat] block.
The program checks the condition and runs an instruction each time it repeats, but because it repeats so fast, it feels like it's checking all the time.
Run the program you made. When you make the room dark, the LED light turns on automatically, and when it gets bright again, it turns off. If you transfer the program to SPACEBLOCK and use standalone mode, you can put it anywhere you like as an automatic light.
It turns on automatically when it gets dark and turns off automatically when it gets bright

Watch it in the video.

QR code for project
Put it inside a box and it looks even more like a real device!
Just put it in a white box and fix the light sensor and panel from the inside, and you have a simple device

Even in the same spot, the measured value changes depending on how you position and point the light sensor. Adjust the value in the condition or move the sensor to match how you set it up.

Watch it in the video.

"Repeating" is also handy for making systems that keep checking something behind the scenes, like in this program. Every programming skill you learn greatly expands the kinds of devices you can make!