Skip to Content
This is the Beta version of our new Learning Paths approach. Please email feedback.

Relay

relay.ino
/* Project: Smart Home Component: Relay Sketch Description: This sketch has functions to activate and deactivate a relay, thus turning a connected electrical appliance on and off. Author: STEMVentor Educonsulting This code is copyrighted. Please do not reuse or share for any purpose other than for learning with subscribed STEMVentor programs. This code is for educational purposes only and is not for production use. */ // LIBRARIES // PIN DEFINTIONS // See board_parameter.h // GLOBAL CONSTANTS // GLOBAL VARIABLES // INITIALIZE OBJECTS // LOCAL FUNCTIONS void switchRelayOn(uint8_t relay_id) { if(relay_id == LIGHT_RELAY){ digitalWrite(LIGHT_RELAY_PIN, HIGH); is_light_on = true; } else if(relay_id == PUMP_RELAY){ digitalWrite(PUMP_RELAY_PIN, HIGH); is_pump_on = true; } delay(2000); // Force a delay to avoid rapid switching. } void switchRelayOff(uint8_t relay_id) { if(relay_id == 1){ digitalWrite(LIGHT_RELAY_PIN, LOW); is_light_on = false; } else if(relay_id == 2){ digitalWrite(PUMP_RELAY_PIN, LOW); is_pump_on = false; } delay(2000); // Force a delay to avoid rapid switching. } void relaySetup() { pinMode(LIGHT_RELAY_PIN, OUTPUT); pinMode(PUMP_RELAY_PIN, OUTPUT); // Test the relays with a switch on and off. switchRelayOn(1); switchRelayOff(1); switchRelayOn(2); switchRelayOff(2); Serial.println("Relay setup successfully!"); }