Arduino project no. 3: 2-relay module
Written by RHIBA Zakaria, Zakpy Academy
Sometimes we want the Arduino to control devices that run on mains power, like a lamp or a fan. But the Arduino works at 5 V, it cannot drive 220 V directly. That is where a relay module comes in: the Arduino controls the relay, and the relay controls the 220 V.
In this project, we turn on a 220 V lamp when we press a button.
⚠ Careful, 220 V is dangerous. Do all the wiring with the power off, and never touch the relay terminals while the lamp is plugged in. If you are a beginner, start with an LED and a battery instead of the lamp.
What you will learn
How a relay module works.
Read a button with digitalRead() and the internal pull-up resistor.
Drive an output from the state of an input.
What you need
An Arduino Uno or Mega board
A 5 V 2-relay module
A push button
A 220 V lamp with its socket and cable
Wires
Wiring
Relay module / button
Arduino
Module VCC
5 V
Module GND
GND
Module IN1
pin 8
Button
between pin 9 and GND
220 V side: cut only one wire of the lamp (the live wire) and connect both ends to the COM and NO (normally open) terminals of relay 1.
#define RELAY_IN1 8 // l'entrée IN1 du module relais#define BUTTON1 9 // le bouton
The setup() function
The relay pin is an output. The button is an input, and we turn on the internal pull-up resistor with INPUT_PULLUP: the pin is HIGH when the button is released, and LOW when it is pressed.
voidsetup(){pinMode(RELAY_IN1,OUTPUT);digitalWrite(RELAY_IN1,HIGH);// relais au repos au démarragepinMode(BUTTON1,INPUT_PULLUP);}
The loop() function
We read the button and drive the relay. On most 2-relay modules, the relay switches on when its input is LOW:
The comments in the code are in French, like the .ino file you download.
/* * Projet : commander une lampe 220 V avec un module relais et un bouton * Zakpy Academy - RHIBA Zakaria * Carte : Arduino Uno ou Mega * * Le module relais s'active quand son entrée IN1 est à LOW (c'est le cas * de la plupart des modules 2 relais). Le bouton est branché entre la * broche 9 et le GND, avec la résistance de pull-up interne de l'Arduino : * bouton appuyé = LOW, bouton relâché = HIGH. * * ATTENTION : le côté 220 V du relais est dangereux. On câble tout * hors tension, et on ne touche jamais aux bornes quand c'est branché. */#define RELAY_IN1 8 // l'entrée IN1 du module relais#define BUTTON1 9 // le bouton qui commande le relaisvoidsetup(){pinMode(RELAY_IN1,OUTPUT);digitalWrite(RELAY_IN1,HIGH);// relais au repos au démarragepinMode(BUTTON1,INPUT_PULLUP);// pull-up interne}voidloop(){intbutton1Value=digitalRead(BUTTON1);// on lit l'état du boutonif(button1Value==LOW){digitalWrite(RELAY_IN1,LOW);// bouton appuyé : relais activé, lampe allumée}else{digitalWrite(RELAY_IN1,HIGH);// bouton relâché : relais au repos, lampe éteinte}}
Going further
Use the second relay to control a second device with a second button.
Make a toggle button: one press turns on, the next press turns off.
Control the relay from your phone (Bluetooth or Wi-Fi). This is the base of the King House smart home project.
Projet Arduino n°3 : module 2 relais
Écrit par RHIBA Zakaria, Zakpy Academy
Parfois on veut que l'Arduino commande des appareils qui marchent sur le secteur, comme une lampe ou un ventilateur. Mais l'Arduino travaille en 5 V, il ne peut pas commander directement du 220 V. C'est là qu'on utilise un module relais : l'Arduino commande le relais, et le relais commande le 220 V.
Dans ce projet, on allume une lampe 220 V quand on appuie sur un bouton.
⚠ Attention, le 220 V est dangereux. On fait tout le câblage hors tension, et on ne touche jamais les bornes du relais quand la lampe est branchée. Si tu débutes, commence avec une LED et une pile à la place de la lampe.
Ce qu'on va apprendre
Comment marche un module relais.
Lire un bouton avec digitalRead() et la résistance de pull-up interne.
Commander une sortie selon l'état d'une entrée.
Le matériel
Une carte Arduino Uno ou Mega
Un module 2 relais 5 V
Un bouton poussoir
Une lampe 220 V avec sa douille et son câble
Des fils
Le câblage
Module relais / bouton
Arduino
VCC du module
5 V
GND du module
GND
IN1 du module
broche 8
Bouton
entre la broche 9 et GND
Côté 220 V : on coupe un seul fil de la lampe (la phase) et on branche les deux bouts sur les bornes COM et NO (normalement ouvert) du relais 1.
Pour bien comprendre le fonctionnement du relais, tu peux lire cette documentation : Last Minute Engineers.
Les broches
#define RELAY_IN1 8 // l'entrée IN1 du module relais#define BUTTON1 9 // le bouton
La fonction setup()
La broche du relais est une sortie. Le bouton est une entrée, et on active la résistance de pull-up interne avec INPUT_PULLUP : la broche est à HIGH quand le bouton est relâché, et à LOW quand on appuie.
voidsetup(){pinMode(RELAY_IN1,OUTPUT);digitalWrite(RELAY_IN1,HIGH);// relais au repos au démarragepinMode(BUTTON1,INPUT_PULLUP);}
La fonction loop()
On lit le bouton et on commande le relais. Sur la plupart des modules 2 relais, le relais s'active quand son entrée est à LOW :
/* * Projet : commander une lampe 220 V avec un module relais et un bouton * Zakpy Academy - RHIBA Zakaria * Carte : Arduino Uno ou Mega * * Le module relais s'active quand son entrée IN1 est à LOW (c'est le cas * de la plupart des modules 2 relais). Le bouton est branché entre la * broche 9 et le GND, avec la résistance de pull-up interne de l'Arduino : * bouton appuyé = LOW, bouton relâché = HIGH. * * ATTENTION : le côté 220 V du relais est dangereux. On câble tout * hors tension, et on ne touche jamais aux bornes quand c'est branché. */#define RELAY_IN1 8 // l'entrée IN1 du module relais#define BUTTON1 9 // le bouton qui commande le relaisvoidsetup(){pinMode(RELAY_IN1,OUTPUT);digitalWrite(RELAY_IN1,HIGH);// relais au repos au démarragepinMode(BUTTON1,INPUT_PULLUP);// pull-up interne}voidloop(){intbutton1Value=digitalRead(BUTTON1);// on lit l'état du boutonif(button1Value==LOW){digitalWrite(RELAY_IN1,LOW);// bouton appuyé : relais activé, lampe allumée}else{digitalWrite(RELAY_IN1,HIGH);// bouton relâché : relais au repos, lampe éteinte}}
Pour aller plus loin
Utilise le deuxième relais pour commander un deuxième appareil avec un deuxième bouton.
Fais un bouton « interrupteur » : un appui allume, un autre appui éteint.
Commande le relais depuis le téléphone (Bluetooth ou Wi-Fi). C'est la base du projet de maison connectée King House.
مشروع Arduino رقم 3: وحدة مرحّلين
إعداد: RHIBA Zakaria، Zakpy Academy
نريد أحيانًا أن تتحكم لوحة Arduino في أجهزة تعمل بكهرباء الشبكة، مثل مصباح أو مروحة. لكن اللوحة تعمل بجهد 5 فولت، ولا يمكنها التحكم مباشرة في جهد 220 فولت. هنا نستخدم وحدة المرحّل: اللوحة تتحكم في المرحّل، والمرحّل يتحكم في دائرة 220 فولت.
في هذا المشروع نشغل مصباحًا يعمل بجهد 220 فولت عندما نضغط على زر.
⚠ انتبه، جهد 220 فولت خطير. أنجز كل التوصيلات والكهرباء مفصولة، ولا تلمس أطراف المرحّل أبدًا والمصباح موصول بالشبكة. إذا كنت مبتدئًا، ابدأ بمصباح LED وبطارية بدل المصباح.
ما الذي سنتعلمه
طريقة عمل وحدة المرحّل.
قراءة زر باستخدام digitalRead() ومقاومة الرفع الداخلية.
التحكم في مخرج حسب حالة مدخل.
المكونات
لوحة Arduino Uno أو Mega
وحدة مرحّلين بجهد 5 فولت
زر ضاغط واحد
مصباح 220 فولت مع القاعدة والسلك
أسلاك توصيل
التوصيل
وحدة المرحّل / الزر
لوحة Arduino
طرف VCC في الوحدة
5 فولت
طرف GND في الوحدة
GND
طرف IN1 في الوحدة
المنفذ 8
الزر
بين المنفذ 9 و GND
من جهة 220 فولت: نقطع سلكًا واحدًا فقط من سلكي المصباح (سلك الطور) ونوصل طرفيه بالطرفين COM و NO (مفتوح في الوضع العادي) في المرحّل الأول.
لفهم طريقة عمل المرحّل بشكل أفضل، يمكنك قراءة هذا الشرح (بالإنجليزية): Last Minute Engineers.
المنافذ
#define RELAY_IN1 8 // l'entrée IN1 du module relais#define BUTTON1 9 // le bouton
الدالة setup()
منفذ المرحّل مخرج. أما الزر فهو مدخل، ونفعّل مقاومة الرفع الداخلية باستخدام INPUT_PULLUP: تكون قيمة المنفذ HIGH عندما يكون الزر غير مضغوط، و LOW عند الضغط عليه.
voidsetup(){pinMode(RELAY_IN1,OUTPUT);digitalWrite(RELAY_IN1,HIGH);// relais au repos au démarragepinMode(BUTTON1,INPUT_PULLUP);}
الدالة loop()
نقرأ حالة الزر ونتحكم في المرحّل. في معظم وحدات المرحّلين، يعمل المرحّل عندما تكون قيمة مدخله LOW:
التعليقات داخل الشيفرة باللغة الفرنسية، كما في ملف .ino الذي يمكنك تحميله.
/* * Projet : commander une lampe 220 V avec un module relais et un bouton * Zakpy Academy - RHIBA Zakaria * Carte : Arduino Uno ou Mega * * Le module relais s'active quand son entrée IN1 est à LOW (c'est le cas * de la plupart des modules 2 relais). Le bouton est branché entre la * broche 9 et le GND, avec la résistance de pull-up interne de l'Arduino : * bouton appuyé = LOW, bouton relâché = HIGH. * * ATTENTION : le côté 220 V du relais est dangereux. On câble tout * hors tension, et on ne touche jamais aux bornes quand c'est branché. */#define RELAY_IN1 8 // l'entrée IN1 du module relais#define BUTTON1 9 // le bouton qui commande le relaisvoidsetup(){pinMode(RELAY_IN1,OUTPUT);digitalWrite(RELAY_IN1,HIGH);// relais au repos au démarragepinMode(BUTTON1,INPUT_PULLUP);// pull-up interne}voidloop(){intbutton1Value=digitalRead(BUTTON1);// on lit l'état du boutonif(button1Value==LOW){digitalWrite(RELAY_IN1,LOW);// bouton appuyé : relais activé, lampe allumée}else{digitalWrite(RELAY_IN1,HIGH);// bouton relâché : relais au repos, lampe éteinte}}
للتعمق أكثر
استخدم المرحّل الثاني للتحكم في جهاز ثانٍ بزر ثانٍ.
اصنع زرًا يعمل كمفتاح: ضغطة تشغل المصباح، والضغطة التالية تطفئه.
تحكم في المرحّل من الهاتف (Bluetooth أو Wi-Fi). هذا هو أساس مشروع المنزل الذكي King House.