Power Management is an extension that can help improve battery life by controlling your micro:bit's power consumption.
Imagine building a weather station that logs data every hour. You wouldn't want to have to change the batteries every day, right? By strategically using the Power Management extension, you can put your micro:bit into a low-power state when it's not actively collecting data, significantly extending its battery life.
Getting Started: Adding the Extension
- Open the MakeCode editor for the micro:bit v2 (makecode.microbit.org).
- Click on the "Advanced" drawer.
- Select "Extensions."
- Search for "power" (or something similar – the exact name may vary slightly) and add the Power Management extension.
You should now see new blocks in the MakeCode toolbox under a "Power" category.
Key Power Management Blocks
The extension provides several important blocks, but these are the ones you'll likely use most frequently:
- power.deepSleep(): This is your go-to block for putting the micro:bit into its lowest power state. The micro:bit will essentially "go to sleep" until a specific event wakes it up.
- power.wakeUpFrom(EventSource.buttonA) (or other events): This block defines what event will wake the micro:bit from deep sleep. Common events include button presses, accelerometer events, or timed intervals.
- power.setWakeUpTimer(milliseconds): This allows you to wake the micro:bit after a specified amount of time. This is perfect for periodic tasks, like our weather station example.
Example: A Simple Timer Wake-Up
Let's create a simple example where the micro:bit wakes up every 5 seconds, displays "Hello!", and then goes back to sleep.
Code snippet
power.setWakeUpTimer(5000)
basic.showString("Hello!")
power.deepSleep()
This code snippet does the following:
- power.setWakeUpTimer(5000): Sets a timer for 5000 milliseconds (5 seconds). When this timer expires, the micro:bit will wake up.
- basic.showString("Hello!"): Displays "Hello!" on the micro:bit's screen.
- power.deepSleep(): Puts the micro:bit back into deep sleep mode.
This cycle will repeat indefinitely, with the micro:bit waking up every 5 seconds.
Practical Applications: Real-World Examples
- Environmental Monitoring: Imagine a project that monitors soil moisture. The micro:bit could wake up every hour, take a reading from the moisture sensor, log the data, and then go back to sleep. This drastically extends battery life compared to continuously monitoring the sensor.
- Wireless Sensor Networks: Multiple micro:bits could be deployed as sensors in a garden, each monitoring a different aspect (light, temperature, humidity). They could wake up periodically, transmit their data to a central hub, and then return to a low-power state.
- Plant Nanny: A micro:bit could be attached to a plant. It could wake up daily, check the soil moisture, and if it's dry, it could trigger a small servo to water the plant. The rest of the time, it's in deep sleep, conserving power.
- Interactive Art Installations: Imagine an art piece that reacts to movement. The micro:bit could be in a low-power state, waiting for the accelerometer to detect movement. When movement is detected, it could wake up, trigger a light show, and then go back to sleep.
- Personalized Reminders: A micro:bit could be programmed to wake up at specific times of the day to display reminders or play a sound. This could be useful for medication reminders or task prompts.
Tips for Power Management
- Minimize Active Time: The less time the micro:bit spends actively running code, the less power it will consume.
- Use Deep Sleep Wisely: Deep sleep is the most effective way to conserve power. Use it whenever possible.
- Consider Wake-Up Events: Choose wake-up events that are appropriate for your project.
- Test Thoroughly: Always test your power management code thoroughly to ensure it behaves as expected.
Conclusion
The Power Management extension for MakeCode is a game-changer for creating battery-powered micro:bit projects. By understanding and utilizing these powerful blocks, you can build projects that run longer, are more efficient, and truly unleash the potential of your micro:bit. So, go ahead, power up your creativity and build something amazing!