QUESTION
How do I read the device serial number in MicroPython?
I want some unique* way to identify my micro:bit, how can I do this?
ANSWER
from microbit import * display.show('S') def get_serial_number(type=hex): NRF_FICR_BASE = 0x10000000 DEVICEID_INDEX = 25 # deviceid[1] @micropython.asm_thumb def reg_read(r0): ldr(r0, [r0, 0]) return type(reg_read(NRF_FICR_BASE + (DEVICEID_INDEX*4))) while True: if button_a.was_pressed(): display.scroll(get_serial_number()) sleep(1000) display.show('S') sleep(100)
DESCRIPTION
The micro:bit has a register mapped into it's memory called the FICR (factory information configuration register) that stores information programmed in at manufacturing time. One of the items programmed in is a unique* serial number.
The code above uses a little bit of assembly language to read this register value and display it.
MORE INFORMATION
[1] NRF51 SDK header file with register map
* The serial is actually a 64-bit factory generated pseudo-random number etched into each processor. Only 32 bits are used here though (as that makes it consistent for all the micro:bit higher level languages to deal with), and still enables it to be unique for most practical applications. To put this into context, you'd need about 100,000 to 250,000 micro:bits in one place to expect a duplication.
ATTRIBUTION
Based on an idea from Carlos here: https://gist.github.com/microbit-carlos/d3110c69ee30ee2c92feca1397105cb0
This article was written in collaboration with 'Luke' at PyConUK 2017
Thanks to Karl Marklund for clarifying whether devices are truly unique*
REVIEWED
29/10/2017 DW