Toggling Build-in LED

# Toggling Build-in LED "Hello, World!” is often the first step into programming on a PC, a simple tradition that welcomes us to the digital realm. But in the world of microcontrollers, we crave something more tangible—something we can see or touch. That’s where the classic second step comes in: toggling an LED. Let’s bring a little light to our code and make it blink! # Build-in LED The Arduino Uno R3, powered by the ATmega328P microcontroller, is a marvel of accessibility. It boasts 14 digital I/O pins, 6 analog inputs. The built-in LED, tied to pin 13, is a special feature: it’s hardwired with a resistor, so you don’t need extra components to test it. This makes it perfect for beginners. ![Build-in LED](https://cdn.shopify.com/s/files/1/0331/9994/7908/files/Pasted_image_20250310091828.png?v=1741558915) # Code Here’s a simple sketch to toggle the built-in LED on and off every second: ```cpp void setup() { pinMode(LED_BUILTIN, OUTPUT); } void loop() { digitalWrite(LED_BUILTIN, HIGH); // Turn the LED on delay(1000); // Wait 1 second digitalWrite(LED_BUILTIN, LOW); // Turn the LED off delay(1000); // Wait 1 second } ``` With your Uno R3 plugged in, open the IDE, paste the sketch, and select your board under **Tools > Board > Arduino Uno**. Then, pick the right port (e.g., COM3 on Windows or /dev/ttyUSBxxxx on Mac) from **Tools > Port**. Hit the upload button (the right-arrow icon), and in moments, the IDE will send the code to your board. Watch as the Build-in LED on pin 13 begins its rhythmic dance—on, off, on, off—a tiny beacon of your newfound power. ## Code Explanation ```cpp void setup() { } ``` **setup()**: Runs once when the board powers on, configuring build-in LED which is controlled by pin 13 as an output so we can control it. ```cpp void loop() { } ``` **loop()**: Runs endlessly, turning the LED on (HIGH), waiting a second (delay(1000)), turning it off (LOW), and waiting again—creating a steady blink. ```cpp pinMode(pin, mode); ``` The `pinMode` function configures a specific pin on the microcontroller to act either as an **input** or an **output**. • pin: The pin number you want to configure (e.g., LED_PIN). • mode: The mode to set for the pin. It can be one of the following: - INPUT: Configures the pin to read input values (e.g., from a button or sensor). - OUTPUT: Configures the pin to output values (e.g., turn an LED on or off). ```cpp digitalWrite(pin, value); ``` The digitalWrite function sets the output state (HIGH or LOW) of a pin that has been configured as an **output** using pinMode. • pin: The pin number you want to set the state for (e.g., LED_PIN). • value: The desired state of the pin: • HIGH: Sets the pin to a high voltage (typically 3.3V for ESP32 or 5V for Arduino Uno R3 microcontroller), turning on a connected device like an LED. • LOW: Sets the pin to 0V, turning off the connected device. ```cpp digitalWrite(LED_BUILTIN, HIGH); // Turning the LED on digitalWrite(LED_BUILTIN, LOW); // Turning the LED off ```

RELATED ARTICLES