Hi guys. My website is normally only in italian, but this article was read more and more times (1600 times in 7 months), for this I want to translate it in english. I don’t speak english very well, for this I hope you can understand good.
Today we look at how we can use Arduino to control an external relay. But let’s start with the basics. What is a relay and why we should connect one to the Arduino?
When in fact we close the circuit, the relay coil creates a magnetic field that allows triggers the internal switch. Then, when, we re-open the circuit, the transistor will be closed to the passage of the coil current, BUT the current will continue to pass due to induction from the magnetic field. This result to a current peak which can burn easily the transistor. The diode serves precisely to ensure that the current can be discharged without damage. Now, the question is: Now the question is: how we choose the transistor? Turning the box to find all the technical data we need. This does not mean that we are obliged to bought the same package that among other I bought at random. The important thing is to know what information we need and if you haven’t the datasheet Google is your better friend. We’re going to get one of the NPN and not a PNP (the difference is in the polarity).
If we take the BC547B, one of the numerous in the box, we see that the collector supports a maximum of 45V (Vce), quite sufficient to withstand the required 12V from the relay. The maximum peak current to the collector is 200mA (Ic max = 0.2A) against only 33mA necessary to power the relay. Now we have to properly size the resistance located between the output of Arduino and the base of the transistor. If current relay is 33mA and the hFE of the transistor is 200 we obtain a minimum value of current at the base of 33/200 = 0.165mA but for a variety of reasons related to the nature of the transistors must be multiplied by at least 2 or 3 times coming to 0.33-0.495mA (~ 0.5mA). Therefore, the current at the base of the transistor must be of a minimum of 0.5mA and a maximum of 20mA (half of what can be deliver by Arduino). Knowing this we can easily calculate the value of the resistance. We know that the voltage of Arduino’s output pin is 5V but it must be reduced to 0.7V absorbed by the transistor. Therefor we have 4.3V with resistance values between 4.3/0.5 and 3.4/20 that, after a simple conversion of units, give a value between a minimum resistance value of 215 ohms and a maximum of 8600.
We just have to prepare the circuit and create a small software for testing. Clearly a resistance value between the highest will use a smaller current (from Arduino), which is preferable. I used in the test an intermediate value of 4.7K (4700 ohm). Now that you know how do all calculation, please visit this page: it’s aonline specific calculator for choosing the right resistence. It’s in Italian but you can use without speaking that language. For the software we can simply use the example above that makes the internal LED flash, just use exit 13 because in addition to flash the LED there is also the opening and closing of the relay. Be careful that in the scheme of the circuit the collector and emitter are reversed from the reality of the project, but as I said the location is not standardized. I leave you a picture of the project you can see even at full resolution clicking on it and with a video where you can hear the “click” of the relay. You will also notice the external 12V power supply.
The green crocodile is only to take together the wire to the relay. The flicker of relay click is for the unstable connection between wires. Sorry for the imperfect synchronization from audio and video.
No Responses to “Arduino: connect a 12V relay (english version)”