Ollie Smeenk
Published

Using a Switch to Open and Close a Relay

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.

Full instructions provided575
Using a Switch to Open and Close a Relay

Things used in this project

Hardware components

0.5W Solar Panel
×1
SODAQ Moja
×1
1aH battery pack
×1
Grove Relay
×1
Grove Switch (P)
×1
Grove w/o Buckle Cable (any length)
×2

Story

Read more

Code

code.c

C/C++
code.c
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);
 }
}

Credits

Ollie Smeenk

Ollie Smeenk

8 projects • 6 followers
Business-oriented Tinkerer! Ready to engage in any project for- and not for profit!

Comments