micro:bit and HMC5883L magnetometer example

The Honeywell HMC5883L is a surface-mount, multi-chip module designed for low-field magnetic sensing with a digital interface for applications such as lowcost compassing and magnetometry. The HMC5883L includes our state-of-theart, high-resolution HMC118X series magneto-resistive sensors plus an ASIC containing amplification, automatic degaussing strap drivers, offset cancellation, and a 12-bit ADC that enables 1° to 2° compass heading accuracy. The I2C serial bus allows for easy interface. The HMC5883L is a 3.0×3.0x0.9mm surface mount 16-pin leadless chip carrier (LCC).

Applications for the HMC5883L include Mobile Phones, Netbooks, Consumer Electronics, Auto Navigation Systems, and Personal Navigation Devices.

The HMC5883L utilizes Honeywell’s Anisotropic Magnetoresistive (AMR) technology that provides advantages over other magnetic sensor technologies. These anisotropic, directional sensors feature precision in-axis sensitivity and linearity. These sensors’ solid-state construction with very low cross-axis sensitivity is designed to measure both the direction and the magnitude of Earth’s magnetic fields, from milli-gauss to 8 gauss. Honeywell’s Magnetic Sensors are among the most sensitive and reliable low-field sensors in the industry

HMC5883L

HMC5883L

Connection

Code

#include <Wire.h> //I2C Arduino Library
 
#define addr 0x1E //I2C Address for The HMC5883
 
void setup(){
 
  Serial.begin(9600);
  Wire.begin();
 
 
  Wire.beginTransmission(addr); //start talking
  Wire.write(0x02); // Set the Register
  Wire.write((byte)0x00); // Tell the HMC5883 to Continuously Measure
  Wire.endTransmission();
}
 
 
void loop(){
 
  int x,y,z; //triple axis data
 
  //Tell the HMC what regist to begin writing data into
  Wire.beginTransmission(addr);
  Wire.write(0x03); //start with register 3.
  Wire.endTransmission();
 
 
 //Read the data.. 2 bytes for each axis.. 6 total bytes
  Wire.requestFrom(addr, 6);
  if(6<=Wire.available()){
    x = Wire.read()<<8; //MSB  x 
    x |= Wire.read(); //LSB  x
    z = Wire.read()<<8; //MSB  z
    z |= Wire.read(); //LSB z
    y = Wire.read()<<8; //MSB y
    y |= Wire.read(); //LSB y
  }
 
  // Show Values
  Serial.print("X Value: ");
  Serial.println(x);
  Serial.print("Y Value: ");
  Serial.println(y);
  Serial.print("Z Value: ");
  Serial.println(z);
  Serial.println();
 
  delay(500);
}

 

Links
1pcs/lot GY-271 , HMC5883 HMC5883L module electronic compass compass module three-axis magnetic field sensor

micro:bit and HMC5983 magnetometer example

The Honeywell HMC5983 is a temperature compensated three-axis integrated circuit magnetometer. This surface-mount, multi-chip module is designed for low-field magnetic sensing for applications such as automotive and personal navigation, vehicle detection, and pointing.

The HMC5983 includes our state-of-the-art, high-resolution HMC118X series magnetoresistive sensors plus an ASIC containing amplification, automatic degaussing strap drivers, offset cancellation, and a 12-bit ADC that enables 1° to 2° compass heading accuracy. The I²C or SPI serial bus allows for easy interface. The HMC5983 is a 3.0×3.0x0.9mm surface mount 16-pin leadless chip carrier (LCC).

The HMC5983 utilizes Honeywell’s Anisotropic Magnetoresistive (AMR) technology that provides advantages over other magnetic sensor technologies. Honeywell’s anisotropic, directional sensors excel in linearity, low hysteresis, null output and scale factor stability over temperature, and with very low cross-axis sensitivity. These sensors’ solid-state construction is designed to measure both the direction and the magnitude of magnetic fields, from milli-gauss to 8 gauss. Honeywell’s Magnetic Sensors are among the most sensitive and reliable low-field sensors in the industry.

 

Schematic

 

microbit and hmc5983

microbit and hmc5983

 

Code

You will need to install the following library – https://github.com/arduino/HMC5983

[cpp]

#include <HMC5983.h>
#include <Wire.h>

HMC5983 compass;

void setup () {
Serial.begin(9600);
compass.begin(); // use “true” if you need some debug information
}

void loop() {
float c = -999;
c = compass.read();
if (c == -999) {
Serial.println(“Reading error, discarded”);
} else {
Serial.println(c);
}
delay(500);
}

[/cpp]

 

Output

Open the serial monitor

41.96
72.31
77.81
75.22
73.74
77.98
80.32
81.14
81.37
82.08

 

Link

GY-282 HMC5983 Replace HMC5883L High-precision High-sensitivity Temperature Compensation Triaxial Compass IIC SPI Module

Use the micro:bit magnetometer with the Arduino IDE

Freescale’s MAG3110 is a small, low-power, digital 3-axis magnetometer. The device can be used in conjunction with a 3-axis accelerometer to realize an orientation independent electronic compass that can provide accurate heading information. It features a standard I2C serial interface output and smart embedded functions.

The MAG3110 is capable of measuring magnetic fields with an output data rate (ODR) up to 80 Hz; these output data rates correspond to sample intervals from 12.5 ms to several seconds. The MAG3110 is available in a plastic DFN package and it is guaranteed to operate over the extended temperature range of -40°C to +85°C.

You have a micro:bit so you already have one on the board

 

Code

You need to import the Sparkfun library for the MAG3110 to run this example – https://github.com/sparkfun/SparkFun_MAG3110_Breakout_Board_Arduino_Library/archive/master.zip

 

#include <SparkFun_MAG3110.h>
 
MAG3110 mag = MAG3110();
 
void setup() 
{
  Serial.begin(9600);
  mag.initialize(); //Initializes the mag sensor
  mag.start();      //Puts the sensor in active mode
}
 
void loop() {
 
  int x, y, z;
  //Only read data when it's ready
  if(mag.dataReady()) 
  {
    //Read the data
    mag.readMag(&x, &y, &z);
 
    Serial.print("X: ");
    Serial.print(x);
    Serial.print(", Y: ");
    Serial.print(y);
    Serial.print(", Z: ");
    Serial.println(z);
 
    Serial.println("--------");
    delay(1000);
  }
}

 

Testing

Open the serial monitor and move your micro:bit through the various axis, you should see something like this

 

X: 218, Y: 65109, Z: 65274
——–
X: 71, Y: 64965, Z: 49
——–
X: 598, Y: 65036, Z: 248
——–
X: 423, Y: 65338, Z: 564
——–
X: 348, Y: 61, Z: 565
——–
X: 413, Y: 65532, Z: 576
——–
X: 502, Y: 65024, Z: 65352