micro:bit and sharing sensors with a Raspberry Pi

"Can a Raspberry Pi use the sensors on a micro:bit?"

This was the question that was asked whilst sat in the pub the other evening.

A few evenings later a micro:bit was connected via i2c to a Raspberry Pi and i2cdetect was ran on the Pi:

pi@raspberrypi:~ $ i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- 0e -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- 1d -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --

This shows that the Accelerometer (0x1D) and Magnetometer (0x0E) on the micro:bit has been detected by the Pi.

A quick bit of Python on the Pi and the device id of the Magnetometer can be read:

#!/usr/bin/python3

import smbus
bus = smbus.SMBus(1)    
dev_addr=0x0e
id=bus.read_byte_data(dev_addr,0x07)
print(hex(id))

which displays 0xC4, the contents of the read only register at 0x07 on the Magnetometer:

pi@raspberrypi:~ $ python3 test.py 
0xc4

The result showed that the sensors on the micro:bit can be reached and queried successfully.


More reading:

Accelerometer on the micro:bit

Magnetometer on the micro:bit

What is i2c

i2c pinout on the Pi