Attiny13 Led Project

Thread Starter

Engin SUNAL

Joined Nov 11, 2023
4
I'm trying to make a Christmas themed LED project using Attiny13.
But I have problems.

The code works differently when I run the same code with 3 batteries and 1 battery.In the code I wrote, the LEDs must first blink sequentially and then all together. The code works correctly (First picture) with 3 batteries. But when I run the code with 1 battery(Second picture), only the 1st LED lights up and nothing else happens.

The same happens when I reduce the number of LEDs from 10 to 4. I was wondering if the battery was not strong enough.

Some people have made similar projects with more LEDs than this with 1 battery. What do you think is the problem? Hardware? Software?




C-like:
#define LED_PIN_1 0

#define LED_PIN_2 1

#define LED_PIN_3 2

#define LED_PIN_4 3

#define LED_PIN_5 4


void setup() {

  pinMode(LED_PIN_1, OUTPUT);

  pinMode(LED_PIN_2, OUTPUT);

  pinMode(LED_PIN_3, OUTPUT);

  pinMode(LED_PIN_4, OUTPUT);

  pinMode(LED_PIN_5, OUTPUT);

}


void loop() {

 

  digitalWrite(LED_PIN_1, HIGH);

  digitalWrite(LED_PIN_2, LOW);

  digitalWrite(LED_PIN_3, LOW);

  digitalWrite(LED_PIN_4, LOW);

  digitalWrite(LED_PIN_5, LOW);

  delay(1000);

  

  digitalWrite(LED_PIN_1, HIGH);

  digitalWrite(LED_PIN_2, HIGH);

  digitalWrite(LED_PIN_3, LOW);

  digitalWrite(LED_PIN_4, LOW);

  digitalWrite(LED_PIN_5, LOW);

  delay(1000);

  

  digitalWrite(LED_PIN_1, HIGH);

  digitalWrite(LED_PIN_2, HIGH);

  digitalWrite(LED_PIN_3, HIGH);

  digitalWrite(LED_PIN_4, LOW);

  digitalWrite(LED_PIN_5, LOW);

  delay(1000);

  

  digitalWrite(LED_PIN_1, HIGH);

  digitalWrite(LED_PIN_2, HIGH);

  digitalWrite(LED_PIN_3, HIGH);

  digitalWrite(LED_PIN_4, HIGH);

  digitalWrite(LED_PIN_5, LOW);

  delay(1000);

  

  digitalWrite(LED_PIN_1, HIGH);

  digitalWrite(LED_PIN_2, HIGH);

  digitalWrite(LED_PIN_3, HIGH);

  digitalWrite(LED_PIN_4, HIGH);

  digitalWrite(LED_PIN_5, HIGH);

  delay(1000);

  

  digitalWrite(LED_PIN_1, LOW);

  digitalWrite(LED_PIN_2, LOW);

  digitalWrite(LED_PIN_3, LOW);

  digitalWrite(LED_PIN_4, LOW);

  digitalWrite(LED_PIN_5, LOW);

  delay(1000);

  

  digitalWrite(LED_PIN_1, HIGH);

  digitalWrite(LED_PIN_2, HIGH);

  digitalWrite(LED_PIN_3, HIGH);

  digitalWrite(LED_PIN_4, HIGH);

  digitalWrite(LED_PIN_5, HIGH);

  delay(1000);

  

  digitalWrite(LED_PIN_1, LOW);

  digitalWrite(LED_PIN_2, LOW);

  digitalWrite(LED_PIN_3, LOW);

  digitalWrite(LED_PIN_4, LOW);

  digitalWrite(LED_PIN_5, LOW);

  delay(1000);

    

  digitalWrite(LED_PIN_1, HIGH);

  digitalWrite(LED_PIN_2, HIGH);

  digitalWrite(LED_PIN_3, HIGH);

  digitalWrite(LED_PIN_4, HIGH);

  digitalWrite(LED_PIN_5, HIGH);

  delay(1000);

  

  digitalWrite(LED_PIN_1, LOW);

  digitalWrite(LED_PIN_2, LOW);

  digitalWrite(LED_PIN_3, LOW);

  digitalWrite(LED_PIN_4, LOW);

  digitalWrite(LED_PIN_5, LOW);

  delay(1000);

  

}
 
Last edited by a moderator:

Sensacell

Joined Jun 19, 2012
3,400
Maybe you will get some help if you re-draw the diagram as an actual schematic.
I have no idea what is going on there in that image.

But in general, those coin batteries have a lot of internal resistance, and I see no current limiting resistors in the image.
This means the LED current is uncontrolled and probably dropping the Vcc down to where the MCU crashes.
 

Thread Starter

Engin SUNAL

Joined Nov 11, 2023
4
3 AAA in series and a 3.3V low dropout regulator.

Edit to add: or a Lithium ion cell.
Actually, I will make a small pcb like a keychain. That's why I'm thinking of using CR2032. Also, there is no need to use a regulator. What do you think?
 

Sensacell

Joined Jun 19, 2012
3,400
https://www.hackster.io/381906/attiny13a-pcb-star-wearable-63ce67

I hate designs like this- these are traps for beginners.
The designer of this thing probably spent many hours trying to figure out how to make this work by "bending" design rules in clever, complex ways- all in the name of making it as small and minimal as possible.
If you make what looks to be and innocent change, the circuit stops working- and figuring out why is not a trivial affair.

Yes-you can get this to work, but it's using parts in a manner that involves deep understanding of the details.
Better to learn the basics first - learn about using resistors to control the current through LEDs- rather than relying on the impedance of an output pin, which is a very poor practice.
Power your MCU via a stable regulated source.
 

Ya’akov

Joined Jan 27, 2019
8,974
Welcome to AAC.

You must use current limiting resistors for the LEDs, for two reasons:

  • To control the overall load by reducing the current to the LEDs to a value that provides enough illumination while avoiding the diminishing returns if adding brightness that isn’t perceptible but costs a lot in terms of power consumption.

  • To balance the apparent brightness of the different colored LEDs—they will not look the same given the same current and this will give an odd appearance.

You can create color “channels” by which I just mean selecting a resistor to put in series with the GPIO pin for a given color and wiring the LEDs in parallel connected to it. Remember the ATTiny13A has robust outputs, but they do have limits—40mA per pin and 200mA between \(\mathsf{V_{cc}}\) and GND

It is not a good idea to push the per pin current to the maximum as this is the absolute maximum rating and so give yourself a budget of 30mA-35mA per color channel. Calculate the load based on the number of LEDs and how much current each will consume after the limiting resistor is in place.

Remember, you are using a very low capacity cell to power this, one not designed for the sort of current demand you are going to place on it. So, everything you can do to reduce the consumption by the LEDs is worthwhile—battery life is already going to be very short.

Be realistic about how bright the LEDs need to be, it is surprising how much light LEDs can produce with very low current. Consider calibrating the LED brightness for a normally lit room rather than for, say, bright sunlight.

As far as your code goes, I assume it “works” but it is pure brute force. Though this has nothing to do with your current (no pun intended) problem, it merits attention. There are a number of ways you could rewrite it to make bug fixes, enhancements, and reuse much easier.

Think in terms of not repeating the same or nearly the same code over and over. Instead, write functions that do things like turn the colors* on and off with a single call, for example in might look like:


ledControl(RED, ON)
ledControl(RED, OFF)

Or something similar, rather than a long list of pin states. This is just one way you might be able to do it, and you should think about the most convenient way to describe what you want the LEDs to do (e.g.: logically related groups, states, delays, &c.) and then write functions so that your main loop is almost nothing more than a list of function calls that describes displays you want to see.

Good luck.
 
Top