Friday 31 August 2018

Arduino Switch Interface

        Here in this topic, we are interfacing an SPST(Single Pole Single Throw ) push button with Arduino, For that We need the following components


  1.  Arduino
  2. LED
  3. USB cable 
  4. 220 Ohm Resistor
  5. SPST 

  In this, we are interfacing two SPST push buttons with Arduino to control the LED connected in the 13th pin here the 2 SPST push buttons are connected to pin 2 and 3 of Arduino when one button press the led will on and when we press the second switch the led will turn off quickly

Step 1: Write the following code in Arduino IDE

const int led=13,sw=2;
int bs;
int sw1();

void setup()
{
  pinMode(led,OUTPUT);
  pinMode (sw,INPUT);
  pinMode (3,INPUT);
  digitalWrite(led,LOW);
  Serial.begin(9600);
}
void loop()
{
  int k;
  k=sw1();
  if(k==1)
  {
   digitalWrite (led,HIGH);
    Serial.println("Button 1 pressed led on");
    delay(10);
  }
 else if(k==2)
  {
    digitalWrite(led,LOW);
    Serial.println("Button 2 pressed led off");
    delay(10);
  }
}

int sw1()
{
 int x=0;
  bs=digitalRead(sw);
  while(bs==1)
  {
     bs=digitalRead(sw);
    x=1;
  }
   bs=digitalRead(3);
  while(bs==1)
  {
     bs=digitalRead(3);
    x=2;
  }
  return x;


}

Step 2: Wire the Circuit diagram and upload the sketch

No comments:

Post a Comment