.. note::
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
**Why Join?**
- **Expert Support**: Solve post-sale issues and technical challenges with help from our community and team.
- **Learn & Share**: Exchange tips and tutorials to enhance your skills.
- **Exclusive Previews**: Get early access to new product announcements and sneak peeks.
- **Special Discounts**: Enjoy exclusive discounts on our newest products.
- **Festive Promotions and Giveaways**: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [|link_sf_facebook|] and join today!
.. _1.3.3_c:
1.3.3 Relay
=================
Introduction
------------
In this project, we will learn to use a relay. It is one of the commonly
used components in automatic control system. When the voltage, current,
temperature, pressure, etc., reaches, exceeds or is lower than the
predetermined value, the relay will connect or interrupt the circuit, to
control and protect the equipment.
Required Components
------------------------------
In this project, we need the following components.
.. image:: ../img/list_1.3.4.png
It's definitely convenient to buy a whole kit, here's the link:
.. list-table::
:widths: 20 20 20
:header-rows: 1
* - Name
- ITEMS IN THIS KIT
- LINK
* - Raphael Kit
- 337
- |link_Raphael_kit|
You can also buy them separately from the links below.
.. list-table::
:widths: 30 20
:header-rows: 1
* - COMPONENT INTRODUCTION
- PURCHASE LINK
* - :ref:`cpn_gpio_board`
- |link_gpio_board_buy|
* - :ref:`cpn_breadboard`
- |link_breadboard_buy|
* - :ref:`cpn_wires`
- |link_wires_buy|
* - :ref:`cpn_resistor`
- |link_resistor_buy|
* - :ref:`cpn_led`
- |link_led_buy|
* - :ref:`cpn_transistor`
- |link_transistor_buy|
* - :ref:`cpn_relay`
- |link_relay_buy|
* - :ref:`cpn_diode`
- |link_diode_buy|
Schematic Diagram
-----------------
.. image:: ../img/image345.png
Experimental Procedures
-----------------------
**Step 1:** Build the circuit.
.. image:: ../img/image144.png
**Step 2**: Open the code file.
.. raw:: html
.. code-block::
cd ~/raphael-kit/c/1.3.3
**Step 3:** Compile the code.
.. raw:: html
.. code-block::
gcc 1.3.3_Relay.c -lwiringPi
**Step 4:** Run the executable file.
.. raw:: html
.. code-block::
sudo ./a.out
After the code runs, the LED will light up. In addition, you can
hear a ticktock caused by breaking normally close contact and
closing normally open contact.
.. note::
If it does not work after running, or there is an error prompt: \"wiringPi.h: No such file or directory\", please refer to :ref:`install_wiringpi`.
**Code**
.. code-block:: c
#include
#include
#define RelayPin 0
int main(void){
if(wiringPiSetup() == -1){ //when initialize wiring failed, print message to screen
printf("setup wiringPi failed !");
return 1;
}
pinMode(RelayPin, OUTPUT); //set GPIO17(GPIO0) output
while(1){
// Tick
printf("Relay Open......\n");
digitalWrite(RelayPin, LOW);
delay(1000);
// Tock
printf("......Relay Close\n");
digitalWrite(RelayPin, HIGH);
delay(1000);
}
return 0;
}
**Code Explanation**
.. code-block:: c
digitalWrite(RelayPin, LOW);
Set the I/O port as low level (0V), thus the transistor is not energized
and the coil is not powered. There is no electromagnetic force, so the
relay opens, LED does not turn on.
.. code-block:: c
digitalWrite(RelayPin, HIGH);
set the I/O port as high level (5V) to energize the transistor. The coil
of the relay is powered and generate electromagnetic force, and the
relay closes, LED lights up.
Phenomenon Picture
------------------
.. image:: ../img/image145.jpeg