In several projects one big task is the control of a DC motor, and by "control" i mean start/stop and switch between cw and ccw movement and to proper use some endstops.
I saw several ways to approach this probelm, some are clever, some aren't so smart. Since i mostly saw "less than optimal" solutions i decided to write this tutorial.
First: as long as the motor is "small enough" the cheap and easy solution is to use an H-bridge transistor based control module. The most common devices are the Arduino's "Motor Shield" (amazon, banggod) or the L298N module (amazon, banggod), which is the same concept of a motor shield but its control pins are designed to be more accessible.
Second: this task becomes harder as the motor becomes bigger, at some point the required current can't be managed by the most common h-bridge modules. Moreover most transistor based H-bridge doesn't provide galvanic isolation (even if it's theoretically feasilble it's just not a feature of the most common h-bridge modules), and this could also be an issue for your project if it's sensitive to electrical noise.
Then what should you do to a big DC motor?
Step 1: First Solution (bad)
The first solution is somehow suboptimal but still it's the usual solution that you'll see surfing the web.
Several projects use four relay to build a mechanical copy (relays are mechanical switches) of a transistor H-bridges. Long story short: the four NO connections (NO means "normally open", the relay's contacts that don't conduct current when the relay is not active) are set up as the conceptual H-bridge switches.
While the concept is theoretically valid the practical application isn't so effective.
The main issue is that the H-brige requires a precise control of its switches: you must first open a switch and only after a little delay close the other one. If you do the opposite bad things will happen because of an inherent issue. For example: lets pretend that the top lef switch (S1 in the image) is closed. In this case you can safely close only one of the switches on the right (S3 or S4), for instance we can assume to have the S4 closed, so the motor is spinning in one direction. In order to change the spinning direction you have to open S1 and S4 and then close S2 and S3. On the paper is not a big deal, in reality a "little yet not negligible" amount of time is phisically required to open a relay. This means that you need to introduce at least some delay between the operations.
If you have both S1 and S2 or S3 and S4 closed at the same time you'll short the power supply, and this could cause damages to the power supply and/or the circuit itself.
You may replay: ok, good to know, but you would have the same issue with transistor based H-bridges, with relay or bigger contactor is only a little more time expensive. Yes, indeed, it's generally valid for every H-bridge but the transistor module is faster, it use smaller current and it's mostly used paired with a microcontroller (maybe with it's own library).
Moreover, because of shit happens, the situation could be worse: if you overload a relay you risk to melt its contacts and you might not be able to open it also using the proper command input, so aven a brief mistake can lead to a destructive runaway.
Beside the safety reasons 4 relays are not an optimal solution because only two relays are actually required, so happy days: a better solution actually do exist and it's even cheaper!
Step 2: Second Solution (good)
If you discard the H-bridge, transistor based or four relay based, and if you take a two relay module (amazon, bangood)and follow this wiring scheme you'll be able to properly control a DC motor in both directions without any risk of short circuit and without spending unnecessary money.
The wiring is quite easy, you only have to connect the power supply plus and minus "at the same side" of the two relays. Fast and easy. With this solution you'll have only four possible outcomes, which are all safe and that won't damage anything. By the way, the module is also optocoupled, therefore the control circuit is two times protected from the power side of the device.
As you can see in the second image it doesn't matter if you don't provide a delay or if you give a wrong input, the motor won't behave in the right way (well, you are providing a wrong input, what do you expect? miracles?) but the circuit and the power supply will suffer no damages. Every possible configuration is ok, and that's not the case with four relays. Funny story: in some forum some people were discussing about adding two more relay to the first four to -somehow. provide a mechanical interlock and keep the four ones safe, I call this the Xzibit (Pimp My Ride) solution.
Please note that the Xzibit solution is still less safe than the proposed one because the two additional relays AREN'T a 100% safe interlock because they can't assure that two relays on the same bridge side can't be close at the same time. As stated before a relay can be "glued" in a conducting state despite the control signal.
A proper mechanical interlock is intrinsic in the two realys, if properly used, in this case using a single relay to act as a full side of the H-brige. A relay can be "glued" in one state but it can't conduct current on both terminals!
There's another possibile advantage of this solution respect to the transistor H-bridge: if you set the two relays in the same state you'll short circuit the motor's terminal. This short circuit allows the circulation of currents that can brake the motor thanks to the Lenz's Law. This law basically says that if you try to spin a DC motor you'll cause a variation in the magnetic field in which the motor's coils are in, therefore you are creating a potential difference at the coil's ends.If this voltage can be "discharged" through a connector it will create a counter-rotating force that tends to contrast the spinning momentum, so it tries to brake the motor. You can easily try this effect with any DC motor. Try to spin a motor with its contact open and then shortcircuited, you'll notice the difference.
Please note that "spinning power" will be dump on the short circuit conductor, which is a common cable, therefore the motor can dissipate only a certain amount of power. If you exceed the conductor's disspating power you'll burn it.
Step 3: How to Use the Relay Module
This module is really easy to use. You have to provide 5V to give it power and you have to pull to GND voltage the two input pins. You can do that using a microcontroller, a PLC, or you can also do that with electromechanical switches. For instance, with an arduino you can use the digitalWrite function on the digital pin which is connected the input terminal on the relay module. Since you have to pull the voltage to ground you have to use digitalWrite(pin, LOW) to activate a relay. This ible wasn't ment to be a relay-arduino tutorial but if you want also this write a comment and i'll provide few lines of code as an example.
Step 4: Electromechanic Example
Think about a winch, a hoist, a garage door, a gate or generally about any mechanism, operated by a DC motor which that has two movement direction and its proper range of movement is well defined (not a fan, which has no limit switches ne need to reverse the rotation).
With a three-way switch (three-pole unipolar diverter) and two endstop switches we have all the control we need.
Wire your module as per the attached image: the endstops must be wired on the NC (normally close) and C (common) contacts, the switch must be connected to ground on the central pin and to one side of each endstops on the other pins. The remaining two endstops wires must be connected to the two input pin on the module. Now you only have to provide 5V to power up the module and the right voltage to power up your motor. You might find interesting this portable variable power supply that i've described in this ible.
If you did the wiring properly this device will behave in this way: with the 3 way switch in the central position the motor should brake, no matter of the endstops state. If you move the switch the motor should spin in one direction until the related endstop is pressed. Once the endstop is pressed the motor goes again in a braking configuration and it will remain steady as long as the endstop is pressed or the switch isn't switched to the other side. If you move the switch to the other side the motor will spin again in the other direction untill the other endstop is pressed or the switch moved in the central position.
It's convenient, itn'st it?
Step 5: Upgrade
Your task might be required to control the motor speed but the previous example can set the motor only in the forward/step/backward state.
The trasnsitor based H-bridge can control the motor speed using the PWM technic, which is nothing more than a fast flickering of the transistors. This input is perceived by the motor as an intermediate voltage and not as a stream of on/off voltage. Theoretically the relays can do a PWM modulation, practically they are too slow and they will have a very short life because they will worn out (mechanical) while transistors won't.
Don't worry, we can use the same technic used in the trasistor based H-brige by adding another module wired in series to the motor's power supply. Good news: to do that we do need only one big transistor an not four as it would be required for the same motor with a transistor based H-bridge.
Step 6: Downgrade
If you landed in this tutorial looking for a "DC motor control" how-to but you don't actually need this much current, you can also choose other modules. Luckily there are some integrated module with all the discussed features (start/stop - cw/ccw, 0/100 speed). If you need less than 3A of current for your motor you can take one of these: amazon, bangood. The downside is that ther's some hack to do if you want to integrate some endstops that don't simply cut the power (otherwise you won't move the motor untill you'll manually unlock the endstop).
That's all, happy DC motor, folks!
ps: if you find this -ible interesting or it helped you please consider a little paypal donation, it will be appreciated.