Email: Password: Remember Me | Create Account (Free)
Robotics using the 8052


Connecting an LED to the 8052


  LEDs
The AT89C52 
Connecting LEDs to the 8052 micrcontroller is something nearly everyone wants to do. It can give you a visual indication of your program running, or just look pretty. Whatever you want them for, connecting them is such a regularly asked question this page is here to provide the answers.

There are few descisions you need to make before using any of the circuits below. So ask yourself these questions first:

What type of LED do you want to use?
Do you want to use the microcontrollers regulated supply for power, or an unregulated one?
How many LEDs do you want to connect?, if it's more than a couple, then these circuits, probably aren't for you, and you should use a driver.


There are a number methods I will show here. Each have their merits, and pitfalls, you know your project better than anyone, so it's up to you in the end.


Have a browse down the list, at the bottom of each circuit description you'll find the Pros and Cons of using that particular method.



  The Quick and Dirty Method
Circuit 1 


The circuit to the left shows a really quick way of connecting an LED to a 8052. You can connect this circuit up very quickly and dumb a program on the microcontroller to flash it on and off.

For example, if the I/O line was P1.0, then writing a ONE to the pin will switch off the LED, and writing an ZERO will switch it on.

Also, the LED will be on until the microcontroller gets going, and you switch it off. This doesn't look very professional, and might not be the function you desire.

The Resistor is 330 ohms, which will allow 15mA through the LED, which will be enough to make it nice and bright. You need to look on your datasheet for the microcontroller you are using to ensure that the Port can sink that much current. If it can't take that much, you will have to increase the resistor value until it is within the specifications of the chip.

You calculate this value by using Ohms Law :- V/I = R or Voltage Divided by Current = Resistance.

We're using 5 Volts here, and want 15mA through the resistor, a 'normal' LED uses about 15mA and has a voltage drop of about 2v (1.8 for RED and 2.1v for GREEN for a standard LED) so the calculation is :-

(5-2) / 0.015 (15 milliamps) = 200 Ohms so that's Ohms

If your controller can take only 10mA then we have to recalculate it as :-

(5-2) / 0.010 (10 milliamps) = 300 Ohms.

If your controller can take more current than this, just increase the figure to what the LED specifications are (whatever it is 20mA should be plenty).

Pros

  • Low component count.
  • Easy to make.

Cons

  • Must use the regulated microcontroller supply.
  • Possibility of frying your microcontroller if you use the wrong resistor.
  • LED is on at power-up.




Using a NPN Transistor



This circuit shows a better way of connecting an LED using a NPN Transistor, in this case I have used a 3906, which can handle around 100mA which is plenty for what we want.

Here the LED is off when the micrcontroller starts, and will be switched on by writing ZERO to the port. It can be turned off again by writing a ONE to the port.

The main benefit of this configuration is that it is only using one extra component, but it does allow a lot more current to flow through the LED. In the case of the 3906, it will allow at least 100 times as much current through the LED as is flowing from the port to ground. This mean you'll burn out the LED not your microcontroller.

A 330 Ohm resistor should be plenty for our needs here. But you can always lower the value if you want a make the LED brighter.






Pros

  • Can be driven from an unregulated source, as long as the transistor can handle it.
  • Easy enough to make.

Cons

  • More components that you'd thing you would need.
  • LED is on at power-up.





The Third method


More components that you can shake a stick at with this one, who'd have though you would need so much to do so little.
To be honest, I've put this one in to show you can have many variations on the theme here. This particular circuit decouples the LED drive from the microcontroller. It uses a transistor to switch on another transistor which drives the LED.
























Pros

  • A good circuit to drive loads from.
  • LED is off at power-up.

Cons

  • More components than you can shake a stick at.
  • LED is off at power-up.



A Simpler method


This circuit is slightly simpler than the one above, it must still use the regulated supply, but it has the advantage of having the LED off when it powers up, which looks slightly more professional. Write a ZERO to the port pin to switch it on and a ONE to switch it on.


The 270 Ohm resistor is responsible for the brightness of the LED in this circuit.























Pros

  • A good circuit to drive an LED, nice and simple.
  • LED is off at power-up.

Cons

  • More components that you'd like



As above, but using a NPN rather than a PNP


Lots of people will tell you to just us a NPN instead of an PNP or vice versa, and turn bits of the circuit upside down, here's what they mean. The other side-effect of doing this is that the LED will be ON at power on, and you write a ZERO to switch it off and a ONE to switch it ON.

The 270 Ohm resistor is, again, responsible for the brightness of the LED in this circuit.

















Pros

  • A good circuit to drive an LED, nice and simple.

Cons

  • More components that you really need.
  • LED is on at power-up.







Replacing the 10K resistor with a Zener diode


In this circuit, we've replaced the 10K resistor with a zener diode, which has a breakdown voltage of about 2.2 volts. We've also moved the 270 Ohm resistor below the transistor and made it 100 Ohms. You supply this circuit with any voltage you like, and as long as the transistor can dissapate the power you'll be fine. For example 12V would be no problem at all for this circuit. What's so different? Well in the other circuits, you couldn't add anymore LEDs in this one you can chain a load of them together, and they will all be a constant brightness. They will simply draw more power from the supply. So if you put 10 LEDs in there they will be as bright as 1 LED in the previous circuit.

The 100 Ohm resistor and the zener diode are responsible for the power through the LED(s)













Pros

  • A good circuit to drive an LED, nice and simple.
  • It can drive lots of LED in series.
  • It can take a lot of voltage without any of it accidentally going and frying your microcontroller.
  • LED is off at power-up.

Cons

  • More components that you really need, for a simple LED being illuminated.





Types of LED available.

There are many types of LED available, what I call the 'standard' LED is the mass produced red LED, which has a voltage drop of about 2 volts, and draws about 15-20mA. Other types are available, for example the Agilent HLMP-17xx, at 3mA they are as bright as most 'standard' LEDs.

If you used one of these in the first circuit a 1K resistor would be needed.



Summary

As you can see there are lots of options for getting an LEd to light up, personally I use the first one, it's simple and I can remove it from the circuit if I need the pin it is using.





Questions or comments? post them here.
Back to my homepage
Back to the 8052 user homepages
Back to the 8052 homepage