I'm using my Arduino to control a relay switch, which has an extension cord attached to it (see photo).

The relay is connected to the Arduino via pin 9, which is set to pinMode(9, OUTPUT);

Do I need any resistors or capacitors or diodes attached to this? Almost feels too simple, given I'm running AC power through the relay. But maybe no?

It definitely works, but I want to make sure I'm not going to burn something out (or down).

enter image description here

  • 1
    Majenko's answer describes how to switch a relay. However DO NO DO THIS. Breadboards are not designed for mains voltages or currents. If you are playing with mains power you need proper isolation. – Milliways Jul 24 '16 at 0:20

Yes, that is too simple.

The Arduino's IO pin is not designed to directly drive a relay. It needs to go through a transistor first, and you must include a diode to absorb the back-EMF generated by the collapsing magnetic field.

Google for "Arduino Relay" and you will find hundreds of thousands of examples.

In short, the circuit you need to create is this:

enter image description here

| improve this answer | |
  • What's the purpose of the transistor? – Shpigford Jul 24 '16 at 19:54
  • It acts as a switch turning the relay on and off. – Majenko Jul 24 '16 at 19:54
  • Is that not the function of the Arduino? Sending digitalWrite(RELAY, HIGH); through one of the pins to the relay switches the relay. – Shpigford Jul 24 '16 at 20:01
  • 1
    No. The arduino isn't powerful enough to switch a relay reliably. It can switch the transistor and then the transistor switches the relay. Technically the transistor is acting as a current amplifier.. – Majenko Jul 24 '16 at 20:03
  • 1
    The key word that I used was reliably. You are most likely drawing too much current from the port for sustained operation. That is bad. You have no back emf protection. That is even worse. I'd give it a week tops before you have to throw your Arduino away. – Majenko Jul 24 '16 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.