This project shows how to simply Fade-In and Fade-Out a LED by using delay function to control pulse width what is sometimes called Software PWM (Pulse Width Modulation). In our circuit a LED is connected to PB0 and it is made to Fade the LED In and Out alternately. The code is on Github, click here.
Parts Required
- ATtiny13 – i.e. MBAVR-1 development board
- Resistor – 220Ω, see LED Resistor Calculator
- LED
Circuit Diagram
Firmware
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 DELAY_MAX (512) #define DELAY_MIN (1) #if DELAY_MAX < 1 || DELAY_MIN < 1 # warning "Value of DELAY_MAX and DELAY_MAIN should be from range <1, 2^16>" #endif #if !(DELAY_MAX > DELAY_MIN) # warning "Value of DELAY_MAX should be greater then DELAY_MIN" #endif int main(void) { uint16_t delay = DELAY_MIN; uint8_t dir = 0; /* setup */ DDRB = 0b00000001; // set LED pin as OUTPUT PORTB = 0b00000001; // set LED pin to HIGH /* loop */ while (1) { PORTB &= ~(_BV(LED_PIN)); // LED off _delay_loop_2(delay); PORTB |= _BV(LED_PIN); // LED on _delay_loop_2(DELAY_MAX - delay); if (dir) { // fade-in if (++delay >= (DELAY_MAX - 1)) dir = 0; } else { // fade-out if (--delay <= DELAY_MIN) dir = 1; } } }