пятница, 10 июля 2015 г.

Arduino Relay Tutorial

Arduino Relay Tutorial
Arduino Relay Tutorial

An Arduino Uno driving 1 relay on an 8 relay board.
Arduino Uno and the 8-channel relay board.

The first relay board I used has 8 relays and provides a driver transistor and an LED for each. This is typical of the relay boards available. When you write a HIGH, or 5V, to the relay board input pin, the transistor comes on, energizing the relay's coil. When you write a LOW, or 0V, to the relay board input pin, the transistor turns off, relaxing the coil.


int pin7_Value = 0;void setup() {
pinMode(7, OUTPUT);
digitalWrite(7,pin7_Value);}void loop() {
digitalWrite(7,pin7_Value);
pin7_Value = ~pin7_Value;
delay(1000);}

The code is fairly simple. A LOW is written to start, then the port bit is toggled every second from on to off or off to on.


An Arduino Uno driving 1 relay on a 4 relay board.
Arduino Uno and the 4-channel relay board.

The second relay board I used has 4 relays and provides an opto-isolator, a driver transistor, and an LED for each. This is less typical, but a safer choice if you want to keep high voltage and low voltage systems separated. The code was the same, but these relays are energized by writing a LOW to the port pin, rather than a HIGH.


The opto-isolator has an led and a photo transistor in the same package. You can see the "large" black device above and to the left of the lit LED. With opto-isolators you can keep the power supplies for the relays and the Arduino completely separate - even the grounds can be separate.


Transistor Relay Driver

The schematic shows how you can use a transistor to drive a relay. The transistor must be able to handle the current the relay requires. When the port pin D2 is written with a HIGH, the transistor will turn on, causing current to flow through the relay. When the port is written with a LOW, the transistor turns off, and the relay "relaxes", or turns off.


To determine the current requirement, measure the relay coil with an ohmmeter. Divide 5V by the result and you will get the current in amps. The 2N3904 shown has a maximum current of 0.2 amps. Typically larger relays require more current. Don't run it over 0.1 amps, and then only if you have good airflow. You can also use a power MOSFET or a larger transistor with a metal tab, like a TIP120, if you need to drive a larger relay.


When the relay is de-energized the magnetic field collapses and actually generates a high voltage across the terminals. This voltage will destroy the transistor if not blocked by the blocking diode, D1. Be careful with the polarity of the diode. Hooking it up backwards will also damage the transistor.


Original article and pictures take https://arduino-board.com/tutorials/relays site


Комментариев нет:

Отправить комментарий