I was looking at a bunch of different tutorials online for an auto plant watering system to get an idea of how people architected them and I came out with 2 questions.
Every single tutorial that I read uses a relay module... why? I don't understand the purpose. Can't I just hook up a 5v DC water pump directly to the arduino and control it by setting the pin to output and using HIGH/LOW?
The moisture sensors used all have a "middleman" PCB, that appears to have a potentiometer. I assume this is to control sensor sensitivity? Same question, couldn't this also be done with code by reading the raw input?
Bare with me, I come from a software background and am relatively new to the hardware world
I have a separate project I'm working on using a micro servo motor that I connected directly to the arduino and control in that way (which is what made me question this in the first place)
Could you please elaborate on why this caps around 20mA (I believe the servo draws more)
And how the relay module fixes that?
An interface is often needed to protect the controller from external harsh electrical environments/signals.
Often a voltage other than Arduino 5 volts is needed to get sensors to function; in cars the sensors are 12v so an interface is needed to get down to the Arduino 5v levels.
Far away sensors need special signal conditioning to reach the controller, fibre optics, current loops, RS232 ±, SPI, I 2C, and many many more.
This is the exact answer I was looking for with this part of the question, thank you! Seems the answer is very situational, but more often than not an interface WILL be needed.
This was designed for many applications like you’re suggesting, but every sensor, valve and solenoid/ motor has different characteristics.
You could cut corners to realise a one-off solution, but as soon as you replace a valve, or use a different part - you’re almost starting again.
To make the unit compatible with as many user options as possible, the relays ‘isolate’ the low power electronics from the unknown requirements of the driven equipment.
Same for sensors, which often operate with and require voltages not used by the controller’.
Of course, there are a lot of other features which are ‘free’, because they’re ‘only software’. It takes a bit of thinking to get it right when the equipment is unattended, running 24/7 in what might be hostile environments, and must work when expected.
An IO pin will be fried at 40mA, and 20 is the safe maximum it can sustain, say a very bright LED. You won't be powering a servo from an IO pin though, only controlling it: the control current is very little. But it's also not good practice to use the Arduino 5V as a "power supply"; servos even micro servos should really have an external supply.
What a relay does, is switch the higher current from a decent supply, with only a very low current from an IO pin to control the relay. So the high current to the motor comes nowhere near the Arduino.
So between your answer and the other one I received about anything >40mA frying IO pins, would you say the reason is a mixture of "best practices" for generic compatibility and that, in this case, finding a micro water pump that would be capable of being powered directly by the arduino is unlikely?
There are three powers in action with a relay module
The power consumed by whatever is being switched- in your case the pump. That definitely needs its own supply, of whatever voltage and current the pump's spec lists.
The power consumed by the relay coil when it's energised. That depends on the relay; those blue Songle style ones are often 5V and sometimes 12V. If I recall from checking before, they need about 70mA, but you will need to check. That 70mA at 5V can be supplied by the Arduino 5V- I do that for testing one module- but you would probably not want to power more than one. You also need to conside electrical isolation as a safety thing: powering the relay coil from the Arduino circumvents any isolation built in to the relay module. Look for those ones with the "JD" connection which allows proper isolation.
And last, the power to control the relay module. Modules usually (or perhaps I should say often) use an opto-isolator to do that, and that's literally just an led in a chip, so well inside the 20mA IO limit provided you use the right resistor.
The best practice is to start with a block diagram showing all componets and their interfaces. Thus let you to find out all system requiremts for your smart watering system.
Have a nice day and enjoy coding in C++.
The Arduino can only handle a small amount of current. The motor you are trying to drive will need much more than this... so if you try to drive it directly you will likely damage the Arduino. A relay acts as a switch... the switch is turned on & off using a small current (from the Arduino)... but the switch itself can handle much more than this.
Even the relay low current circuit can sometimes need more than the Arduino can supply. For that reason, most of the ready made relay modules also come with some additional circuitry to amplify the signal current, plus isolation/protection against other potential electrical issues that could damage the Arduino.
While the logic circuits operate at 5 V (or 3.3 V), you are more likely to find a pump that uses 12 V, Even so, it will likely require a substantial proportion of or more than one Amp and when dealing with motors, you need to consider the actual "stall current" which is the current the motor draws when it is not moving, either because it is somehow jammed, or as should be obvious, the brief moment you first apply power to it.
So you need to control that substantial current using the 20 mA available from an Arduino (which is to say, the more basic ones such as the Nano because many later ones or alternatives have even lower ability to control current).
A relay is one option, but even the commonly available ones require something such as 90 mA at 5 V, so you actually need to use a transistor to control the relay. This is most easily and practically done using a ready-built module containing the relay, transistor, protective diodes, resistors and often an opto-isolator to separate the relay circuit itself from the Arduino controlling it - for various reasons. Note that now the relay itself require a power supply (of that 90 mA or so per relay) to operate, separate from the Arduino.
And as mentioned, the Arduino does not supply power for anything. You always need a power supply for the specific purpose, a regulated 5 V supply for the Arduino and for typical relay modules.
However, some sort of transistor could actually switch the pump instead of a relay. Nowadays, the proper sort of "transistor" for this purpose is a FET, and you need one which is specified to be adequately controlled by a "logic level" of substantially less then 5 V (or even 3.3 V).
I am making no suggestions - nor should anyone else - as to exactly what FET you might use until and unless you fully specify exactly what pump you would be using.