Go Down

Topic: Adding timer to electronic safe using ATTINY85 (Read 1 time) previous topic - next topic

jordos

Nov 24, 2016, 09:34 am Last Edit: Nov 24, 2016, 09:43 am by jordos
Hello all,

I've modified a simple electronic safe with an ATTINY85 in order to add a basic timer function. The intention is that it will only allow you to open the safe once every X days.
I've got it working, but the safe runs on 4xAA batteries and after adding the microcontroller to the circuit it will only last about 4-5 days.
I don't know the current draw of the original circuit and I don't know the current draw of the microcontroller running my code.
The ATTINY85 has it's fuses set to run at 1MHZ, with BOD disabled.
The safe is this one: http://www.varo.com/3069/11593/safes/motsa07el-mini-safe-170x230x170.aspx.
It works by sending a signal through a resistor to a solenoid. Unfortunately I couldn't find anything on the chip it uses. If needed I'll post a picture of the circuit.
Below is the code I use. I put the MCU in the place of the resistor. The MCU uses the watchdog timer in intervals of 8 seconds to sleep for the desired amount of time. (it doesn't have to be terribly accurate) After that it will enable interrupts on pin 0 and power down. Once an interrupt is received it will set pin 2 high, this pin goes to the resistor and to the solenoid. Again it goes to sleep, and when another interrupt is triggered on pin 0 (the chip of the safe disarms the lock for about 5 seconds, during this time it will keep the signal HIGH) it will disable interrupts again and the timer restarts. Between the chip of the safe and pin 0 there is no resistor. Maybe this is a bad idea? Any other thoughts on how to improve battery life?

Code: [Select]

#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>

#define DEBUG (false)

#define INT_PIN (0)
#define OUT_PIN (2)
#define LED_PIN (1)

/***************************************************
 *  Name:        ISR(WDT_vect)
 *
 *  Returns:     Nothing.
 *
 *  Parameters:  None.
 *
 *  Description: Watchdog Interrupt Service. This
 *               is executed when watchdog timed out.
 *
 ***************************************************/
EMPTY_INTERRUPT(WDT_vect);

EMPTY_INTERRUPT(PCINT0_vect);
/***************************************************
 *  Name:        enterSleep
 *
 *  Returns:     Nothing.
 *
 *  Parameters:  None.
 *
 *  Description: Enters the arduino into sleep mode.
 *
 ***************************************************/
void enterSleep()
{
  //noInterrupts();
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();
  //ADCSRA &= ~_BV(ADEN); // ADC off
  //power_all_disable();
  //interrupts();
   
  sleep_cpu();
 
  //noInterrupts();
  sleep_disable(); /* First thing to do is disable sleep. */
  //power_all_enable();
  //ADCSRA |= _BV(ADEN); // ADC on
  //interrupts();

  #if DEBUG
    digitalWrite(LED_PIN, HIGH);
    for (uint16_t dbg = 0; dbg < 65535; dbg++);
    digitalWrite(LED_PIN, LOW);
  #endif
}



/***************************************************
 *  Name:        setup
 *
 *  Returns:     Nothing.
 *
 *  Parameters:  None.
 *
 *  Description: Setup for the serial comms and the
 *                Watch dog timeout.
 *
 ***************************************************/
void setup()
{
  pinMode(INT_PIN,INPUT);
  pinMode(OUT_PIN,OUTPUT);
  pinMode(LED_PIN,OUTPUT);
  ADCSRA &= ~_BV(ADEN); // ADC off
  power_all_disable();
}



/***************************************************
 *  Name:        enterSleep
 *
 *  Returns:     Nothing.
 *
 *  Parameters:  None.
 *
 *  Description: Main application loop.
 *
 ***************************************************/
void loop()
{
  digitalWrite(OUT_PIN, LOW);
  digitalWrite(LED_PIN, LOW);

  /*** Setup the WDT ***/
  wdt_reset();
  /* In order to change WDE or the prescaler, we need to
   * set WDCE (This will allow updates for 4 clock cycles).
   * WDTCR on ATTINY85 / WDTCSR on ATMEGA
   */
  WDTCR |= (1<<WDCE) | (1<<WDE);
  /* set new watchdog timeout prescaler value */
  WDTCR = 1<<WDP0 | 1<<WDP3; /* 8.0 seconds */
  /* Enable the WD interrupt (note no reset). */
  WDTCR |= _BV(WDIE);

  // sleep for 8 seconds x 43200 = 96 hours = 4 days
  for (uint16_t i = 0; i < 1800; i++)
    enterSleep();

  wdt_reset();
  wdt_disable();

  GIMSK |= _BV(PCIE); // Enable Pin Change Interrupts
  PCMSK |= _BV(PCINT0); // Use PB0 as interrupt pin
  enterSleep();

  #if DEBUG
    digitalWrite(LED_PIN, HIGH);
  #else
    digitalWrite(OUT_PIN, HIGH);
  #endif
  enterSleep();

  PCMSK &= ~_BV(PCINT0); // Turn off PB0 as interrupt pin
}


PS. I realise an MCU is not required for this task, but I'm not sure how to do it using just electronics.
edit: PPS. I use this board http://makbit.com/web/firmware/breathing-life-into-digispark-clone-with-attiny-mcu/ to mount the ATTINY85. I've desoldered it's power LED. It also has a linear regulator but I am bypassing it by using the unregulated 5V input.

SteveP76

#1
Nov 24, 2016, 05:09 pm Last Edit: Nov 24, 2016, 05:16 pm by SteveP76
First off these "safes" are very insecure both in terms off non destructive entry and destructive entry.
But for sake off device hacking they are fun, take look at the transistor close to solenoid output.
Is it s8050 transistor and 1K base resistor with two mcu(BJ8P1539) output?.
Running mine with arduino mini pro 3.3v and rc522 rfid reader 69 days or ~2.700 000 cycles so far.
Good luck with your project.
Btw I use the same battery setup you do 4xAA.

jordos

Hey, thanks for the reply, it's encouraging to know yours is lasting such a long time. I don't think we have quite the same kind of setup though, I only spot one MCU for one. Here's a picture, the markings are a bit unclear though (I had to remove a sticker first)
The blue resistor is where I modded in my own circuit (not in this picture). The flatcable goes to the keypad, the blue wires go to the solenoid and the red/black wires go to the batteries.

SteveP76

Looks similar, pin P64 and P65 are both connected to 1K base resistor and s8050 transistor
in my pcb. Solenoid will draw 160 -180mA in use not very power efficient.

jordos

yeah, the pins for the solenoid at least are the same! :D
that's a lot of power! But my main concern here is the 'idle' current draw. As long as I can keep that to a minimum I think I'll be fine. It doesn't have to run for years, just quite a bit longer than 4-5 days...

jordos

I've measured the current draw of the ATTINY85 running my code (with #define debug true) while not connected to the safe and got ~1.28mA/h. While it's not as low as it could be, I guess that is because of the extra components like diodes etc... on the development board, and because debug mode flashes a led briefly. Anyway, I don't think 1.28mA/h makes a big impact on 4-5 days so I assume the cause lies somewhere else.

Go Up