4 Relay Shield(linksprite) humble beginnings

4 Relay Shield(linksprite) humble beginnings

This sketch turns each of the 4 relays on and off in turn, it continues until you enter '0' in the serial monitor.

  • 335 views
  • 0 comments
  • 0 respects

Components and supplies

About this project

Originally I tried using a couple of single pole single throw relays to try to control a motor to go either back or forward. The first was a little complicated, and stuff started to smell like burning. Then I tried a double pole double throw relay, but not connected to the arduino....just in case. This was achieved by 2 buttons where one was pressed, then the motor would go forward, press both then the motor went backward. Then I had to think of using diodes and transistors to limit the amount of current switching. Needless to say from there I put it on hold.

So, this last week I bought the relay shield, it's got all the protection and programmability that I needed in the first place, ie connecting it to the Arduino and controlling the relays through the digital ports.... oh and no smell.

This setup now for the relay shield that I'm using is so that the program(sketch) turns each of the relays on then off. When the serial monitor is up and running it shows which relay is activated, by the end of going through each a question quickly pops up asking to run again? at that point or actually anytime enter a '0' and the loop exits...after it's finished the loop, otherwise it just goes through the loop again.

The eventual usage:

The relay board needs the 9v connected to the relay shield, else they don't work. the 6v battery pack is connected to the n/c and n/o twisted so that when one is off the other is on and vice versa. The common pins go to the motor.





Code

Relay loop 4 of themArduino
A test base for using a 4 relay shield
/*
  Relay shield
  Turns on a relay on, then off.
  then you are given  choice to continue or quit by two to four 0's to quit

*/
int pinSel;
int nextPin;
int yorn;
// Open serial monitor, you can restart the board by closing the monitor then
// opening it again.
void setup() {
  // initialize digital pins as outputs for relays.
  for (int pinSel = 4; pinSel < 8; pinSel++) {
    pinMode(pinSel, OUTPUT);
  }
  Serial.begin(9600);
}

// the loop function runs over and over, or 0's to quit
void loop() {
Serial.print("Enter 1 for forward, 2 for backward, 0 to stop ?_");
pinSel=Serial.read();
Serial.println(pinSel);



  
  for (int nextPin = 4; nextPin < 8; nextPin++) {
    Serial.println(nextPin);
    digitalWrite(nextPin, HIGH);   // turn relay on(D4-D7)
    delay(1000);              // wait for a second
    digitalWrite(nextPin, LOW);    // turn relay off
    delay(2000);              // wait for 2 seconds
  }
  Serial.println("Run again?");
  yorn = Serial.read();
  if (yorn == '0')
  {
    leave();          // quit the loop
  }
  else {
    Serial.println("continuing");   // do nothing and loop again
  }
}

void leave() {
  Serial.println("exiting");
  delay(1000);
  exit(yorn);
}

Schematics

Relay shield
4 relays controllable via digital ports
Relayshield%5b1%5d

Comments

Add projectSign up / Login