From 3aecf50cc878a773528cc448ac9bb64a2afb19af Mon Sep 17 00:00:00 2001 From: ladyada Date: Wed, 11 Jun 2025 18:01:04 -0400 Subject: [PATCH] Initial library implementation for STHS34PF80 infrared sensor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add basic library structure with header and implementation files - Implement constructor, destructor, and begin() method - Add register definitions with doxygen comments - Include WHO_AM_I chip identification verification - Add basic test example sketch - Verified working on hardware with I2C address 0x5A 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- Adafruit_STHS34PF80.cpp | 70 ++++++++++++++++++++ Adafruit_STHS34PF80.h | 69 +++++++++++++++++++ examples/test_sths34pf80/test_sths34pf80.ino | 23 +++++++ 3 files changed, 162 insertions(+) create mode 100644 Adafruit_STHS34PF80.cpp create mode 100644 Adafruit_STHS34PF80.h create mode 100644 examples/test_sths34pf80/test_sths34pf80.ino diff --git a/Adafruit_STHS34PF80.cpp b/Adafruit_STHS34PF80.cpp new file mode 100644 index 0000000..a79633d --- /dev/null +++ b/Adafruit_STHS34PF80.cpp @@ -0,0 +1,70 @@ +/*! + * @file Adafruit_STHS34PF80.cpp + * + * @mainpage Adafruit STHS34PF80 infrared sensor library + * + * @section intro_sec Introduction + * + * This is a library for the STHS34PF80 infrared sensor + * + * Designed specifically to work with the Adafruit STHS34PF80 breakout: + * http://www.adafruit.com/products/ + * + * These sensors use I2C to communicate, 2 pins are required to interface. + * + * Adafruit invests time and resources providing this open source code, + * please support Adafruit and open-source hardware by purchasing + * products from Adafruit! + * + * @section author Author + * + * Written by Ladyada for Adafruit Industries. + * + * @section license License + * + * MIT license, all text here must be included in any redistribution + * + */ + +#include "Adafruit_STHS34PF80.h" + +/*! + * @brief Instantiates a new STHS34PF80 class + */ +Adafruit_STHS34PF80::Adafruit_STHS34PF80() {} + +/*! + * @brief Cleans up the STHS34PF80 + */ +Adafruit_STHS34PF80::~Adafruit_STHS34PF80() { + if (i2c_dev) { + delete i2c_dev; + } +} + +/*! + * @brief Initializes the hardware and detects a valid STHS34PF80 + * @param i2c_addr I2C address to use + * @param wire The Wire object to be used for I2C connections + * @return True if initialization was successful, otherwise false + */ +bool Adafruit_STHS34PF80::begin(uint8_t i2c_addr, TwoWire *wire) { + if (i2c_dev) { + delete i2c_dev; + } + + i2c_dev = new Adafruit_I2CDevice(i2c_addr, wire); + + if (!i2c_dev->begin()) { + return false; + } + + Adafruit_BusIO_Register chip_id = Adafruit_BusIO_Register( + i2c_dev, STHS34PF80_REG_WHO_AM_I, 1); + + if (chip_id.read() != 0xD3) { + return false; + } + + return true; +} \ No newline at end of file diff --git a/Adafruit_STHS34PF80.h b/Adafruit_STHS34PF80.h new file mode 100644 index 0000000..9777405 --- /dev/null +++ b/Adafruit_STHS34PF80.h @@ -0,0 +1,69 @@ +/*! + * @file Adafruit_STHS34PF80.h + * + * This is a library for the STHS34PF80 infrared sensor + * + * Designed specifically to work with the Adafruit STHS34PF80 breakout: + * http://www.adafruit.com/products/ + * + * These sensors use I2C to communicate, 2 pins are required to interface. + * + * Adafruit invests time and resources providing this open source code, + * please support Adafruit and open-source hardware by purchasing + * products from Adafruit! + * + * Written by Ladyada for Adafruit Industries. + * + * MIT license, all text here must be included in any redistribution. + * + */ + +#ifndef __ADAFRUIT_STHS34PF80_H__ +#define __ADAFRUIT_STHS34PF80_H__ + +#include "Arduino.h" +#include +#include +#include + +#define STHS34PF80_DEFAULT_ADDR 0x5A ///< Default I2C address for the STHS34PF80 + +#define STHS34PF80_REG_LPF1 0x0C ///< Low-pass filter configuration 1 register +#define STHS34PF80_REG_LPF2 0x0D ///< Low-pass filter configuration 2 register +#define STHS34PF80_REG_WHO_AM_I 0x0F ///< Device identification register +#define STHS34PF80_REG_AVG_TRIM 0x10 ///< Averaging configuration register +#define STHS34PF80_REG_CTRL0 0x17 ///< Control register 0 (gain mode) +#define STHS34PF80_REG_SENS_DATA 0x1D ///< Sensitivity data register +#define STHS34PF80_REG_CTRL1 0x20 ///< Control register 1 (ODR configuration) +#define STHS34PF80_REG_CTRL2 0x21 ///< Control register 2 (boot, function access, one-shot) +#define STHS34PF80_REG_CTRL3 0x22 ///< Control register 3 (interrupt configuration) +#define STHS34PF80_REG_STATUS 0x23 ///< Status register +#define STHS34PF80_REG_FUNC_STATUS 0x25 ///< Function status register +#define STHS34PF80_REG_TOBJECT_L 0x26 ///< Object temperature LSB register +#define STHS34PF80_REG_TOBJECT_H 0x27 ///< Object temperature MSB register +#define STHS34PF80_REG_TAMBIENT_L 0x28 ///< Ambient temperature LSB register +#define STHS34PF80_REG_TAMBIENT_H 0x29 ///< Ambient temperature MSB register +#define STHS34PF80_REG_TOBJ_COMP_L 0x38 ///< Compensated object temperature LSB register +#define STHS34PF80_REG_TOBJ_COMP_H 0x39 ///< Compensated object temperature MSB register +#define STHS34PF80_REG_TPRESENCE_L 0x3A ///< Presence detection LSB register +#define STHS34PF80_REG_TPRESENCE_H 0x3B ///< Presence detection MSB register +#define STHS34PF80_REG_TMOTION_L 0x3C ///< Motion detection LSB register +#define STHS34PF80_REG_TMOTION_H 0x3D ///< Motion detection MSB register +#define STHS34PF80_REG_TAMB_SHOCK_L 0x3E ///< Ambient shock detection LSB register +#define STHS34PF80_REG_TAMB_SHOCK_H 0x3F ///< Ambient shock detection MSB register + +/*! + * @brief Class that stores state and functions for interacting with the STHS34PF80 + */ +class Adafruit_STHS34PF80 { +public: + Adafruit_STHS34PF80(); + ~Adafruit_STHS34PF80(); + + bool begin(uint8_t i2c_addr = STHS34PF80_DEFAULT_ADDR, TwoWire *wire = &Wire); + +private: + Adafruit_I2CDevice *i2c_dev; +}; + +#endif \ No newline at end of file diff --git a/examples/test_sths34pf80/test_sths34pf80.ino b/examples/test_sths34pf80/test_sths34pf80.ino new file mode 100644 index 0000000..34800b6 --- /dev/null +++ b/examples/test_sths34pf80/test_sths34pf80.ino @@ -0,0 +1,23 @@ +// Basic test for STHS34PF80 infrared sensor + +#include "Adafruit_STHS34PF80.h" + +Adafruit_STHS34PF80 sths; + +void setup() { + Serial.begin(115200); + while (!Serial) delay(10); + + Serial.println("Adafruit STHS34PF80 test!"); + + if (!sths.begin()) { + Serial.println("Could not find a valid STHS34PF80 sensor, check wiring!"); + while (1) delay(10); + } + + Serial.println("STHS34PF80 Found!"); +} + +void loop() { + delay(1000); +} \ No newline at end of file