Skip to content

Commit

Permalink
Init STHS34PF80
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinyu-Zhao committed Jul 26, 2024
1 parent 70d4cfb commit 85d4fc7
Show file tree
Hide file tree
Showing 11 changed files with 4,287 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug-report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ body:
label: Issue checklist
description: Please double-check that you have done each of the following things before submitting the issue.
options:
- label: I searched for previous reports in [the issue tracker](https://github.com/m5stack/M5Stack/issues?q=)
- label: I searched for previous reports in [the issue tracker](https://github.com/m5stack/M5-STHS34PF80/issues?q=)
required: true
- label: My report contains all necessary details
required: true
43 changes: 7 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,48 +1,19 @@
# Product Name
# M5-STHS34PF80

## Overview

### SKU:xxx
<img src="https://m5stack.oss-cn-shenzhen.aliyuncs.com/resource/docs/products/unit/UNIT-TMOS%20PIR/4.webp" alt="basic" width="350" height="350">

Description of the product
### SKU:U185

## Related Link

- [Document & Datasheet](https://docs.m5stack.com/en/unit/product_Link)

## Required Libraries:

- [Adafruit_BMP280_Library](https://github.com/adafruit/Required_Libraries_Link)

## License

- [Product Name- MIT](LICENSE)

## Remaining steps(Editorial Staff Look,After following the steps, remember to delete all the content below)
Contains M5Stack-**Unit TMOS PIR** related case programs.

1. Change [clang format check path](./.github/workflows/clang-format-check.yml#L9-L15).
2. Add License content to [LICENSE](/LICENSE).
3. Change link on line 78 of [bug-report.yml](./.github/ISSUE_TEMPLATE/bug-report.yml#L78).

```cpp
Example
# M5Unit-ENV

## Overview

### SKU:U001 & U001-B & U001-C

Contains M5Stack-**UNIT ENV** series related case programs.ENV is an environmental sensor with integrated SHT30 and QMP6988 internally to detect temperature, humidity, and atmospheric pressure data.
**TMOS PIR** is a highly sensitive infrared sensor unit for presence and motion detection. It uses the STHS34PF80 chip solution. It communicates with the M5 device via I2C communication (default address: 0x5A). Its working principle is based on the blackbody radiation principle described by Planck's law, and can monitor ambient temperature and detect human presence and movement. The sensor can distinguish between stationary and moving objects and has an 80-degree field of view, providing a wide detection range. In addition, it supports adjustable sampling frequency and gain mode to adapt to different application requirements. Suitable for a variety of applications such as alarm systems, smart lighting and occupancy detection.

## Related Link

- [Document & Datasheet](https://docs.m5stack.com/en/unit/envIII)

## Required Libraries:

- [Adafruit_BMP280_Library](https://github.com/adafruit/Adafruit_BMP280_Library)
- [TMOS PIR - Document & Datasheet](https://docs.m5stack.com/en/products/sku/U185)

## License

- [M5Unit-ENV - MIT](LICENSE)
```
- [M5-STHS34PF80- MIT](LICENSE)
146 changes: 146 additions & 0 deletions examples/Hat_TMOS_BasicReadings/Hat_TMOS_BasicReadings.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
*
* SPDX-License-Identifier: MIT
*/

/*
* @file BasicReadings.ino
* @author Tinyu(tinyu@m5stack.com)
* @brief TMOS basic readings
* @version 0.1
* @date 2024-07-22
*
*
* @Hardwares: M5StickCPlus + Hat TMOS
* @Platform Version: Arduino M5Stack Board Manager v2.1.1
* @Dependent Library:
* M5_STHS34PF80 https://github.com/m5stack/M5_STHS34PF80
*/
#include <M5StickCPlus.h>
#include "M5_STHS34PF80.h"

M5_STHS34PF80 TMOS;

uint8_t motionHysteresis = 0;
int16_t motionVal = 0, presenceVal = 0;
uint16_t motionThresholdVal = 0, precenceThresholdVal = 0;
sths34pf80_gain_mode_t gainMode;

#define inteeruptMode /// Enable interrupt mode

#ifdef interruptMode
bool volatile interruptFlag = false;
int intPin = 36;
// ISR to set the triggered interrupt
void isr1() {
interruptFlag = true;
}
#endif

void setup() {
M5.begin();
M5.Lcd.setTextColor(YELLOW);
M5.Lcd.setRotation(3);
M5.Lcd.setTextSize(2);
M5.Lcd.setCursor(50, 10);
M5.Lcd.println("Hat-TMOS TEST");
Serial.println("Hat-TMOS TEST");
M5.Lcd.setCursor(120, 35);
M5.Lcd.setTextColor(WHITE);

// Initialise I2C and devices
while (TMOS.begin(&Wire, STHS34PF80_I2C_ADDRESS, 0, 26) == false) {
M5.Lcd.setCursor(60, 35);
M5.Lcd.setTextColor(RED);
M5.Lcd.println("Device init error");
Serial.println("I2C Error - check I2C Address");
Serial.println("Error setting up device - please check wiring.");
delay(200);
}

Serial.println("Open the Serial Plotter for graphical viewing");
TMOS.setTmosODR(STHS34PF80_TMOS_ODR_AT_2Hz);
// TMOS.setMotionThreshold(0xFF);
TMOS.setPresenceThreshold(0xC8); // Default value
TMOS.setMotionThreshold(0xC8);
TMOS.setPresenceHysteresis(0x32);
TMOS.setMotionHysteresis(0x32);

////Default mode, may cause inaccurate detection of MotionValFlag bit.
// TMOS.setGainMode(STHS34PF80_GAIN_DEFAULT_MODE);

// Decreasing gain mode, detection distance decreases
TMOS.setGainMode(STHS34PF80_GAIN_WIDE_MODE);
TMOS.setTmosSensitivity(0xff);

#ifdef interruptMode
// Set INT pin to be triggered on rising and falling edges of INT pin
pinMode(intPin, INPUT);
// Attach interrupt to the pin as a digital pin that triggers on a change
attachInterrupt(digitalPinToInterrupt(intPin), isr1, CHANGE);
// Route all interrupts from device to interrupt pin
TMOS.setTmosRouteInterrupt(STHS34PF80_TMOS_INT_OR);
// Enable the presence and motion interrupt source
// (see page 17 of application note AN5867 for more information)
TMOS.setTmosInterruptOR(STHS34PF80_TMOS_INT_ALL);
// Set interrupt value to pulsed on the INT pin
TMOS.setInterruptPulsed(1);
#endif

TMOS.resetAlgo();

TMOS.getGainMode(&gainMode);
TMOS.getMotionThreshold(&motionThresholdVal);
TMOS.getPresenceThreshold(&precenceThresholdVal);
TMOS.getMotionHysteresis(&motionHysteresis);
Serial.printf("precenceThresholdVal:%x, motionThresholdVal:%x, motionHysteresis:%x GainMode:%x\n",
precenceThresholdVal, motionThresholdVal, motionHysteresis, gainMode);
delay(1000);
}

void loop() {
#ifndef interrinterruptMode
// Real-time detection of presence and motion infrared energy values
sths34pf80_tmos_drdy_status_t dataReady;
TMOS.getDataReady(&dataReady);
if (dataReady.drdy == 1) {
#else
// Interrupt detection of presence and motion infrared energy values
if (interruptFlag == true) {
interruptFlag = false;
#endif

sths34pf80_tmos_func_status_t status;
TMOS.getPresenceValue(&presenceVal);
TMOS.getMotionValue(&motionVal);
TMOS.getStatus(&status);

M5.Lcd.fillRect(106, 48, 80, 20, BLACK);
M5.Lcd.setCursor(2, 50);
M5.Lcd.printf("Presence:%d", presenceVal);

M5.Lcd.fillRect(167, 70, 100, 20, BLACK);
M5.Lcd.setCursor(2, 70);
M5.Lcd.printf("PrescenceFlag:%s", status.pres_flag ? "Det" : "None");

M5.Lcd.fillRect(85, 88, 60, 20, BLACK);
M5.Lcd.setCursor(2, 90);
M5.Lcd.printf("Motion:%d", motionVal);

M5.Lcd.fillRect(167, 110, 100, 20, BLACK);
M5.Lcd.setCursor(2, 110);
M5.Lcd.printf("MotionValFlag:%s", status.mot_flag ? "Det" : "None");

// Output only if presence/motion is detected
if (status.pres_flag == 1) {
TMOS.getPresenceValue(&presenceVal);
Serial.printf("Prescence Detected! PrescenceValue:%d\n", presenceVal);
}

if (status.mot_flag == 1) {
TMOS.getMotionValue(&motionVal);
Serial.printf("Motion Detected! MotionValue:%d\n", motionVal);
}
}
}
113 changes: 113 additions & 0 deletions examples/Unit_TMOS_BasicReadings/Unit_TMOS_BasicReadings.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
*
* SPDX-License-Identifier: MIT
*/

/*
* @file BasicReadings.ino
* @author Tinyu(tinyu@m5stack.com)
* @brief TMOS basic readings
* @version 0.1
* @date 2024-07-22
*
*
* @Hardwares: M5Core + Unit TMOS
* @Platform Version: Arduino M5Stack Board Manager v2.1.1
* @Dependent Library:
* M5_STHS34PF80 https://github.com/m5stack/M5_STHS34PF80
*/
#include <M5Stack.h>
#include "M5_STHS34PF80.h"

M5_STHS34PF80 TMOS;

uint8_t motionHysteresis = 0;
int16_t motionVal = 0, presenceVal = 0;
uint16_t motionThresholdVal = 0, precenceThresholdVal = 0;
sths34pf80_gain_mode_t gainMode;

void setup() {
M5.begin();
M5.Lcd.setTextColor(YELLOW);
M5.Lcd.setTextSize(2);
M5.Lcd.setCursor(80, 10);
M5.Lcd.println("Unit-TMOS TEST");
Serial.println("Unit-TMOS TEST");
M5.Lcd.setCursor(120, 35);
M5.Lcd.setTextColor(WHITE);

// Initialise I2C and devices
while (TMOS.begin(&Wire, STHS34PF80_I2C_ADDRESS, 21, 22) == false) {
M5.Lcd.setCursor(60, 35);
M5.Lcd.setTextColor(RED);
M5.Lcd.println("Device init error");
Serial.println("I2C Error - check I2C Address");
Serial.println("Error setting up device - please check wiring.");
delay(200);
}

Serial.println("Open the Serial Plotter for graphical viewing");
TMOS.setTmosODR(STHS34PF80_TMOS_ODR_AT_2Hz);
// TMOS.setMotionThreshold(0xFF);
TMOS.setPresenceThreshold(0xC8); // Default value
TMOS.setMotionThreshold(0xC8);
TMOS.setPresenceHysteresis(0x32);
TMOS.setMotionHysteresis(0x32);

////Default mode, may cause inaccurate detection of MotionValFlag bit.
// TMOS.setGainMode(STHS34PF80_GAIN_DEFAULT_MODE);

// Decreasing gain mode, detection distance decreases
TMOS.setGainMode(STHS34PF80_GAIN_WIDE_MODE);
TMOS.setTmosSensitivity(0xff);
TMOS.resetAlgo();

TMOS.getGainMode(&gainMode);
TMOS.getMotionThreshold(&motionThresholdVal);
TMOS.getPresenceThreshold(&precenceThresholdVal);
TMOS.getMotionHysteresis(&motionHysteresis);
Serial.printf("precenceThresholdVal:%x, motionThresholdVal:%x, motionHysteresis:%x GainMode:%x\n",
precenceThresholdVal, motionThresholdVal, motionHysteresis, gainMode);
delay(1000);
}

void loop() {
sths34pf80_tmos_drdy_status_t dataReady;
TMOS.getDataReady(&dataReady);

if (dataReady.drdy == 1) {
// Real-time detection of presence and motion infrared energy values
sths34pf80_tmos_func_status_t status;
TMOS.getPresenceValue(&presenceVal);
TMOS.getMotionValue(&motionVal);
TMOS.getStatus(&status);

M5.Lcd.fillRect(104, 68, 80, 20, BLACK);
M5.Lcd.setCursor(0, 70);
M5.Lcd.printf("Presence:%d", presenceVal);

M5.Lcd.fillRect(165, 90, 100, 20, BLACK);
M5.Lcd.setCursor(0, 90);
M5.Lcd.printf("PrescenceFlag:%s", status.pres_flag ? "Detected" : "None");

M5.Lcd.fillRect(83, 108, 60, 20, BLACK);
M5.Lcd.setCursor(0, 110);
M5.Lcd.printf("Motion:%d", motionVal);

M5.Lcd.fillRect(165, 130, 100, 20, BLACK);
M5.Lcd.setCursor(0, 130);
M5.Lcd.printf("MotionValFlag:%s", status.mot_flag ? "Detected" : "None");

// Output only if presence/motion is detected
if (status.pres_flag == 1) {
TMOS.getPresenceValue(&presenceVal);
Serial.printf("Prescence Detected! PrescenceValue:%\n", presenceVal);
}

if (status.mot_flag == 1) {
TMOS.getMotionValue(&motionVal);
Serial.printf("Motion Detected! MotionValue:%d\n", motionVal);
}
}
}
16 changes: 16 additions & 0 deletions library.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "M5-STHS34PF80",
"description": "Library for M5Stack Unit TMOS",
"keywords": "M5Stack Driver STHS34PF80",
"authors": {
"name": "M5Stack",
"url": "http://www.m5stack.com"
},
"repository": {
"type": "git",
"url": "https://github.com/m5stack/M5-STHS34PF80.git"
},
"version": "0.0.1",
"frameworks": "arduino",
"platforms": "espressif32"
}
10 changes: 10 additions & 0 deletions library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name=M5-STHS34PF80
version=0.0.1
author=M5Stack
maintainer=M5Stack
sentence=Library for M5Stack Unit TMOS.
paragraph=See more on http://M5Stack.com
category=Sensors
url=https://github.com/m5stack/M5-STHS34PF80
architectures=esp32
includes=M5_STHS34PF80.h
Loading

0 comments on commit 85d4fc7

Please sign in to comment.