Arduino: Play With Red And Green LEDs

Introduction

 
In the last article, we used a single LED with a resistor and a delay() method. This time, we will use the same theory to do something with two LEDs and switches at a regular interval. There is nothing new in this, still, we are trying to improvise the last theory and implement something in the layout.
 
Background
 
We need the following basic units for this layout:
  • Arduino UNO
  • Small Breadboard
  • Red and Green LEDs
  • 1k Ohm Resistor (2x)
  • 5 Jumper Wires
Before we start the coding part, start the wired part first and then we will move to the coding.
 
 
So, what I'm doing here is, we have two LEDs and with two leads (positive and negative). Whereas, both the negative leads are connected to a 1k Ohm resistor to balance the voltage difference in between.
 
And the positive lead of the Green LED is in PIN 12 whereas Red's positive LED is in PIN 8.
 
Note: You are free to use any of the pins but ensure these are constant voltage digital pin. As you can see, some pins have the tilde sign (~) that denotes PWM (Pulse Width Modulation).
 
In those pins, we don't get a constant voltage. Instead, all the pins are a constant digital pin that supplies either HIGH or LOW voltage.
 
Constant Digital PIN is: 0, 1, 2, 4, 7, 8, 12, and 13. (Here, we can choose among them.)
 
The PWM Pins are: 3, 5, 6, 9, 10, and 11. (We'll explain further when to use these pins.)
 
Sketch
  1. int RED = 8; // RED LED at PIN 8  
  2. int GREEN = 12; // GREEN LED at PIN 12  
  3. void setup() {  
  4.   
  5.     pinMode(RED, OUTPUT);  
  6.     pinMode(GREEN, OUTPUT);  
  7. }  
  8. void loop() {  
  9.     digitalWrite(RED, HIGH);  
  10.     digitalWrite(GREEN, LOW);  
  11.     delay(1000);  
  12.     digitalWrite(RED, LOW);  
  13.     digitalWrite(GREEN, HIGH);  
  14.     delay(1000);  
  15. }  
In the first two lines, we have declared Red and Green to define its Pin configuration. Then, in the setup() block we used the pimMode() method that decides the input/output behavior of the unit along with the PIN number.
 
Finally, we have a loop() block where we ed Red and HIGH to the digtitalWrite() method to turn on the Red LED and then use a delay() method with 1 second as the wait time. At the same time, we didn't want Green to go HIGH, so we put that Green LED to LOW.
 
And last, we did the reverse of the preceding code where the Red went LOW and the Green went HIGH.
 
 

Conclusion

 
So, next, we will try to learn about the Serial Monitor that is one of the debugging tools for Arduino. Along with that, we will try to explore the RGB LED.
 
Until then, enjoy your Arduino UNO and learn a few more things.