top of page

In this tutorial you will learn the value of a switch, with its High and Low function. In addition you will learn how to use a relay as an actuator. 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 would be very useful.

 

Required components:

SODAQ Moja

0.5W Solar Panel

1aH battery pack

Grove Relay

Grove Switch (P)

2x Grove w/o Buckle Cable (any length)

 

First, plug the Switch into the D2/D3 pin in the always on row.

Then, plug the Relay into the D4/D5 pin in the always on row.

Next, plug the 0.5W solar panel and the 1A LiPo battery into the respective sockets (see http://www.sodaq.net/#!getting-started/c21ma for a board diagram)

 

Turn on the SODAQ board, compile and upload the following sketch from the arduino IDE onto the SODAQ Board, and then unplug the USB cable from the computer when it is working:

 

You should see the light on the relay turning on when you switch to HIGH on the Grove switch, and turning off when you switch to LOW.

const int switchPin = 2; // the number of the switch pin

const int relay = 4; // the number of the relay

int switchState = 0;

 

void setup()

{

 Serial.begin(9600);

 pinMode(switchPin, INPUT);

 pinMode(relay, OUTPUT);

}

 

void loop()

{

 switchState = digitalRead(switchPin);

 if (switchState == HIGH)

 {

  digitalWrite(relay, HIGH); delay(100);

 }

 else

 {

  digitalWrite(relay, LOW);

 }

}

5. Using a Switch to Open and Close a Relay

© 2013 SODAQ. All rights reserved.

  • Facebook Classic
  • Twitter Classic
bottom of page