ATtiny13 – randomly flashing LED with PRNG based on LFSR

This Project simply flashes the LED in random manner. It uses PRNG (Pseudo Random Number Generator) based on LFSR (Linear Feedback Shift Register) to generate 16-bit (pseudo) random numbers. The less significant bit of randomly generated number is a major factor in decision to on/off the LED. The code is on Github, click here

Parts Required

Circuit Diagram



This code is written in C and can be compiled using the avr-gcc. More details on how compile this project is here.

#include <avr/io.h>
#include <util/delay.h>

#define LED_PIN         PB0
#define LFSR_SEED       (91)
#define DELAY           (64)

static uint16_t
        static uint16_t cnt16 = LFSR_SEED;
        return (cnt16 = (cnt16 >> 1) ^ (-(cnt16 & 1) & 0xB400));


        /* setup */
        DDRB = 0b00000001; // set LED pin as OUTPUT
        PORTB = 0b00000000; // set all pins to LOW

        /* loop */
        while (1) {
                if (prng_lfsr16() & 1) {
                        PORTB |= _BV(LED_PIN); // LED on
                } else {
                        PORTB &= ~(_BV(LED_PIN)); // LED off

Leave a Comment