Using the relay controller
Now that we have assembled the relay controller, we will build the Arduino sketch to make the circuit do as we like. We will create the sketch in such a way that it will be able to control the relay through the push button. To do so, you will need to follow the code given in these steps:
- First, we will declare the relay and the button pins. The relay is pin 7, while the button is pin 8. To do this, follow the code:
int relayPin = 7; int buttonPin = 8;
- Next, we will create variables for the button states:
int buttonState; int lastButtonState = LOW; int previousButtonState = LOW;
- To debounce the button and achieve a stable state for the relay, we will add the following code. We will declare a default last debounce time and debounce delay:
long lastDebounceTime = 0; long debounceDelay = 50;
- Then, we will create a variable for the state of the relay:
int relayState = LOW;
Inside the
setup()
function of the sketch, we will set the pins for the relay and the button. The button...