Drive LED or Opto-isolators with AVR

This might be seem very simple to many of you, but I still get questions about simple microcontroller interfacing. So I will put a thread of notes about interfacing AVR microcontrollers to devices like LED’s, relays, I2C, etc.

As you might know Diodes require pretty small current. This current depends on diode type and can be from 3mA up to 20mA and more. Working voltage is from 1.5 to 4V.

One AVR pin can sink up to 20mA of current; it is convenient to connect diode directly to it with current limiting resistor. Never connect diode to pin without resistor – you may damage your AVR as your current may exceed the 20mA limit!

There are two ways to drive LED’s with AVR: sink and source. Sink method is better, because tour microcontroller doesn’t have to generate current to power LED’s. This may be critical when connecting more Diodes to AVR.

image001.png

When connecting more than one diode, just remember that there is a maximum current that microcontroller can drive. For example Atmega8 maximum current can be 200mA.

The same situation is with opto-isolators as inside them there are the same diodes packed together with sensors. Optoisolators are convenient when you need to untie the controller from power circuits.

FYI: Current limiting resistor can be calculated very simply. Find out maximum current LED can handle and divide applied voltage by this current then you will have resistance of your resistor. R=5V/20mA=250Ohm. I usually double this resistance as I don’t like to drive my LED’s on maximum.

2 Responses to Drive LED or Opto-isolators with AVR

  1. Your calculation of the resistance does not take into account the voltage drop of the LED. The voltage you divide by the current is the supply LESS the LED voltage drop. eg Blue LED drop is ~3V so 5V-3V = 2V. 2V/20ma = 100 ohm. Red LED drop is ~1.8V so 3.2V/20ma = 160 ohm. Green ~2.2V drop (140 ohm). These are the MINIMUM resistance values since – as you correctly stated – the maximum sink current of an AVR is 20 ma per pin.

    Also, if you are advising people to interface silicon components to relays, insist they use a freewheeling diode across the coil to prevent back EMF spikes damaging components. Idealy this should be a ‘fast’ diode, but that is getting picky.

    Otherwise good stuff dude.

  2. Yep you are right. For smaller supply voltages is more visible, but for higher this can be neglected to simplify calculations and do it faster. But if keep in mind that it is better to chose at least 50% higher resistor values to preserve LEDs lifetime, such calculation can be accepted. Any way thanks for pointing that out.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

To submit your comment, click the image below where it asks you to...