-1

Can someone please tell me what I'm doing wrong?

The motor's not turning at all, never mind changing speed.

enter image description here

  • 2
    Please post your code (copy and paste) - not a screenshot of it. That we we can try it ourselves, or copy an offending line. Format the code using the code formatting markdown (four leading spaces). For help see Markdown help. You should be able to do this by selecting the code and pressing Ctrl+K to have your browser do this for you. – Nick Gammon Oct 23 '16 at 0:47
1

You should copy and paste the text of the code into your question, not a screenshot of it. Then we can copy and paste the problem lines into the answer.

You are varying up and down the variable motorVoltage in two loops. However you are not then doing an analogWrite with the new value. It should be more like this:

while (motorVoltage <= 255)
  {
  analogWrite (motorPower, motorVoltage);
  motorVoltage++;
  delay (200);
  }
0

A0 is an analog input. You can't output from it. You can only output on analog input pins by using defining them as 14,15,16...

For the best result, try changing the motor pin to something like pins 9, 10 or 11. They are my favorite PWM pins.

  • Guys I've moved the pin to one of the PWM pins and it's still not giving me any gradual changes. Also weirdly I'm able to manually set the analogWrite for my analog in pins and get lower output voltages. Noone seeing anything wrong with my code? – Garion Bracken Oct 23 '16 at 0:16
  • Yes, you are not doing the analogWrite inside any of your loops. All you are doing is changing a variable. – Nick Gammon Oct 23 '16 at 0:48
0

It looks like you are attempting to perform hardware PWM on an Analog Input Pin.

Assuming that you are using an Arduino Uno, try changing your first line of code to use one of the digital output pins which support PWM. You can use any of pins 3, 5, 6, 9, 10, or 11. Then connect that pin to your motor. (Note that you will likely have to use a transistor or driver circuit between the pin and the motor since the motor may well require more current to operate than the Arduino output can directly supply.)

If you are using a different Arduino other than the Uno, look at the documentation to find one of the PWM pins and try using that.

  • It's a pretty small motor that I took from an old printer. It runs off of the 5v and the 3.3v pins and from the analog pin when I analogWrite 255 or a bit lower. Admittedly it won't run at low analogWrites like 50. It's a clone Arduino Mega. – Garion Bracken Oct 23 '16 at 0:09
  • @GarionBracken: If you try to analogWrite() to a pin that is not PWM-capable, you will get either 0 (if writing less than 128) or 5 V (if writing something ≥ 128). What you wrote is essentially equivalent to digitalWrite(motorPower, motorVoltage >= 128 ? HIGH : LOW); – Edgar Bonet Oct 23 '16 at 9:31

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.