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); 
  • "I know that it's probably not going to work." it will work. it is called low-side switching. most bi-directional motors are in part driven this way for example.
    – dannyf
    Jul 7 '17 at 15:31
  • 6
    @dannyf - no, it won't work, and the user already stated that it did not work. This is because while topologically similar to a half-bridge of power FETs, the output drivers on chip aren't up to the task of running a motor. You've developed a very bad habit of posting throwaway incomplete and as a result at times misleading things like this, though fortunately at least this time you didn't abuse the answer form to do so. Jul 7 '17 at 15:52

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.

  • 9
    This is also improper - you should not be powering the motor from the Arduino. Also bipolar transistors as you have drawn do a poor job of motor control compared to modern FET's, and using a 2.2K base resistor will likely limit things further. When bipolar transistors are used, effectively "dual stage" darlingtons are the norm. Jul 7 '17 at 14:33
  • 1
    @Chris. You are right. The proper way to do is using an opto-isolator to drive a relay connected to proper power supply. Thank you for your valuable insight.
    – user31481
    Jul 7 '17 at 14:41
  • 4
    No, a relay is not a good way to do this either. As already explained, one should use an FET driver. Jul 7 '17 at 14:42
  • @Chris. Waiting your proper answer to this question.
    – user31481
    Jul 7 '17 at 14:46
  • 1
    Perhaps next time if you wait before you post you might see one. I came here from the question review page intending to post an answer, but given all the noise that's been made around the actual topic which had to be dealt with first, the actual question just doesn't feel interesting enough to bother with any more. Jul 7 '17 at 14:49

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.

  • 4
    The L298 and L293 are very antiquated high-loss devices best avoided. Especially in a battery powered application, one cell of the battery pack will go largely to feeding the losses of those driver chips, rather than turning the motor. These really need to stop being mentioned by default - use an FET solution instead, for example the TB6612FNG replaces the L29x on better motor shields. Jul 7 '17 at 14:37
  • Thank you for your comment, @ChrisStratton. I agree with you and I'll update the answer accordingly. Jul 7 '17 at 14:39

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.)


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.

  • > I just tried what you suggest, and it works fine,... It shouldn't surprise anyone that it worked. Because the structure of a typical GPIO pin is a h bridge or a low side switch. So it has to work. That's just basic electronics.
    – dannyf
    Jul 8 '17 at 15:20
  • > with only one (big) caveat... And that's no surprise as well. Whenever you drive a load, you have to understand its limitations. And in this case the limitation is severe in terms of drive current and drive voltage.
    – dannyf
    Jul 8 '17 at 15:21
  • A big advantage is that those pins generally have clamping diodes. For inductive loads, they act as free wheeling diodes.
    – dannyf
    Jul 8 '17 at 15:22

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.

  • 2
    This is bad, bad advice. The LED you're looking at likely draws 30 mA maximum. Even a small DC motor will draw many times that amount, and far more than the GPIO pins can sink.
    – Sneftel
    Jul 8 '17 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.