Tutorial 001: Blinking LED x1

This is very simple tutorial that shows how to connect a LED to the Tinusaur board and write the “Hello World” of the microcontrollers – very simple program that makes a LED to blink.

Since the Tinusaur board is a very standard ATtiny breakout board this could be applied to almost any other board that has ATtiny microcontroller.

The code was tested to work with ATtiny13, ATtiny25, ATtiny45 and ATtiny85 but will probably work with other microcontrollers too.

Prerequisites

Tinusaur Tutorial 001 schematics

We assume that:

  • The Tinusaur board was already successfully assembled.
  • The USBasp ISP programmer connected to the computer and to the board and is properly working.
  • The development environment was setup.

Note: It is not the subject of this tutorial to show how to assemble the board or setup the programmer and the development environment.

Hardware

The LED D1 should be connected to the PB0 of the microcontroller which is pin 5 on the chip. This should be done through the resistor R1 and to the GND.

The LED, marked as D1, is just a standard light emitting diode.

The resistor, marked as R1, is 270 to 330 ohm.

Software

The program is very simple.

#include <avr/io.h>
#include <util/delay.h>
#define LED1_PORT PB0
int main(void) {
	DDRB |= (1 << LED1_PORT);
	for(;;) {
		PORTB |= (1 << LED1_PORT);
		_delay_ms(500);
		PORTB &= ~(1 << LED1_PORT);
		_delay_ms(500);
	}
	return (0);
}

The first meaningful line is this:

#define LED1_PORT PB0

It defines the port that will be used for the LED.

Then we have to setup that port as an output. This is done with the following line of code:

DDRB |= (1 << LED1_PORT);

The DDRB is the data direction for port B. What this does is this:

  • shifts the “1” on left, N-times depending on the LED1_PORT value; and …
  • does bitwise “OR” with the value in the port register DDRB.

The most important fragment of the code is this:

for (;;) {
	PORTB |= (1 << LED1_PORT);
	_delay_ms(200);
	PORTB &= ~(1 << LED1_PORT);
	_delay_ms(400);
}
Tinusaur Board with LED

What it does is this:

  • Begin an infinite loop.
  • Set the LED port bit to “1” – LED will be turned on. This is bitwise “OR” operation with “1”.
  • Wait a little. The delay function simply does N-number of “empty” loops.
  • Clear the LED port bit to “0” – LED will be turned off. This is bitwise “AND” operation with “0”.
  • Wait a little.
  • Do that again …

That’s it.

WinAVR

If you’re using WinAVR you need to go to the project folder and do this:

$ make

Then program it using AVRDUDE:

$ avrdude -c usbasp -p t85 -B 0.5 -U flash:w:"main.hex":a

This should put the binary code up on the microcontroller and the LED should start blinking immediately.

Source code

The source code is available on Bitbucket at this address: https://bitbucket.org/tinusaur/tutorials/src/default/tut001_blinking_led_x1/.

Leave a Comment