Arduino Project : Switch On Switch Off Menggunakan Push Button

Project ini merupakan project sederhana untuk pemula tentang bagaimana memanfaatkan Arduino sebagai perangkat untuk keperluan switch on/off menggunakan push button. Dimana pada project ini, kita akan menggunakan 4 buah push button, 3 diantaranya untuk mengontrol masing-masing output (dalam hal ini LED) dan 1 switch lagi untuk mengontrol seluruh output secara bersamaan.

Komponen yang dibutuhkan :
• Arduino UNO Board
• Bread Board
• LED 3 buah
• Push Button kecil 3 buah
• Push Button besar 1 buah
• Resistor 220 Ohm 3 buah
• Kabel Jumper

Skema :

Sketch :

/*
* This is the Arduino code for push button to push ON and Push OFF
* Permission granted to share this code given that this
* note is kept with the code.
* Disclaimer: this code is “AS IS” and for educational purpose only.
*/
int pbuttonPin = 5; // connect input to main push button
int pbuttonPin1 = 2; // connect input to push button 1
int relayPin1 = 10; // Connect output to LED 1
int pbuttonPin2 = 3; // connect input to push button 2
int relayPin2 = 11; // Connect output to LED 2
int pbuttonPin3 = 4; // connect input to push button 3
int relayPin3 = 12; // Connect output to LED 3
int val = 0; // push value from pin 5
int val1 = 0; // push value from pin 2
int val2 = 0; // push value from pin 3
int val3 = 0; // push value from pin 4
bool i=false; //State of LED 1
bool j=false; //State of LED 2
bool k=false; //State of LED 3
void setup() {
pinMode(pbuttonPin, INPUT_PULLUP);
pinMode(pbuttonPin1, INPUT_PULLUP);
pinMode(relayPin1, OUTPUT);
pinMode(pbuttonPin2, INPUT_PULLUP);
pinMode(relayPin2, OUTPUT);
pinMode(pbuttonPin3, INPUT_PULLUP);
pinMode(relayPin3, OUTPUT);
}
void loop() {
val = digitalRead(pbuttonPin); // read the main push button value
val1 = digitalRead(pbuttonPin1); // read the push button 1 value
val2 = digitalRead(pbuttonPin2); // read the push button 2 value
val3 = digitalRead(pbuttonPin3); // read the push button 3 value
if(val==LOW)
{
i=!i; //changing state of LED 1
j=i; //changing state of LED 2
k=i; //changing state of LED 3
digitalWrite(relayPin1, i); //turning on/off LED 1
digitalWrite(relayPin2, j); //turning on/off LED 2
digitalWrite(relayPin3, k); //turning on/off LED 3
delay(200);
}
if(val1==LOW)
{
i=!i; //changing state of LED 1
digitalWrite(relayPin1, i); //turning on/off LED 1
delay(200);
}
if(val2==LOW)
{
j=!j; //changing state of LED 2
digitalWrite(relayPin2, j); //turning on/off LED 2
delay(200);
}
if(val3==LOW)
{
k=!k; //changing state of LED 3
digitalWrite(relayPin3, k); //turning on/off LED 3
delay(200);
}
}

Inti dari sketch di atas adalah bagaimana kita dapat membaca state dari masing-masing LED (hidup atau mati), kemudian mengolah state tersebut untuk menghidupkan atau mematikan output (LED). Dengan menggantikan LED oleh Relay pada project ini, maka anda telah mendapatkan home automation sederhana untuk mengontrol peralatan rumah seperti lampu, kipas angin, dll.

Demikian project kali ini, semoga bermanfaat dan terima kasih atas kunjungannya.

Reff :
https://www.electronicshub.org/arduino-based-home-automation-using-tv-remote/
http://robojax.com/learn/arduino/?vid=robojax-pushButton-on-off

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s