Powering a motor with PWM

I think I might be being a bit stupid but lets see.

I am trying to control a motor with PWM. I know the motor works just fine since if I put a battery pack to it (2AA batteries), it runs just fine.

But, when I connect it to an Arduino, I can't get anything. My wiring is:

PWM pin to positive lead of motor and the ground lead connected to the ground of the Arduino. I have an analog sensor wire between the PWM pin and the positive lead and another sensor wire between the ground lead and the Arduino ground.

My sketch is exactly this:

int powerPin = 33;

void setup() {
  Serial.begin(9600);
  
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(powerPin, OUTPUT);
  
  analogWrite(powerPin, 0);
}

void loop() {
  
  float power;
  float ground;
  int read1;
  int read2;
  
  analogWrite(powerPin, 100);
  read1 = analogRead(A0);
  read2 = analogRead(A1);
  power = read1 * (5.0 / 1023.0);
  ground = read2 * (5.0 / 1023.0);
  
  Serial.print("HIGH: ");
  Serial.print(power);
  Serial.print(" - ");
  Serial.println(ground);
  delay(1000);
  
  analogWrite(powerPin, 0);
  read1 = analogRead(A0);
  read2 = analogRead(A1);
  power = read1 * (5.0 / 1023.0);
  ground = read2 * (5.0 / 1023.0);
  
  Serial.print("LOW: ");
  Serial.print(power);
  Serial.print(" - ");
  Serial.println(ground);
  delay(1000);
}

But my serial reads are always this:

HIGH: 0.12 - 0.00 LOW: 0.00 - 0.00 HIGH: 0.12 - 0.00 LOW: 0.00 - 0.00

and the motor does nothing.

What am I doing wrong here?

int powerPin = 33;

What arduino do you have? Is pin 33 a PWM pin?

You cannot drive a motor directly with the Arduino. An output pin can only, safely, handle 20mA. Your motor very likely needs way more than that. You need to use your battery to supply the motor through a transistor or relay or motor shield.

@groundfungus, I agree with you 95% of the motors can't be powered from arduino outputs. But some can. I have done it. Most arduinos can source 40 ma, and sink 40 ma. continually. And for a few milliseconds, triple that.

But, first of all, what do you think of the OP sending out his PWM on pin 33? I am confused by that.

Sorry, the pin 33 was on a chipKit Uno32 which isn't Arduino but is close. Now that I made it home, I am trying an Arduino Micro but with the same results.

Basically I am trying to build a quad-copter so I have to be able to regulate the power to each motor via programming. How can I put in some amount of current but regulate it 100% from the Arduino?

If D33 does not work, then you will not get desired results.

  1. Have you changed fro D33 now?
  2. What is your new sketch?
  3. What arduino board are you using?

Changing powerPin to pin 9 on and Arduino Mico doesn't help anything (PWM on 9 on the Micro). How though, can I alter the current to the motor via the Arduino?

With pin 9 connected I get these reads:

HIGH: 0.00 - 0.00 LOW: 0.01 - 0.00 HIGH: 0.00 - 0.07 LOW: 0.00 - 0.00 HIGH: 0.00 - 0.00 LOW: 0.01 - 0.00 HIGH: 0.00 - 0.07

shiznatix:
How though, can I alter the current to the motor via the Arduino?

So - how many more pins are you going to fry on your Arduino until you finally get a clue and use a transistor or mosfet to control your motor?

There are very, very few motors that can be run directly from an Arduino pin; even the really tiny motors you see being used for vibration in cell phones, or in those little mini toy helicopters, generally will pull more current than what the ATMega chip can supply. Do you even know how much current your motor pulls when loaded (better yet, when stalled)? Probably well in excess of 20 mA, which is the maximum you should consider pulling from any single pin on your Arduino (yes, I know the datasheet says the max is 40 mA; and the max for the entire chip as 200 mA; but that is a -maximum- which, in proper engineering, you should never hit or get close to; going for 50% under is the better choice for the long life of your pins and chip).

Set it up right and use a transistor or mosfet (or some other current buffer system) to supply the proper amount of current to your motor.

If you don't know the current the motor draws, test it! check the current with an amp meter at 5 volts. What does it dray?

If you don't know the current the motor draws, test it! check the current with an amp meter at 5 volts. What does it dray?

You can try to drive a motor with 9 PWM pins at the same time. Use a limiting resistor on each pin so 20mA is from each pin. Then 180mA can come from Arduino. Add a resonant LC circuit to increase efficiency. LC in series with the PWM shorted node from 9 pins.

HIGH: 0.00 - 0.00 LOW: 0.01 - 0.00 HIGH: 0.00 - 0.07 LOW: 0.00 - 0.00 HIGH: 0.00 - 0.00 LOW: 0.01 - 0.00 HIGH: 0.00 - 0.07

I agree with what everybody is trying to tell you, the motor draws more than the arduino can supply. You serial results show it, you are only getting 0.01 up to 0.07 volts because the output is over loaded when it is trying to output. Suggestion, read read read, google google google, how to run motors from arduino, there is so much info out there, and examples, that this sort of question should not have to be asked so much. This site has a playground section, go to it, (its listed on the nav bar above) and look up on the side tab "interfacing with hardware." So much effort has gone into supplying the information in playground and no one seems to use it. =(

Tom

shiznatix: Basically I am trying to build a quad-copter

First hint your motor is going to draw wayy more than 20mA. Probably more like 2000mA for even the tiniest quadcopters.

Ok, thanks everyone for the information. I will do more research and see what I can find.

If you don't yet have a volt meter (volt, ohm, amps), it would be worth while to get one.