ATtiny13 “Candle” Flickering Lamp

The theory

To achieve random variation of LEDs behavior, like candle flicker, we need to generate random numbers. Embedded systems (like ATtiny13) generally use some hardware based pseudo-random number generators like linear feedback shift registers (LFSRs).

This code is use 32-bit Galois LFSR (Linear-feedback shift register) random number generator. The bit positions that affect the next state are called the taps (32, 31, 29 and 1). Bits that are not taps are shifted one position to the right unchanged. The taps, on the other hand, are XORed with the output bit before they are stored in the next position. The new output bit is the next input bit. The effect of this is that when the output bit is zero, all the bits in the register shift to the right unchanged, and the input bit becomes zero. When the output bit is one, the bits in the tap positions all flip (if they are 0, they become 1, and if they are 1, they become 0), and then the entire register is shifted to the right and the input bit becomes 1.

Based on the random values generated by the LFSR, random of LEDs are switched on. Between two port updates, a random amount of delay is executed. The value of the delay time is also derived from the LFSR value. The seed value of the LFSR is set to 1.



Code source: “tinyAVR Microcontroller Project for the Evil Genius” page 40-41

* "tinyAVR Microcontroller Project for the Evil Genius" page 40-41
#include <avr/io.h>
#include <util/delay.h>
int main(void){
 unsigned long lfsr = 1;
 unsigned char temp;
 DDRB = 0xff;  // makes the all pins an output
   lfsr  = (lfsr >>1) ^ (-(lfsr & 1u) & 0xd0000001u); /* taps 32 31 29 1 */
   temp  = (unsigned char) lfsr;          //take lower
   DDRB  = ~temp;                         //Declare those pins as output where temp has zero
   PORTB = temp;                          //Give the value of 0 to the pins declared output
   temp  = (unsigned char) (lfsr >> 24);


More images:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.