Connecting a Relay Module to a Microcontroller

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today i want to talk about how we can drive high current high voltage applications using only a microcontroller and a relay to accomplish this we're going to take a closer look at a few different types of modules you might find across the internet we'll first go over the components and connections available on each module then we'll go over the schematics to get a better understanding of how each module works and finally we'll demonstrate how to connect these modules into our circuits throughout this video i'll touch on important considerations to be mindful of to ensure you don't damage your microcontroller or worse the item connected to the relay we'll start with the text information on the relay which will provide some indication to what the operating characteristics are when using a microcontroller such as an arduino uno to control the relay we are looking for an operating voltage of 5vdc this is usually displayed on the top of the relay in the part number it is important to verify this value up front as many relays are designed to operate at other voltages such as 12 or 24 volts we can also see what the current rating is at different voltages it is important to remember that this is the rating that the manufacturer of the relay gave that specific part and is not necessarily the appropriate rating of the module itself to the left of the relay we see a three position terminal block here is where we'll connect the load that we want to control with the module these terminal positions are labeled nc for normally closed com for common and no for normally open what this means is that when the relay is not active there is an electrical connection between the normally closed and common positions however when the relay is active the common flips its connection from normally closed to the normally open position the current rating of these terminal blocks is one of the places you have to be careful even if the current rating of the relay states something greater than 10 amps a good quality terminal block is rarely rated higher than 10 amps along the same word of caution you'll want to look at the copper trace widths of your relay module as well two of the three modules i have here provide wide three millimeter traces between the relay and the terminals and one of them only has one and a half millimeter traces generally speaking you'll want a three millimeter trace to handle up to 10 amps i'll make a note to myself to only use this one relay module for applications that require two or three amps max to the right of our relay we see the control circuitry this includes the main transistor a protection diode an optocoupler and control input pins here we'll be able to connect our microcontroller as the control circuit and provide the voltage potential to power the relay there are also a couple leds to indicate that the module has power and when the module is activated a unique feature of the first module is the ability to switch between two voltage sources by moving the vcc and jdvcc jumper the second module has the feature to select between using a high or low input to activate the relay the third module is a bit more bare bones lacking features such as an optocoupler or voltage control jumpers let's take a look at the schematic for the first module here we see the optocoupler voltage source jumper main transistor protection diode relay module and the terminal positions probably the most common way to connect this style of module is with the vcc and jdvcc jumper in place this means that the relay will be using the vcc as the voltage source for driving the relay and activating the opto coupler in this configuration we would connect our microcontroller's 5 volt rail to the vcc pin and one of the gpio pins to the in1 pin finally we'll connect the ground pin of the micro to the ground pin of the relay board when the gpio pin is low meaning there's zero voltage potential at im1 then current will flow between vcc and im1 this is known as sinking current into the gpio pin our gpio pin effectively acts like a ground pin for the connection to vcc it is typically okay to sync current into a gpio but each microcontroller will have limits to how much current a gpio pin can handle in the case of an arduino uno the atmega328ps gpio pins can sync up to 40 milliamps max but it's a good practice to keep the sustained amount under 20 milliamps the optocoupler only draws about 5 milliamps so this isn't a problem speaking about the optocoupler when current is passed through the led inside the device it will activate a photo transistor that photo transistor will in turn activate the main transistor allowing current to flow from vcc jdvcc rail to ground this will also activate the relay causing the switch inside of it to flip from normally closed to normally open in this configuration we have a small issue to be aware of since we're connecting vcc with jdvcc if something were to happen to the electrical circuit on the relay module there is the potential for the issue to feed back into our microcontroller so even though we're driving the input through an optocoupler a path back to our micro exist via the 5 volt rail to protect our micro and completely opto isolate it from the relay we need to remove the jumper between vcc and jdvcc we also need to disconnect the ground pins between the two boards to provide the required power to operate the relay an external power supply can be used here we'll connect the positive wire of the external power supply to jdvcc and the negative wire to the ground pin on the relay module in this configuration our microcontroller will still sync current between the vcc pin and in1 when im1 is low activating the optocoupler now when the main transistor is activated instead of pulling power from the microcontroller's 5 volt rail it will come from the external power supply this will of course activate the relay now if something were to go wrong with the relay there's no direct path back to our microcontroller you might be thinking this is all great but i don't want to turn on the relay when the gpio pin is low i want to turn the relay on when the pin is high we can accomplish this by changing up some of the control input connections instead of connecting vcc to the 5 volt rail we can connect it to our gpio pin and then connect the ground pin of the microcontroller to the in1 pin on the relay module now when the gpio pin goes high it will activate the optocoupler turning on the relay a word of caution here though if we're using the gpio pin to drive the vcc input pin of the relay module do not connect the jumper between vcc and jdvcc if you did connect this jumper the gpio pin would be the voltage source for the relay which requires an average of 70 milliamps to operate this will almost always be greater than what a gpio pin is rated to provide the second relay module has generally the same components as the first but in a different configuration you'll see that our optocoupler is now bi-directional the main transistor is now connected to the relay via the emitter instead of the collector and in place of the vcc jdvcc jumper we now have a high low jumper this relay module doesn't provide us with an option to connect an external power supply so we know up front we're limited in terms of protection for our microcontroller here we'll connect the 5 volt rail of the microcontroller to the dc plus pin and the ground pin to the dc minus pin we can then use any of our gpio pins to connect to the in pin on the relay module the same basic principle of operation from the previous module still applies here when one of the leds inside the optocoupler is activated power is applied to the relay via the main transistor the primary difference is that we now have a bi-directional optocoupler this means that there are two leds in different directions that can activate the photo transistor when we have a jumper connected between the center pin and the low pin then the circuit will activate when the gpio pin is low sinking current if the jumper is connected between the center pin and high the gpio pin will become the voltage source this will activate the relay circuit when the gpio pin is high being able to quickly change between sourcing and syncing current to control the module is certainly a nice feature however even though we're controlling the module through an optocoupler we just have to be mindful that there's the potential risk to the micro via the 5 volt rail connection if something were to go wrong our last module is of course less feature rich but that doesn't mean that it can't be useful this module still has a main transistor and protection diode however instead of using an optocoupler this module drives the main transistor directly from the gpio pin of a microcontroller the 5 volt rail and ground are also used to provide power to the relay in this configuration there is no option to activate the relay when the gpio pin is low this will always function as a active high device in this demonstration we're going to use what i consider the safest configuration we have an arduino uno driving our relay module however the relay module is getting its power from an external power supply our load is going to be just this light bulb with a standard electrical socket with the neutral side unbroken all the way back to mains and the hot side broken and split between normally open and common for the control circuit from the arduino we have our ground line going to in1 and our gpio pin 6 going to vcc and in this configuration pin 6 when it goes high we should expect the relay module to turn on and when it's low we should expect the relay module to be off from the power supply the positive rail is going to jd vcc and the negative rail is going to the relay module's ground pin now we mentioned earlier that this relay module is going to pull roughly 70 milliamps that's why we don't want to drive the module itself or the relay itself from the gpio pin we can keep an eye on this current meter to see how much current this module is going to pull so when we press the button we can see everything works the way we expect and we're pulling roughly 77 milliamps of course not everybody's going to want to use an external power supply when they're using their relay board so we can also demonstrate driving or not only driving the relay but providing the power to turn it on and off via the arduino uno b now you can see with everything turned back on the relay module immediately turned on causing our light fixture to turn on and that's because now when pin 6 is low we're pulling our power from vcc across the optocoupler into pin six this is now effectively the ground for that vcc across the optocoupler however when we press our button pin six will go high it'll be at the same voltage potential as vcc and so the optocoupler will turn off turning off the relay in this video we did a deep dive into three commonly found relay modules going over what components and connections are available how the circuits function and demonstrated a practical application i had a lot of fun putting this video together and hope it helps demystify the relay module let me know in the comments below if you'd like to see more videos like this until then thanks for watching
Info
Channel: Brad Henson
Views: 146,035
Rating: undefined out of 5
Keywords: Relay Module, Relay, Controller, Arduino, Schematic, Current, Voltage
Id: FWvEEtrTGRQ
Channel Id: undefined
Length: 11min 40sec (700 seconds)
Published: Fri Mar 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.