It is now time to test the project. As the most important part of the project is the relay module, that’s what we are going to test here. We are simply going to switch the powerswitch on and off continuously every 5 seconds, just to check that it is working.
At this point, you should already plug a device (like the lamp I used as an example) into the project and connect the switch to an electrical socket, to see that all the connections are correctly made.
It starts by declaring on which pin the switch is connected to:
const int relay_pin = 8;
pinMode(relay_pin,OUTPUT);
Then, in the loop() function of the sketch, we set this pin to a HIGH state, switching on the powerswitch:
digitalWrite(relay_pin, HIGH);
delay(5000);
We then switch it off again:
digitalWrite(relay_pin, LOW);
And wait for 5 seconds before repeating the loop() function:
delay(5000);
Note that you can find all the code for this part inside the GitHub repository of the project:
https://github.com/marcoschwartz/arduino-wifi-powerswitch
It’s now time to test the sketch. Make sure that the lamp is correctly connected to the project, and that the male plug is plugged to the mains electricity. Then, upload the Arduino sketch to the board. You should see that every 5 seconds, the powerswitch is switching, turning the device connected to it on and off.
Text editor powered by tinymce.