Scheme for controlling two relays using one output of the microcontroller. Device and relay application examples, how to choose and connect a relay correctly Microcontroller and relay simple switching circuits

What do you need to become a professional developer of programs for microcontrollers and reach such a level of skill that will allow you to easily find and get a job with a high salary (the average salary of a microcontroller programmer in Russia at the beginning of 2017 is 80,000 rubles). ...

We continue the story about connecting a powerful load to the microcontroller. We already know how to connect to the microcontroller and. Now it's the turn to deal with the electromagnetic relay.

At first glance, connecting the relay is the simplest. However, this is a deceptive simplicity. Because, firstly, most relays consume much more current than the microcontroller can provide at the output. And secondly, an electromagnetic relay is an inductive load, which has its own characteristics (more on that later). That is why beginners often disable microcontroller outputs by trying to connect relays to them.

How to connect a relay to a microcontroller and avoid trouble at the same time - a little later. In the meantime, for the very, very beginners, I will tell you very briefly

An electromagnetic relay is a special device that consists of at least four main elements (see figure):

  1. Coil
  2. Core
  3. Anchor
  4. Contact group

The coil (depending on the type of relay) can be designed either for alternating voltage or for direct voltage.

When voltage is applied to the coil, a magnetic field is created around it, which magnetizes the core. Then the armature is attracted to the core and shifts the group of contacts. Depending on the design, the contacts either open, close, or switch. A contact group can contain both normally closed and normally open contacts. And there can be two contacts, or three or more.

When the voltage is removed from the coil, then the contacts return to their original position.

A normally closed (normally closed) contact is a contact that is closed when there is no voltage on the coil. Normally open (normally open), respectively, open when there is no voltage on the coil, and close when voltage is applied to the coil. The figure shows a normally open contact.

On the diagrams and in the descriptions of the relay, abbreviations are usually used: NO - normally open (normally open), NC - normally closed (normally closed).

Main characteristics of the relay

In order to use a relay in your devices (not necessarily on microcontrollers), you need to know whether it is suitable for your purposes or not. To do this, you need to know the characteristics of the relay. Main characteristics:

  1. Type of coil voltage (AC or DC). To connect directly to the microcontroller or through a transistor, only a DC relay can be used (relay contacts, of course, can control both AC and DC).
  2. Coil voltage (that is, what voltage must be applied to the coil so that the armature is reliably magnetized to the core).
  3. Coil current consumption.
  4. The rated current of the contacts (that is, the current through the relay contacts at which they will operate without damage for a long time).
  5. Relay operation time. That is, how long it takes to magnetize the anchor.
  6. Relay release time. That is, how long it takes to demagnetize (release) the armature.

The last two parameters are usually not taken into account. However, in cases where a certain speed is required (for example, the operation of some protection devices), then these values ​​\u200b\u200bmust be taken into account.

Well, finally we got to connecting the load to the microcontroller through a relay. I suggest you remember. If you remember, then you can connect the load to the output of the microcontroller in two ways: with a common plus and with a common minus.

If we want to connect the relay to the microcontroller directly, then the method with a common minus is most likely eliminated, because with this method the microcontroller is able to control a very weak load. And almost all relays consume several tens or even hundreds of mA.

And the method with a common minus also in most cases will not allow you to connect the relay directly to the microcontroller for the same reason (with this method, the microcontroller can usually provide 15-20 mA at the output, which will not be enough for most relays).

Reed relays usually have low current consumption. However, they can only switch small currents.

But there is one trick here. The fact is that the higher the voltage of the relay coil, the lower the current consumption. Therefore, if your device has a power source, for example, 24 V or higher, then you can easily select a relay with an acceptable current consumption.

For example, a relay Finder The 32nd series consumes only 8.3 mA at a coil voltage of 24V.

In this case (when you have two voltage sources), you can connect the relay like this:

How to connect a relay to a transistor

However, it is not possible to use an additional power source in the device in most cases. Therefore, usually the relay is connected to the output of the microcontroller. How to do this, I have already told. Therefore, I will not repeat myself.

Security measures

Relays are typically used when a large load and/or high voltage needs to be controlled.

Therefore, here it is necessary to remember the security measures. It is desirable to separate the low current low voltage circuit from the high voltage circuit. For example, install the relay in a separate housing or in a separate insulated compartment of the housing so that when setting up the device, you do not accidentally touch contacts with high voltage.

In addition, there is a danger of damaging the output of the microcontroller or an additional transistor.

The fact is that the relay coil is an inductive load with all the ensuing consequences.

And there are two risks here:

  1. At the moment the voltage is applied to the coil, the inductive reactance of the coil is zero, so there will be a short-term current surge, significantly exceeding the rated current. But most output transistors withstand this surge, so you don’t have to think about it, but you need to know and understand it.
  2. At the moment of voltage removal (at the moment of breaking the coil supply circuit), self-induction EMF occurs, which can disable the output transistor of the microcontroller and / or an additional transistor to which the relay coil is connected. To avoid this, it is always necessary to connect a protective diode in parallel with the coil (see Fig.). Why this happens, I will not tell. Who cares, remember or study electrical engineering.

IMPORTANT!
Pay attention to the inclusion of the diode. It should turn on just like that, and not vice versa, as some people think.


Many novice radio amateurs begin to get acquainted with electronics with simple circuits, which are full on the Internet. But if this is a control device in which some kind of actuator is connected to the circuit, and the connection method is not indicated in the circuit, then the beginner has a hard time. This article was written to help novice radio amateurs deal with this problem.

DC loads.

The first way is to connect through a resistor

The easiest way - suitable for low-current loads - LEDs.

Rgas \u003d (U / I) - Rн

Where U is the supply voltage (in Volts), I is the allowable current through the circuit (in Amperes), Rн is the load resistance (in Ohms)

The second way - Bipolar transistor

If the consumed load current is greater than the maximum output current of your device, then the resistor will not help here. You need to increase the current. For this, transistors are usually used.

In this circuit, an n-p-n transistor is used, connected according to the OE circuit. With this method, you can connect a load with a higher supply voltage than the power of your device. Resistor R1 is needed to limit the current flowing through the transistor, usually set to 1-10 kOhm.

The third way is a field effect transistor

To control the load, the current of which is tens of amperes (especially powerful electric motors, lamps, etc.), a field-effect transistor is used.

Resistor R1 limits the current through the gate. Since the field effect transistor is controlled by small currents, and if the output of your device to which the gate is connected is in a high-impedance Z-state, the field device will open and close unpredictably, catching interference. To eliminate this behavior, the output of the device is "pressed" to the ground with a 10kΩ resistor.
The field effect transistor has a feature - its slowness. If the allowable frequency is exceeded, it will become overheated.

Alternating current.

The first way is a relay.

The simplest way to control an AC load is with a relay. The relay itself is a high-current load - you need to turn it on through a bipolar or field-effect transistor.

The disadvantages of the relay are its slowness and mechanical wear of parts.

New Articles

● Project 12: Controlling a relay through a transistor

In this experiment, we will get acquainted with a relay with which you can control a powerful load not only direct, but also alternating current with Arduino.

Required components:

The relay is an electrically controlled, mechanical switch that has two separate circuits: a control circuit, represented by contacts (A1, A2), and a controlled circuit, contacts 1, 2, 3 (see Fig. 12.1).

The chains are not connected in any way. A metal core is installed between contacts A1 and A2, when current flows through it, a movable armature (2) is attracted to it. Contacts 1 and 3 are fixed. It is worth noting that the armature is spring-loaded, and until we pass current through the core, the armature will be pressed against pin 3. When current is applied, as already mentioned, the core turns into an electromagnet and is attracted to pin 1. When de-energized, the spring returns the armature to pin 3 again .

When connecting a relay to the Arduino, the microcontroller pin cannot provide the power needed to make the coil work properly. Therefore, it is necessary to amplify the current - put a transistor. For amplification, it is more convenient to use an n-p-n-transistor connected according to the OE circuit (see Fig. 12.2). With this method, you can connect a load with a higher supply voltage than the power supply of the microcontroller.
The base resistor is a limiting resistor. It can vary widely (1-10 kOhm), in any case, the transistor will operate in saturation mode. Any n-p-n-transistor can be used as a transistor. The gain is practically irrelevant. The transistor is selected according to the collector current (the current we need) and the collector-emitter voltage (the voltage that powers the load).

To turn on the relay connected according to the scheme with the OE, you need to apply 1 to the Arduino pin, to turn it off - 0. Let's connect the relay to the Arduino board according to the diagram in fig. 12.3 and write a relay control sketch. Every 5 seconds the relay will switch (on/off). When switching the relay, a characteristic click is heard.
The contents of the sketch are shown in Listing 12.1.

int relayPin = 10 ; // connect to pin D10 of Arduino void setup()( pinMode(relayPin, OUTPUT); // configure output as output (OUTPUT) } // the function is executed cyclically an infinite number of times void loop()( digitalWrite(relayPin, HIGH); // enable the relay delay(5000 ); digitalWrite(relayPin, LOW); // turn off the relay delay(5000 ); )

Connection order:

1. We connect the elements to the Arduino board according to the diagram in fig. 12.3.
2. Load the sketch from Listing 12.1 into the Arduino board.
3. Every 5 seconds there is a relay switching click if you connect the relay contacts, for example, into the gap of a cartridge with an incandescent lamp connected to a 220 V network, we will see the process of turning on / off the incandescent lamp every 5 seconds (Fig. 12.3).


This article discusses the important drivers and proper circuitry needed to securely connect external devices to the I/O of an MCU (Microcontroller Unit, MCU).

Introduction

Once you have an idea for a project, it's very tempting to jump right into connecting the Arduino to circuits and devices like LEDs, relays, and speakers. However, doing this without the correct circuitry can be fatal to your microcontroller.

Many I/O devices draw a lot of current (> 100 mA) that most microcontrollers cannot supply in safe mode, and when they try to provide this amount of current, they often break. Here we come to the aid of special schemes called "drivers" (English - drivers). Drivers are circuits that can take a small, weak signal from a microcontroller and then use that signal to drive some sort of power-consuming device.

For microcontrollers to work properly with external devices, special circuits are sometimes required. These external devices include:

  • Driver circuits
  • Input protection schemes
  • Output protection circuits
  • Isolation circuits

So let's take a look at some of these schemes and see how they work!

Simple Light-Emitting Diode (LED) Driver

This simple circuit is convenient for driving high power LEDs with microcontrollers where the output of the microcontroller is connected to "IN".

When the microcontroller outputs 0, transistor Q1 turns off and so does LED D1. When the microcontroller outputs 1, the transistor turns on and so D1 also turns on. The value of R1 depends on the output voltage of your microcontroller, but values ​​between 1KΩ ~ 10KΩ often work well. The value of R2 depends on the size of the load you are powering, and this circuit is suitable for powering devices up to 1A and no more.

Simple Relay Driver

Devices that draw more than 1A of current and will turn on and off every few seconds are better suited for relays.

Although relays are quite simple (a small electromagnet that attracts a metal arm to close the circuit), they cannot be controlled directly by a microcontroller.

Normal relays require currents around 60mA ~ 100mA, which is too high for most microcontrollers, so the relays require a circuit using transistor control (as shown above). However, instead of a resistor to be used to limit the current, a reverse protection diode (D1) is required.

When the microcontroller (connected to "IN") outputs a 1, then transistor Q1 turns on. This turns on the relay RL1 and as a result the lamp (R2) lights up. If the microcontroller outputs 0, then transistor Q1 turns off, which turns off the relay, and therefore the lamp turns off.

Relays are very common in circuits that require switching AC power circuits and are available for switching 230V and 13A (suitable for toasters, kettles, computers and vacuum cleaners).


Buttons

When connecting a button to a microcontroller, simple problems can sometimes occur. The first (and most annoying) problem comes in the form of bounce, where the button sends a lot of signals when pressed and released.

Buttons are usually a piece of metal that comes into contact with some other metal, but when the buttons make contact they often bounce off (although they are most often tiny). This bounce means the button connects and disconnects a few times before locking in, resulting in a result that briefly looks random. Since microcontrollers are very fast, they can catch this bounce and execute button press events multiple times. To get rid of the bounce, you can use the diagram below. The circuit shown here is a very trivial circuit that performs well and is easy to build.

Input protection: voltage

Not all input devices will be friendly to your microcontroller, and some sources may even be harmful. If you have input sources that come from the environment (e.g. voltage sensor, rain sensor, human contact) or input sources that can output voltages in excess of what the microcontroller can handle (e.g. inductor circuits), then you will need to enable some input voltage protection. The circuit shown below uses 5V zener diodes to limit the input voltages so that the input voltage cannot go above 5V and below 0V. The 100R resistor is used to prevent too much current when the Zener diode picks up the input voltage.


I/O protection: current

The inputs and outputs of microcontrollers can sometimes be protected from too much current. If a device such as an LED draws less current than the maximum output current from the microcontroller, then the LED can be directly connected to the microcontroller. However, a series resistor will still be needed, as shown below, and common series resistor values ​​for LEDs include 470 ohms, 1 k ohms, and even 2.2 k ohms. Resistor series are also useful for input pins in rare cases where microcontroller pins are bad or the input device is experiencing an output current surge.


Level transducers

In the past, most of the signals in a circuit would operate at the same voltage, and this voltage was typically 5V. However, with the increasing technological capabilities of modern electronics, the voltage on new devices is decreasing. Because of this, many circuits include mixed signals, where older parts can operate at 5V while newer parts operate at 3.3V.

Although many hams would prefer to use a single voltage level, the truth is that older 5V parts may not work at 3.3V while newer 3.3V units cannot work at the higher voltage 5 Q. If a 5V device and a 3.3V device want to communicate, then level shifting is required, which converts one voltage signal to another. Some 3.3V devices have 5V "tolerance", which means that a 5V signal can directly connect to a 3.3V signal, but most 5V devices cannot carry 3.3V. To cover both options, below the schematics show conversion from 5V to 3.3V and vice versa.


Isolation: Optoisolator

Sometimes the circuit that the microcontroller needs to communicate with can present too many problems, such as electrostatic discharge (ESD), wide voltage fluctuations, and unpredictability. In such situations, we can use a device called an opto-isolator, which allows two circuits to communicate without being physically connected to each other by wires.

Optoisolators communicate using light, where one circuit emits light which is then detected by another circuit. This means that opto-isolators are not used for analog communication (e.g. voltage levels), but instead for digital communication, where the output is on or off. Optoisolators can be used for both inputs and outputs to microcontrollers where the inputs or outputs could be potentially dangerous to the microcontroller. Interestingly, opto-isolators can also be used for level shifting!


Gunther Kraut, Germany

Logic "1", logic "0" and high impedance. Three output states correspond to three motor states: "forward", "reverse" and "stop"

To control two independent loads, such as relays, two microcontroller I/O ports are usually required. In this case, you have the opportunity to turn on two relays, turn one on and turn off the other, or turn off both. If you do not need to turn on two relays at the same time, you can control the remaining three states using one output of the microcontroller. This uses the high-impedance output state.

This circuit can be used, for example, in the control of electric motors. The direction of rotation of the motor depends on which of its two phases is selected. For phase switching, both classic electromechanical and solid-state MOS relays can be used. Either way, opening both relays will stop the engine.

To control electromechanical relays, the circuit shown in Figure 1 is used. When the logic "1" at the output of the microcontroller, transistor Q 1 turns on the relay REL 1, which allows the motor to rotate in the forward direction. When the output switches to "0", transistor Q 3 opens. This causes the REL 2 contacts to close and the motor starts to rotate in the opposite direction. If the microcontroller port is in a high-impedance state, transistors Q 1 , Q 2 and Q 3 turn off, since the 1 V voltage at the base of Q 2 is less than the sum of the threshold voltages of the base-emitter junctions of Q 1 and Q 2 and the voltage drop across the diode D 1 . Both relays turn off and the motor stops. A voltage of 1 V can be obtained using a voltage divider or emitter follower. Diodes D 2 and D 3 serve to protect the collectors Q 1 and Q 2 from voltage surges that occur when the relay is turned off. Almost any low-power NPN and PNP transistors can be used in the circuit. The choice of D 1 is also unprincipled.

The circuit for driving a MOS relay is simpler, since LEDs can be connected directly to the output of almost any microcontroller (Figure 2). Logic "1" turns on the relay LED S 1, and logical "0" - S 2, opening the corresponding output triacs. When the port enters the high impedance state, both LEDs turn off because the 1.2V DC voltage is less than the sum of the threshold voltages of the two LEDs. Varistors R 3 , R 5 and snubber circuit C 1 , R 4 , C 2 , R 6 serve to protect the MOS relay. The parameters of these elements are selected in accordance with the load.

Loading...Loading...