Slide Switch With Arduino Uno R3

About: PrimeRobotics is a E-Commerce site, which focus on supplying right products to Electronics Hobbyists, Enthusiast & Students.

We have previously learned how to use a button to turn on/off the LED. In this lesson, we are going to use a slide switch to turn on/off an external LED. The slide switch is a device to connect or disconnect the circuit by sliding its handle. They are quite common in our surroundings. Now let's see how it works.

Step 1: ​Components

- Arduino Uno board * 1

- USB cable * 1

- Resistor (220Ω) * 1

- LED * 1

- Slide Switch * 1

- Breadboard * 1

- Jumper wires

Step 2: Principle

Just as its name suggests, slide switch is to connect or disconnect the circuit by sliding its switch handle so as to switch the circuit. The common types of slide switch include single pole double throw, single pole triple throw, double pole double throw, and double pole triple throw and so on. Generally, it is used in circuits with a low voltage and features flexibility and stabilization. Slide switches are commonly used in all kinds of instruments/meters equipment, electronic toys and other fields related.

How it works: The middle pin is fixed. When the handle is pushed to the left, the left two pins are connected; push it to the right, the two pins on the right connect, thus switching circuits.

Step 3:

See the circuit symbol for slide switch and 2 is the middle pi.

Step 4: The Schematic Diagram

Step 5: Procedures

Here we use a slide switch to control the on/off of an LED which is simple. Connect the middle pin of the switch to VCC. Connect one pin at one end to pin 12. After connecting a 10K resistor and a 104 capacitor, connect it to GND (to let the switch output stable level signal). Connect an LED to pin 6. Push the handle of the slide switch to the pin connected with pin 12 which is High level, we can light up the LED at pin 6 by programming

Step 1: Build the circuit.

Step 2:Download the code from https://github.com/primerobotics/Arduino

Step 3:Upload the sketch to the Arduino Uno board

Click the Upload icon to upload the code to the control board.

If "Done uploading" appears at the bottom of the window, it means the sketch has been successfully uploaded.

When you toggle the switch to pin4, the LED lights

Step 6: Code

//Controlling
Led By slide switch

//Turns on and off a LED ,when slide the switch

//Email:info@primerobotics.in

//Website:www.primerobotics.in

/**********************************/

const int switchPin = 12; //the switch connect to pin 12

const int ledPin = 6;//the led connect to pin 6

/**********************************/

int switchState = 0; // variable for reading the pushbutton status

void setup()

{

pinMode(switchPin, INPUT); //initialize thebuttonPin as input

pinMode(ledPin, OUTPUT); //initialize the led pin as output

}

/**********************************/

void loop()

{

//read the state of the switch value

switchState = digitalRead(switchPin);

if (switchState == HIGH ) //if it is,the state is HIGH

{

digitalWrite(ledPin, HIGH); //turn the led on

}

else

{

digitalWrite(ledPin, LOW); //turn the led off

}

}

/************************************/

Step 7: Code Analysis

First, read the state of the switchPin and see whether you have moved the switch handle. If it has been pushed to pin 12, then the switchState is High level, so set ledPin as High level, which means to light up the LED; otherwise, to turn it off.

Share

    Recommendations

    • Pocket Sized Contest

      Pocket Sized Contest
    • Organization Contest

      Organization Contest
    • Weaving Challenge

      Weaving Challenge

    Discussions