Main Content

readMagneticField

Read one sample of magnetic field strength from sensor

Description

example

[magReadings,timestamp] = readMagneticField(bno055) returns the magnetic field along x, y, and z axes of the sensor as a 3-by-1 vector read from the sensor in units of µT (microtesla) along with the timestamp. In ndof mode, the elements of the vector represent calibrated values, whereas in amg mode, the elements represent raw values.

Examples

Read Magnetic Field Strength from Sensor in NDOF Mode

Create an arduino object

a = arduino('COM4', 'Uno', 'Libraries', 'I2C');

Create the BNO055 sensor object.

imu = bno055(a,'OperatingMode','ndof');

Calibrate the sensor using steps mentioned in Calibrate BNO055 Sensors

tic; 
while (toc < 120)
    [status,timestamp] = readCalibrationStatus(BNO055Sensor); 
    if strcmpi(status.Magnetometer,'full')
        break; %If Magnetometer is calibrated proceed further
    end
    pause(1);
end

Read angular velocity data.

[magReadings,timestamp]  = readMagneticField(imu)
magReadings = 3×1

    9.5625   18.1875   23.0625

timestamp = datetime datetime
   21-Jul-2020 15:00:24.753

Read Magnetic Field Strength from Sensor in AMG Mode

Create an arduino object

a = arduino('COM4', 'Uno', 'Libraries', 'I2C');

Create the BNO055 sensor object.

imu = bno055(a,'OperatingMode','amg');

Read angular velocity data.

[magReadings,timestamp]  = readMagneticField(imu)
magReadings =

   16.2797    9.1711  -19.0969


timestamp = datetime datetime
   21-Jul-2020 15:00:24.753

Input Arguments

collapse all

BNO055 sensor object created in either ndof or amg operating mode with the default or specified properties.

Output Arguments

collapse all

The magnetic field values along x, y, and z axes of the sensor as a 3-by-1 vector. In ndof mode, the elements of the vector represent calibrated values, whereas in amg mode, the elements represent raw values.

The time at which MATLAB® receives magnetic field data from the sensor, specified as a datetime.

More About

collapse all

Code Generation Using MATLAB Function Block

  • Use readMagneticField in a MATLAB Function block with the Simulink® Support Package for Arduino® Hardware to generate code that can be deployed on Arduino Hardware.

  • Timestamp returned is always in seconds.

  • readMagneticField returns raw values of the magnetic field strength if the sensor is not calibrated. Ensure you calibrate the sensor before using it in the ndof operating mode. See Calibrate BNO055 Sensors for the steps.

Introduced in R2019a