Connecting an LED (Light Emitting Diode) to an Arduino is one of the simplest circuits you can build and is ideal for learning how to control outputs.
Components:
Arduino (any model, like Uno)
LED
Resistor (220Ω or 330Ω recommended to limit current)
Breadboard (optional)
Jumper wires
Understanding the LED:
Anode (long leg): Connects to the positive voltage (e.g., an Arduino digital pin).
Cathode (short leg): Connects to ground (GND).
Circuit Connections:
Connect the anode (long leg) of the LED to a digital pin on the Arduino (e.g., D13) through a 220Ω resistor.
Connect the cathode (short leg) of the LED to the GND pin on the Arduino.
Wiring Diagram:
Digital Pin 13 (D13) → Resistor (220Ω) → LED anode (long leg)
LED cathode (short leg) → GND
Code Example:
int ledPin = 13; // Pin where the LED is connected
void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as output
}
void loop() {
digitalWrite(ledPin, HIGH); // Turn the LED on
delay(1000); // Wait for 1 second
digitalWrite(ledPin, LOW); // Turn the LED off
delay(1000); // Wait for 1 second
}
How the Code Works:
pinMode(ledPin, OUTPUT): Configures the pin connected to the LED as an output.
digitalWrite(ledPin, HIGH): Turns the LED on by providing a HIGH signal.
digitalWrite(ledPin, LOW): Turns the LED off by providing a LOW signal.
delay(1000): Waits for 1 second (1000 milliseconds) between turning the LED on and off.
Testing:
Upload the code to your Arduino using the Arduino IDE.
The LED should blink on and off at 1-second intervals.
This simple setup can be adapted for more complex projects, such as controlling multiple LEDs or using PWM (Pulse Width Modulation) to adjust brightness.
Comentários