Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Control LED Brightness Using Arduino

Controlling the brightness of an LED is a fundamental project that introduces beginners to the concept of Pulse Width Modulation (PWM) in the Arduino environment. PWM is a technique used to simulate an analog output using digital means, allowing you to control the brightness of an LED by varying the duty cycle of the signal.

Examples:

To control the brightness of an LED using an Arduino, you will need the following components:

  • An Arduino board (e.g., Arduino Uno)
  • A breadboard
  • An LED
  • A 220-ohm resistor
  • Jumper wires

Circuit Setup:

  1. Connect the anode (longer leg) of the LED to one end of the resistor.
  2. Connect the other end of the resistor to a PWM-capable digital pin on the Arduino (e.g., pin 9).
  3. Connect the cathode (shorter leg) of the LED to the ground (GND) on the Arduino.
  4. Ensure all connections are secure on the breadboard.

Arduino Code:

int ledPin = 9; // Pin connected to the LED
int brightness = 0; // Initial brightness
int fadeAmount = 5; // Amount to change the brightness

void setup() {
  pinMode(ledPin, OUTPUT); // Set the LED pin as an output
}

void loop() {
  analogWrite(ledPin, brightness); // Set the brightness of the LED

  brightness = brightness + fadeAmount; // Change the brightness for next time

  // Reverse the direction of the fading at the ends of the fade
  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }

  delay(30); // Wait for 30 milliseconds to see the dimming effect
}

Explanation:

  • analogWrite(pin, value): This function writes an analog value (PWM wave) to a pin. The value can be between 0 (always off) and 255 (always on).
  • The brightness variable controls the LED's brightness level. It is incremented or decremented by fadeAmount to create a fading effect.
  • The if statement checks if the brightness reaches the limits (0 or 255) and reverses the direction of fading.

Testing:

Upload the code to your Arduino board using the Arduino IDE. Once uploaded, you should see the LED gradually increase and decrease in brightness, creating a smooth fading effect.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.