### begin() usage example Source: https://github.com/adafruit/adafruit_hmc5883_unified Example setup() that calls begin() and checks the return value. Note that with this library version begin() always returns true, so the failure branch never executes. ```cpp void setup() { Serial.begin(9600); if (!mag.begin()) { // NOTE: with this library version begin() always returns true, // so this branch never executes — see source Adafruit_HMC5883_U.cpp:162 Serial.println("Ooops, no HMC5883 detected ... Check your wiring!"); while (1); } Serial.println("HMC5883 ready"); } ``` -------------------------------- ### Example: displaySensorDetails() using getSensor() Source: https://github.com/adafruit/adafruit_hmc5883_unified Example function that retrieves sensor metadata via getSensor() and prints the fields to Serial. The sensor_t variable is declared locally and passed by pointer. ```cpp 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("------------------------------------"); } ``` -------------------------------- ### Constructor: Adafruit_HMC5883_Unified(int32_t sensorID = -1) Source: https://github.com/adafruit/adafruit_hmc5883_unified Constructor for the HMC5883 magnetometer sensor. Stores the sensorID in _sensorID; no hardware interaction occurs here, I2C setup happens in begin(). Not valid to call other methods before begin() has run. ```cpp Adafruit_HMC5883_Unified(int32_t sensorID = -1); ``` -------------------------------- ### setMagGain function declaration Source: https://github.com/adafruit/adafruit_hmc5883_unified Declares the setMagGain method that sets the magnetometer's gain and updates internal conversion factors. The gain parameter is one of the HMC5883_MAGGAIN_* enumerators; the default installed by begin() is HMC5883_MAGGAIN_1_3. ```cpp void setMagGain(hmc5883MagGain gain); ``` -------------------------------- ### begin() method signature Source: https://github.com/adafruit/adafruit_hmc5883_unified Initializes the I2C bus and configures the magnetometer. Returns bool, but in this version always returns true; the failure branch in sketches is never taken. ```cpp bool begin(void); ``` -------------------------------- ### bool begin(void) Source: https://github.com/adafruit/adafruit_hmc5883_unified Initializes the magnetometer and establishes communication. Returns whether the connection was successful. ```APIDOC ## bool begin(void) ### Description Initializes the HMC5883L sensor and prepares it for use. ### Method begin ### Return Value - **bool** - Returns whether connection was successful. ``` -------------------------------- ### bool begin(void) Source: https://github.com/adafruit/adafruit_hmc5883_unified Initializes the I2C bus and configures the magnetometer for continuous measurement mode. Returns true unconditionally in this version, so the failure branch in typical usage patterns is never taken. ```APIDOC ## begin() ### Description Initializes the I2C bus and configures the magnetometer. This method must be called before any other methods. ### Method begin ### Signature ```cpp bool begin(void); ``` ### Parameters None. ### Return `bool`. In this version the method unconditionally returns `true` (`Adafruit_HMC5883_U.cpp:162–176`); the return value is not derived from any hardware detection or I2C acknowledgement. The sketch pattern `if (!mag.begin()) { … }` compiles and runs but the failure branch is never taken with this implementation. ### What it does 1. Calls `Wire.begin()` to enable I2C. 2. `write8(HMC5883_ADDRESS_MAG, HMC5883_REGISTER_MAG_MR_REG_M, 0x00)` — enables continuous measurement mode (mode register `MR_REG_M = 0x02` set to `0x00`). 3. `setMagGain(HMC5883_MAGGAIN_1_3)` — sets the gain to the default ±1.3 gauss range (see [`setMagGain`](#setmaggainhmc5883maggain-gain)). ### Request Example ```cpp void setup() { Serial.begin(9600); if (!mag.begin()) { // NOTE: with this library version begin() always returns true, // so this branch never executes — see source Adafruit_HMC5883_U.cpp:162 Serial.println("Ooops, no HMC5883 detected ... Check your wiring!"); while (1); } Serial.println("HMC5883 ready"); } ``` ``` -------------------------------- ### Set gain after begin() Source: https://github.com/adafruit/adafruit_hmc5883_unified Typical flow: set gain right after begin(), before reading events. Uses HMC5883_MAGGAIN_8_1 for widest range and lowest resolution. ```cpp // Typical flow: set gain right after begin(), before reading events if (mag.begin()) { mag.setMagGain(HMC5883_MAGGAIN_8_1); // widest range, lowest resolution } ``` -------------------------------- ### Basic instantiation with unique ID Source: https://github.com/adafruit/adafruit_hmc5883_unified Creates a sensor instance with an arbitrary ID (12345) to distinguish multiple sensors of the same type. Requires Wire, Adafruit_Sensor, and Adafruit_HMC5883_U libraries. ```cpp #include #include #include Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345); ``` -------------------------------- ### Adafruit_HMC5883_Unified class declaration Source: https://github.com/adafruit/adafruit_hmc5883_unified Public API of the unified sensor driver class. Constructor takes an optional sensor ID (default -1). begin() returns connection success, setMagGain() sets magnetic gain, getEvent() returns the latest sensor event, getSensor() fills a sensor_t. Private members and helpers handle I2C register access and raw data capture. ```cpp class Adafruit_HMC5883_Unified : public Adafruit_Sensor { public: /*! * @param sensorID sensor ID, -1 by default */ Adafruit_HMC5883_Unified(int32_t sensorID = -1); bool begin(void); //!< @return Returns whether connection was successful void setMagGain(hmc5883MagGain gain); //!< @param gain Desired magnetic gain bool getEvent( sensors_event_t*); //!< @return Returns the most recent sensor event void getSensor(sensor_t*); private: hmc5883MagGain _magGain; hmc5883MagData _magData; // Last read magnetometer data will be available here int32_t _sensorID; void write8(byte address, byte reg, byte value); byte read8(byte address, byte reg); void read(void); }; ``` -------------------------------- ### Default ID instantiation Source: https://github.com/adafruit/adafruit_hmc5883_unified Creates a sensor instance with the default sensorID of -1. The comment notes the default value. ```cpp Adafruit_HMC5883_Unified mag; // sensorID defaults to -1 ``` -------------------------------- ### Constructor with custom sensor ID Source: https://github.com/adafruit/adafruit_hmc5883_unified Create an Adafruit_HMC5883_Unified instance with a custom sensor ID (12345) or use the default ID (-1). The ID surfaces in sensor events and sensor_t. ```cpp Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345); // custom ID Adafruit_HMC5883_Unified mag2; // ID -1 ``` -------------------------------- ### void getSensor(sensor_t*) Source: https://github.com/adafruit/adafruit_hmc5883_unified Retrieves sensor metadata such as name, version, and range. Populates the provided sensor_t structure. ```APIDOC ## void getSensor(sensor_t*) ### Description Provides sensor information in a sensor_t structure. ### Method getSensor ### Parameters - **sensor** (sensor_t*) - Required - Pointer to a sensor_t structure to receive the sensor details. ``` -------------------------------- ### setMagGain(hmc5883MagGain gain) Source: https://github.com/adafruit/adafruit_hmc5883_unified Configures the measurement range of the magnetometer by setting the gain. This affects the full-scale range and the scaling factors applied to raw readings. ```APIDOC ## setMagGain ### Description Sets the gain of the magnetometer, which determines the measurement range and scaling factors. ### Parameters - `gain` (hmc5883MagGain) - Required - The gain setting to apply. ### Gain Options | Gain (enumerator, value) | ± range | LSB/µT factor, X/Y | LSB factor, Z | |--------------------------|---------|--------------------|---------------| | `HMC5883_MAGGAIN_1_3` (`0x20`) | 1.3 gauss | 1100 | 980 | | `HMC5883_MAGGAIN_1_9` (`0x40`) | 1.9 gauss | 855 | 760 | | `HMC5883_MAGGAIN_2_5` (`0x60`) | 2.5 gauss | 670 | 600 | | `HMC5883_MAGGAIN_4_0` (`0x80`) | 4.0 gauss | 450 | 400 | | `HMC5883_MAGGAIN_4_7` (`0xA0`) | 4.7 gauss | 400 | 255 | | `HMC5883_MAGGAIN_5_6` (`0xC0`) | 5.6 gauss | 330 | 295 | | `HMC5883_MAGGAIN_8_1` (`0xE0`) | 8.1 gauss | 230 | 205 | ``` -------------------------------- ### Set gain to ±2.5 gauss Source: https://github.com/adafruit/adafruit_hmc5883_unified Sets the magnetometer gain to HMC5883_MAGGAIN_2_5 for stronger fields and lower resolution. ```cpp // Stronger fields / lower resolution: ±2.5 gauss mag.setMagGain(HMC5883_MAGGAIN_2_5); ``` -------------------------------- ### Compass heading calculation from event Source: https://github.com/adafruit/adafruit_hmc5883_unified Derives a compass heading in degrees from the magnetic field event using atan2 and a declination angle. Pattern from examples/magsensor/magsensor.ino. ```cpp sensors_event_t event; mag.getEvent(&event); float heading = atan2(event.magnetic.y, event.magnetic.x); heading += 0.22; // declination angle for the location if (heading < 0) heading += 2 * PI; if (heading > 2 * PI) heading -= 2 * PI; float headingDegrees = heading * 180 / M_PI; Serial.print("Heading (degrees): "); Serial.println(headingDegrees); ``` -------------------------------- ### Adafruit_HMC5883_Unified(int32_t sensorID = -1) Source: https://github.com/adafruit/adafruit_hmc5883_unified Constructor for the magnetometer sensor. Accepts an optional user-assigned sensor ID that is used to distinguish multiple sensors in a sketch. ```APIDOC ## Constructor ### Description Creates a new Adafruit_HMC5883_Unified instance with an optional sensor ID. ### Parameters - `sensorID` (int32_t) - Optional - User-assigned ID for this sensor instance. Default is -1. ### Example ```cpp Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345); // custom ID Adafruit_HMC5883_Unified mag2; // ID -1 ``` ``` -------------------------------- ### getSensor() function declaration Source: https://github.com/adafruit/adafruit_hmc5883_unified Declares the getSensor method that fills a sensor_t structure with static metadata. The sensor pointer must be caller-owned and is zeroed before being filled. ```cpp void getSensor(sensor_t*); ``` -------------------------------- ### Polled read of magnetic field event Source: https://github.com/adafruit/adafruit_hmc5883_unified Standard loop that reads a magnetic field event and prints X, Y, Z values in micro-Tesla. Uses mag.getEvent() and delays 500 ms between reads. ```cpp void loop() { sensors_event_t event; mag.getEvent(&event); 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.println(" uT"); delay(500); } ``` -------------------------------- ### void setMagGain(hmc5883MagGain gain) Source: https://github.com/adafruit/adafruit_hmc5883_unified Sets the magnetic gain of the sensor. The gain parameter determines the measurement range and resolution. ```APIDOC ## void setMagGain(hmc5883MagGain gain) ### Description Sets the magnetic gain of the HMC5883L sensor. ### Method setMagGain ### Parameters - **gain** (hmc5883MagGain) - Required - Desired magnetic gain. ``` -------------------------------- ### getEvent function declaration Source: https://github.com/adafruit/adafruit_hmc5883_unified Declares the getEvent method that fills a sensors_event_t with magnetic field data in micro-Tesla. The event pointer must be caller-owned and is zeroed before filling. ```cpp bool getEvent( sensors_event_t*); ``` -------------------------------- ### bool getEvent(sensors_event_t*) Source: https://github.com/adafruit/adafruit_hmc5883_unified Retrieves the most recent sensor event data. Returns whether the event was successfully obtained. ```APIDOC ## bool getEvent(sensors_event_t*) ### Description Fetches the latest magnetometer data as a sensor event. ### Method getEvent ### Parameters - **event** (sensors_event_t*) - Required - Pointer to a sensors_event_t structure to receive the data. ### Return Value - **bool** - Returns whether the event was successfully retrieved. ``` -------------------------------- ### hmc5883MagGain enum definition Source: https://github.com/adafruit/adafruit_hmc5883_unified Defines the magnetometer gain settings used as the parameter type of setMagGain(). The enumerator values are the bit patterns written into configuration register B. The default gain set by begin() is HMC5883_MAGGAIN_1_3 (±1.3 gauss). ```cpp typedef enum { HMC5883_MAGGAIN_1_3 = 0x20, // +/- 1.3 HMC5883_MAGGAIN_1_9 = 0x40, // +/- 1.9 HMC5883_MAGGAIN_2_5 = 0x60, // +/- 2.5 HMC5883_MAGGAIN_4_0 = 0x80, // +/- 4.0 HMC5883_MAGGAIN_4_7 = 0xA0, // +/- 4.7 HMC5883_MAGGAIN_5_6 = 0xC0, // +/- 5.6 HMC5883_MAGGAIN_8_1 = 0xE0 // +/- 8.1 } hmc5883MagGain; ``` -------------------------------- ### getEvent(sensors_event_t* event) Source: https://github.com/adafruit/adafruit_hmc5883_unified Fills a sensors_event_t structure with the most recent magnetic-field reading in micro-Tesla (µT). The event is zeroed before being filled, and the magnetic vector is computed from raw axis data. ```APIDOC ## getEvent(sensors_event_t* event) ### Description Fills a `sensors_event_t` with the most recent magnetic-field reading in micro-Tesla (µT). The event is zeroed with `memset` before being filled, so stale data is cleared. ### Method `bool getEvent(sensors_event_t* event)` ### Parameters #### Parameters - **event** (`sensors_event_t*`) - Required - Output pointer to a caller-owned `sensors_event_t` (defined in `Adafruit_Sensor.h`, the external dependency). It is zeroed with `memset` before being filled, so stale data is cleared. ### Return `bool` — always `true` in this implementation. ### Filled Fields | Field | Type | Value written | |-------|------|---------------| | `event->version` | `uint32_t` | `sizeof(sensors_event_t)` | | `event->sensor_id` | `int32_t` | `_sensorID` (constructor argument) | | `event->type` | `sensors_type_t` | `SENSOR_TYPE_MAGNETIC_FIELD` | | `event->timestamp` | `uint32_t` | `0` (not time-stamped by this library) | | `event->magnetic.x` | `float` | `_magData.x / _hmc5883_Gauss_LSB_XY * SENSORS_GAUSS_TO_MICROTESLA` | | `event->magnetic.y` | `float` | `_magData.y / _hmc5883_Gauss_LSB_XY * SENSORS_GAUSS_TO_MICROTESLA` | | `event->magnetic.z` | `float` | `_magData.z / _hmc5883_Gauss_LSB_Z * SENSORS_GAUSS_TO_MICROTESLA` | ### Request Example ```cpp void loop() { sensors_event_t event; mag.getEvent(&event); 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.println(" uT"); delay(500); } ``` ### Request Example ```cpp sensors_event_t event; mag.getEvent(&event); float heading = atan2(event.magnetic.y, event.magnetic.x); heading += 0.22; // declination angle for the location if (heading < 0) heading += 2 * PI; if (heading > 2 * PI) heading -= 2 * PI; float headingDegrees = heading * 180 / M_PI; Serial.print("Heading (degrees): "); Serial.println(headingDegrees); ``` ``` -------------------------------- ### HMC5883_ADDRESS_MAG macro definition Source: https://github.com/adafruit/adafruit_hmc5883_unified Defines the I2C 7-bit address of the HMC5883L as 0x1E. Used by every I2C transaction in the library. Fixed by hardware; not configurable at runtime. ```cpp #define HMC5883_ADDRESS_MAG (0x3C >> 1) // 0011110x ``` -------------------------------- ### getSensor(sensor_t*) Source: https://github.com/adafruit/adafruit_hmc5883_unified Fills a sensor_t structure with static metadata describing the HMC5883 magnetometer. This method is used to retrieve sensor information such as name, version, ID, type, and measurement ranges. ```APIDOC ## getSensor(sensor_t*) ### Description Fills a `sensor_t` with static metadata describing this sensor. ### Method `void getSensor(sensor_t*)` ### Parameters #### Parameters - **sensor** (`sensor_t*`) - Required - Output pointer to a caller-owned `sensor_t` (defined in `Adafruit_Sensor.h`, the external dependency). It is zeroed with `memset` before being filled. ### Response #### Return `void` #### Filled Fields - **sensor->name** (`char[]`) - `"HMC5883"` (null-terminated) - **sensor->version** (`int32_t`) - `1` - **sensor->sensor_id** (`int32_t`) - `_sensorID` (constructor argument) - **sensor->type** (`sensors_type_t`) - `SENSOR_TYPE_MAGNETIC_FIELD` - **sensor->min_delay** (`uint32_t`) - `0` - **sensor->max_value** (`float`) - `800` (µT, i.e. ±8 gauss) - **sensor->min_value** (`float`) - `-800` (µT, i.e. −8 gauss) - **sensor->resolution** (`float`) - `0.2` (µT, i.e. 2 milligauss) ### Request Example ```cpp 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("------------------------------------"); } ``` ``` -------------------------------- ### HMC5883_ID macro definition Source: https://github.com/adafruit/adafruit_hmc5883_unified Defines the chip ID value 0xD4, matching the expected content of the identification registers. Exported by the header but not checked in the implementation. ```cpp #define HMC5883_ID (0b11010100) ``` -------------------------------- ### hmc5883MagData struct definition Source: https://github.com/adafruit/adafruit_hmc5883_unified Internal raw magnetometer data type used as the private member _magData of the driver class. The orientation field is a placeholder; the private read() always assigns 0.0, with a source comment indicating a ToDo to calculate orientation. ```cpp typedef struct hmc5883MagData_s { float x; //!< Magnetometer x value float y; //!< Magnetometer y value float z; //!< Magnetometer z value float orientation; //!< Magnetometer orientation } hmc5883MagData; ``` -------------------------------- ### hmc5883MagRegisters_t enum definition Source: https://github.com/adafruit/adafruit_hmc5883_unified Register map of the HMC5883L used internally for I2C register addressing. Declared in Adafruit_HMC5883_U.h lines 30–46. Exposed publicly but not consumed by any public method as a parameter; used internally by write8/read8/read. ```cpp typedef enum { HMC5883_REGISTER_MAG_CRA_REG_M = 0x00, HMC5883_REGISTER_MAG_CRB_REG_M = 0x01, HMC5883_REGISTER_MAG_MR_REG_M = 0x02, HMC5883_REGISTER_MAG_OUT_X_H_M = 0x03, HMC5883_REGISTER_MAG_OUT_X_L_M = 0x04, HMC5883_REGISTER_MAG_OUT_Z_H_M = 0x05, HMC5883_REGISTER_MAG_OUT_Z_L_M = 0x06, HMC5883_REGISTER_MAG_OUT_Y_H_M = 0x07, HMC5883_REGISTER_MAG_OUT_Y_L_M = 0x08, HMC5883_REGISTER_MAG_SR_REG_Mg = 0x09, HMC5883_REGISTER_MAG_IRA_REG_M = 0x0A, HMC5883_REGISTER_MAG_IRB_REG_M = 0x0B, HMC5883_REGISTER_MAG_IRC_REG_M = 0x0C, HMC5883_REGISTER_MAG_TEMP_OUT_H_M = 0x31, HMC5883_REGISTER_MAG_TEMP_OUT_L_M = 0x32 } hmc5883MagRegisters_t; ```