From b4e680935a8fac8c8ae8a54432772eb12fcf49e6 Mon Sep 17 00:00:00 2001 From: bobveringa Date: Fri, 7 Jan 2022 00:25:44 +0100 Subject: [PATCH 1/3] Support data ready pin MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add support for data ready pin configuration to the Arduino wrapper layer. An example is added to demo how the data ready  pin feature can be used. --- .../Example4_Data_Ready_Pin.ino | 87 +++++++++++++++++++ keywords.txt | 2 + src/hscdtd008a.cpp | 30 +++++++ src/hscdtd008a.h | 2 + 4 files changed, 121 insertions(+) create mode 100644 examples/Arduino/Example4_Data_Ready_Pin/Example4_Data_Ready_Pin.ino diff --git a/examples/Arduino/Example4_Data_Ready_Pin/Example4_Data_Ready_Pin.ino b/examples/Arduino/Example4_Data_Ready_Pin/Example4_Data_Ready_Pin.ino new file mode 100644 index 0000000..e03fb58 --- /dev/null +++ b/examples/Arduino/Example4_Data_Ready_Pin/Example4_Data_Ready_Pin.ino @@ -0,0 +1,87 @@ +/**************************************************************** + * Example4_Data_Ready_Pin.ino + * HSCDTD008A Library Demo + * Bob Veringa + * Original Creation Date: 2021-01-07 + * + * Distributed as-is; no warranty is given. + ***************************************************************/ + +#include +#include "hscdtd008a.h" + + +// The pin DRDY is connected to on the Arduino. +// It is required to connect a pull down resistor 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 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); +} diff --git a/keywords.txt b/keywords.txt index 255cbd9..8c81d5d 100644 --- a/keywords.txt +++ b/keywords.txt @@ -24,6 +24,8 @@ configureOutputDataRate KEYWORD2 retrieveMagData KEYWORD2 applyOffsetDrift KEYWORD2 getTemperature KEYWORD2 +setDataReadyPinEnabledStatus KEYWORD2 +setDataReadyPinPolarity KEYWORD2 ####################################### diff --git a/src/hscdtd008a.cpp b/src/hscdtd008a.cpp index c5978b3..f00d453 100644 --- a/src/hscdtd008a.cpp +++ b/src/hscdtd008a.cpp @@ -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. * diff --git a/src/hscdtd008a.h b/src/hscdtd008a.h index 9796414..a819d97 100644 --- a/src/hscdtd008a.h +++ b/src/hscdtd008a.h @@ -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); From bf8eef6d21469bc985c52562e0dc78e4cb29fbfe Mon Sep 17 00:00:00 2001 From: bobveringa Date: Sat, 22 Jan 2022 20:32:49 +0100 Subject: [PATCH 2/3] Update example with resistor values. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Changed the example with example value range for the pull down resistor. These values may not work for all devices, as the manufacture seems to have taken some liberal margins with their components. --- .../Example4_Data_Ready_Pin/Example4_Data_Ready_Pin.ino | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/Arduino/Example4_Data_Ready_Pin/Example4_Data_Ready_Pin.ino b/examples/Arduino/Example4_Data_Ready_Pin/Example4_Data_Ready_Pin.ino index e03fb58..f0bb264 100644 --- a/examples/Arduino/Example4_Data_Ready_Pin/Example4_Data_Ready_Pin.ino +++ b/examples/Arduino/Example4_Data_Ready_Pin/Example4_Data_Ready_Pin.ino @@ -2,7 +2,7 @@ * Example4_Data_Ready_Pin.ino * HSCDTD008A Library Demo * Bob Veringa - * Original Creation Date: 2021-01-07 + * Original Creation Date: 2021-01-16 * * Distributed as-is; no warranty is given. ***************************************************************/ @@ -12,7 +12,7 @@ // The pin DRDY is connected to on the Arduino. -// It is required to connect a pull down resistor to this line. +// 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. @@ -27,7 +27,7 @@ void setup() { 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); + // geomag.begin(0x0F); // Initialize the hardware. status = geomag.initialize(); @@ -59,7 +59,7 @@ void setup() { void loop() { int status; - // Make sure a pull down resistor is connected to the data ready line. + // 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); From 37ce3231f6943a126a398a90d4efa2ee6957fb71 Mon Sep 17 00:00:00 2001 From: bobveringa Date: Sat, 22 Jan 2022 20:35:55 +0100 Subject: [PATCH 3/3] Update readme.md Update the supported features list. --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index f6c17fe..e3e15da 100644 --- a/readme.md +++ b/readme.md @@ -15,7 +15,7 @@ It is one of the cheapest geomagnetic / magneto sensors available and seems to b | Measurement Normal State1 | ✔️ | ✔️ | | Temperature compensation | ✔️ | ✔️ | | Offset calibration | ✔️ | ✔️ | -| Data Ready Pin| ❌ | ✔️ | +| Data Ready Pin| ✔️ | ✔️ | | Offset Drift | ✔️ | ✔️ | | Self test| ✔️ | ✔️ | | FIFO | ❌ | ❌2 |