3

When I upload the code, the DC motor makes a humming noise, but doesn't turn.

//2-Way motor control

int motorPin1 =  5;    // One motor wire connected to digital pin 5
int motorPin2 =  6;    // One motor wire connected to digital pin 6

// The setup() method runs once, when the sketch starts

void setup()   {                
  // initialize the digital pins as an output:
  pinMode(motorPin1, OUTPUT); 
  pinMode(motorPin2, OUTPUT);  
}

// the loop() method runs over and over again,
// as long as the Arduino has power
void loop()                     
{
  rotateLeft(150, 500);
  rotateRight(50, 1000);
  rotateRight(150, 1000);
  rotateRight(200, 1000);
  rotateLeft(255, 500);
  rotateRight(10, 1500);
}

void rotateLeft(int speedOfRotate, int length){
  analogWrite(motorPin1, speedOfRotate); //rotates motor
  digitalWrite(motorPin2, LOW);    // set the Pin motorPin2 LOW
  delay(length); //waits
  digitalWrite(motorPin1, LOW);    // set the Pin motorPin1 LOW
}

void rotateRight(int speedOfRotate, int length){
  analogWrite(motorPin2, speedOfRotate); //rotates motor
  digitalWrite(motorPin1, LOW);    // set the Pin motorPin1 LOW
  delay(length); //waits
  digitalWrite(motorPin2, LOW);    // set the Pin motorPin2 LOW
}

void rotateLeftFull(int length){
  digitalWrite(motorPin1, HIGH); //rotates motor
  digitalWrite(motorPin2, LOW);    // set the Pin motorPin2 LOW
  delay(length); //waits
  digitalWrite(motorPin1, LOW);    // set the Pin motorPin1 LOW
}

void rotateRightFull(int length){
  digitalWrite(motorPin2, HIGH); //rotates motor
  digitalWrite(motorPin1, LOW);    // set the Pin motorPin1 LOW
  delay(length); //waits
  digitalWrite(motorPin2, LOW);    // set the Pin motorPin2 LOW
}

Why doesn't this work?

Update:

I plugged it into a different port (5v) as it is a 5v motor, but now it only turns left, not right. It just hums when it goes right.

migrated from electronics.stackexchange.com Jan 19 '15 at 2:16

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

  • 1
    If you want to close this, close it is a duplicate of something. It is anything but "unclear" - in fact, the question is rather admirably complete in presentation compared to the relatively beginner technical mistake being made. And while no specifications have been provided for the motor, the fact of the matter is that there are very few which would work directly connected like this, so that's almost irrelevant to understanding the problem. – Chris Stratton Jan 19 '15 at 1:16
  • 1
    This question does not belong on the Arduino site. It should be sent back to EE stack exchange (where it was quite properly asked in the first place), as the issue of motor drive electronics would be exactly the same with any small microcontroller, Arduino or otherwise. Politically motivated question dumping needs to stop. – Chris Stratton Jan 19 '15 at 4:43
  • 1
    For the reason, just check the first 2 answers to your question! – jfpoilpret Jan 19 '15 at 5:25
  • 1
    @ChrisStratton yes, this does seem like EE could keep it. However, just because it could be on topic there doesn't make it off topic here. Bash scripts are all OK on Super User, Stack Overflow, Ask Ubuntu, and Unix and Linux. – Anonymous Penguin Jan 19 '15 at 16:05
  • 1
    Although the question may provide the beginning of an answer on EE.SE, an answer here may be better fitted, e.g. using the right components, according to Arduino max pin current, which is specific. Personally I don't see much issue with moving this question here. – jfpoilpret Jan 19 '15 at 18:00
4

The IO drivers of most common logic chips or microprocessors such as the ATmega on your Arduino are nowhere near strong enough to drive typical electromagnetic motors.

To drive a motor, you should use a higher current switching device - a standard solution for bidirectional control being an H bridge part such as a TB6612FNG, L293D, L298, etc.

These are commonly sold on "motor shields" which you can plug into your Arduino, if you want to experiment with the concept before building something (and if you do want to build something, reviewing such a working schematic first would be a good idea).

You can in theory put an L293D or its DIP-packaged work-alikes on your breadboard, but you will need some care to get it right, especially if you use one of the other parts which requires external catch diodes.

Finally, you should not be powering most motors from the Arduino's onboard regulator or a USB port, but rather should use an external power supply.

=============

Responding to your edit, by changing connections to place the the motor between one pin and the 5v supply, you have done two things:

1) You have made it so that the voltage difference across the motor can be applied in one direction only, therefore it can only rotate in one direction.

2) You have made it so that only the N-channel MOSFET in an output driver is involved in powering the motor. That change is almost a useful idea, as N-channel devices are in fact much more capable than P-channel devices of similar dimension (and practical capacitance, voltage rating, and other tradeoffs) due to the greater mobility of negative charger carriers.

But this is still not really a solution, as it is unlikely that your motor's current rating is within even the current sink rating of the N-channel side of the ATmega output.

If you want a simple, unidirectional motor control, you could pursue this idea using one external N-channel MOSFET (or even a biploar NPN power transistor, which will be less efficient but also less delicate) to connect the unpowered side of the motor to ground.

If you want bidirectional control, you will need either + and - power supplies, or four external switching devices taking the roles of the low current N-FET and P-FET totem poles inside each of the two on-chip IO pin drivers you started with.

2

This is a huge simplification of @Chris Stratton's post, for beginners.

To put it simply, the power that runs your motor is going through your arduino. Your arduino is not made to carry that sort of power.

What you want is an H-Bridge - see https://itp.nyu.edu/physcomp/labs/motors-and-transistors/dc-motor-control-using-an-h-bridge/ - the H Bridge is specially made to carry the power needed.

Note that this page is for a 9v motor, not a 5v motor like yours - you will need to find a more appropriate power supply than a 9v battery (or, you need to find a 9v motor). If you try to power it through USB, you will be drawing the power through your computer - you may damage it!

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.