# Saving data when Power is lost

**URL:** https://forum.makecode.com/t/saving-data-when-power-is-lost/22303
**Category:** micro:bit
**Created:** [September 6, 2023, 6:34pm UTC](https://forum.makecode.com/t/saving-data-when-power-is-lost/22303 "2023-09-06T18:34:37Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![instant-veemo](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/instant-veemo/32/10098_2.png) [@instant-veemo](https://forum.makecode.com/u/instant-veemo)
#### Post date: [September 6, 2023, 6:34pm UTC](https://forum.makecode.com/t/saving-data-when-power-is-lost/22303/1 "2023-09-06T18:34:37Z")

</div>

I’m trying to get data to be saved on my Micro:Bit that remains there when power is lost but I can’t find how. I’ve tried Aria (Opera’s build in AI based on ChatGPT) but no correct response. Is it possible as Micro Python can.

---

<div class="post-metadata">

### Author: ![bsiever](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/bsiever/32/1032_2.png) [@bsiever](https://forum.makecode.com/u/bsiever)
#### Post date: [September 6, 2023, 10:16pm UTC](https://forum.makecode.com/t/saving-data-when-power-is-lost/22303/2 "2023-09-06T22:16:31Z")

</div>

Hi,

Following another forum member’s request (@mwest226 's request here: [Pull CPP files (Online makecode.microbit.org) - #4 by bsiever](https://forum.makecode.com/t/pull-cpp-files-online-makecode-microbit-org/19532/4)), I did create an extension with blocks that can be used to save and restore text from flash: [https://github.com/bsiever/microbit-pxt-flashstorage](https://github.com/bsiever/microbit-pxt-flashstorage) .

Here’s an example using it: [https://makecode.microbit.org/S34893-47565-25042-23479](https://makecode.microbit.org/S34893-47565-25042-23479) (I just put this example together and haven’t tested it on a micro:bit yet).

Bill

---

<div class="post-metadata">

### Author: ![instant-veemo](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/instant-veemo/32/10098_2.png) [@instant-veemo](https://forum.makecode.com/u/instant-veemo)
#### Post date: [September 7, 2023, 5:09pm UTC](https://forum.makecode.com/t/saving-data-when-power-is-lost/22303/3 "2023-09-07T17:09:28Z")

</div>

Ive tried it on my Microbit V2.00 and when pressing A the number goes up by 1 an and removing then inserting power it displays the last saved number as the code instructs. Thanks for the help!

---

<div class="post-metadata">

### Author: ![mwest226](https://avatars.discourse-cdn.com/v4/letter/m/e19adc/32.png) [@mwest226](https://forum.makecode.com/u/mwest226)
#### Post date: [August 10, 2024, 9:14pm UTC](https://forum.makecode.com/t/saving-data-when-power-is-lost/22303/4 "2024-08-10T21:14:08Z")

</div>

I’m testing the limits of the @bsiever 's extension on the microbit and I cannot get my microbit to store more than 5 pairs. I believe I should be able to store 21 pairs (key limited to 16 bytes and data limited 32 bytes). I wonder if it is my code that is the problem. Have you been able to store more pairs. Thank you

---

<div class="post-metadata">

### Author: ![bsiever](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/bsiever/32/1032_2.png) [@bsiever](https://forum.makecode.com/u/bsiever)
#### Post date: [August 12, 2024, 1:57am UTC](https://forum.makecode.com/t/saving-data-when-power-is-lost/22303/5 "2024-08-12T01:57:33Z")

</div>

Hmmm…Looks like there’s an intentional limit of 5 pairs on the micro:bit v2 (but not on the v1). See: [Defined Limit](https://github.com/lancaster-university/codal-core/blob/509086cc8590465041b15493ab52b56e7071c110/inc/drivers/KeyValueStorage.h#L46) and [Where it’s used](https://github.com/lancaster-university/codal-core/blob/509086cc8590465041b15493ab52b56e7071c110/source/drivers/KeyValueStorage.cpp#L135). Weird — may want to open an [issue](https://github.com/lancaster-university/codal-core/issues) about it. It _might_ be wasting most of a block of flash (but there may have also been some reason for that choice).

Possible alternatives:

- It may be possible to write something that directly interacts with the flash rather than using the `KeyValueStorage`.
- It may also be possible to write something that interacts with the space used for the Data Logging blocks

Both of those would require a C++ based extension.
