5

So what exactly I am trying to do is control a motor and be able to turn it both ways without any extra parts.

So if the hmp was on (the plus pole) and the hmm (the minus pole) acted like ground the motor should start turning right.

But if the hmp acted in this case as ground and the hmm like the plus pole it should start turning the other way.

I know that it's probably not going to work. I'm just experimenting.

int Hmp=12; // the plus pole
int Hmm=11; // the minus pole

while (HR=0 || Sr > Sl+300) { 
  pinMode(Hmp, OUTPUT);
  digitalWrite(Hmp, HIGH);
  pinMode(Hmm, INPUT);
}

while (HR=1 || Sl > Sr+300) {
  pinMode(Hmp, INPUT);
  pinMode(Hmm, OUTPUT);
  digitalWrite(Hmm, HIGH); 
}
Enric Blanco
  • 2,124
  • 1
  • 14
  • 25

5 Answers5

10

No, it's not going to work - even worse, you risk frying your Arduino -, for the following reasons:

  1. The Arduino data pins can't source (neither sink) enough current for that to work.

  2. The inductive kickback of the motor could fry Arduino pins.

The right way to do what you want to do is using an H-bridge controlled by your Arduino data pins. There are lots of Motor Driver PCB modules for Arduino (most of them based on the inefficient L298 driver IC, which you should ignore and look instead on those based on the TB6612FNG driver IC or similar, because it would be a far better solution) that implement this function.

Enric Blanco
  • 2,124
  • 1
  • 14
  • 25
3

You have to put both pin in OUTPUT mode. You digitalWrite() the "positive" pin HIGH and the "negative" pin LOW.

But, it's not a good idea in this case:

  1. Motor's power requirement exceeds digital output pin current of 40 mA.
  2. Back EMF. Every time you stop the motor, there is a brief current pulse back to your pins. Bad.

The proper ways to control a motor is this:

Arduino controlled motor

This will control only one direction. For changing directions, you need a H-Bridge. An H bridge is an electronic circuit that can drive the motor in both directions.

linhartr22
  • 606
  • 5
  • 10
3

I don't think that's going to work.

You'll be better off using a cheap module like the Pololu Pololu DRV8838 which implements a MOSFET H-bridge.

(Note: I'm not related in any way with Pololu.)

Lucky
  • 41
  • 4
2

I just tried what you suggest, and it works fine, with only one (big) caveat: you can only power this way very low power motors. I did this to drive the linear motor that moves an HDD head assembly. This motor works perfectly fine with less than 18 mA.

Note that I switch the Arduino pins between the HIGH and LOW states, not going through INPUT. Setting the pin to INPUT would send the motor's inductive kickback through the pin's protection diodes. On the other hand, the inductance of this motor is presumably tiny...

For anything that can draw more than 40 mA, I would use an H-bridge. For something between 20 and 40 mA, I may take the risk of driving it directly if it's for an experiment where I am not too much concerned about the risk of loosing my Arduino.

Edgar Bonet
  • 45,094
  • 4
  • 42
  • 81
0

is it possible to make normal pins act like ground pins?

the answer is yes: i'm looking right now at a blinking led that straddles between two GPIOs with one of them permanently (via code) grounded so i can blink it via the other GPIO pin.

Obviously the usual limitations that apply to a GPIO pin apply here.

dannyf
  • 2,813
  • 11
  • 13