Skip to content

Instantly share code, notes, and snippets.

@unixfg
Created April 20, 2024 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unixfg/1e0020c48533965a50c2fcaf4bae9631 to your computer and use it in GitHub Desktop.
Save unixfg/1e0020c48533965a50c2fcaf4bae9631 to your computer and use it in GitHub Desktop.
#include <Arduino.h>
#include <Wire.h>
uint8_t i2cAddress = 0x22;
uint8_t intPin = 3;
bool interruptFlag = false;
void isr1()
{
interruptFlag = true;
}
void writeRegister(uint8_t reg, uint8_t value) {
Wire.beginTransmission(i2cAddress);
Wire.write(reg);
Wire.write(value);
Wire.endTransmission();
}
void setup()
{
Serial.begin(115200);
delay(5000);
Wire.begin(0, 1);
pinMode(intPin, INPUT);
attachInterrupt(digitalPinToInterrupt(intPin), isr1, RISING);
// Set up sensor configurations
writeRegister(0x06, 0x26); // Z Threshold
writeRegister(0x08, 0x64); // Enable Interrupt, 10us latch, through int pin
writeRegister(0x02, 0x46); // Z-Channel Only and Sleep 50ms
writeRegister(0x01, 0x23); // Operating Mode W&S
}
void loop()
{
if (interruptFlag == true)
{
interruptFlag = false;
Serial.println("Threshold Reached!");
writeRegister(0x01, 0x23);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment