Main Content

read

Read data from accelerometer and magnetometer sensors, or read data from an I2C device, connected to BBC micro:bit board

Description

data = read(microbitObj) reads data from both the accelerometer and magnetometer sensors connected to the BBC micro:bit board, specified as a microbit object. The data is returned as a timetable.

example

datai2c = read(i2cdevObj,numBytes) reads data from the I2C device, specified as a device object, based on the number of bytes.

example

datai2c = read(i2cdevObj,numBytes,precision) reads data from the I2C device, specified as a device object, based on the number of bytes and data precision.

example

Examples

collapse all

Read the acceleration data and magnetic field data along the three axes of the BBC micro:bit hardware connected to the serial port COM4, and display the data as a timetable.

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 

Input Arguments

collapse all

BBC micro:bit hardware connection, specified as a microbit object created with the microbit function.

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 device function.

Number of bytes of data to be read from the I2C device, specified as a scalar.

Data Types: double

Precision of data to read from the I2C device.

Output Arguments

collapse all

The three-dimensional data of the BBC micro:bit board acceleration and the surrounding magnetic field intensity, returned as a timetable. The 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.

The value of data read from the I2C device, returned as a vector based on the precision

Version History

Introduced in R2017b