Skip to Content

HSCDTD008A Magnetometer with Arduino

HSCDTD008A Magnetometer with Arduino

In this tutorial you will learn how to use the HSCDTD008A 3-Axis Magnetometer with an Arduino. The HSCDTD008A is essentially an electronic or digital compass that allows you to measure the strength of earth’s magnetic field (and other magnetic sources) along three axis.

Magnetometers can be used in many different applications. They can help navigation systems to find direction and orientation. In robotics, they improves spatial awareness and keeps robots on track. Smartphones use them for compass features and augmented reality. Drones use them for stabilization and navigation. Wearable devices like fitness trackers and smartwatches also use magnetometers for tracking activities and navigation.

Required Parts

You will need an HSCDTD008A magnetic sensor and a microcontroller for this project. I used an Arduino Uno but any other Arduino or any ESP32/ESP8266 board will work as well.

HSCDTD008A 3-Axis Magnetometer

Arduino

Arduino Uno

USB Data Sync cable Arduino

USB Cable for Arduino UNO

Dupont wire set

Dupont Wire Set

Half_breadboard56a

Breadboard

Makerguides.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to products on Amazon.com. As an Amazon Associate we earn from qualifying purchases.

Features of the HSCDTD008A

The HSCDTD008A is tiny sensor (1.6×1.6×0.65mm) that can measure the strength of magnetic fields along three axis with an accuracy of 0.15μT / LSB (T= Tesla). The picture below shows the actual IC – just a tiny square block.

HSCDTD008A IC
HSCDTD008A IC

The three axis of the sensor are oriented as shown below and the output value of each axis is positive when turned toward magnetic north.

Axis of the HSCDTD008A (source)

Depending on the orientation of the IC relative to a magnetic field (e.g. earth’s magnetic field) the output value for each axis will change. This allows you, for instance, to navigate since you can determine the orientation of the sensor in space.

Internal Block Diagram of the HSCDTD008A

The following picture shows the Internal Block Diagram of the HSCDTD008A. You can easily identify the 3-axis Magnetic Sensor, the Multiplexer (MUX) used to read the 3-axis, the Analog-Digital (AD) converter for the Temperature Sensor and the I2C interface.

Internal Block Diagram of HSCDTD008A
Internal Block Diagram of HSCDTD008A

The Temperature Sensor is needed, since the output voltage of a magnetic sensor can change significantly with temperature and the HSCDTD008A has therefore a built in temperature sensor to compensate for changes in ambient temperature.

The IO pins of the HSCDTD008A are as follows: AVDD is Analog Supply Voltage, DVDD is Digital Supply Voltage, VSS is Ground, SDA and SCL are the lines of the I2C interface, DRDY is an interrupt pin signalling that data is ready, and TEST1 and TEST2 are internal test points during chip production.

For more details consult the datasheet for the HSCDTD008A that is linked below:

Breakout board for the HSCDTD008A

The HSCDTD008A is too small to directly connect to an Arduino and also doesn’t work with 5V. Therefore, you typically need a breakout board as shown below. The small square chip in the center is the HSCDTD008A chip. The board also has a voltage regulator, so that you can connect the board to 5V or 3.3V microcontrollers.

Breakout board for HSCDTD008A
Breakout board for HSCDTD008A

Apart from the power supply, the pins are essentially the same as discussed for the internal block diagram. VCC is the Supply Voltage, GND is Ground, SDA and SCL are the lines of the I2C interface, DRY is the data ready pin, and T0 and T1 are test points, which you will not need.

The current consumption of the module is typically 60μA when active and only 3μA in standby mode. As mentioned before, the supply voltage is + 3.3V to + 5V and the communication level is 3.3V.

Connecting HSCDTD008A to Arduino

Thanks to the I2C interface of the HSCDTD008A, connecting it to an Arduino is simple. First, connect the SCL and SDA pins of the HSCDTD008A breakout board to the corresponding pins on the Arduino board as shown below. Next, connect ground to GND and 3.3V (or 5V) to VCC of the HSCDTD008A and that’s it.

Connecting HSCDTD008A to Arduino
Connecting HSCDTD008A to Arduino

Next, let us write some code to test the function of the HSCDTD008A sensor.

Code for measuring magnetic field with HSCDTD008A

Before you can measure magnetic fields with the HSCDTD008A sensor, you will have to install a library. In the following we are going to use the HSCDTD008A Library by Bob Veringa. To install it, just search for HSCDTD008A in the Library Manager, find the one by Bob Veringa and press “INSTALL”. The picture below shows how that looks like:


Installing HSCDTD008A Library by Bob Veringa via Library Manager
Installing HSCDTD008A Library by Bob Veringa via Library Manager

With the library installed, let’s try the sensor out. The following code reads the strength of the magnetic field along the three axis (X, Y, and Z) and prints them to the Serial monitor every half a second.

#include "hscdtd008a.h"

HSCDTD008A geomag;

void setup() {
  Serial.begin(9600);
  geomag.begin();
  hscdtd_status_t status = geomag.initialize();
  if (status != HSCDTD_STAT_OK) {
    Serial.println("Can't initialize sensor!");
  }
  geomag.temperatureCompensation();
}

void loop() {
  hscdtd_status_t status = geomag.startMeasurement();
  if (status == HSCDTD_STAT_OK) {
    Serial.print("X:");
    Serial.println(geomag.mag.mag_x);  
    Serial.print("Y:");
    Serial.println(geomag.mag.mag_y);
    Serial.print("Z:");
    Serial.println(geomag.mag.mag_z);
  } 
  delay(500);
}

Let’s break down the code into its components for a clearer understanding.

Library Inclusion

The first line includes the necessary library for the HSCDTD008A magnetometer. This library contains the functions and definitions needed to communicate with the sensor.

#include "hscdtd008a.h"

Creating an Instance

Here, we create an instance of the HSCDTD008A class named geomag. This instance will allow us to access the functions defined in the library to interact with the magnetometer.

HSCDTD008A geomag;

Setup Function

In the setup() function, we initialize the serial communication at a baud rate of 9600. This allows us to send data to the Serial Monitor for debugging and monitoring purposes.

Serial.begin(9600);

Next, we call the begin() method on the geomag instance to initialize the magnetometer. After that, we check the status of the initialization using the initialize() method. If the status is not HSCDTD_STAT_OK, we print an error message to the Serial Monitor.

geomag.begin();
hscdtd_status_t status = geomag.initialize();
if (status != HSCDTD_STAT_OK) {
  Serial.println("Can't initialize sensor!");
}

Finally, we call the temperatureCompensation() method to ensure that the readings from the sensor are adjusted based on the ambient temperature, which can affect magnetic measurements.

geomag.temperatureCompensation();

Loop Function

In the loop() function, we start a measurement by calling the startMeasurement() method. We again check the status of this operation. If the status is HSCDTD_STAT_OK, we proceed to read the magnetic field values.

hscdtd_status_t status = geomag.startMeasurement();
if (status == HSCDTD_STAT_OK) {

We then print the magnetic field values for the X, Y, and Z axes to the Serial Monitor. The values are accessed through the mag property of the geomag instance and the values are in micro Tesla (µT):

Serial.print("X:");
Serial.println(geomag.mag.mag_x);  
Serial.print("Y:");
Serial.println(geomag.mag.mag_y);
Serial.print("Z:");
Serial.println(geomag.mag.mag_z);

Finally, we introduce a delay of 500 milliseconds before the next measurement cycle begins, allowing for a readable output on the Serial Monitor.

 delay(500);

Running the Code

If you upload and run the code, you should start to see magnetic field measurements, similar to the ones below, appearing on the Serial Monitor:

Output of HSCDTD008A on Serial Monitor
Output of HSCDTD008A on Serial Monitor

If you now tilt and rotate the sensor you can monitor the changing values of the magnetic field measurements for the three axis on the Serial Plotter:

Output of HSCDTD008A on Serial Plotter
Output of HSCDTD008A on Serial Plotter

And that’s it! This tutorial should give you a good starting point for your own applications.

Conclusions

In this tutorial you learned how to use the HSCDTD008A 3-Axis Magnetometer with an Arduino. Common application for a Magnetometers are as digital compass and to improve accuracy in robot or drone navigation.

Note that, while Magnetometers are similar to Hall Effect Sensors, they are usually not suitable for the same applications such as measuring current or contactless speed measurements of rotating objects. See our tutorial Read Fan Speed Signal with Arduino and ACS712 Current Sensor And Arduino, if you want to learn more about that.

Happy Tinkering ; )