What is a Switch?
A Switch can be any device that has two state 0 or 1 (on/off). This can be use to break and make connection in an electric circuit.It comes in different forms and the one you are seeing on this page is just a type of switch. Henceforth, for the purpose of this project, any switch at can be used.
Remember that a switch may directly be manipulated by a person as a control signal to an electric circuit or a system.
Setting Up the Switch on the Arduino board
Components
- 1 Arduino
- 1 LED
- 1 220 ohm resistor for the LED
- 1 10K ohm resistor for the LED
- 2 data wires
Images
Codes for programming the Switch
Hi Everybody, my name is Elizabeth Yeboah and this is project 3. Copy and paste this code in your sketch whiles following the video tutorial for the setup.
// The switch codes
int led = 13; // my LED is connected to pin 13
int switchPin = 2; // my switch is connected to pin 2
int switchState = 0; // initialize the variable as 0
void setup() { // Tell the arduino that pin 13 is to be treated as an OUTPUT
pinMode(led, OUTPUT); //command(variable,mode);
pinMode(switchPin, INPUT); // Tell the arduino that pin 2 is to be treated as an INPUT
}
void loop() {
// set the variable to the value of the pin. This determines if the switch is on or off. //syntax //variableName = digitalRead(pin); switchState = digitalRead(switchPin); // Use an if statement to take an action. We compare the variable with the value of the switchPin.
if(switchState == 1){
//if the switch is ON
//turn the led ON using digitalWrite() by setting the mode to HIGH. This sends 5 volts out of the assigned pin
//digitalWrite(variableName, MODE);
digitalWrite(led, HIGH);
// if the switch is not on, then do the following action
}
else{
//turn the led OFF using digitalWrite() by setting the mode to LOW. This stops the 5 volts on the assigned pin
digitalWrite(led, LOW);
}
}
// The switch codes
int led = 13; // my LED is connected to pin 13
int switchPin = 2; // my switch is connected to pin 2
int switchState = 0; // initialize the variable as 0
void setup() { // Tell the arduino that pin 13 is to be treated as an OUTPUT
pinMode(led, OUTPUT); //command(variable,mode);
pinMode(switchPin, INPUT); // Tell the arduino that pin 2 is to be treated as an INPUT
}
void loop() {
// set the variable to the value of the pin. This determines if the switch is on or off. //syntax //variableName = digitalRead(pin); switchState = digitalRead(switchPin); // Use an if statement to take an action. We compare the variable with the value of the switchPin.
if(switchState == 1){
//if the switch is ON
//turn the led ON using digitalWrite() by setting the mode to HIGH. This sends 5 volts out of the assigned pin
//digitalWrite(variableName, MODE);
digitalWrite(led, HIGH);
// if the switch is not on, then do the following action
}
else{
//turn the led OFF using digitalWrite() by setting the mode to LOW. This stops the 5 volts on the assigned pin
digitalWrite(led, LOW);
}
}
Watch the Switch Video
Please if this tutorial was helpful, send me your comments. Thanks
By Lis Brown.