Saturday, April 11, 2015

Arduino Relay Control

Previously I have blogged about using relays with an Arduino, and SainSmart relays specifically. A while back I received a 4 relay, and a 8 relay board, and both of them used negative logic, i.e. a LOW activated them, and a HIGH deactivated them. Yesterday I received a single relay module as part of a kit, and found that this module uses positive logic, i.e. a High activates it, and a LOW deactivates it.

Being a 5v relay, it's able to be driven directly from the arduino power. If you have a lot of additional hardware, you may want to consider a separate 5v supply, and common ground.

We are printing the relay state to the serial monitor, but the LED on the relay board also signifies whether it's active or not.

Only 3 pins are used, 5v ("V"), Gnd ("G"), and a data pin. We are using pin 7 in this example, and connects to the relay "S" (signal) pin.

Here is my test code:

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(7, OUTPUT); //don't forget to declare the pin as output!
}

void loop() {
  // put your main code here, to run repeatedly:
digitalWrite(7, HIGH);
Serial.println("Active");
delay(5000);
digitalWrite(7, LOW);
Serial.println("Inactive");
delay(5000);

}

No comments:

Post a Comment