ATTINY402 | NEED SLEEP OPERATION WITH TRIGGER WAKE UP | ARDUINO IDE | TAMPER DETECTION SYSTEM | PLEASE HELP! #1225
Unanswered
msaraswat15031997
asked this question in
Q&A
Replies: 1 comment
-
|
A couple of tips
With sleep mode, make sure your external circuitry is not drawing any power |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
it is based on attiny402 , coin cell cr2302, limit switch for tamper detection, connector with i2c and updi, power. the attiny is connected as such, pin 1 of the ic is vcc, pin 2 is the limit switch, pin 3 is the battery read adc, pin 4 is the sda, pin 5 is the scl, pin 6 is the updi programming pin, pin 7 is the led, and the pin 8 is the ground. the design only uses smd components expect the limit switch most of them are 603 size, the tampering works as follows, attiny stays in deep sleep mode, until the limit switch is released triggering an interrupt to the attiny, it wakes up , and registers a flag in the eeprom that the device has been tampered, whenever a master i2c device wants to read, the attiny sends the eeprom flags and the battery voltage, if the battery gets below a certian voltage eg. 1.8 - 2 V then low battery flag is also set in the eeprom,
MY CURRENT READINGS ARE 80 micro ampere to 380 micro ampere . but i need to run this on a coin cell for a year at minimum.
:(
#include <Wire.h>
#include <Arduino.h>
#include <EEPROM.h>
#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/io.h>
#define SLAVE_ADDRESS 0x08
const int buttonPin = 0;
const int ledPin = 4;
volatile bool buttonInterruptFlag = false;
volatile bool tamperFlag = false;
const int EEPROM_TAMPER_FLAG_ADDR = 0;
const int EEPROM_FW_ID_ADDR = 1;
const int FW_ID_SIZE = 20;
void setup() {
// Create a unique firmware identifier using compilation date and time
const char currentFwId[FW_ID_SIZE] = DATE " " TIME;
// Buffer to read stored firmware identifier from EEPROM
char storedFwId[FW_ID_SIZE];
EEPROM.get(EEPROM_FW_ID_ADDR, storedFwId);
// Check if the stored firmware ID matches the current one
if (strncmp(storedFwId, currentFwId, FW_ID_SIZE) != 0) {
// Firmware has been updated; reset tamper flag and update firmware ID in EEPROM
EEPROM.update(EEPROM_TAMPER_FLAG_ADDR, 0);
EEPROM.put(EEPROM_FW_ID_ADDR, currentFwId);
tamperFlag = false;
} else {
// Firmware is the same; read the tamper flag from EEPROM
tamperFlag = EEPROM.read(EEPROM_TAMPER_FLAG_ADDR);
}
// Disable the ADC for lower power consumption
ADC0.CTRLA &= ~ADC_ENABLE_bm;
// Setup button and LED pins
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
// Initialize I²C as a slave
Wire.begin(SLAVE_ADDRESS);
Wire.onRequest(requestEvent); // Register callback for I²C master requests
// Attach an external interrupt to the button pin
attachInterrupt(digitalPinToInterrupt(buttonPin), buttonISR, FALLING);
}
void loop() {
// Check if the button press was detected
if (buttonInterruptFlag) {
buttonInterruptFlag = false;
// Only update if the tamper flag is not already set
if (!tamperFlag) {
tamperFlag = true;
EEPROM.update(EEPROM_TAMPER_FLAG_ADDR, 1); // Persistently store the tamper flag
}
// Enter idle sleep mode to reduce power consumption
set_sleep_mode(SLEEP_MODE_IDLE);
sleep_enable();
sleep_mode(); // Sleep until an interrupt occurs
sleep_disable();
}
// Simple ISR: only sets a flag
void buttonISR() {
buttonInterruptFlag = true;
}
// I²C request callback: sends the tamper flag status to the master
void requestEvent() {
Wire.write(tamperFlag ? 1 : 0);
}
Beta Was this translation helpful? Give feedback.
All reactions