Hey folks, I would love some help with some attiny13 chips I have that begin to act unstable as power on the battery provided with my circuit starts to draw lower.
I've read a few topics here already and some of the excellent Nick Gamon's blog posts mentioned here.
My circuit is very simple and consists of an attiny13 connected to an LED and a TTP223 which acts as a touch sensor on my board. Schematics below:
I've written most of the code myself (but also copied some of Nick Gamon's code for the sleep functionality). Here's what I have:
#include <avr/sleep.h>
#include <avr/interrupt.h>
#include <Heart.h>
/*
Android Beating Heart V2- Sketch
Marcos Placona 2019
*/
const int switchPin = 3;
const int statusLED = 4;
Heart heart(statusLED, 1.0);
void setup() {
pinMode(switchPin, INPUT);
digitalWrite(switchPin, HIGH);
pinMode(statusLED, OUTPUT);
// Flash quick sequence so we know setup has started
for (int k = 0; k < 10; k = k + 1) {
if (k % 2 == 0) {
digitalWrite(statusLED, HIGH);
}
else {
digitalWrite(statusLED, LOW);
}
delay(250);
} // for
} // setup
void sleep() {
GIMSK |= _BV(PCIE); // Enable Pin Change Interrupts
PCMSK |= _BV(PCINT3); // Use PB3 as interrupt pin
ADCSRA &= ~_BV(ADEN); // ADC off
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // replaces above statement
sleep_enable(); // Sets the Sleep Enable bit in the MCUCR Register (SE BIT)
sei(); // Enable interrupts
sleep_cpu(); // sleep
cli(); // Disable interrupts
PCMSK &= ~_BV(PCINT3); // Turn off PB3 as interrupt pin
sleep_disable(); // Clear SE bit
//ADCSRA |= _BV(ADEN); // ADC on
sei(); // Enable interrupts
} // sleep
ISR(PCINT0_vect) {
// This is called when the interrupt occurs, but I don't need to do anything in it
}
void loop() {
sleep();
heart.beatForPeriod(10);
}
You can view the code for the heart library here but it's probably not super relevant.
What I am seeing is that when powering this from a solid 3V3 source (i.e. directly from an Arduino Nano) I get the desired effect and everything works perfectly. When powering this from a CR1220 3V battery, the LED flashes incessantly and the board never goes to sleep.
I do, however, have one board I programmed a few months ago that work correctly with any batteries I put on it. And this is using all the same hardware. I'm pretty sure I used the same code but can't be so sure about which options I used on the Arduino IDE when programming it. This is what I have now, and I tried using different Brownout detection level options.
Any help here would be much appreciated! Thanks in advance!