Let’s connect hardware first,
Note: "Always run LEDs below their maximum current to avoid damage. For example, if the max current is 25mA, aim for 10–15mA. This helps keep the LED cool and extends its life, especially since heat can reduce the safe current limit."
To ensure a 10 mA current through the LED, we need to select an appropriate resistor based on the supply voltage.
Case 1: 5V Supply
Standard resistor values near 320 Ω: 330 Ω or 300 Ω (whichever is available).
Similarly, Case 2: 3.3V Supply
Standard resistor value: 150 Ω.
The LED can be connected to a GPIO pin in two ways
Method 1: Sourcing Current from GPIO
Method 2: Sinking Current to GPIO
So, by selecting a proper resistor and connecting the LED correctly, we can implement the task.
Below are the solutions to the given task using different microcontrollers
We’re using an STM32 NUCLEO-F103RB board, which runs at a 3.3V logic level.
We can connect an LED to any GPIO. So, we will connect the LED to the GPIO pin PA8(D7).
Circuit Diagram
Project Setup in STM32CubeIDE
HAL_Init()
→ Initializes the HAL library.SystemClock_Config()
→ Configures system clock.MX_GPIO_Init()
→ Configures GPIO pins.main.c
GPIO Initialization
This configuration is generated inside MX_GPIO_Init()
, where PA8 is set as a push-pull output with low speed, suitable for driving an LED.
// Inside MX_GPIO_Init()
GPIO_InitStruct.Pin = GPIO_PIN_8;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
Pin/LED Macros for Code Readability
These macros make your code more readable by giving meaningful names to the LED's port and pin.
#define LED_PORT GPIOA // GPIO port connected to the LED
#define LED_PIN GPIO_PIN_8 // GPIO pin connected to the LED
Main Firmware Logic
The LED state is controlled inside an infinite loop (while (1)
) by using the HAL functions:
HAL_GPIO_WritePin()
— to set or reset the output state of PA8.HAL_Delay()
— to create a delay in milliseconds between state changes.We can implement either of the two common wiring configurations to toggle the LED, depending on how the LED is connected to our hardware:
Method 1: Source Current Configuration
(Anode connected to PA8, cathode connected to GND through a resistor)
while (1) {
// Turn LED ON
HAL_GPIO_WritePin(LED_PORT, LED_PIN, GPIO_PIN_SET);
HAL_Delay(400); // Wait for 400 milliseconds
// Turn LED OFF
HAL_GPIO_WritePin(LED_PORT, LED_PIN, GPIO_PIN_RESET);
HAL_Delay(800); // Wait for 800 milliseconds
}
Method 2: Sink Current Configuration
(Cathode connected to PA8, anode connected to VCC through resistor)
while (1) {
// Turn LED ON
HAL_GPIO_WritePin(LED_PORT, LED_PIN, GPIO_PIN_RESET);
HAL_Delay(400); // Wait for 400 milliseconds
// Turn LED OFF
HAL_GPIO_WritePin(LED_PORT, LED_PIN, GPIO_PIN_SET);
HAL_Delay(800); // Wait for 800 milliseconds
}
The complete STM32CubeIDE project (including .ioc
configuration, main.c
, and HAL files) is available here:
We are using the ESP32 DevKit v4 development board and programming it using the Arduino IDE.
The GPIO pins of the ESP32 provide a 3.3 V output.
To safely interface an LED and limit the current to 10 mA, we use a 150-ohm resistor, as calculated earlier.
We can connect an LED to any GPIO. So, we will connect the LED to the GPIO pin G4.
We want to blink an LED such that it stays ON for 400 milliseconds and OFF for 800 milliseconds, repeating this cycle continuously.
Code for Method 1 (Source Current):
//Hardware configuration
#define LED_PIN 4
//Timing configuration
#define ON_TIME_MS 400
#define OFF_TIME_MS 800
//Initialize the hardware
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
digitalWrite(LED_PIN, HIGH); // LED is turn on
delay(ON_TIME_MS); // wait for 400 millisecond
digitalWrite(LED_PIN, LOW); // LED is turn off
delay(OFF_TIME_MS); // wait for 800 milliseconds
}
Code for Method 2 (Sink Current):
//Hardware configuration
#define LED_PIN 4
//Timing configuration
#define ON_TIME_MS 400
#define OFF_TIME_MS 800
//Initialize the hardware
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
digitalWrite(LED_PIN, LOW); // LED is turn on
delay(ON_TIME_MS); // wait for 400 millisecond
digitalWrite(LED_PIN, HIGH); // LED is turn off
delay(OFF_TIME_MS); // wait for 800 milliseconds
}
pinMode(led, OUTPUT);
- In setup(), configures pin 4 as an output pin.digitalWrite()
- To turn the LED on/off.delay()
- Used to provide a delay in milliseconds.ESP32 GPIOs can source/sink up to 40mA, but are practically limited to ≤12mA to ensure reliability.
When sourcing current (GPIO set HIGH at 3.3V), the voltage drops with increasing current, indicating internal resistance (~20–40Ω).
E.g.
We are using the Arduino UNO development board and programming it using the Arduino IDE.
The GPIO pins of the Arduino UNO provide a 5V output.
To safely interface an LED and limit the current to 10 mA, we use a 330-ohm resistor, as calculated earlier.
We can connect an LED to any GPIO. So, we will connect the LED to the GPIO pin 4.
We want to blink an LED such that it stays ON for 400 milliseconds and OFF for 800 milliseconds, repeating this cycle continuously.
To control the LED, we toggle the GPIO pin by setting it either HIGH or LOW, depending on the wiring method used.
Code for Method 1 (Source Current):
const int led = 4; // LED is connected to the pin 4
void setup() {
pinMode(led, OUTPUT); // Set the LED pin as an output
}
void loop() {
digitalWrite(led, HIGH); // LED is turn ON
delay(400); // waits for a 400 msec second
digitalWrite(led, LOW); // LED is turn OFF
delay(800); // waits for a 800 msec second
}
Code for Method 2 (Sink Current):
const int led= 4; // LED connected pin 4
void setup() {
pinMode(led, OUTPUT); // Set the LED pin as an output
}
void loop() {
digitalWrite(led, LOW); // LED is turn ON
delay(400); // waits for a 400 msec
digitalWrite(led, HIGH); // LED is turn OFF
delay(800); // waits for a 800 msec
}
const int led = 4;
- Declares a constant integer variable named "led" with value 4, meaning the LED is connected to digital pin 4.pinMode(led, OUTPUT);
- In setup(), configures pin 4 as an output pin.digitalWrite()
to turn the LED on/off and delay()
to create timing.This shows that GPIO has an internal resistance of 25 ohms.