🌐 This is an automatic translation of a topic from Underc0de, the largest Spanish-speaking hacking and IT community. Read the original (Spanish) and reply →
ANTRAX ·
In this new post we're going to talk about how to operate a relay with Arduino. First of all, we need to know that a relay is a device that works like a switch, allowing an independent electrical circuit to be turned on or off. In other words, we can activate higher-voltage, high-consumption electronic devices through our microcontrollers (for example, turning on a light bulb that runs on 220V through our microcontroller).<br /><br />For this tutorial I used some HUI KE relays that work at 5V, bought on eBay at a very low price, specifically the HUI KE HK4100F-DC5V-SHG. Here are some images and the internal connections:<br /><br />
<br /><br />As you can see, these relays are very small and perfect for working with our microcontrollers, since they run at 5V, with no need for external power supplies to activate their coil. Here's the connection diagram made in Fritzing and a schematic made in Eagle:<br /><br />
<br /><br />As you can see, very few components are needed to build the control circuit:<br /><br />* 5V relay (in this case from HUI KE, any other will do).<br />* BC547 transistor.<br />* 10k resistor.<br />* 1N4001 diode.<br /><br />As always, here's a photo of the build and a video of the circuit working; in this case the bulb turns on for one second and stays off for ten seconds:<br /><br />
/*Programa realizado por Javier Couto Rego "Regata" para<br />www.tallerarduino.wordpress.com<br /> <br />Encendiendo una bombilla a 220V con un Arduino y un relé<br />a 5V*/<br /> <br />int rele=8;<br /> <br />void setup()<br />{<br /> pinMode(rele,OUTPUT);<br />}<br /> <br />void loop()<br />{<br /> //Nota tenemos el relé conectado como Normalmente Abierto<br /> //así solo se activará la carga cuando activemos la bobina<br /> //del relé, para que funcione al revés cambiaremos el cable<br /> //a la posición Normalmente Cerrado<br /> digitalWrite(rele,HIGH); //Activamos la bobina del relé y encendemos la bombilla<br /> delay(1000); //durante 1 segundo<br /> digitalWrite(rele,LOW); //Desactivamos la bobina del relé y apagamos la bombilla<br /> delay(10000); //durante 10 segundos<br />}
<br /><br />Source: Taller arduino
fermino ·
I like it, I like it :)<br />But... one tip.<br />In the code, use a constant instead of a variable.<br /><br />Código: c
const int rele=8;
<br />or else<br />Código: c
#define rele 8
<br /><br />In this code you could perfectly well use #define, but in more complex code it isn't recommended. It's better to use const.<br />From what I understand, #define defines a constant before the program is compiled.<br />That is, if I have<br />Código: c
#define led1 8<br />#define led12 9<br /><br />void setup()<br />{<br /> pinMode(led1,OUTPUT); //El compilador, antes de compilar, reemplaza led1, por 8, y led 12, por 9. Entonces, esto resultaría en: pinMode(8,OUTPUT);<br /> pinMode(led12,OUTPUT); //Acá se produce el error. El compilador encuentra la cadena led1 (dentro de led12) y la transforma en 8, quedando así, pinMode(82,OUTPUT);<br />}<br />
<br /><br />So, this code wouldn't generate errors, but it's still recommended to use const.<br /><br />Los enlaces son solo para miembros. Registrate o iniciá sesión<br />Los enlaces son solo para miembros. Registrate o iniciá sesión
79137913 ·
HELLO!!!<br /><br />Recommendation...<br /><br />The Arduino's 5 volts can switch a relay, but not many, because it doesn't have enough current for several of them to work.<br /><br />Solution: build a setup with an external power supply, e.g. a cell phone charger.<br /><br />In my case I use a 5 V 3 amp supply and I can switch several without any problems.<br /><br />THANKS FOR READING!!!