3. Lighting an LED
3.1 LED Structure
An LED, or light-emitting diode, is a semiconductor light source. Its main structure includes the following parts:
- Package: usually made of plastic or glass, used to protect the internal components.
- Light-emitting material: the core part of the LED, made of special semiconductor materials such as common InGaN, indium gallium nitride, or AlInGaP, aluminum indium gallium phosphide.
- Chip: the light-emitting diode chip that produces light.
- Leads: metal leads that provide electrical connection.
- Solder joints: soldered points that connect the LED chip and the leads.
- Electrodes: used to connect the semiconductor material to the external circuit, usually made of metal.
- Reflector cup: a structure that improves light output by reflecting emitted light toward the front.
3.2 LED Lighting Principle
The lighting principle of an LED is based on semiconductor characteristics. In a semiconductor, there are two types of charge carriers: electrons in N-type semiconductor material and holes in P-type semiconductor material. When N-type and P-type semiconductor materials are joined, a junction is formed at their interface. When a suitable voltage is applied, holes and electrons in the junction recombine and release energy. This energy is released as photons, producing light.
3.3 LED Driving Principle
LED driving means providing suitable current and voltage from a stable power supply so that the LED can operate and light normally. There are two main LED driving methods: constant current and constant voltage. Current-limited constant-current driving is the most common method because LEDs are sensitive to current, and current above the rated value may damage them. Constant-current driving keeps the current stable and helps ensure LED safety.
Driving an LED is relatively simple: connect its positive and negative terminals to the corresponding positive and negative sides of the microcontroller circuit. There are two LED connection methods: current sinking and current sourcing.
- Current sinking means the LED supply current is provided externally and sinks into the MCU. The risk is that if the external power supply changes, the MCU pin may be damaged.
- Current sourcing means the MCU provides voltage and current to the LED. If an MCU GPIO directly drives an LED, the drive capability is weak and may not provide enough current to drive the LED.
Note that LEDs of different colors have different corresponding voltages. The current must not be too large. A current-limiting resistor of about 220 ohms to 10K ohms is usually required. The larger the resistance, the dimmer the LED.
3.4 LED Schematic
In the development board schematic, the LED uses a current-sinking connection. The LED anode is connected to the 3.3 V power supply, and the cathode is connected through a current-limiting resistor to GPIO48. According to the LED driving principle, the LED can be turned on by driving GPIO48 on the board low.
3.5 LED Driving Flow
In Arduino, call pinMode(pin, mode); to set GPIO48 as output mode, then call digitalWrite(pin, value); to set GPIO48 to output high or low level.
3.5.1 Set the Pin Mode
pinMode() is a function in the Arduino programming language used to set the operating mode of a specified pin. Its syntax is:
pinMode(pin, mode);Here, pin is the pin number to set, and mode is the operating mode to set. It can be one of the following:
INPUT: Sets the specified pin as input mode to receive external signals or sensor data. In this mode, the pin reads the level of the external signal. Note that the pin may be floating in this mode, causing unstable readings. To solve this problem, use an external pull-up or pull-down resistor, or use the internal pull-up resistor described below.INPUT_PULLUP: Sets the pin to internal pull-up input mode. In this mode, the pin is connected to an internal pull-up resistor, which keeps a floating pin at a high level. When the external level is low, the reading switches toLOW.OUTPUT: Sets the specified pin as output mode to send electrical signals or control external devices. In this mode, the pin can output a high level,HIGH, or a low level,LOW. It can be used to drive LEDs, relays, and other external devices.INPUT_PULLDOWN: Sets the pin to internal pull-down input mode. In this mode, Arduino connects the input to a resistor tied to ground, ensuring the input remains low. When the external circuit is disconnected or in a high-impedance state, the Arduino input pin remains low. Example code usingpinMode:c// Set pin 6 to output mode pinMode(6, OUTPUT); // Set pin 2 to input mode pinMode(2, INPUT);1
2
3
4In the example above, the first line sets GPIO6 to output mode and can be used as a pin for controlling an external device. The second line sets GPIO2 to input mode and can be used as a pin for receiving signals from an external sensor.
3.5.2 Set Pin Output
digitalWrite() is an Arduino function used to set the level of a digital pin. It sets a digital pin to HIGH or LOW. When a pin is configured as OUTPUT, this function can change the pin level and affect the component connected to the pin.
The function syntax is:
digitalWrite(pin, value);Here:
pin: the digital pin number you want to write to.value: the level to set, eitherHIGHorLOW.HIGHmeans high level, andLOWmeans low level.
Example:
pinMode(13, OUTPUT); // Set pin 13 to output mode
digitalWrite(13, HIGH); // Set digital pin 13 output to HIGH2
In this example, the first line sets pin 13, GPIO13, to output mode. The second line sets pin 13 to HIGH, so the pin outputs 3.3 V.
Note that if the pin is not first set to
OUTPUTusingpinMode(), callingdigitalWrite()may not have any effect.
❓What is a digital pin?
A digital pin is an input/output, or I/O, pin on a microcontroller, not limited to Arduino, or other electronic device. As the name suggests, digital pins process binary and discrete levels, usually divided into HIGH, such as 5 V or 3.3 V, and LOW, such as 0 V or ground. These pins are mainly used to communicate with, control, or detect other digital devices or components.
3.6 LED Lighting Verification
Set GPIO48, which is connected to the LED on the development board, as output mode and output a low level to turn on the LED.
Write the following code:
// Run only once
void setup()
{
// Set GPIO48 as output mode
pinMode(48,OUTPUT);
// Set pin 48 to output low level, turning on the LED
digitalWrite(48,LOW);
}
// Loop continuously
void loop()
{
}2
3
4
5
6
7
8
9
10
11
12
13
Click the Upload button to upload, or flash, the code to the development board.
When Hard resetting via RTS pin... appears, the download is complete.
3.7 LED Lighting Effect
After the code is downloaded, the LED marked G48 on the development board will stay on.