Monday, 22 September 2014

Reading a Push-Button with Arduino Uno

                   This example will explain you how to interface a Push Button(Push to ON switch) with Arduino UNO. The Push Button is connected to the Pin No.7 of Arduino and a LED is connected to the Pin No.13 of Arduino as shown in the Schematic below. A 1K ohm resistor is being used along with Push Button for connecting it to Pin no.7, this is to normally pull down the Pin No.7 to ground when the pin is not read otherwise the pin will foat sometimes and lead to wrong reading by Arduino.

                     Another 220 ohm resistor is also being used with LED, this is to limit the current to the LED approximately to 23mA (5V/220 ohm = 23 mA). After wiring the complete schematic, write the code shown below into the Arduino IDE and build it. Now, if you press the Push Button, immediately the LED becomes ON, if you leave the Push Button, immediately the LED becomes OFF.

Code 1:

// Reading a Push Button through Arduino UNO.
int ledPin = 13;                        // Pin for the LED
int inputPin = 7;                       // Pin for Push Button
int val = 0;                               // variable for reading the pin status

void setup()
{
pinMode(ledPin, OUTPUT); // configure ledPin as output
pinMode(inputPin, INPUT);  // configure inputPin as input
}

void loop()
{
val = digitalRead(inputPin);       // read input value
if (val == HIGH) // check if the input is HIGH (button released)
{
digitalWrite(ledPin, HIGH);       // turn LED ON
}
else
{
digitalWrite(ledPin, LOW);       // turn LED OFF
}
}

Same above code can be simply modified as for blinking of LED when the Push Butt
on is pressed totally OFF the LED when the Push Button is released from pressing.

Code 2:

// Reading a Push Button through Arduino UNO.
int ledPin = 13;                        // Pin for the LED
int inputPin = 7;                       // Pin for Push Button
int val = 0;                                // variable for reading the pin status

void setup()
{
pinMode(ledPin, OUTPUT);   // configure ledPin as output
pinMode(inputPin, INPUT);   // configure inputPin as input
}

void loop()
{
val = digitalRead(inputPin);   // read input value
if (val == HIGH) // check if the input is HIGH (button released)
{
digitalWrite(ledPin, HIGH);   // Blinking of LED starts here
delay(100);                              // set the delay of 100 milli Seconds for blinking
digitalWrite(ledPin, LOW);
delay(100);
}
else
{
digitalWrite(ledPin, LOW);  // turn LED OFF
}
}

Tuesday, 9 September 2014

Blinking a LED

Blinking a LED

            This example will explain you how to make a LED to blink continuously with a set time delay. The digital out put pin being used in the following circuit is 13 of Arduino Uno. Any LED should not be directly connected to the digital output of Arduino. You have to control the current to the LED. In this schematic a resistor of 220 ohm, 1/4 w is used to limit the current to 23 mA (5V/220 ohm = 23 mA). Anode of the LED is to be connected to Pin 13 of Arduino through a 220 ohm resistor. Cathode of the LED is to be connected to the ground. Both +5V and Ground terminals are available in the Arduino Board itself. After making the following wiring, write code in the Arduino IDE.

      

Code 1:
/*
  Blink
  LED connected to the pin of Arduino is made ON and OFF for one second each alternatively.
This is repeated for ever. 
 */

int led = 13;

void setup()
{                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

void loop()
{
  digitalWrite(led, HIGH);   // turn ON the LED (HIGH  = 1 level)
  delay(1000);                    // wait for a second (i.e. 1000 mS)
  digitalWrite(led, LOW);   // turn OFF the LED (LOW = 0 level)
  delay(1000);                      // wait for a second  (i.e. 1000 mS)
}

compile the above code with the Arduino IDE you can see the magic in the LED burning. If you want to make LED to blink fast, the milli second delay can be reduced and checked. If you want to experiment another circuit mentioned below, you can do so.


       By making the above circuit, you can have the circuit as decorative lamps running. For making the above circuit, either you can use same colour LEDs or different colour LEDs. For the above circuit, you can use the following code.

Code 2:
/*
  Blink3Led
  LED connected to the pins of Arduino are made ON and OFF one by one with a delay of one second. 
This repeated for ever. 

 */

int led1 = 11;
int led2 = 12;
int led3 = 13;

void setup()
{                
  // initialize the digital pins as outputs.
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);     
}

void loop()
{
  digitalWrite(led1, HIGH);   // turn ON the LED1 (HIGH  = 1 level)
  digitalWrite(led2, LOW);   // turn OFF the LED2(LOW = 0 level)
  digitalWrite(led3, LOW);   // turn OFF the LED3 (LOW = 0 level)
  delay(1000);                      // wait for a second  (i.e. 1000 mS)
  digitalWrite(led1, LOW);   // turn OFF the LED1(LOW = 0 level)
  digitalWrite(led2, HIGH);   // turn ON the LED2 (HIGH  = 1 level)
  digitalWrite(led3, LOW);   // turn OFF the LED3 (LOW = 0 level)
  delay(1000);                      // wait for a second  (i.e. 1000 mS)
  digitalWrite(led1, LOW);   // turn OFF the LED1(LOW = 0 level)
  digitalWrite(led2, LOW);   // turn OFF the LED2(LOW = 0 level)
  digitalWrite(led3, HIGH);   // turn ON the LED3 (HIGH  = 1 level)
  delay(1000);                      // wait for a second (i.e. 1000 mS)
}

 While experimenting the above, you can learn some thing in the field of micro-controller programming and hardware. If you really enjoyed my posts. Please comment, that will encourage me to post many more. You can also comment in both ways!