Introduction
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.
Components
– 1 * SunFounder Uno board
– 1 * Breadboard
– 1 *Slide Switch
– 1 * LED
– 1 * Resistors (220ohm)
– 1 * USB cable
– Jumper wires
Principle
Slide Switch
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.
See the circuit symbol for slide switch and 2 is the middle pin.
Principle: 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.
Experimental Procedures
Step 1: Build the circuit
Step 2: Open the code file
Step 3: Select correct Board and Port
Step 4: Upload the sketch to the SunFounder Uno board
When you toggle the switch to pin4, the LED lights.
Code Analysis
Code Analysis 3-1 Read the switch state to turn on/off the LED
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.
Code
//Controlling Led By slide switch //Turns on and off a LED ,when slide the switch //Email:support@sunfounder.com //Website:www.sunfounder.com //2015.5.7 /**********************************/ 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 } } /************************************/ |