I have a DC motor and I want it to turn on a very simple need, but I tried a lot of stuff but it won't work, I hooked the motor up to a small battery and it worked fine, so something is off with my code, can someone help me?
Code:
Are you trying to power the motor with an Arduino pin? That will not work. You need a transistor or motor driver to power the motor with an external power supply.
Yes, but the relay must be driven by a transistor because a pin can't, directly, drive a relay either. The external power will be required for the relay coil, too. A relay takes a certain amount of current to hold it closed. It will draw that current all the time that is is closed.
A transistor driver draws much less current and the MOSFET draws nearly 0 current.
A relay cannot accept PWM for motor speed control. A transistor or motor driver can.
So, yes, a relay will work. It is less efficient and practical, though.