Appearance

Dark Light

Did You Know?

Conventional farmers use around 300 different pesticides to grow foods that are sold in supermarkets everyday.

Using a Relay With the Arduino

If you want to use the Arduino with AC appliances/devices (say, to turn on a lamp or an AC motor), you'd need a relay. A relay is basically an electro-magnetic switch - when you apply current to it, it opens/closes a circuit. Be careful when working with AC circuits/high voltages, though - shit can go wrong and you can get hurt.
Parts:

  • 1 Arduino micro-controller
  • A 5V DC relay (I'm using a cheap SONGLE ® relay rated for 10A)
  • 1 solderless bread-board
  • Jumper wires, alligator clips
  • 1 LED (optional)


The relay

The relay has 5 pins:


You can test the relay using a multimeter; when the relay is switched on, the resistance reading drops to near zero on the multimeter. You can also hear the relay "clicking" as the switch opens/closes. The LED shows the open/closed state.

Code


int ledPin = 8;
int relaySwitchPin = 7;

void setup() {
  pinMode(relaySwitchPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  digitalWrite(relaySwitchPin, HIGH);  
  digitalWrite(ledPin, HIGH); 
  delay(5000);                       
  digitalWrite(relaySwitchPin, LOW);
  digitalWrite(ledPin, LOW);
  delay(5000);                    
}