micro:bit Project 5: Electronic Candle
In this project, we are going to make a device simulating the candle to reduce carbon emissions and contribute to environmental protection. Then we can make an electronic candle on our own birthday and blow it out!
Connect the sound sensor to P0;
Connect the LED module to P1.
To build an electronic candle, we must first know how the sound sensor works. We can connect the sound sensor to the micro: bit and see how strong our sound is by looking at the number displayed on the LED panel.
Task1： Measure the Volume
Goal: When the analog value of sound sensor is less than 50, the on-board LED displays a number "1", indicating that the volume is low; when it is greater than 50, the onboard LED displays the number "2", indicating that the volume is high.
STEP1: We will put the LED module aside for a moment and focus on the logic part. Drag the conditional statements code and the logic statement code that values the numbers from “Logic” into "forever". The function of the conditional statement code is to set the conditions for triggering the condition for displaying the onboard numbers. If the logic statement on the right side is met, the code to the right of "then" should be executed, whereas the code to the right of "else" should be executed if the logic statement is No met.
STEP2: The sound sensor is connected to P0, which means we will use the “analog read” function to achieve the readout on P0. As explained before, the analog signal ranges from 0 to 1023.
STEP3: Now, we will need to let the program determine the level of the voice. The operator "<" under the “Logic” does the job. As shown in the figure below, When the analog value of sound sensor is less than 50, the on-board LED displays a number "1", indicating that the volume is low; when it is greater than 50, the onboard LED displays the number "2", indicating that the volume is high.
Make some noise around the sensor, or simply blow some air to test if the micro: bit is able to show different number according to the volume.
Also, if the sensor is being too responsive, you may need to change the “50” after the “<” operator. It also works the same way around if the sensor is not responsive.
Task 2: electronic candles
Goal: The micro: bit can measure the volume using the sound sensor, based on that, we want to use it to control the candle. Here is what we are going to program, Once we blow air towards the sensor, the candle goes off for a second.
STEP1: We will use the “if else” function again in our program. However, instead of controlling an LED panel, we just need to control the LED connects to P2. The logic is very straight forward, once the volume exceeds the preset value, the light goes, vice versa. Since we want the candle to keep on burning, the program should be put inside the “forever” loop.
STEP 2: However, that’s still not enough! When we blow air to the sensor, the volume will only exceed the preset value for a very short time. Sometimes it happens so fast that we won’t be able to notice the change. To solve this issue, we will need to add a “Pause” function to extend the duration. Say if the duration set to be 3000 (3000ms = 3 seconds), once we blow the candle, the light will go off for 3 seconds, and then go back to the beginning of the “forever “loop and light up again.
Here is how the program looks like.