#18 Add a Relay Module to your Arduino project - Hints, Tips & Traps
Вставка
- Опубліковано 26 вер 2024
- Wow! Who knew that adding a typical 5-volt relay module to your Arduino could be so easy yet at the same time harbour some gotchas that will make you wonder what's going on?
If you need to drive some external circuitry from your Arduino module then a relay module is a fantastic item to use - but there are some gotchas to be aware of, and having some knowledge in how they work will help you refine your project.
I give you the low down on what makes these tick and how to avoid some potential traps that could leave you scratching your head!
PLEASE NOTE: In July 2017, an observant Arduinite (#Alexus) has spotted that the circuit diagram for this device is wrong - there should be no direct connection between the collector of Q1 and JD-VCC as that would be a short circuit whenever Q1 was switched on! Luckily the actual device is fine, just the circuit diagram that is wrong.
As always, Thanks For Watching.
And my channel is here:
/ ralphbacon
How can I remember this? Memory tip: "See" Ralph Bacon, geddit?
I don't know how many times over the years I've watch this video to "remind" myself how the JD-VCC works, again thanks for the detailed explanation!!!
Ha, ha! Yes, it can get confusing. I've just implemented a 4-channel relay for my pond controller. And yes, I used the JDD-VCC pin too!
Thanks for taking the time to describe what the JD-VCC/VCC pins are for, and how they work. I was having a hard time finding out, searching the Internet.
Glad it helped, Gary. Just don't short anything out using that dangerously placed jumper!
Brilliant down to earth understandable for someone like me new to trying to control external devices with an Arduino. Thanks.
I'm glad it helped, Ian, thanks for posting.
Thank you so much, just subscribed as I think this is such a great learning channel as you have the ability to explain and inform. A rare skill please keep them coming and looking forward to what's to cum. Respect.
This the best explanation I found about the subject.
Thank you very much, everything clear!
Glad it was helpful!
this was so helpful ..was stuck on a project for days ..so enlightening ..may the Heavens richly bless you thank you
Glad it helped!
This video may be 5 years old but still very educational and usefull, help me alot. Thanks!
I had some issues running the code you used but i got it working with afew modifications 🙏🏻
Great to hear, Ray! It would be interesting to know what the changes were?
Finally a vid that explains why you are sinking the supplied voltage to the onto-coupling.
Thank you.
No problemo, Mick, glad it cleared that up.
Thanks for describing what the JD-VCC/VCC pins are for, and how they work. Watched many other UA-cam videos on the subject on relay modules and this was overlooked. Nice job.
Glad it was helpful!
I looked for quite a while to find info explaining the 3pins with jumper on this board, then I found your video .... Perfect, thank you.
Glad it helped you, thanks for posting.
That's a really good video; kudos for that. All I would add, when you're talking about why not to use it with the jumper attached (ie, powering the coils from the Arduino and compromising the opto protection) is the current aspect. Those Songles draw about 70mA each, so if you have an 8-relay version of the board and energise all of them at once you're up in the 1/2Amp realms and you don't want to subject the Arduino 5V pin to that.
Excellent point you make there, Jim, I'll make a note for when I next mention relays.
I have just received my relay module which is exactly the same as modules shown in this video. Great thing I came across this video as I am just about to start my project.
I knew you had ordered one Robert (Benny is my spy) so it is not a coincidence that you have found this video! Have Fun but do NOT short out those header pins (JCC-VCC-GND) !!!
Thanks for a great video. I was having trouble understanding my relays because because a single relay module I have actually turns on with the "HIGH" command but all the other relay modules turn off with this command. After watching your video I was able to understand how this one works.
Glad it helped you, Mark, thanks for posting.
Simple to follow. The circuite diagram really illustrates whats happening. Thanks.
Glad it helped!
Thanks for a great video! You are right about the jumper at around 15:30 and newbie assumptions. One might assume the jumper is to be in one of the two possible positions. I removed mine and turned it 90 degrees and placed it on the VCC pin as a reminder.
Yes, I do that with jumpers too! But they do have a habit of then jumping off, never to be seen again! That's why I bought a pack of 50 some time ago. Glad you found it useful, Mike, good to hear from you.
Thanks for this video on relays. It clarified a lot of things for me.
You are most welcome Roberta Laidman, I'm glad you like the video. Nice to hear from you.
The code I used to demonstrate switching the relays alternately can be found here: db.tt/HlkvoTbM
Let me know if you have any issues downloading the code (or understanding it)
Best explanation yet! Thank you for the video.
Glad you enjoyed it!
Long time, no see...I hope you're well. I figured I'd drop by to vampire some more of your knowledge as I just acquired one of these funny little guys for a two channel 12V multi light vehicle project I've gotten myself into elbow deep at the moment. STILL the number ONE channel I go to to find help when I stray into dangerous...err...new waters, electrically speaking. Thanks again for sharing your knowledge and explanations that the rest of us apes can rightly understand and properly digest without falling asleep or frying our eyebrows. ;)
Best wishes mate.
Good to hear from you again, it's been a while. This device would work very well in a car (low voltage) and help protect your Arduino too. Thanks for your kind words about my channel, I'm glad you like the videos. Thanks for posting.
Thanks for explaining that light flickering issue. Didn't know it was due to setting pins as an output.
Thanks for that, webslinger2011, glad it was clear, nice to hear from you.
Thanks for your video sir. I have the problem with my relay, when start on, all the relay was not behave like i want. The reason, i already noted based on your explaination worth to try out. Tq sir
You're most welcome and good luck with your project.
Great video - well explained. This has given me a much better understanding of this relay module and how to use it correctly with an Arduino board. Thank you.
You are most welcome Shaun R, I'm glad you like the video. Nice to hear from you.
Wow I learned a lot about these relay modules with this video - Thank You so much-!!!
Glad you liked it, Russel, thanks for posting.
Thanks for the details. I am clearly not as skilled at reading circuit diagrams, and had no idea what was happening with my 16 channel relay. However, now I suspect I am inadvertently shorting out my GPIO IC with the connections from my Arduino to the GND, along with the 16 Pins.
Again, your expertise is much appreciated!
I read from your other post that things are now working. I'll comment on your other post...
Love your videos Ralph. Always that that bit of extra information that makes you go: "I've learned something new" :)
Thanks for that, Michael Hedberg, glad you learning! Nice to hear from you.
omg!, thank you so much! I had this issue with powering up an Arduino. The relay would activate within the first few microseconds, turning on the contactor of an AC motor,and of course, that shouldn't happen, It was driving me nuts
Glad it helped you Manuel!
I spent weeks and days trying to figure out why my servo spins when i turn the "isolated" relay on. And it takes 13 minutes to solve it with this video ❤❤
I'm so glad it helped, Patrick!
Thanks sir you explained very well. The same module you using is available in my country at the price of less then dollar 😀
Glad it helped! I'm using the 4-relay module in my most recent project and that was pretty inexpensive too.
Thanks, the video is very helpful. Everything's explained in detail.
Glad it was helpful!
Amazing video, very pedagogical, beginner friendly and focused on the point. Thank you for your time sir. Your friendly and humble cyber-student. :-) Much love from Sweden
Nice to hear from you Josef, glad you're liking the videos!
Thank you for this video it helped me alot while controling relay module through raspberry pi.
Glad it helped, Mayur!
Thanks for the detail, good stuff!
Glad you liked it Gary, thanks for posting.
I don't know if it's already been pointed out but the back emf diode and relay coil are short circuited. {Q1 collector to JD-VCC line.}
Ha! I never noticed that before! Not my circuit diagram, but from the seller. The boards works at least, so we can safely assume that the replay coil is not shorted like that. Well spotted, Glen, you get the Eagle Eyed award for... well, for the last 6 years. 👁️😜
This is great info, just what I needed to know, thank you!
You are most welcome dikke j, I'm glad you like the video. Nice to hear from you.
Such a clear explanation, thanks!
Glad it was helpful!
This was a awesome complete guide for relay module❤ I want to add a arduino to one of my home appliance so Im using a relay. the power outlet voltage is 220 volt and also the machin input power is 220 volt too and as you know arduino input is 5 volt so I was wondering is it possible to use a voltage decreaser module from 220 to 5 volt for my arduino instead of using adaptor for the my arduino and adaptor for the machine and just plug the both at the same time with same power cord directly to the power?
If you have your mains voltage as part of the project anyway, I would use a mini PCB-mounted 5v PSU. I've used this many times and they are good if you use a good quality one. Hi-Link are OK.
They might do a screw terminal version (so not designed for a PCB) if you look hard enough.
Also check out "open frame 5v PSU" which I've also used with great success. These always have some sort of screw terminal but are larger than the mini PSUs I mentioned above.
Thanks for the hint about setting the relay HIGH before setting the mode to OUTPUT. I had been trying to figure out that no matter what I did, it insisted on activating the relay after being powered up or reset during a compile and load. I even tried it without sending a HIGH or a LOW and it still reset during the compile and load.
Glad the tip worked, Grumpy Old Man. Funnily enough, I learned this from Arduino guru Nick Gammon. I queried why he did it and what it was doing behind the scenes, and he explained. And here we are, some years later, with the tip being passed on! Thanks for posting.
Ralph, you have pointed out some important nuances with this little relay board others have ignored. Thanks quite a bit for your kind video. Well done!
Glad you liked the video ACADMan, thanks for posting.
Thanks Ralph for a very helpful video. It seems when using a 5v relay things are pretty straight forward and easy but, the problem starts going beyond 5v even 12v could still be all right since can use the VIN on Arduino but as far as I understand it and for the benefit of others you cannot use Arduino to trigger a 24v Relay. Although 12 is still ok feeding the Vin with an external 12v. Generally it seems triggers can only be done within 10% of the Relays Voltage rating as in min. 9v required for a 12v one and 21v to trigger a 24v relay!
Glad you found it helpful, Cy.
You can always trigger a higher voltage relay if you supply the necessary voltage independently, usually controlled via a N-MOSFET. So the Gate of the MOSFET is switched on via the Arduino (via a 100Ω resistor), the Source of the MOSFET is connected to GND and the Drain of the MOSFET is connected to one side of the coil with the 12/24v connected to the other side. All Grounds are connected together (common ground). A simple TO-92 outline 2N7000 MOSFET works well, usually.
Also now-a-days Relays mostly come with the bi-directional Optos meaning no JD-Vcc! A bad Idea since JD-Vcc and the external power option not only creates total Isolation but also much less current draw on the Arduino since it only needs to light an Led and has nothing to do with the relay coil which gets powered by the external source.
@@RalphBacon Yes quite so, but then you'd be making a relay of your own to get the ready made one going. It's just best for everyone to know, if they want to get an MU like Arduino to trigger the relay, to NEVER buy a relay with more than 12v nominal rating. That is a 5v one can be run with no other changes necessary and the 12v one only with connecting a 12v supply to vin and then, only the MU/Arduino input signal line connected to Arduino and everything else on both sides input/output must be connected directly to the external supply which should not be leass than 9v also feeding the VIN. The relays with J-D VCC more versetile/safer draing much less current from thr Arduino Vs. the ones without it(my 2 cents after suffering 2 days of searching and trial error to get a 12v relay without JD-VCC going). I often wonder, why the Cinese don't invest a few bucks to write a proper tutorial on their product, I hope it is NOT intentional?
Perhaps in Chinese, the phrase "invest a few bucks" is often misunderstood as "keep the costs down"?
Thank you. Very good explanation
Glad it was helpful!
Great video - very informative, thanks!
Thanks for your post, Broken Barrel Productions, good to hear from you.
Great video! Thanks 👍
Glad you liked it!
Great information, and well done! Thanks mate
Thanks for that, Ernesto Melo, nice to hear from you.
Can you just add a flyback diode at the JD part instead of removing the jumper? I don't have a separate 5v power supply.
If you don't have (or don't want) a separate power supply for the JD-VCC part of the circuit, Kayleb, just keep the jumper connected on the board, JD->JD-VCC.
Replacing the jumper with a rectifier diode (eg IN4004) might give a modicum of extra protection should things go awry in the "other side" of the board, but don't rely on it. That's assuming the voltage drop across the diode doesn't affect the operation of the relay (which I don't think it will).
Adding a flyback diode _instead_ of removing the jumper doesn't come into it here, I'm not sure how you see that helping you. Let me know!
Thanks for the video,
But I am a bit confused by the closing comments, you mention the protection diodes and that all you need to do is connect the Arduino to those inputs (easy),
But a bulk of the video was discussing the issue of jumper pin and the two circuits not being isolated.
So is it ok to use the Relay with jumper between VCC/JD-VCC and the Arduino powering the whole thing? Do the protection diodes protect the Arduino?
I'm sorry to have confused you; it was because I was trying to cover two points here.
You can connect the jumper between VCC and JD-VCC to power everything, then connect up to the Arduino as described in the video. Because the relay module is isolated as far as the GPIO pin of the Arduino is concerned (that opto-isolator) there is no need for any further protection. Note that the relay energises when the GPIO pin goes LOW (not HIGH as you might expect) because the current flows from VCC into the LED then into the GPIO pin then to GND.
If the Arduino were to ever power a bare relay (coil) it would need the protection of a diode connected across the coil contacts so that when the relay was de-energised, the back-EMF (potentially hundreds or thousands of volts) caused by the collapsing magnetic field of coil did not find its way back to the GPIO pin.
Regarding that VCC/JD-VCC jumper, if you were to power the relay module with an independent power source (and removing the jumper) it would improve the isolation as all the Arduino environment now sees is the LED within the opto-coupler. But it's not necessary to do this.
Have I confused you even more now, Andrew?
Thank you this has been a problem for my self I want to switch 2 relays on when I push a button but they must not switch when powered up those code lines will save me a large headache lol regards
You are most welcome Steve Edwards, I'm glad the video helped you. Nice to hear from you.
Great job! I will be referring to your site again!
Thanks for that, Jeremy dePrisco, nice to hear from you.
Great work
Thank you so much 😀
Hi.. I have a 12V power supply for the Arduino. The 5V from the Arduino I use for the output pins/ opto coupler diodes...for the Relais I use Vin from the Arduino Board... which is the 12V before the Arduino Voltage regulator.... This is not a full galvanic isolation but I think the Arduino is well protected this way...Do you think so too?
Yes, correct, Stefan. The Arduino is not totally protected but pretty good. Good enough, anyway!
Well explained, thank you.
Glad it was helpful!
Thanks immensely!
Glad you found it useful!
are nano d4 and d5 not able to sink as much current as the other discrete outputs ?
Those 2 outputs do not pull to 0volts, but rather 2.5vdc
On an Arduino Uno and Arduino Nano D4 and D5 are just normal GPIO pins and should pull low in the usual manner. Here's an official schematic: bit.ly/47YFExC
But why HIGH - is turn it off, while it sent 5V in circuit, and LOW turn them on, while on circuit is just few mV?
Because we are SINKING the current from VCC through the device and then through the Arduino to ground. So the output GPIO pin has to be LOW to allow the current to flow in that direction. Make sense?
Is there anyway u can tell me what’s needed to accomplish a simple task? So , I have 2 sets of 20 header pins, let’s call them source 1 and source 2. And I only have 20 pins as an output. I want to use a physical button to select between which source i want to use, either source 1 or 2 but never both at the same time. So in theory is basically a 20 pin ribbon cable switch so to speak with 2 separate sources and 1 20 pin ribbon cable out so to speak, with a switch to select which source I want. Thanks any help would be appreciated
I'm guessing that a mechanical switch for 20 cables is out of the question (it would be for me).
I would consider using a 20-way (or 2 x 10-way) digital switch IC. These are integrated circuits that switch signals (only signals, no high power stuff) from one source or the other.
Purely as an example, here's a link to a 4-way, DIL switch so can see what I mean: bit.ly/digital-switch-IC
You would have to design a custom PCB to make best use of these switches with a socket for the ribbon cable connector - very do-able but worth researching (and perhaps trying out a smaller switch like the one above first).
Depending on your soldering / PCB design skills, there are ICs in DIP format ( through-hole like above) or SMD QFP (square) with much higher switch counts.
So, what if you want to only use 1 power supply but still have the protection of isolated circuits? You can't really be creating devices with multiple power supplies. Can my external power supply be made to supply power to both the arduino and the relay module? Would I need to add in some sort of additional protection circuits to divide them?
If you use the _same_ PSU for both parts of the project then they are not isolated. You _could_ use the same primary transformer with separate secondary outputs (eg 2 x 5v output) to provide better isolation but, frankly, dual PSUs are the only way to give total isolation.
This has given me a much better understanding of the relay module and how to use it. Good stuff. Great video detailing and well explained. Thanks for the details.
Can you offer any advice for me please- I want to use two numbers of ON / OFF or push switch (one each for relay) two do jobs separately. Could you please give a sketch for the same and code.
Thanks in advance.
Sushil Damle
I'm happy this has given you a better understanding of relays in general, Sushii, but I'm not able to write bespoke sketches for you!
That part of the job is down to you. Break the project down into manageable chunks and think about how they might interact. Only start coding when you have the design at least "working on paper".
@@RalphBacon Thanks for your guidance. I have tried doing the same, but I found that the turnout used to be energized where in I do not want the same as there is every possibility for the turnout getting burned out. Once I pressed the button track should change from Left to Right and when I either press the same button or have another button, when pressed track changes from Right to Left. The track should be energized only for the duration when button is pressed. Your advice please. Thanks once again.
When you press a button the pin will go HIGH or LOW (depending on how you have wired the button up). Whilst it is that state, the output (keep checking in the loop) keep the output HIGH. When the state changes then make the output LOW. Try this with a very small sketch and an LED to prove you can do this. Let me know how you get on.
This is good exactly I needed...thanks
Glad it was helpful!
i am trying to switch between 2 batteries of 12V and 80A and 12V 20A, i am confused in choosing a relay. Would you recommed me which type of relay should i use?
thanks
It depends on how much current you will draw _in your circuit_ . If the circuit only draws 2A then a relay rated for _at least twice_ that (so 4A+) will be fine.
But if you intend using anything much higher (eg 16A) then be very careful that you find a relay that can handle that much continuous current. I always *double* the rating to be safe.
On my 16 channel relay, I removed the ground connection from the Arduino to the relay, so the ONLY connections between the Arduino and the relay board are the IO Pins. Everything works fine now, and no more shorting out :-). I obviously have a separate power supply to control the relays, and it has a ground connection. I wonder where the current flows from the IO pins, when they are set to HIGH? Does the current flow from one pin set to HIGH to another pin set to LOW? If that's the case, am I always required to have at least one pin set to LOW?
OK, let's figure this out. The ONLY two connections you need to get this to work are the VCC (+5v) from the Arduino and a GPIO pin configured as OUTPUT. So the current flows from the (Arduino's) VCC through the opto-isolator LED pin (IN1) and back from GND to the Arduino's GPIO pin set to LOW (to let the current flow - set it to HIGH to stop it). But...
But if you are also powering the relay from the Arduino's 5v supply, then you need that jumper in place (to join VCC to JD-VCC) and that GND (ground) connection (back to the Arduino) too. This is good for testing it all hangs together.
If you are powering the relay side of things independently then you need to remove the jumper, and connect the independent power supply to JDC-VCC and ground. The ground does NOT need to go back to the Arduino. This is the best option as you're then totally isolated.
Not sure if this clarifies things or not, Mr RisingNShouting? Let me know!
Ralph S Bacon
Yup, that helps a lot! Thanks Ralph, you're the best!
If the pin is set Low at the point of being set as Output, and you want to avoid this, shouldn't the pins be set High AFTER the Output command (lines 10&11) in the code, rather than before (lines 7&8)?
It doesn't really matter whether you set the pin's state (LOW, HIGH) either before or after the "pinMode" command.
But if you set it _after_ the pinMode you may get a tiny (1μS) pulse which you might not want. The guru Nick Gammon once told me that setting it _before_ was in fact a bit safer.
@@RalphBacon excellent, thanks for your reply.
Hi Ralph., do U have a video, where U send signal to ESP32/8266 , to which is connected relay, and another ESP32 has a microphone, and listen to voice commands ? I need to program something like this, to get rid of DC cables from battery to switch.. Cables will be only from battery to LED.
Voice commands requires a lot more processing than what the ESP32 can ever give you. You would need, I would think, at the very least, a link to a cloud service (such as Amazon's Alexa service, which is long winded by easy enough to do) for this all to work.
@@RalphBacon Thanks, bud I have to do it myself I do not want cloud, a lot of work ahead for me to do it.
If you wanted both relays to be off at bootup, would you set the pins to low before using pinMode?
Yes; Nick Gammon, Arduino guru, once told me to set the pins LOW before changing the pinMode to avoid the tiny pulse that will otherwise appear on the relay pin, which might cause the relay to chatter.
Thanks for a great Video
Glad you enjoyed it!
Excellent video - thank you. I notice on some coding for relays there is a line in Setup - Serial.begin (115200); . Can anyone tell me if it's important when operating these relays please?
That line has no bearing on the relay part of the sketch, John. What it is telling Arduino is that the speed of data transmission from the Arduino back to the PC/Mac (in the Serial Monitor, aka debugging window) is at that speed. So you can then send information back like this:
Serial.println("Relay ON");
I tend to use 9600 which is a bit slow but very stable!
Hi
I think I have that problem with my ESP32 board, I have interface it with the relay after some times like 2 minutes of working nicely, it stops working at all until I have to disconnect to power supply (I supplied it from my computer) and I reuploaded the code. Tomorrow I will test your method and see.
Thank you
Be careful with the ESP32 GPIO pins, they are 3v3 and might not be enough to trigger the relay.
Also, you should be monitoring the serial output of the ESP32 (at 115200 bps) as it will tell you why it's crashed (eg Panic, or Brown Out Detected).
@@RalphBacon I succeeded to interface that, guess what I it works well. Thank you for the tutorial:) 😍😍😍
If i use same connection in esp 8266 with 8 channel relay board on which pin of esp 8266 i hv to i hv to connect vcc
You must be careful with the ESP8266 as it is a 3v3 device.
The module you have will probably have a 5v pin which can be used for the JD-VCC relay supply (if you are not using a separate power for that side of things). For the opto-coupler LED you can use any pins (choose ones that aren't PWM-capable or that is used for I2C or SPI) connected to the IN1 - IN8 pin.
If the current limiting resistor (R1 in my video) is not already on-board choose the wisely. If you are using a 5v supply as VCC then limit the current to 10mA so your resistor will be about 200 ohms (you have two LEDs in series on that board, one embedded in the optocoupler and the visible one). If you use 3v3 your resistor will be about 150 ohms but there is a possibility it won't work at all at 3v3 due to the LEDs voltage drops. Try it and see!
Remember that the relay switches ON when the digital pin connected to the INx pins is LOW (your µC will SINK the current from VCC via the current limiting resistor).
Can the alternate power source be something other than a breadboard power supply? Maybe something a little more friendly for a portable application?
The power for the relay (if you are using a dedicated, separate power source for the relay) can be anything you want - but it will need to supply power for as long as the relay remains energised.
For portable, battery powered devices you really need a _latching_ relay that switches to the open (or closed) position and stays like that even if you remove the power. You can then switch it back again (via a different coil pin) the same way and remove the power. I hope this helps. Portable devices don't really need an isolated relay anyway, so you can power it from the same supply as the µController.
Your video was very helpful.Thank you : )
It makes me happy to hear that, woopscoop! That was video #18, there are now more than 100 videos on this channel so I guess you will enjoy many, many more of them as time goes by! Thanks for posting.
Great demo. Can you help me? I have a MT3608 step up converter. I want to increase my output power from the arduino uno to ignite an electric match for my rocket parachute deployment. 9v battery is my power source . Can I power the MT directly from the battery and use arduino as a switch only? I used a blink program to send power to MT3608 thus increasing voltage, but not amps. ??? Any diagram help would be great.
If the 9v can ignite the match, Mark, then the Arduino can connect the 9v into a totally separate electrical circuit via the relay. Rough circuit here:
www.dropbox.com/s/h1bvuntfivrmw5d/9v%20match%20circuit.png?dl=0
Do watch the demo explanation in conjunction with that circuit.
I'm trying to make a water pump trigger. Would I put the relay before or after the step up? Anyone know? So if water hits a certain level start a 12v pump.
I'm confused about the "step up" bit in your question! The Arduino GPIO will trigger the relay on and off, but the 12v for the pump is controlled by that relay (and never connects anywhere to the Arduino side of things).
Can you clarify the "step up" part of your question?
So, why not drive the output HIGH to activate the optocoupler.. [Reverse the polarity's]
Because the datasheet suggests that the LED inside the optocoupler is connected to +5v and the current sunk (sinked?) via the μController.
However, as the terminals are totally isolated I suppose you could drive the LED from the μController (instead of +5v) and connect the other end to GND, that should work too. But do check what the manufacturer of the board has done, they might have made assumptions on how you're supposed to connect it up.
This video is very helpfull thank you sir:)
Glad it was helpful!
my realy is good but when i connect the realy with aurdino it not working what should i do
Follow the example in my demo _exactly_ and it should work.
Thanks very much, very helpful. One silly question I have though, is seeing as I'd like to keep my 12V source (deep cycle agm battery) fully charged, if I were to keep the trickle chargers (12V 1A from 240 mains adapter) cables attached to the battery, and activate a 12V 5A draw via the relay, will the charger/adapter pop, or will the battery deal with the draw? Please excuse my ignorance!
If you are _directly_ charging the battery from a charger the the current for any circuit will come from both battery and charger, depending on which has the higher voltage. If the charger can only supply 1A (at which point the voltage will drop) all the "missing" current will come from the battery. It would be better to use a dedicateds battery charger circuit (like the ones for LiPOs) that allow charging and powering a circuit without anything getting stressed. Check out solar panel charging modules.
@@RalphBacon ah like an MPPT controller? Thank you kindly for the reply too. Best wishes
I think its an output pin because you are writing to it, not reading from it.
edit: I'm talking about at 3:36
Can you just put this into context, please, Ryan? At which point in the video does this come up? From what you say if you are writing to a pin it will probably be an output pin - but if you write to an input pin it just turns on the pull-up resistor!
@@RalphBacon 3:36
Right, the point I was making at that point in the video was that although the current flow was coming IN to the Arduino (from VCC and the diode), the pinMode was still an OUTPUT - something that confuses beginners.
Do we need separate ground too ? On a 4 relay modul there is no ground pin next to vcc ! Then we have external vcc on jd-vcc pin and ground on to the gnd pin on the input header.
If the relay board is controlled by optocouplers, Jean, then you do *NOT* need to connect the grounds together. Why? Because the Arduino (or whatever) is effectively just lighting up an LED between a GPIO pin and it's own ground (via that optocoupler). End of. The relay board now does whatever it needs to, by using its own, entirely separate power supply. Fantastic isolation between the two parts. I hope this is clear, if not let me know. Thanks for asking a great question.
I had been using an LED on my breadboard to represent the optocoupler and set the ledState to HIGH for 'on' and took the other pin to ground. Would that not work as well?
To turn the relay on, you need to SINK the current. So the pin needs to go low. The current flows from VCC thought the diode into the Arduino and then (internally) to ground. Is this what you're asking? If so, you can model this on your breadboard the same.
And what do you do when you have only one 5v power supply? Then it means no isolation is possible, right? Maybe a DC to DC isolator will do the trick?
What is happening if I take out the jumper and I split the power supply so that 5v -> jdvcc, gnd of the power supply -> relay module optocouplers gnd and on the other pins, VCC from 5v+ Arduino pin, inputs from Arduino outputs and gnd remains unconnected. Arduino also is powered by power supply
Well, it's not so bad, Marius, if the relay board is powered by the same 5v as your Arduino, it's just that the level of isolation is reduced. At least this board gives you that choice. And, don't forget that the actual signal path is always isolated. Basically, it's risk vs convenience that we just all weigh up. Good point, thanks for posting.
I have a similar board, but is a bit different. It does not have the troublesome ground next to JD-Vcc and Vcc, just the latter two pins with a jumper, which I have removed. When connecting my independent 5 Vdc power supply, do I use JD-Vcc and the ground pin that is next to the relay inputs? Thanks
That's right, Steve. The independent power supply (for the relay) goes to JD-VCC and the ground is common so must go to the GND pin on the board. If you have a multimeter you can prove that JD-VCC is not connected to the Arduino board at all.
@@RalphBacon Good idea; thanks for your support!
Too late, RIP my ESP32 will be missed i ended up shorting VCC and GND :(
Maybe not. Shorting out those two pins would overload the AMS1117 voltage regulator but your ESP32 should survive (the power doesn't flow through the ESP32). If you can find the AMS1117 (might be an NCP1117) you could replace it if your SMD soldering is any good, or just attach 3.3 volts to the VDD pin. If it springs back into life you must somehow replace the voltage regulator as that also receives 5v via the USB plug to generate the required 3.3v (or just keep the 3.3 v supply going). Worth playing about with.
Can someone help in my project?
I used coin sensor and interrupt pin in arduino to catch the pulse that coin sensor sends. Everytime the relay clicks or goes from low to high, the arduino interprets it as the pulse of coin sensor. Any advice?
Has the relay got a reverse voltage (aka back EMF) protection diode across the coil? You might also need a snubber across the relay contacts to prevent a pulse being generated, more details in video #131 ua-cam.com/video/OAmXypXwkeg/v-deo.html
@@RalphBacon im using the same relay module you used in the video
do you have a tutorial where the relays could be triggered through a humidity sensor depending on its readings?
+Reptile_Edge
I've no specific video on that, but if you're using something like a DHT11 humidity sensor then it's as simple as reading the data via its one-wire system and triggering the relay at a certain humidity point. No different to using a DS18B20 temperature device in its implementation really. In fact, the DHT11 returns both humidity and temperature together. It just uses a different library.
If you look at the Arduino playground website (playground.arduino.cc/Main/DHT11Lib) there an example of how to read that device. All you then need to do is turn ON or OFF or relay at certain humidity/temperature points.
Well done.
Thanks for your post, Jeff Bluejets, good to hear from you.
Very useful thanks 👍
Glad to hear that.
Great video!
Thanks for your post, Cedomir Valcic, good to hear from you.
So this might be a stupid question but when using the external power input on the relay module, can you use the same power supply, like the breadboard power supply you're showing, to power the Arduino as well or does this defeat the purpose? It would be nice to have one psu powering both devices because it saves some space!
Not a stupid question at all, dikke j. You can indeed use the same 5v source to power both Arduino and the bare relay (coil). Either drive the relay coil _directly_ via a transistor from a GPIO pin (using a protection diode across the coil), or use an opto-isolated relay as I showed here, which protects the Arduino (and you) better - and arguably is easier to use!
@@RalphBacon Awesome, I thought it would be possible but I just wanted to make sure since this is all new to me. Thank you very much for being so helpful :)
Awesome video! Does anyone know where i could get more info or paid online training on how to write code for arduinos and relays
There are doubtless many, many books on the subject, as well as videos, not least on this channel.
However, the best way to learn is by doing, tweaking and understanding. You should at least be able to understand each and every line in your sketch - even if it's written by someone else.
Start small, build on that. If you try and design the equivalent of NASA using an Arduino UNO you will become frustrated and eventually give up. My list of videos:
bit.ly/UA-camVideoList-RalphBacon
Hi, i would like to know if the 24v rated relay modules could be triggered by a 24v signal or the 5v signal is a must .
Thank you.
There are *two voltage ratings* for a relay, Ionut. The first is the COIL voltage, and determines how much voltage you need to trigger (switch) the relay on and off. The second voltage/current is how much current the relay can carry. So a 5v relay with a rating of 24V/5A means that it requires 5V (no more) to trigger it, and can carry up to 24V at 5A maximum. Don't forget the protection diode across the coil, which will be present on a module board like I used, but you will have to add one if you use a bare relay. Is this clear?
@@RalphBacon Thank you for response although i'm not sure i did put the question the right way in order to get the right answer. For example the relay module is this www.ebay.co.uk/itm/1-channel-24v-relay-module-board-shield-for-arduino-with-optocoupler-ZT/362992628401?_trkparms=aid%3D1110001%26algo%3DSPLICE.SIM%26ao%3D1%26asc%3D20190920091355%26meid%3D0afe53015d8d4b729b7d7ba4cf4e6a36%26pid%3D100036%26rk%3D2%26rkt%3D7%26sd%3D402261796421%26itm%3D362992628401%26pmt%3D1%26noa%3D1%26pg%3D2047675&_trksid=p2047675.c100036.m2109 . Not talking about load rating. So, all i need to know is what voltage will require the input pin to trigger the relay on/off via optocoupler. Are the 24v coil modules built to be managed by 24v inpun signal, the 12v coil modules by 12v input signal, the 5v coil modules by 5v input signal or all the relay modules no matter the coil voltage is, are built to be triggered by an input signal in range within let's say 3.3v - 5v ? I'm talking strictly about the way they come from factory, about what range of signal voltage they can handle safely. I want to know if to this 24v coil relay module i can apply 24v as signal input without frying the input zone of module. I hope i made myself understood and i apologize for giving you such a headache. It's all because my bad english. Thank you again.
It's not your English that's a fault here, Ionut, just the rubbish documentation on these products! I can't even see the optical isolator, on the board you link to. I suspect the 24v refers to the JD-VCC that goes to the optoisolator (see my video at about 01:58). All the optoisolators require you to SINK the current from VCC (NOT JD-VCC) to ground. I would carefully wire this up to 5v first and test this out as per my demo. Then you can ramp up JD-VCC to your 24V and hope it works. this site has better pictures: www.ebay.co.uk/itm/5V-12V-or-24V-1-2-4-8-16-Channel-Relay-Module-Arduino-PI-ARM-AVR-DSP-PIC-PLC-UK/362951292175
The 24v relay switching voltage and the VCC for the optoisolator are separate - you don't want 24v anywhere near your Arduino!
@@RalphBacon Thank you for response,. i'm sorry i sent you the wrong link, in fact the relay i bought is exactly from the link you sent to me ( the 24v - 2 channel relay module). My first thought was that if the JD-VCC and VCC are bridged by jumper and board being supplied by 24v, the input pins could accept 24v as signal. I don't use arduino, this relay will control the spindle of my cnc router which controller has the output pins providing 24v signal. In other scenario i could remove the jumper to feed separately the relay coil with 24v and for inputs zone i will provide the 5v TTL from breakout board . I guess and hope this way will work.
Thank you.
Why do you connect VCC and then set the pin to HIGH to keep it closed, instead of connecting the ground and setting the pin to HIGH only when you want it opened? I read some comments saying you can only switch on relay at a time like that, but I'm not sure I understand why... Is there something I'm missing? I have my relays coming in tomorrow and don't want to blow up my ESP8266 hahaha
I don't fully understand the question, or what you think I'm doing, but I can tell you the Esp8266 will SINK the current for the LED on the relay board's optoisolator when set LOW (HIGH is off). So the other side of the LED is connected to VCC. Take a close look at the circuit diagram in the video. Still confused? Let me know!
@@RalphBacon Thank you! My parts just arrived. I'll be trying to figure it out this weekend :) will definitely revisit your video then.
@@RalphBacon Ahhhhh now I understand!!! Took me a bit of tinkering, but your video makes total sense now! Thanks!
Great stuff! don't let the magic smoke escape!
After watching this video, I'm more confused about relays than I was before. Muckery...
Oh dear, Doug, that wasn't the intention at all. If you can specify the areas which confuse you I'll do my best to untangle confusion and provide a clear path forward...
Can we link those relay to our car starter motor?
Good grief, no! Your starter motor will take many, many amps to turn the engine, far more than this relay will handle. You need a proper automotive part to do that.
Great info! Might save me killing on of my boards.
Oh no! Don't kill your board. I can't condone any cruelty to any board. Be kind. It will then work again because it loves you.
@@RalphBacon I won't! I'm always careful !
hey Mr Ralph can you please help me with my project here about 2 sensors , LCD, RELAY MODULE AND ARDUINO ITS my first time using them.
You need to start with some simple things first, Khulile. Try getting an LED to flash (and know why it's doing it that way). Then move onto the relay module and get that working. Then do the sensor... the only way to succeed in a project is to break it down into small, manageable chunks. That way you learn how to do each part and can build upon your knowledge. There are plenty of videos on this channel for beginners like you, but you must walk before you can run.
I can't find 30a relay modules with with seprate power pins
Can anyone suggest one?
Depends where you are in the world, Alex. In the UK try RS or Farnell. In the US I'm sure Mouser or DigiKey must have something?
Thanks
No problem!
Thanks for all tuto!
+raillersing Hirina You are most welcome! I hope it's helping you get your Arduino project(s) further forward. Remember to spread the word and, if you haven't already done so, please subscribe so you don't miss future videos.
Hi!!Can you explain the variable type boolean...I dont understand it and I have not found tuto about it. I saw it in the book: Exploring Arduino. My problem is: I have never seen this in language C. I can give you the book if you give your email.
Sure.
A boolean variable (bool) can be either TRUE or FALSE. For more details see here: www.arduino.cc/en/Reference/BooleanVariables
thanks
Your video was great however, in spite of me following you my 4 channel relay (which appears ok) doesn't seem to switch. I don't hear anything happening but the led is lit. I trying to turn on a 12v pump which is prompted by a moisture sensor. I've been struggling forever with what should be a simple task (according to your video). I would be very much in your debt if you are able to advise me what I might be doing wrong and help me out.
Sure Danny, let's see if we can crack this.
1. Firstly here's a good circuit diagram of what is happening: electronics.stackexchange.com/questions/196139/why-is-designed-active-low If you don't understand this, don't worry, it's not essential but it would help you if you tried to follow the circuit.
2. So when you connect your Arduino's 5v pin to the VCC (but REMOVE the jumper for now) and connect the relay board's IN pin to the Arduino's GPIO OUTPUT pin that you are using to control the relay, when you set that OUTPUT pin to LOW the light will come on. When you set it HIGH the light will go off. The relay will not click. Get this working FIRST.
This is because the current is flowing from the Arduino's power supply (5v) via the relay board's optoisolator LED, out via the IN pin (this should be called OUT not IN) to your Arduino and hence, internally, to ground.
3. Now, if you ALSO want to power the relay with the same 5v from your Arduino, attach the jumper from VCC to JD-VCC, the Arduino's 5v to the other VCC pin, and the GND pin on the relay module to the Arduino's GND pin (there's more than one GND, any will do). Now when you set that GPIO pin to LOW it will light the light AND pull-in with a "click" that relay.
4. If this works all well and good but remember that you COULD isolate it better by removing the jumper, connect the JD-VCC and GND pins on the relay to a SEPARATE 5v power supply. That way, the two parts of the circuit are totally, 100%, separated. But it's your choice!
Now, which bit of this works and which bit does not?
Understood. Running a test program - channel connected on then off for 1 sec , relay powered from separate 5v supply, ground not connected.
The leds on the relay flash 1 sec as expected but no sound from the relay.
Is it DEAD!?
When I first had it and ran a test there was a clicking sound - no more. I suspect I've fried it.
Thanks for detailed reply - immense help.
I followed you steps 1 - 4.
Step 2 - worked as you said - great!
Step 3 - again worked as you said.
In fact a 4 channel relay I thought I had destroyed came back to life. I now have a collection of relays!
Step 4 - Not so lucky here.
Relay LEDs poorly lit. (4 channels). Following a test sequence HIGH/LOW on each of the 4 relays. The relay being activated becomes slightly brighter and the remaing 3 still remain lit although very dimly. The leds seem to indicate that the relay is working but it isn't if you follow me. Still no sound from the relay.
I put a meter across the the voltage supply - 4.95v. Is it enough? so I increased the supply to 7.5v - no improvement
I reverted to Step 3, attached some components to the circuit I'm building. The relays ceased to function...
I am trying to create a control system for my greenhouse -
1. Overhead water spraying activated by moisture sensors water via 12v submersible pump.
2. Air bouyancy via 2 x 12v dc Fans. These will be initiated by a DS18B20 sensor and also PWM (hopefully)
3. LCD 20 x 4 for visual readout of all the above.
4 MCUs either UNO or Nano (preferred) Sketch developed by myseld (not very elegant I suppose).
The moment I attach modules to the relay it doesn't work. It seems to drain the Uno or NAno. The fans startup but continue unabated. I susupect they are drawing power from the MCU
I've been at this for a month now and am beginning to doubt myself and my capabilities. I done other diy electronics successfully but this is beginning to make me miserable.
I know it's a lot to ask, but, I hope you can help me out with this nightmare.
Thanks
why is there a shortcut between JD-VCC and Q1?
Now, Alexus, when you say "shortcut" between JD-VCC and Q1 I'm assuming you mean the VCC to JD-VCC jumper? Remember that the JD-VCC is the power to the "other side" of the circuit - that is, the transistor (Q1), half of the opto-isolator and the relay itself. By shorting JD-VCC to VCC we power the entire board with power from VCC. Not truly opto-isolated, which is why the option is there to power the "other side" independently". The video shows the schematic at 02:07 which makes this clearer. This is then covered in some detail in the video when I power the circuit from two sources. Perhaps I have misunderstood your question so do let me know whether I have wrong end of the proverbial stick!
Sorry i mean "short circuit" not shortcut...
At 2:07 between the wire JD-VCC and the collctor of Q1 is a wire-bridge, wich is parallel to the free-wheeling diode and the relais.
What does this wire do? A Short circuit? :D
How strange. Not your post, Alexus, that was very observant indeed. The circuit diagram for this device is basically WRONG. And only you spotted this! Obviously there should be no link between the collector of Q1 and JD-VCC (not unless you want the power to be shorted to ground as soon as Q1 was switched on). So basically the collector of Q1 should go to one side of the relay coil and the anode of the diode, which are shown correctly. No connections between the collector of Q1 and JD-VCC. I just cannot believe that (a) I did not spot this when I did the video, and (b) that in all that intervening time no-one else has mentioned it (and the Arduinites on this channel are a pretty observant lot). So thanks for posting, I shall make a small comment in the video description about this (and credit you, obviously). Thanks for posting.
Thank you for this post. I was thinking the same thing. Good pick up Alexus
If i understood correctly with external power supply on gnd and jd-vcc, module need vcc from a board for in pins to make a circle. And if board works on 3,3v than vcc MUST be connected with 3,3V and not 5V external (say)? Otherwise 3,3V board could be damaged?
Just to be sure we both agree on what is required, Dejan, let's summarise:
1. The power from your Arduino goes to VCC
2. The "IN" pin goes to the Arduino output pin that SINKS the current when LOW (it should be labelled OUT pin, really)
3. If you choose to supply 5v to JD-VCC and do NOT use the jumper then it will work as expected
4. If you DO use the jumper so that VCC and JD-VCC are joined then you must ensure that the voltage to your Arduino is the expected voltage of 5v.
5. If you DO use the jumper and are using a 3v3 board then you must reduce the voltage (from VCC) down to 3v3 before it reaches the Arduino.
Just remember that the Arduino (or whatever board you use) is SINKING the current from the IN pin. This image is very clear: electronics.stackexchange.com/questions/196139/why-is-designed-active-low
I hope this helps. Good question, thanks for posting.
4-CH relay module don't have separate GND for external power so with 3,3V board connections should goes like this:
1. GND should be connected with GND on board and external power supply GND (all three)
2. IN pins on board digital pins
3. VCC on 3,3V board pin
4. JD-VCC on 5V VCC of external power
This way board should be safe from and IO pins will work on 3,3V, and relay on 5V.
?
No, don't connect the GND of the external 5v to the Arduino, it doesn't need it (not unless you are powering the Arduino by using the jumper). All the Arduino needs is the VCC pin connected, so current flows FROM the Arduino, through the LED and back in via the IN pin GPIO (where it will flow internally to the Arduino's GND). By keeping the GND pin purely for the external power supply you have totally separated the two supplies (unless you use the jumper). Make sense?
Just tried. Yes.
Thank you.
Glad it all worked out for you, Dejan, makes it all worthwhile!
can i use high trigger or are all relays low trigger? lazy and dont want to rewrite code ha
Many require the GPIO pin to go LOW (to _sink_ ) the current, but I have a couple that have a jumper and allow high OR low usage. I bought them from Amazon as I was in a rush but, frankly, the prices are sometimes equivalent to the Far East markets, once they have added on VAT and postage. Here's a link: amzn.to/49rzrL5
@RalphBacon I had done a little more research since writing the comment and found ones that can do both.
Your videos are great BTW, used one to learn to code an arduino to work a solenoid hydraulic valve block for a piece of farm equipment.
Now to go from code and simulation on the breadboard(which was useful, damn button bouncing!) to a prototype pcb for a controller. Any advice for a total novice?
a somewhat common mistake is using a mechanical relay when a solid state relay or device is a better choice;
1) fast switching speeds (on/off/on)
20 longevity
3) fast turn-on
4) High voltage or current loads
5) need to have minimum current drain
But you did miss one thing, Kenn, that is not so good: HUGE size (compared to a mechanical relay) especially if we factor in the ridiculous heatsink for higher currents. I have...
I have resisted doing a video on my SSR precisely for that reason. It certainly would not fit in the original consumer unit (fuse box) so it would have to be mounted externally, suitably insulated in a box which then requires drilling etc.
Some relays are fakes, of course. Rated at 20A and can barely conduct 5A without melting (as other videos I've done have shown). But I've sourced (from a respected UK company) two very nice, high current mechanical relays, with all the correct CE certification that would do the job. But, I'm still thinking of that SSR... What do do!?!
@@RalphBacon :) to be sure relays have their advantages......including zero loss to the load, multiple poles, isolation .....
@@RalphBacon personally I'd go with the SRS!