In this tutorial, you will learn how to use a relay.
Relay:
A relay is an electrically operated switch. Many relays use an electromagnet to mechanically operate a switch, but other operating principles are also used, as in solid-state relays. Relays are used where it is necessary to control a circuit by a low- power signal (with complete electrical isolation between control and controlled circuits), or where several circuits must be controlled by one signal. The first relays were used in long-distance telegraph circuits as amplifiers. They repeated the signal coming in from one circuit and re-transmitted it on another circuit. Relays were used extensively in telephone exchanges and early computers to perform logical operations.
The figure below shows the pin diagram and wiring method of this relay: After the control terminal of the relay is connected to GPIO, GND and VCC, the relay will perform pull-in/release actions correspondingly when the GPIO outputs high/low level, and the bouncing sound of the internal spring can be heard during the process; if the power supply and wiring of the load terminal are normal at this time, the load will start and shut down with the action of the relay.
Please note: Before opening the file, ensure that you have installed the Arduino IDE development environment and completed the installation of relevant components such as the board support package and driver corresponding to the board.
int relayPin = 18;
void setup()
{
pinMode(relayPin, OUTPUT);
}void loop()
{
digitalWrite(relayPin, HIGH); // turn the relay on (HIGH is the voltage level)
delay(2000); // wait for a second
digitalWrite(relayPin, LOW); // turn the relay off by making the voltage LOW
delay(2000); // wait for a second
}
This relay is easy to operate. When a high level is input, the internal electromagnet pulls in to switch on the load circuit and light up the LED; when a low level is input, the electromagnet releases to disconnect the load circuit and turn off the LED. Based on this principle, small current can be used to drive high-voltage loads.