Control a relay with the Arduino

FunduinoPublished on

Mit einem kleinen Steuerstrom größere Verbraucher schalten

Relais sind Schalter, durch die ein größerer Strom fließen kann, als der Mikrocontroller selbst liefern könnte. Mit einem ganz kleinen Steuerstrom aktivierst du das Relais, das dann auch größere elektrische Geräte schaltet.

Im Programmbeispiel schaltet das Relais zwei LEDs abwechselnd für jeweils zwei Sekunden.

Single-channel relay module with the NC, C, and NO screw terminals
5V relay board with NC, C, and NO terminals

You need this

1-channel relay module - 5 V, switches up to 250 V / 10 A 1-channel relay module - 5 V, switches up to 250 V / 10 A 1,72 € *Item no. F23107168About the product — 1-channel relay module - 5 V, switches up to 250 V / 10 A
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino 11,12 € *Item no. F23107480About the product — Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
830-point Breadboard - solderless breadboard for Arduino 830-point Breadboard - solderless breadboard for Arduino 2,90 € *Item no. FUN-1010553About the product — 830-point Breadboard - solderless breadboard for Arduino
Breadboard Cables Male-to-Male - 40 Pieces, 20 cm Breadboard Cables Male-to-Male - 40 Pieces, 20 cm 1,74 € *Item no. FUN-1010558About the product — Breadboard Cables Male-to-Male - 40 Pieces, 20 cm
LED 5 mm - 100 Pieces, 5 Colors, 2 V LED 5 mm - 100 Pieces, 5 Colors, 2 V 1,97 € *Item no. F23107316About the product — LED 5 mm - 100 Pieces, 5 Colors, 2 V
Resistor Assortment 100 Pieces - 5 Values 100 Ω to 10 kΩ, 0.5 W Resistor Assortment 100 Pieces - 5 Values 100 Ω to 10 kΩ, 0.5 W 1,72 € *Item no. F23107216About the product — Resistor Assortment 100 Pieces - 5 Values 100 Ω to 10 kΩ, 0.5 W

All 6 parts together 21,17 € *Ready to ship immediately · Delivery time: 1-3 business days

* All prices include statutory VAT, plus shipping costs.

Choose a microcontroller

Pins, schematic, and sketch in the steps change with your selection. It remains saved.

How a relay works

The relay module has three terminals for the switched circuit:

  • NC – Normally Closed, connected to C when at rest
  • C – Common, the shared center contact
  • NO – Normally Open, open when at rest

As long as the relay is not energized, current flows from C to NC, and the LED connected there lights up. When the relay is energized, the center contact switches from NC to NO: the second LED lights up and the first one turns off. In the example program, the relay switches between the two LEDs every two seconds.

Caution: Some relay modules energize when you connect GND to pin IN, while others energize when 5 volts are applied there. You can easily find out which version you have: connect the signal pin to GND and then to 5V on the board. You can clearly hear the relay module switch with a loud click. With a module of the first type, the two LEDs’ behavior in the sketch is simply reversed.

Safety notice: We strongly advise against using this relay board to switch household appliances or other loads connected to mains voltage.

Single-channel relay module with the NC, C, and NO screw terminals
5V relay board with NC, C, and NO terminals

Wire and program

Build the circuit according to the schematic. Connect the relay module’s IN pin to pin 7, and connect the power supply to 5V and GND.

If you’re using the Arduino UNO for the first time: Set up the hardware and software.

Schematic: Relay board and two LEDs on the Arduino UNO
Circuit diagram for the Arduino UNO
Sketch for the Arduino UNO
int RELAIS = 7;
void setup()
{
  pinMode(RELAIS, OUTPUT);
}

void loop()
{
  delay(2000);
  // Switch relay
  digitalWrite(RELAIS, HIGH);
  delay(2000);
  digitalWrite(RELAIS, LOW);
}

Download: sketch-for-the-arduino-uno.ino

Board in the Arduino IDEArduino Uno

You don’t need an additional board package for this. The UNO is included in the Arduino IDE by default.

Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.