Relays

From Reef Projects

Jump to: navigation, search

Relay Testing

  • This is just some code to test the relay using a push button.
  • I modified [this code] and used [this wiring] for the button.
  • Made sure the polarity on the relay is correct.

I had been testing the need for a resistor in line with the relay.  I decided it wasn't needed.

Code:

int relay = 11; // choose the pin for the relay
int inPin = 1;   // choose the input pin (for a pushbutton)
int val = 0;     // variable for reading the pin status

void setup() {
  pinMode(relay, OUTPUT);  // declare relay wire as output
  pinMode(inPin, INPUT);    // declare pushbutton as input
}

void loop(){
  val = digitalRead(inPin);  // read input value
  if (val == HIGH) {         // check if the input is HIGH (button released)
    digitalWrite(relay, LOW);  // turn relay OFF
  } else {
    digitalWrite(relay, HIGH);  // turn relay ON
  }
}
Personal tools