Arduino: Play With LEDs

This article shows how to have a little fun playing the LEDs in Arduinos.

Introduction

In my last article, I introduced what Arduino is. I also showed you a very basic sketch of the LEDs. Now, this time we will go with the LEDs, but this time we will try some fanct things, like blinking a LED at constant intervals. or using two LEDs and each will on or off respectively.

Background

This is also a very fundamental series, where we are using:

  • Arduino UNO
  • Breadboard
  • 3mm LED (2x)
  • Jumper Wire
  • Resistor (1k Ohms)

First, we will try to blink a LED at a constant interval. And then we will use two LEDs and turn them on one after another. Within that, we will learn how to write basic Arduino code.

Part 1

In this part, we will turn on a LED with 5 Volts and try to learn basic electronics.

So, first we will set up the layout.



Here, we have a Red 3mm LED. And its positive lead goes to the 5 Volt pin of Arduino and the negative lead goes to GND (ground). For all of this, we have put a 1k Ohm resistor to the negative lead since we want Arduino GND to be the least amount of voltage. However, we are trying to increase the resistance between 5 Volts to 0 Volts (GND).

Note: We don't need any sketch (or code) to demonstrate this as, Arduino UNO is constantly ing 5 Volts to that pin. The 5V pin is among those pins you can't code for.

Part 2

We will now improvise the preceding connection and try to blink the LED.

So, the connection would be like:



And the code will be like:

  1. <code>  
  2. int LED =12; // PIN 12  
  3. void setup()  
  4. {  
  5. pinMode(LED, OUTPUT);  
  6. }  
  7. void loop()  
  8. {  
  9. digitalWrite(LED, HIGH); // Light UP  
  10. delay(1000); // Wait for 1 second.  
  11. digitalWrite(LED, LOW); // Light Down  
  12. delay(1000); // Wait for 1 second  
  13. }  
  14. </code>  
Simply, we are trying to blink a LED, in other words turn it on and off for a second. And that continues until you turn your Arduino off.

We already know there must be two predefined methods, setup() and loop(). Where setup() determines the PIN configuration and the Arduino board layout. Whereas Loop() decides the logic and the condition of the Arduino Board (that what we call the actual coding).

In the very first line, we defined the LED that stores 12 because the LED is in PIN 12. So, whenever we want to point the LED we always its PIN number.

Next, in the setup() block we defined the pinMode() method that actually asks for the INPUT/OUTPUT mode. So, the LED behaves as an OUPUT and that is the reason why we are instructing like pinMode(LED, OUTPUT). In other words, LED (PIN 12) is an OUTPUT unit and I want it to behave as an OUTPUT unit when we call high that LED or PIN 12. And, when we get inside the loop() block then there is the two phases, the HIGH LED and the LOW LED and in between we use the method delay (for microseconds) that haults for the assigned microseconds. We ed 1000ms and that is 1 second. And, digitalWrite() is a method that instructs the digital unit to either go HIGH or LOW with the PIN number as the first argument.

Structure goes like

First we make the LED HIGH (the ON state) and then delay() for 1 second. Next, we make the LED LOW (the OFF state) and then delay() for 1 second. After every action it puts your Arduino on hold.



Conclusion

So, this time we have a brief idea about Arduino codes and we explored a few methods, like pinMode(), digitalWrite() and delay(). In my next article I will write some new sketches and experience more of Arduino.