Help needed with unstable power on attiny13

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!

Please post a hand drawn circuit diagram, with pins and parts properly labeled, plus links to sensor modules.

Image links are broken. Post them in line, following this guide.

jremington:
Image links are broken. Post them in line, following this guide.

My apologies. Images uploaded now

Your schematic does not show the required bypass capacitor across the MCU power terminals. Minimum value 100 nF. An additional 10 - 100 uF cap in parallel will also help.

If the Brown Out Detector is enabled, how did you set the fuses?

jremington:
Your schematic does not show the required bypass capacitor across the MCU power terminals. Minimum value 100 nF. A 10 - 100 uF cap will also help.

I don't have that. I'll add it in and report back. But I also don't have that on the board I mentioned works well. Can you please point to me to any documentation on that?

jremington:
If the Brown Out Detector is enabled, how did you set the fuses?

I didn't. Sorry, I'm new to this. I just selected the option in the Arduino IDE

You need bypass capacitors in just about any circuit, but especially ones with a complex IC. Sometimes circuits will work without one, but it is really a bad idea to count on that. Google "bypass capacitor" or "decoupling capacitor" for 11,400,000 discussions.

For typical documentation, consult the TTP223 data sheet you linked. Note the example circuit diagram, and this comment:

  1. The C1 capacitor must be used between VDD and VSS; and should be routed with very short tracks to the device’s VDD and VSS pins (TTP223-BA6/TTP223N-BA6).

The MCU data sheet documents the Brown Out Detector. It is probably resetting the MCU when the battery voltage drops, as a result of excessive current through the LED.

Increase R1 to at least 470 - 1K Ohms. An efficient LED can be quite bright with just 1 mA current.

Increase R1 to at least 470 - 1K Ohms. An efficient LED can be quite bright with just 1 mA current.

Bingo! That was totally it! I replaced the resistor for a 1K and the circuit is fixed! I've tested this. a million times on a 56 ohm resistor after using the calculator here. Which always gave me this result:

This is the LED I am using!

I guess this is provided I always get a stable 3V, which in my case will not happen as this is battery powered. I feel like I learned a lot here. Thanks :wink:

As for my code, would you change anything on it, or does it look good?

You still need the decoupling capacitor. It is likely to fail unpredictably without.