Build a shy flower project on the micro:bit. The flower will wave vigorously when it's dark, but will slow down its waving when it's bright!
YOU WILL NEED
- Breakout board
- Light sensor
Connect the light sensor to the micro:bit
Plug the micro:bit into a breakout board to break out the external pins. We will plug the light sensor into Pin P1 on the breakout board.
Program the light sensor
Try out this simple program to check that the light sensor is working. As you move your hands to cast a stronger or weaker shadow on the light sensor, the readings should change.
Attach the servo motor
Attach the servo motor to Pin P2 on the breakout board.
Programming the servo motor
Test that the servo motor works using the simple code here. This code will move the servo horn back and forth continually. Remember to set the pin on the code block to Pin P2.
Now we have ensured that our light sensor and our servo motor component works. The next step is to have the input of the light sensor control the frequency of the servo motor's movements.
Create two variables: light value to store the value from the light sensor, and period which will be a calculated value that determines the frequency of the servo motor.
A dark environment will set the period of the servo motor to about 200ms, while a bright environment will set the period to about 3000ms.
We now use the value of the period variable to control how long the servo motor waits before it does another rotation.
We replace the duration of the pause with the period variable.
Voila! We are done with the programming. Create a simple flower decoration using construction paper and pipe cleaners, attach it to the servo motor, and mount the whole project on the side of the table to give the servo motor clearance to rotate.
You will also have to adjust the angle of rotation so that the flower will be more or less vertical. In our experiment and based on the angle that we attach the servo horn, we found the angles between 90 and 150 good.
As we tried it out, we also adjusted the mapping from the light value to the period to try and make our flower more responsive. In this screenshot, you will see that the values of our mappings have changed.
* Use a ultrasonic distance sensor instead of a light sensor
* Make the program more sophisticated and responsive. In the screenshot, we make it so that the flower stops waving when it's very bright.
Made this activity? Good Job! Now spread the word. Share a photo of your creation on social media with #letsgethacking #ShyFlower