

PinMode(BUTTON_PIN, INPUT_PULLUP) // set arduino pin to input pull-up mode Int currentButtonState // the current state of button Int lastButtonState // the previous state of button Int relayState = LOW // the current state of relay Arduino Code – Button Toggles Relay Without Debouncing const int BUTTON_PIN = 7 // Arduino pin connected to button's pinĬonst int RELAY_PIN = 3 // Arduino pin connected to relay's pin Now copy the following code and upload it to Arduino IDE Software.
#Control high voltage devices arduino relay tutorial how to
Here is a simple step-by-step guide on “ How to Add Libraries in Arduino IDE“. Installing Librariesīefore you start uploading a code, download and unzip the following libraries at /Progam Files(x86)/Arduino/Libraries (default), in order to use the sensor with the Arduino board. Here is a simple step-by-step guide on “ How to install Arduino IDE“. Make connections according to the circuit diagram given below.įirst, you need to install Arduino IDE Software from its official website Arduino.

Use the serial monitor to display the current state of the relay:.Use an if statement to check if the button has been pressed:.Int buttonState = digitalRead(buttonPin) In the loop() function, read the state of the pushbutton:.In the setup() function, configure the pushbutton and relay pins as inputs and outputs:.Declare a variable to store the current state of the relay:.Declare the pin numbers for the pushbutton and relay module as constants:.Open the Arduino IDE and create a new sketch (program).Connect the pushbutton to pin 2, the relay module to pin 8, and the AC appliance to the relay module.
