Running an Electric Motor from Digital Pins

I am trying to run an electric motor from digital pins. The motor works fine when connected to 5V/3.3V and GND but does when I upload the follosing code and connect to Pin 6 and 7 as shown on the photo.

void setup() {
  pinMode(7, OUTPUT);    // sets the digital pin 13 as output
    pinMode(6, OUTPUT);    // sets the digital pin 13 as output
  Serial.begin(9600);
}

void loop() {

   
  digitalWrite(7, LOW);
  digitalWrite(6, HIGH); 

}

STOP!

You can't connect a motor directly to the pins of an Arduino or any other processor. Those pins are for control ONLY. You will break your Arduino. Even though it is just a cheap rip off board.

You need at least a transistor and a diode to drive a motor.

Read:- Motors 1

Thanks Mike. Can I ask how is it that it runs from 5V or 3.3V directly? Is it about available current? Also couldnt I just use the outpit pin as Ground and the 5V pin as charge?
The reason for this is I only want to do small movement of motor not working all the time. It is to move an object on the motor for a few degree rotation.

Because the 5V supply can easily supply 500mA or more. The 5V of digitalWrite(..., HIGH); is only rated for 20mA (although 40mA won't kill it.)

You can find really tiny motors that will run on that current. They are smaller than your little fingernail.

amirf:
I am trying to run an electric motor from digital pins. The motor works fine when connected to 5V/3.3V and GND but does not work when I upload the follosing code and connect to Pin 6 and 7 as shown on the photo.

One possibly useful piece of advice ----- not just for arduinos --- but anything in general. If you want to connect something to something else ..... then a recommendation is to first look online for lots of supporting information (photos, diagrams etc) that demonstrates what you want to achieve.

This means ...... if you can't find or see any tutorials showing photos or diagrams demonstrating a motor connected directly to the output pins of an arduino ---- then do not go ahead with it. That is - avoid blindly doing something like that.

For example, we don't normally blindly connect a relatively small DC motor across the AC mains terminals in our homes. We normally need to first find out (learn) procedures about connecting an electronic component to something....... like a suitable power source (before going ahead with it). That's to avoid damage to equipment, people, property etc.

amirf:
Also couldnt I just use the outpit pin as Ground and the 5V pin as charge?

Not sure what you mean by that? First off why make ground go through a pin when you can connect it directly?
If you mean charge up a capacitor and then fire it off into the motor three are several things wrong with that.
First you need a very big capacitor, then you need some sort of charging circuit, then you need to electrically connect the motor to the capacitor to trigger the movement. The circuit to do the final bit is more complex than the one you need to drive the motor correctly, like in the link I posted.

amirf:
Thanks Mike. Can I ask how is it that it runs from 5V or 3.3V directly? Is it about available current?

That's one showstopper yes, but there's also inductive kickback to consider - that normally destroys
semiconductor devices unless the appropriate protection circuitry such as free-wheel diodes are
employed to prevent high voltage transients.

Basic rule with logic signals is they are signals, they are not power.

They cannot handle high currents,
they cannot handle high voltages,
they cannot safely drive large capacitances,
they cannot safely drive inductive loads.

They drive other logic inputs, perhaps even a small LED+resistor (but you need to check the datasheet for how
much current, which can be a few mA only depending on the chip in question).

Also couldnt I just use the outpit pin as Ground and the 5V pin as charge?

Again a logic output pin is not power, it cannot play the role of ground.

The reason for this is I only want to do small movement of motor not working all the time. It is to move an object on the motor for a few degree rotation.

Motors need lots of current and power, expecting a capacitor to store the kind of energy
required for this is pretty much doomed, unless you go the huge capacitors, or super capacitors.

amirf:
Is it about available current? Also couldnt I just use the outpit pin as Ground and the 5V pin as charge?

The digital pins can only handle low currents (around 20mA) and importantly it doesn't matter whether the current is being "pulled" out of the pin (e.g. it's at 5V) or being "pushed" into the pin (e.g. it's at 0V/Gnd potential). The limit is still the same. Way too low for a motor.

Steve

amirf:
I am trying to run an electric motor from digital pins.

Then a ➚motor shield is probably what you are looking for. And as the others here already said: digital pins are to control things, not to drive them.

Or, if you don't need to reverse you can make a [u]MOSFET driver circuit[/u].

Or, you could use a relay but you'd loose PWM speed control (if you need that) and regular electro-mechanical relays usually also need a driver, or you can get a re-assembled relay-board with a built-in driver circuit. There are solid state relays that work directly from the Arduino but you have to find the "right" one because AC solid stage relays don't work with DC (and vice-versa), and sometimes there are other considerations.

Thanks guys. As I mentioned I am really trying to make small movements of a few degrees not running a motor. However, it was very informative. Greatly appreciate your help.

amirf:
trying to make small movements of a few degrees not running a motor.

Making a small movement is running the motor. And every time it starts to move, it draws the stall current.

But how are you planning to sense the "few degrees" and stop the motor in the right place? DC motors are not good at that....

amirf:
Thanks guys. As I mentioned I am really trying to make small movements of a few degrees not running a motor. However, it was very informative. Greatly appreciate your help.

What are you actually trying to achieve? Perhaps some other way of doing it would be better? DC
motors are designed to spin at high speed efficiently, doesn't sound like that's of any use here.

amirf:
As I mentioned I am really trying to make small movements of a few degrees not running a motor.

Making small movements of a few degrees is usually much easier with a servo. That's pretty much what they're designed for.

Steve

So there's 3 answers in a row basically saying that "DC motors" and "small movements" do not belong on the same sentence :wink:

Or put another way, xyproblem.info

amirf:
As I mentioned I am really trying to make small movements of a few degrees not running a motor.

Look into stepper motors. These I think are exactly what you are looking for.

amirf:
Thanks guys. As I mentioned I am really trying to make small movements of a few degrees not running a motor. However, it was very informative. Greatly appreciate your help.

We know what you mean here. You're attempting to control a motor. Suitable hardware and software is needed here.

On the hardware side -- what you want to do is to have suitable hardware to 'drive' the motor. And the digital output ports of an arduino do not provide enough power (or current) to drive this motor --- and not enough to drive any typical motor.

You have to look into interface devices - like 'motor driver' modules that is able to be an interface device that links the arduino to the motor.

If the arduino had been purposely built to drive a motor directly with its digital pins, then your motor would have worked (- would have spun). But the arduino wasn't designed to do that. So you need a suitable motor driver module to get some activity happening.