AVR microcontrollers
Atmel AVR AT90S8515, ATmega8, ATtiny2313 and ATtiny15 microcontrollers.

AVR is a series of microcontrollers from Atmel. Fitted with A/D converters, comparators, timers, interrupts, internal oscillator, etc. Flash memory is used for the main program, SDRAM for variables and EEPROM for values that needs to be saved through a power loss. Cheap, fast and easy make them perfect for home automation projects. Speeds up to 20 Mhz, USART (e.g. RS-232, RS-485) and low power consumption are some other benefits. A starting kit such as the STK500 is a good basis, with this you can test and program a variety of devices. A full list of developing tools can be found at Atmel.com.

Table of content

Getting started

Programming

There are a lot of different IDEs and compilers available, here is a few of them:

Devices

This is some of the devices I most commonly use, for the complete list see Atmels device list:

Basic circuit

Basic AVR ATtiny2313 circuit
Schematic diagram of an AVR ATtiny2313 with 7 inputs and 8 outputs.

This basic circuit has 7 inputs and 8 outputs and uses a ATtiny2313 with internal oscillator. This is maximum capacity for this device if you don’t count PA.0, PA.1 and PA.2, which is normally used for a crystal oscillator and a reset switch.

Try placing C1 and C2 as close to the device as you can, this might improve the stability of the power supply; which is a good thing.

Power

L78S05CV voltage regulator curcuit
Schematic drawing of a L78S05CV voltage regulator circuit.

Most AVR microcontrollers run on 5 volts, which you can easily get by using a voltage regulator and a couple of capacitors. I normally use 47 µF for C1 and C2. A voltage regulator like the L78S05CV can deliver 2 amps and have a maximum input voltage of 35 volts. Remember to mount a heat-sink, especially with high input voltage and/or high load.

External crystal

AVR ATtiny2313 with external oscillator
Schematic drawing showing a AVR ATtiny2313 with external crystal oscillator.

If you are going to use an external crystal oscillator it must be connected between XTAL1 and XTAL2 with a 22pF capacitor to GND on both sides. Remember to set the fuse bits according to your oscillator, and if you are going to use serial communication; pick a frequency that works with your baud rate.

I/O

Digital inputs

Pull-up resistor circuit
Schematic diagram showing a pull-up resistor circuit. Used for e.g. AVR inputs.

If your input has a pull-up resistor it will be pulled high when not active. So you need to pull it down (low) to trigger it, meaning that it is triggered by ground. Note that this also inverts your inputs, so you need to handle that in your programming.

Pull-down resistor circuit
Schematic diagram showing a pull-down resistor circuit. Used for e.g. AVR inputs.

When your input has a pull-down resistor it is pulled low when not active and you activate it by pulling it up (high).

Analog inputs

Analog inputs doesn’t require pull-ups or pull-downs, they simply measure the input voltage. The value of the input is relative to the AREF (analog reference) pin, meaning that if this pin has 5 volts, and your input has 2.5 volts you will get a value of 512 (half of 1023, which is the maximum of a 10 bit ADC). Remember to power the ADC by connecting it’s AVCC and GND pins to power.

To measure higher voltages than the input can handle; you can use a voltage divider, this simply divides the voltage down by using two resistors. And for restive sensors you can use a Wheatstone bridge.

Digital outputs

NPN transistor amplification
Schematic drawing showing output signal amplification with a single NPN transistor.

You can power an LED (20 mA) of the AVR output without any additional components, but any more than that and it’s going to need some help. For this we use transistors, a bipolar NPN transistor is a good choice. You will need a resistor on the base pin, otherwise too much current will pass through the transistor and you will break it.

NPN and PNP transistor amplification
Schematic drawing showing output signal amplification with a NPN and PNP transistor.

If you are going to drive some heavy stuff a small bipolar transistor is not going to be enough. You need a power transistor, but these do not have the same amount of amplification so it’s a good idea to place it after your small bipolar transistor.

PWM outputs can be connected the same way.

Components

LEDs

LED pin-out
Pin-out for an LED.

You will pretty much always need a resistor when powering LEDs. Here is a useful tool for calculating the value of that resistor: LED Resistor Calculator.

Useful reading:

Transistors

Transistor pin-out
Pin-out for different TO92 transistors.

Useful reading:

Relays

Relay circuit
Relay circuit, with protection diode and transistor.

When connecting a relay to a digital output it must have a protection diode, this prevents a power surge from flowing back into the input when the relay releases. This surge can cause the microcontroller to restart, it may even break the output.

Diodes

Diode pin-out
Pin-out for a diode.

Electricity only flows one way through a diode, just remember that it also has a voltage drop in the forward direction.

Useful reading:

Serial

Getting your AVR device to communicate over a serial line opens up a whole new world of opportunities, and is totally bad ass! But; to do that we need some additional components to get our signal levels right. I’ve added a simple schematic for RS-232 (standard computer serial port) and RS-485 (multidrop communication over long wire spans).

RS-232

AVR to RS-232 using MAX232
Schematic diagram showing a MAX232 circuit used between a RS-232 signal and a AVR microcontroller.

With this you can communicate with your AVR device from your computer using the serial port, which is cool. See resources tagged with RS-232 on this site.

RS-485

AVR to RS-485 using MAX485
Schematic diagram showing a MAX485 circuit used between a RS-485 signal and a AVR microcontroller.

Now; with this you can build a whole network of AVR devices operating on the RS-485 network, very cool! You will need to handle the addressing in your application though. See resources tagged with RS-485 on this site.

Now you are ready for some serious AVR projects! :)

Please leave a comment or tweet if you have any questions or feedback on this article.

Videos

Image gallery

External links

Article: Getting started with the AVR microcontroller series by Thomas Jensen is licensed under CC BY-SA 4.0 with attribution required.

Similar articles

  • No similar articles found...

  •   Created Feb 27, 2015
  •   Last modified 4 days ago

Comments