Arduino - Blinking LED
Arduino - Your First Project: Blinking an LED
Section titled “Arduino - Your First Project: Blinking an LED”Blinking an LED is the “Hello, World!” of the hardware world. It’s a simple yet powerful first step that teaches you the fundamental structure of an Arduino sketch, how to control an output pin, and how to bring your code to life. Mastering this will give you a solid foundation for all future projects.
Method 1: Blinking an External LED
Section titled “Method 1: Blinking an External LED”Components Required
Section titled “Components Required”- 1 × Breadboard
- 1 × Arduino UNO R3 (or any other model)
- 1 × LED (any color)
- 1 × 220Ω or 330Ω Resistor
- 2 × Jumper Wires
Circuit Assembly
Section titled “Circuit Assembly”- Identify LED Polarity: An LED has two legs, a long one and a short one. The long leg is the positive terminal (anode), and the short leg is the negative terminal (cathode).
- Connect the LED: Insert the LED into the breadboard.
- Connect the Resistor: Connect one end of the resistor to the same breadboard row as the LED’s long (positive) leg.
- Connect to Arduino Pin: Use a jumper wire to connect the other end of the resistor to digital pin 13 on the Arduino.
- Connect to Ground: Use another jumper wire to connect the LED’s short (negative) leg to one of the GND (Ground) pins on the Arduino.
Why Use a Resistor? (Ohm’s Law in Action)
Section titled “Why Use a Resistor? (Ohm’s Law in Action)”An LED offers very little resistance to electricity. Connecting it directly to an Arduino pin would allow too much current to flow, which could instantly burn out the LED and potentially damage the Arduino pin. The resistor limits the current to a safe level. It’s a crucial component for protecting your electronics.
Method 2: The Easiest Start (Blinking the Built-in LED)
Section titled “Method 2: The Easiest Start (Blinking the Built-in LED)”Most Arduino boards, including the UNO, have a small LED directly on the board, usually labeled ‘L’. This LED is internally connected to a pin, which we can control using the LED_BUILTIN constant. This method requires no external components and is the perfect way to test if your board is working.
Arduino Code (for Built-in LED)
Section titled “Arduino Code (for Built-in LED)”This sketch will work for both methods. If using an external LED on pin 13, it will blink along with the built-in LED, as LED_BUILTIN on the UNO is an alias for pin 13.
/* Blink The "Hello, World!" of microcontrollers. Turns an LED on for one second, then off for one second, repeatedly.*/
// The setup function runs once when you press reset or power the boardvoid setup() { // Initialize the digital pin (LED_BUILTIN) as an output. // This tells the Arduino we want to send voltage out of this pin. pinMode(LED_BUILTIN, OUTPUT);}
// The loop function runs over and over again forevervoid loop() { digitalWrite(LED_BUILTIN, HIGH); // Turn the LED on (HIGH is 5V) delay(1000); // Wait for a second (1000 milliseconds) digitalWrite(LED_BUILTIN, LOW); // Turn the LED off by making the voltage LOW (0V) delay(1000); // Wait for a second}Understanding the Code
Section titled “Understanding the Code”void setup() { ... }: This is where you put your setup code. It runs only once, after the Arduino is powered on or reset.void loop() { ... }: This function contains the main logic of your program. It runs continuously in a loop as long as the board has power.pinMode(pin, mode): Configures a specific pin to behave as either anINPUTor anOUTPUT.digitalWrite(pin, value): Writes a digital value (HIGHorLOW) to a pin.HIGHsupplies 5V, andLOWsupplies 0V.delay(ms): Pauses the program for the specified amount of time in milliseconds.
Troubleshooting Common Problems
Section titled “Troubleshooting Common Problems”- LED doesn’t light up: Check that it’s not inserted backward (long leg should be connected to the pin via the resistor). Double-check your wiring.
- Code won’t upload: Ensure you have selected the correct Board and Port in the Arduino IDE under the
Toolsmenu. Check your USB cable. - LED is very dim or burns out immediately: You may be using the wrong value resistor. 220Ω to 1kΩ is generally safe for standard LEDs.
Challenge: Create an SOS Signal
Section titled “Challenge: Create an SOS Signal”Can you modify the code to blink an SOS signal in Morse code? (Hint: S = 3 short blinks, O = 3 long blinks. A short blink could be delay(250) and a long blink delay(750)).