HMC5883L Compass demo
2025.04.25 14:09
// i2c Address search : found 0x1E
// for Compass HMC5883L on ESP32-S3 super mini
// Line connection : SDA connected to 4, SCL to 5
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>
// I have tried many other library, however, only Adafruit library WORKED!
/* Assign a unique ID to this sensor at the same time */
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
void displaySensorDetails(void)
{
sensor_t sensor;
mag.getSensor(&sensor);
Serial.println("------------------------------------");
Serial.print ("Sensor: "); Serial.println(sensor.name);
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" uT");
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" uT");
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" uT");
Serial.println("------------------------------------");
Serial.println("");
delay(500);
}
void setup() {
Serial.begin(115200);
Serial.println("HMC5883 Magnetometer Test"); Serial.println("");
Wire.begin(4, 5); // SDA:4, SCL:5
/* Initialise the sensor */
if(!mag.begin())
{
/* There was a problem detecting the HMC5883 ... check your connections */
Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
while(1);
}
/* Display some basic information on this sensor */
displaySensorDetails();
}
void loop() {
/* Get a new sensor event */
sensors_event_t event;
mag.getEvent(&event);
/* Display the results (magnetic vector values are in micro-Tesla (uT)) */
Serial.print("X: "); Serial.print(event.magnetic.x); Serial.print(" ");
Serial.print("Y: "); Serial.print(event.magnetic.y); Serial.print(" ");
Serial.print("Z: "); Serial.print(event.magnetic.z); Serial.print(" ");Serial.println("uT");
// Hold the module so that Z is pointing 'up' and you can measure the heading with x&y
// Calculate heading when the magnetometer is level, then correct for signs of axis.
float heading = atan2(event.magnetic.y, event.magnetic.x);
// Once you have your heading, you must then add your 'Declination Angle', which is the 'Error' of the magnetic field in your location.
// Find yours here: http://www.magnetic-declination.com/
// Mine is: -13* 2' W, which is ~13 Degrees, or (which we need) 0.22 radians
// If you cannot find your Declination, comment out these two lines, your compass will be slightly off.
float declinationAngle = 0.22;
heading += declinationAngle;
// Correct for when signs are reversed.
if(heading < 0)
heading += 2*PI;
// Check for wrap due to addition of declination.
if(heading > 2*PI)
heading -= 2*PI;
// Convert radians to degrees for readability.
float headingDegrees = heading * 180/M_PI;
Serial.print("Heading (degrees): "); Serial.println(headingDegrees);
delay(500);
}
Comment 0
No. | Subject | Author | Date | Views |
---|---|---|---|---|
Notice | For the first time user of ESP32-S3 super mini users. | me | 2025.04.25 | 44 |
11 |
Object Pascal App getting data from ESP32-S3 (Parsing)
![]() | me | 2025.04.29 | 43 |
10 |
Sound Sensor Test
![]() | me | 2025.04.27 | 41 |
9 |
Laser distance sensor VL53L0X
![]() | me | 2025.04.27 | 37 |
8 |
9DOF : Getting Pitch & Roll with ICM20948 v2
![]() | me | 2025.04.27 | 40 |
7 |
Formating output
![]() | me | 2025.04.27 | 34 |
6 | Blinking Built-in RGB without delay() | me | 2025.04.25 | 39 |
5 | Built-in RGB led Demo | me | 2025.04.25 | 42 |
4 | Servo Demo | me | 2025.04.25 | 40 |
» |
HMC5883L Compass demo
![]() | me | 2025.04.25 | 39 |
2 |
I2C Address Search
![]() | me | 2025.04.25 | 37 |
1 | Serial Sample (ASCII Table) | me | 2025.04.25 | 39 |