Read data from accelerometer and magnetometer sensors, or read data from an I2C device, connected to BBC micro:bit board
Read the acceleration data and magnetic field data along the three
axes of the BBC micro:bit hardware connected to the serial port
and display the data as a
m = microbit('COM4'); data = read(m) data = Time Acceleration MagneticField ___________________ _______________________ ____________________ 25-Jun-2017 17:15:10 0.0781 -8.0124 0.0273 -33.2 -9.8 -134.8
Create object for an I2C device that is already connected to I2C bus.
microbitObj = microbit('COM3'); scanI2CBus(microbitObj)
ans = 1×2 string array "0xE" "0x1D"
i2cdev1 = device(microbitObj, 'I2CAddress',"0xE");
i2cdev1 = device with properties: Interface: "I2C" I2CAddress: 14 ("0xE") SCLPin: "P19" SDAPin: "P20" BitRate: 100000 (bits/s)
Use the I2C device object to read 6 bytes of data.
datai2cdev1 = read(i2cdev1, 6)
datai2cdev1 = 1×6 double row vector 0 0 252 0 0 0
Use the I2C device object to read 6 bytes of data with a precision of uint8.
datai2cdev1 = read(i2cdev1, 6, 'uint8')
datai2cdev1 = 1×6 double row vector 0 0 0 0 196 0
microbitObj— BBC micro:bit hardware connection
BBC micro:bit hardware connection, specified as a
created with the
i2cdevObj— I2C device connection
Device connection to an I2C device, specified as a device object, connected to the
I2C bus on the BBC micro:bit board. The I2C device object is created using the
numBytes— Number of bytes to read
Number of bytes of data to be read from the I2C device, specified as a scalar.
precision— Data precision
Precision of data to read from the I2C device.
data— Data read from Accelerometer and Magnetometer sensors
The three-dimensional data of the BBC micro:bit board acceleration and the
surrounding magnetic field intensity, returned as a
data is measured along the x-, y-, and z-axes, relative to the position of the board.
The date and time (shown in the first column) at which the measurements were read are
based on the system clock.
datai2c— Data read from I2C device
The value of data read from the I2C device, returned as a vector based on the