Storing and retrieving values in non volatile data


How do I store non volatile data in MicroPython?

How do I create, read, and write files in MicroPython?

How do I store a variable in non volatile data and retrieve it again on reset?

How do I store a variable so that it is remembered when the micro:bit is turned off?

How do I stop my variables being lost when I power off the micro:bit?


nonvolatile, non volatile, non-volatile, variable, persistent


The MicroPython language has a feature that allows you to read, write, and delete files. You can have multiple files. You can also access those files from the Mu editor directly via it’s FILES button.

However, if what you really want to do is just store a single number in non volatile memory so that it is retained when the micro:bit is turned off, and retrieve it again when you turn the micro:bit on, it’s not immediately obvious how you would do this.

The full version of Python has a ‘pickle’ module that allows arbitrary data values to be stored to files. Also there is a dbm module, which allows a mini key-value database to be stored in a file. Neither of these modules have been ported into the standard distribution of MicroPython on the micro:bit.


One relatively simple way to meet this need of storing and retrieving values in the non-volatile memory, is to just use a file with a single value written to it. If you only have one or two values to store, just use more files. Here is an example of such a solution, that includes various error checks and recovery modes for things like the file not existing, the file having invalid data in it, or the filing system becoming full up:


Paste this code into your edit window.

FLASH it to your micro:bit


from microbit import *

def get_nv_data(name):
        with open(name) as f:
            v = int(f.read())
    except OSError:
        v = 0
            with open(name, "w") as f:
        except OSError:
            print("Can't create file %s" % name)

    except ValueError:
        print("invalid data in file %s" % name)
        v = 0

    return v

def set_nv_data(name, value):
        with open(name, "w") as f:
    except OSError:
        print("Can't write to file %s" % name)

count = get_nv_data("data.txt")

count = count + 1
set_nv_data("data.txt", count)



Every time you press the reset button on the back of the micro:bit, it will add one to the stored number and re-display it.

If you remove the power from your micro:bit and later plug it in, it will have remembered that number.


The version of MicroPython that is built into the Mu editor (http://codewith.muhas a filing system built into it.

The version of MicroPython on the microbit.org website (http://python.microbit.org/editor.html) has a filing system built into it.

The version of MicroPython on the microbit.co.uk BBC website does not have a filing system build into it.

The above code is an example of how to store one integer variable value in a text file. We hope to write a more flexible example in the future, that allows any number of data variables to be read and written from a single file (e.g. like a config file).

0 found this helpful
All for Joomla All for Webmasters