Arduino LED code
Sure! Here is an example of Arduino code to control an LED, along with explanations for each step:
int ledPin = 13; // Define the pin number for the LED
void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as an output
}
void loop() {
digitalWrite(ledPin, HIGH); // Turn on the LED
delay(1000); // Wait for 1 second
digitalWrite(ledPin, LOW); // Turn off the LED
delay(1000); // Wait for 1 second
}
Step 1: Declare a variable ledPin
and assign it the value 13
. This represents the pin number on the Arduino board to which the LED is connected.
Step 2: The setup()
function is called once when the Arduino is powered on or reset. In this function, we configure the LED pin as an output using the pinMode()
function. This tells the Arduino that we will be sending signals to the LED pin to control it.
Step 3: The loop()
function is called repeatedly after the setup()
function. In this function, we control the LED by turning it on and off with specific timing.
Step 4: digitalWrite(ledPin, HIGH)
sets the LED pin to a high voltage, turning on the LED.
Step 5: delay(1000)
pauses the program execution for 1 second. This keeps the LED on for 1 second.
Step 6: digitalWrite(ledPin, LOW)
sets the LED pin to a low voltage, turning off the LED.
Step 7: delay(1000)
pauses the program execution for 1 second again. This keeps the LED off for 1 second.
Step 8: The program goes back to the beginning of the loop()
function and repeats the steps, turning the LED on and off in a continuous loop.
By following these steps, you can control an LED using an Arduino board. Feel free to modify the code to suit your specific requirements.