Das achte Türchen

What do the names Hans Christian Ørsted and Joseph Henry have to do with our advent calendar? They laid the physical foundations for a technical revolution 200 years ago. In 1820 Ørsted discovered the magnetic effect of electric current and thus created the basis for things that everyone knows today: the electromagnet, the transformer and the electric motors. And Henry invented the electromagnetic relay in 1835, without which neither the simple switching of electrical circuits nor telegraphy over great distances would have been possible.

Even if the transistor is now used as a switch millions of times on a small chip , today with the relay you have found a component in the advent calendar  that is still used in a variety of ways, because with a small voltage or amperage you can switch large voltages and large currents on or off. The physical magic word for this is "galvanic" separation.

Even if the name goes back to the Italian doctor Luigi Galvani, this principle has nothing to do with his frog leg experiments. Rather, it is a complete separation of two circuits in order to avoid malfunctions or even damage. We can read in the data sheet of the Uno (actually the most robust of all micro controllers and therefore so popular) that the data pins are 5V, max. 40mA and thus provide 200mW. We use it to light up an LED, but our coffee machine stays cold.

If we take a closer look at the relay module, next to each blue cuboid - the actual relay - we see three screw connections on one side and electronic components and pins for our jumper cables on the other, i.e. also a spatial separation of the two circuits.

Working on the mains voltage is life-threatening. We don't want to lose our customers that way.
(We don't really want to lose you at all.) If you are not familiar with this, leave this work to an electrician. And make sure that the contacts are built-in so that they cannot be reached.Let's first look at the side with the screw terminals for the high voltage. And so, of course, comes here first warning:

The relay says 10A 250VAC and 10A 30VDC. I first questioned these values ​​and therefore made a small experiment with a 2kW kettle on the terrace. The relay switched the kettle on and off without any problems and did not heat up despite the 9A. You can actually find these relays in switchable sockets, but then safely installed.

For our experiments we use the relays for small voltages from 5 to 12 volts; this is not dangerous. But we can switch loads on and off that require more current than the 15 or 40mA of our output pins of the micro controller for safe operation. We can switch on, switch off or switch between two loads under program control. How does it work?

The positive cable from the battery is screwed to the middle connection (sometimes referred to as CO = common). If we want to switch something on with the relay, the cable goes from the NO connection to the consumer (ground from the battery goes directly to the consumer). NO stands for "normally open", ie an open switch that is closed when the relay is actuated. The third connection is called NC for “normally closed”, ie normally closed; this contact is opened when the relay is actuated. The two outer connections can of course also be used as changeover switch.

Now finally we look at the electronics on the other side:

Quite a lot of components to ensure that the relay should only be controlled with 5V. An LED as a control lamp and the two resistors R in SMD design are quickly identified. D denotes a diode, Q denotes an NPN bipolar transistor, and the black IC is a so-called optocoupler.

Optocouplers are also used for galvanic separation by switching an LED on and off in one circuit, while a light-sensitive sensor, usually a transistor, detects the respective signal in the second circuit; thus a separation of the circuits with optical coupling. This is done because the relay is not an unproblematic load in our control circuit. And since the relay converts the magnetic energy back into electrical energy when it is switched off (a current in the opposite direction), the freewheeling diode is also required.

And now comes the special thing about the relay circuit: Of course, electronic circuits need a voltage supply with VCC = 5V and GND, but that the relays switch when the respective inputs are connected to GND, surprised me. So we have a reverse logic, and for the program code that means that we have to set the respective output pin to LOW so that the relay is activated.

One last connection, which, regardless of how many relays there are on the module, only exists once, has yet to be explained: the actual power supply for the relays.

Two pins, on which a short-circuit plug sits in the delivery state, are marked with VCC on one side and with JD-VCC on the other side. This means that you can choose whether the voltage supply for the relays comes from the micro controller (short-circuit plug set, VCC looped through) or from an external voltage source (instead of the short-circuit plug, the positive pole of the external voltage source is connected to JD, the negative pole must then still be used connected to GND of the micro controller). Incidentally, with the module with 16 relays you have no choice; With so many relays, an external power supply is always required.

With the relay module, we have achieved a clear separation of the circuits for the control with the micro-controller and for the consumer loads through the relay and the optocoupler and optionally also the external voltage supply of the relay.

And with the micro-controller we can then carry out switching processes time-controlled or linked to conditions. Let the fan run for 5 minutes every hour or when “there is thick air”.

As a simulation, I wrote the following sketch, in which, in addition to two relays, I also use a bi-color LED and an MQ-2 air sensor to detect LPG, i-butane, propane, methane, alcohol, hydrogen and smoke. After 60 seconds the yellow LED (red component 255, green component 128) and relay 1 are switched on for 5 seconds. If the MQ-2 exceeds its setpoint (here 200), the LED turns red and the relay 2 is switched on for 5 seconds.

#define LED_BUILTIN 13
const int ledred = 6; // the PWM pin the red LED is attached to
const int ledgreen = 5; // the PWM pin the green LED is attached to
const int relay1 = 7; // normal digital out pin
const int relay2 = 8; // normal digital out pin
int sensorPin = A0; // MQ-2 attached to analog input A0
int sensorValue = 0; // analog value btn 0 and 1023
int count = 0; // counter
int ledState = LOW; // ledState used to set the LED_BUILTIN

// Use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 1000; // interval at which to blink (milliseconds)
unsigned long currentMillis;

void setup () {
  Serial.begin (9600);
  // set the digital pins as output:
  pinMode(ledred, OUTPUT);
  pinMode(ledgreen, OUTPUT);
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  digitalWrite(relay1, HIGH);
  digitalWrite(relay2, HIGH);

void loop() {
  currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    // if the LED is off turn it on and vice-versa:
  if (ledState == LOW) {
    ledState = HIGH;
  } else {
    ledState = LOW;
// set the LED with the ledState of the variable:
  digitalWrite(LED_BUILTIN, ledState);
  if (count >= 30) {
    count = 0;
  if (analogRead(sensorPin) > 200) {

void stateyellow() {
  analogWrite(ledred, 255);
  analogWrite(ledgreen, 128);
  digitalWrite(relay1, LOW);
  analogWrite(ledred, 0);
  analogWrite(ledgreen, 0);
  digitalWrite(relay1, HIGH);

void statered() {
  analogWrite(ledred, 255);
  digitalWrite(relay2, LOW);
  analogWrite(ledred, 0);
  digitalWrite(relay2, HIGH);

Here Sketch for Download 

We wish you a nice Advent and Christmas season.



F. Weidmann

F. Weidmann

Der Abstand zwischen Relaisspule und Relaiskontakt ist nicht groß genug (Kriechstromabstand). Daher zum schalten von höheren Spannungen (z.B. 220VAC) aus sicherheitstechnischen Gründen nicht geeignet. Für nähere Informationen siehe auch “www.mikrocontroller.net/articles/Leiterbahnabstände”.

Bernd Albrecht

Bernd Albrecht

Stellungnahme AZ-Delivery:
Diese Kritik ist berechtigt. Auch wenn diese Relais grundsätzlich geprüft sind, übernimmt AZ-Delivery ausdrücklich keine Verantwortung bei Verwendung mit Netzspannung.
Die bestimmungsgemäße Verwendung geht bis 50 Volt.

Max Hüttmeier

Max Hüttmeier

Wenn ich in der Beschreibung der Relaiskarten das richtig lese, sind diese nicht für das Schalten von Netzspannung ausgelegt / freigegeben / geprüft. Auch wenn das Relais selber es kann.
Können Sie dazu etwas sagen?



ich verstehe nicht ganz wie hier ein Artikel über 10A 250VAC Relais geschriebne wird aber die Angebotenen Artikel sind nur 50V AC, 5A. Was ich auch nicht verstehe ist warum auf deren Abbildung keine Kennwerte mehr stehen, ich würde mir so ein Relais nie und nimmer kaufen.
Gruß aus Bayern

Leave a comment

All comments are moderated before being published