Skip to content

Commit

Permalink
Merge pull request #8 from bobveringa/feature/7-data-ready-pin
Browse files Browse the repository at this point in the history
Feature/7 data ready pin
  • Loading branch information
bobveringa authored Jan 22, 2022
2 parents b5a63a8 + 37ce323 commit 86d38af
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/****************************************************************
* Example4_Data_Ready_Pin.ino
* HSCDTD008A Library Demo
* Bob Veringa
* Original Creation Date: 2021-01-16
*
* Distributed as-is; no warranty is given.
***************************************************************/

#include <Arduino.h>
#include "hscdtd008a.h"


// The pin DRDY is connected to on the Arduino.
// It is required to connect a pull down resistor (between 1k and 4k) to this line.
const int data_ready_pin = 2;

// Create an instance of the sensor.
HSCDTD008A geomag;


void setup() {
hscdtd_status_t status;

Serial.begin(9600);

geomag.begin();
// If you know the I2C address is different than in the provided
// data sheet. Uncomment the line below, and configure the address.
// geomag.begin(0x0F);

// Initialize the hardware.
status = geomag.initialize();
if (status != HSCDTD_STAT_OK) {
Serial.println("Failed to initialize sensor. Check wiring.");

// Halt program here.
while (true) { delay(1); }
}
// Compensate for temperature.
geomag.temperatureCompensation();

// Set the device in the Normal State (required for reading using ODR).
geomag.setStateNormal();

// Configure (ODR) Output Data Rate.
// Valid options are:
// HSCDTD_ODR_0_5HZ (0.5Hz)
// HSCDTD_ODR_10HZ (10Hz)
// HSCDTD_ODR_20HZ (20Hz)
// HSCDTD_ODR_100HZ (100Hz) (Does not work with this example)
geomag.configureOutputDataRate(HSCDTD_ODR_20HZ);

// Enable data ready pin output.
geomag.setDataReadyPinEnabledStatus(HSCDTD_DEN_ENABLED);
}


void loop() {
int status;

// Make sure a pull down resistor (between 1k and 4k) is connected to the data ready line.
// Check if data is ready by digital read.
status = digitalRead(data_ready_pin);

if (status == HIGH) {
// If there is data ready, retrieve it from the device.
geomag.retrieveMagData();

// And print the result.
Serial.print("X: ");
Serial.print(geomag.mag.mag_x);
Serial.print("uT,\t");

Serial.print("Y: ");
Serial.print(geomag.mag.mag_y);
Serial.print("uT,\t");

Serial.print("Z: ");
Serial.print(geomag.mag.mag_z);
Serial.print("uT");

Serial.println("");
}
// Wait a bit before checking again.
delay(5);
}
2 changes: 2 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ configureOutputDataRate KEYWORD2
retrieveMagData KEYWORD2
applyOffsetDrift KEYWORD2
getTemperature KEYWORD2
setDataReadyPinEnabledStatus KEYWORD2
setDataReadyPinPolarity KEYWORD2


#######################################
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ It is one of the cheapest geomagnetic / magneto sensors available and seems to b
| Measurement Normal State<sup>1</sup> | ✔️ | ✔️ |
| Temperature compensation | ✔️ | ✔️ |
| Offset calibration | ✔️ | ✔️ |
| Data Ready Pin| | ✔️ |
| Data Ready Pin| ✔️ | ✔️ |
| Offset Drift | ✔️ | ✔️ |
| Self test| ✔️ | ✔️ |
| FIFO || ❌<sup>2</sup> |
Expand Down
30 changes: 30 additions & 0 deletions src/hscdtd008a.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,36 @@ hscdtd_status_t HSCDTD008A::applyOffsetDrift(float x_off, float y_off,
}


/**
* @brief Set the Data Ready Pin Enabled Status
*
* @param den
* @return hscdtd_status_t
*/
hscdtd_status_t HSCDTD008A::setDataReadyPinEnabledStatus(hscdtd_den_t den)
{
return hscdtd_set_data_ready_pin_enable(&this->device, den);
}


/**
* @brief Set the Data Ready Pin Polarity
*
* The polarity can be configured as:
* - Active LOW
* - Active HIGH (Default).
*
* This option is only available if Data Ready Pin output is enabled.
*
* @param drp
* @return hscdtd_status_t
*/
hscdtd_status_t HSCDTD008A::setDataReadyPinPolarity(hscdtd_drp_t drp)
{
return hscdtd_set_data_ready_pin_polarity(&this->device, drp);
}


/**
* @brief Get the temperature value.
*
Expand Down
2 changes: 2 additions & 0 deletions src/hscdtd008a.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class HSCDTD008A {
hscdtd_status_t configureOutputDataRate(hscdtd_odr_t odr);
hscdtd_status_t retrieveMagData(void);
hscdtd_status_t applyOffsetDrift(float x_off, float y_off, float z_off);
hscdtd_status_t setDataReadyPinEnabledStatus(hscdtd_den_t den);
hscdtd_status_t setDataReadyPinPolarity(hscdtd_drp_t drp);

int getTemperature(void);

Expand Down

0 comments on commit 86d38af

Please sign in to comment.