Open / Close Relay

Relay with switch

In this example we will demonstrate the use of a Switch, with its HIGH and LOW settings. In addition we demonstrate how to use a Relay as an actuator. This example is another demonstration of the use of the digital pins for both input and output. A digital signal is received from the Switch and is used to activate and deactivate the Relay. The Relay in this example does not serve any real purpose, but if an electronic device such as a coffee-machine were connected to its screw terminal, it could be used to power on and off that device.

08293

Required Components

  • SODAQ Mbili Board
  • 0.5W Solar Panel
  • 1aH Battery Pack
  • Grove Relay
  • Grove Switch
  • 2x Grove Cable (any length)

Required Libraries

  • none

Hardware Setup

You should refer to both the board diagram and Grove sockets page for additional information.

  1. First, plug the Relay into the socket for the digital pins D4 D5.
  2. Then, plug the Switch into the Grove socket for the digital pins D20 D21.
  3. Next, plug the 0.5W solar panel and the 1A LiPo battery into their respective sockets.

Turn on the SODAQ Mbili board, compile and upload the following sketch from the Arduino IDE onto the SODAQ Mbili board, and then unplug the USB cable from the computer when it has completed the upload.

Sketch Code

#define SWITCH_PIN 20 //Use digital pin 20 for the switch
#define RELAY_PIN 4 //Use digital pin 4 for the relay
int switchState = 0;

void setup()
{
  //Set the digital pin modes
    pinMode(SWITCH_PIN, INPUT);
    pinMode(RELAY_PIN, OUTPUT);
}

void loop()
{
  //Read the current state of the switch
    switchState = digitalRead(SWITCH_PIN);

    if (switchState == HIGH)
    {
    //If the switch is set to HIGH, turn the relay on
      digitalWrite(RELAY_PIN, HIGH); 
        delay(100);
    }
    else
    {
    //If not, turn the relay off
        digitalWrite(RELAY_PIN, LOW);
    }
}

You should see the light on the relay turning on when you the Switch is set to HIGH, and turning off when you set the Switch to LOW.

Sketch Code Breakdown

Globals

Here we specify the digital pins we will use for both the Relay and the Switch. Additionally, we declare a global variable, switchState, of type int to store the switch state. This is initialised to 0.

#define SWITCH_PIN 20 //Use digital pin 20 for the switch
#define RELAY_PIN 4 //Use digital pin 4 for the relay
int switchState = 0;

setup()

Here we set the pin mode for the two digital pins we are using. This is done through a call to pinMode() with the first parameter specifying the pin to be set and the second parameter specifying the mode for that pin. The Switch pin is set to INPUT mode while the Relay pin is set to OUTPUT mode.

void setup()
{
  //Set the digital pin modes
    pinMode(SWITCH_PIN, INPUT);
    pinMode(RELAY_PIN, OUTPUT);
}

loop()

Here we read the state of the Switch by reading a value from the digital pin that it is connected to. This is done using the digitalRead() method which returns a value matching the built in constants of HIGH or LOW. We then modify the state of the output pin connected to the Relay using the digitalWrite() method, passing the value returned from the Switch. An if/else conditional is used to determine whether to switch the Relay on or off. After switching the relay on, a small delay of 100ms is added (using delay()) to allow the relay activate properly.

void loop()
{
  // Read the current state of the switch
    switchState =digitalRead(SWITCH_PIN);

    if (switchState == HIGH)
    {
      //If the switch is set to HIGH, turn the relay on
        digitalWrite(RELAY_PIN, HIGH); 
        delay(100);
    }
    else
    {
      //If not, turn the relay off
        digitalWrite(RELAY_PIN, LOW);
    }
}