This is an old revision of the document!


 

Light-emitting Diode

Theory

5 mm legged LED

A light-emitting diode (LED) is a semiconductor device that emits light when a forward voltage is applied. The acronym for light-emitting diode is LED. There are different color combinations of diodes, and the diodes can also emit white light. Like a standard diode, the LED has two contacts: an anode and a cathode. On drawings, the anode is marked as “+” and the cathode as “-“.

Schematic symbol of LED and it's polarity

When forward voltage is applied, an LED’s anode is connected to the positive voltage and the cathode to the negative voltage. The voltage of the LED depends on the LED’s color: longer wavelength (red) ~2 V, shorter wavelength (blue) ~3 V. Usually, the power of an LED is no more than a couple of dozen milliwatts, which means the electrical current must be in the same range. When applying greater voltage or current a LED may burn out.

If the LEDs are used specially to illuminate, it is wise to use special electronic circuits that would regulate current and voltage suited for LEDs. However, LEDs are quite often used as indicators, and they are supplied directly from the microcontroller’s pins. Since the supply voltage for microcontrollers is usually higher than the voltage for LEDs, there must be a resistor connected in series with the LED, which limits current and creates the necessary voltage drop. Instructions to calculate the proper resistor can be found in the electronics chapter.

LEDs are produced in a variety of casings. Most common LEDs with feet have a 3 mm or 5 mm diameter round shell and two long metal connector pins. The longer pin is the anode, the shorter one is the cathode. Surface mounted casing LEDs (SMD – Surface Mounted Device) have a T-shaped symbol on the bottom to indicate the polarity, where the roof of T stands for the location of the anode and the pole marks the cathode.

Polarity of legged and SMD LED's

HomeLab Practice

The HomeLab controller control module has one single indicator LED, whose anode is connected through a resistor to a power supply, and the cathode is connected to the controller's pin. In order to switch on and off this LED, the LED pin should be defined as the output and set low or high accordingly. Which means if the pin is set high, the LED is turned off, and if the pin is set low, the LED is turned on. Basically, it would be possible to connect the LED also so that the anode is connected to the pin of microcontroller, and the cathode is connected to the earth (somewhere there has to be a resistor too) – in that case when the pin is set as high, the LED shines and when the pin is set as low the LED is switched off.

All practical examples for the HomeLab kit, including LED switching, utilize HomeLab’s pin library. Pin library includes data type pin, which contains addresses of pin-related registers and pin bitmask. If a pin-type variable is created in the program and then initialized by using the macro function PIN, the pin can be used freely with this variable (pin) throughout the whole program without being able to use registers. Here are two example programs that do exactly the same thing, but one is created based on HomeLab’s library, while the other is not. The debug LED, led_debug in the HomeLab library, has been described as PB7. The Debug LED is physically located in the Controller module.

// HomeLab Controller module LED test program, which
// is based on HomeLab library
#include <homelab/pin.h>
 
// LED pin configuration.
pin led_debug = PIN(B,7);
 
// Main program
int main(void)
{
	// Configuring LED pin as an output
	pin_setup_output(led_debug);
 
	// Lighting up LED
	pin_clear(led_debug);	
}
// HomeLab II Controller module LED test program, which
// accesses  registers directly
#include <avr/io.h>
 
// Main program
int main(void)
{	
	// Configuring LED pin as an output
	DDRB |= (1 << 7);	
 
	// Lighting up LED
	PORTB &= ~(1 << 7);
}

The first example uses the pins library (pin.h file). First, a pin-type variable named debug led is created in the program, which holds information about the LED pin. In the main program, this pin will be set as output by using pin_setup_output function. After that, the pin is set as low by the function pin_clear. As a result, the LED will glow. In the second example, variables are not used; setting the LED output and lighting it will be done by changing the port B data direction and output registers values. The reader who knows more about AVR notices that in both examples, there is no need to give a command to the light LED, because the default output value of the AVR is zero anyway, but here it is done by means of correctness.

What is the difference between the use of the library and the registers? The difference is in the comfort – the library is easier, because you do not need to know the registers’ names and their effects. The most important benefit of a library is adaptability. Using registers, you must change the register names and bitmasks throughout the entire program in order to change the pin. When using the library, it must be done only at the beginning of the program, where the pin variable is initialized. Using registers has one deceptive advantage – usage of pins is direct, and it is not done through program memory and time-consuming functions. However, newer AVR-GCC compiler versions are so smart that they transform the library’s functions to exactly the same direct commands for manipulating registers as if they were done directly in the program. It must be said that compilers can optimize the code only when it deals with constant single variables, not with volatile variables that are changing during work, and with arrays.

In addition to the Controller module, LEDs are also located on the User interface module board. They are connected electrically in the same way as the Controller module’s LED, which means the cathode is connected to the AVR pin. For more information, see the module's hardware reference. In addition to pin_set and pin_clear commands one can use led_on and led_off commands to control LED pins. The following table shows the LED constants that are described in the library and the corresponding Controller module pins. Green, yellow, and red LEDs are located in the user interface module.

Constant nameAlternative name HomeLab I & II pinDescription
led_debugLED0PB7 Blue LED on the Controller module
led_greenLED1PC3 Green LED
led_yellowLED2PC4 Yellow LED
led_redLED3PC5 Red LED

HomeLab library-based example program, which uses LED constants, looks as follows:

// LED test program for HomeLab User interface module
#include <homelab/pin.h>
 
// Main program
int main(void)
{
	// Configuring LED pins as an output
	pin_setup_output(led_red);
	pin_setup_output(led_yellow);
	pin_setup_output(led_green);	
 
	// Lighting up red and green LED
        led_on(led_red);
	led_on(led_green);
        // Turn off yellow LED
	led_off(led_yellow);
}

Task to be implemented

  • Make a blinking RED LED with a frequency of 1 Hz
  • Simulate a standard traffic light for cars.
en/iot-open/practical/hardware/itt/avr/led.1756804463.txt.gz · Last modified: 2025/09/02 09:14 by raivo.sell
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0