Embarking on embedded electronics often begins with a simple, yet foundational project: making an LED blink. This tutorial focuses on using the compact and versatile ATtiny85 microcontroller to achieve this classic "Hello World" of hardware, complete with a pushbutton input for interaction.
Why the ATtiny85?
The ATtiny85 is a powerful 8-pin microcontroller from Atmel (now Microchip), known for its small form factor, low power consumption, and affordability. Despite its size, it packs significant processing power, making it ideal for projects where space and budget are critical. It's an excellent choice for learning microcontroller basics without the complexity of larger boards like an Arduino Uno.
Understanding the Circuit Diagram
Our circuit demonstrates how to control an output (an LED) and read an input (a pushbutton) using the ATtiny85. This simple setup forms the basis for countless embedded projects.
Key Components and Their Functions:
- U1: ATtiny85 Microcontroller: This is the brain of our circuit. It's an 8-pin IC. Pin 8 (VCC) is connected to the +5V power supply, and Pin 4 (GND) is connected to ground, providing power to the chip. The ATtiny85 executes the code we upload, controlling the LED and reading the button's state.
- D1: LED (Light Emitting Diode): Our visual output. It lights up when current flows through it in the correct direction. The anode (longer leg) is connected to Pin 5 (PB0) of the ATtiny85, and the cathode (shorter leg) is connected to resistor R2.
- R2: Current-Limiting Resistor (270Ω): Crucial for protecting the LED from excessive current and the ATtiny85's output pin from damage. Without it, the LED would likely burn out instantly. When the ATtiny85 sets PB0 to HIGH, current flows through R2 and D1.
- Pushbutton: Our input device. One side is connected to ground. The other side is connected to Pin 1 (PB5) of the ATtiny85 and also to resistor R1. When pressed, it connects PB5 to ground.
- R1: Pull-up Resistor (10kΩ): This resistor is connected between +5V and Pin 1 (PB5). It ensures that when the pushbutton is not pressed, Pin 1 is pulled up to +5V (a HIGH state). When the button is pressed, it overrides R1 and pulls Pin 1 down to ground (a LOW state). This prevents the input pin from "floating" and picking up random electrical noise.
- +5V Power Supply & Ground: These provide the necessary electrical power for the entire circuit to function, establishing a common reference point.
How the Circuit Operates (The Blink Logic)
The core operation relies on the ATtiny85's ability to control its output pins. For the LED to blink, the microcontroller is programmed to perform the following sequence:
- Set Pin 5 (PB0) to HIGH (+5V). This allows current to flow from PB0, through the LED (D1), through the current-limiting resistor (R2), and to ground, thus turning the LED ON.
- Wait for a specified duration (e.g., 500 milliseconds).
- Set Pin 5 (PB0) to LOW (0V). This stops current flow through the LED, turning it OFF.
- Wait for another specified duration.
- Repeat the cycle indefinitely.
The pushbutton adds an interactive element. The ATtiny85 continuously monitors Pin 1 (PB5). When the button is pressed, PB5 goes LOW; when released, R1 pulls PB5 back to HIGH. Your code can use this input to, for example, toggle the LED's blink pattern, start/stop the blinking, or change its speed.
Practical Applications
While a blinking LED might seem simple, this project is a stepping stone for many practical applications. The ATtiny85, with its compact size, is ideal for:
- Status Indicators: Blinking LEDs can indicate device status (e.g., power on, error, processing).
- Simple User Interfaces: The pushbutton input can be expanded to control various device functions.
- Low-Power Devices: Its efficiency makes it suitable for battery-powered sensors or small gadgets.
- Wearable Technology: Its small footprint fits well into compact designs.
- Model Making & Robotics: Controlling small motors, servos, or other visual effects.
Getting Started with Code
To make the ATtiny85 blink, you'll write a small program, typically using the Atmel Studio IDE with WinAVR compiler which has the ATtiny core already installed. The following is the led blink code for ATtiny85:
#ifndef F_CPU
#define F_CPU 8000000UL // 8 MHz internal clock (default)
#endif
#include <avr/io.h>
#include <util/delay.h>
int main(void) {
// 1. Set PB0 as output (Pin 5 on the physical chip)
// DDRB is the Data Direction Register for Port B
DDRB |= (1 << PB0);
while (1) {
// 2. Turn LED ON
PORTB |= (1 << PB0);
_delay_ms(500);
// 3. Turn LED OFF
PORTB &= ~(1 << PB0);
_delay_ms(500);
}
return 0;
}
This ATtiny85 AVR C program begins by defining the CPU clock frequency as 8 MHz using F_CPU, which ensures that timing functions like _delay_ms() work accurately with the microcontroller’s internal oscillator. It then includes the necessary AVR libraries for hardware register control and delay generation. Inside the main function, the DDRB register is configured so that PB0 (physical pin 5 on the ATtiny85) acts as an output by setting its corresponding bit to 1. The program then enters an infinite loop where PORTB is used to control the output state of PB0: first setting the pin HIGH to turn the connected LED on, waiting for 500 milliseconds, then clearing the pin LOW to turn the LED off, followed by another 500-millisecond delay. This continuous cycle creates a blinking LED effect with a one-second total period, demonstrating basic digital output control and timing on the ATtiny85.
This basic ATtiny85 LED blink tutorial not only illuminates an LED but also lights the path to understanding fundamental microcontroller concepts. Build this circuit, upload your code, and watch your first embedded system come to life!
Related Web Pages: