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

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."
What You Need & Getting Ready
- SPACEBLOCK Standard Package [SPACEBLOCK, USB cable, battery box]
- 8×8 RGB LED Matrix Panel (LED panel)
- Jumper wires [3 wires, pin header to pin socket]
- Ambient Light Sensor Module (light sensor)
- A computer for programming

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.

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

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.

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.

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.

Let's See What the Light Sensor Measures!

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.

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

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.

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.




Watch it in the video.

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.