I'm trying to control the speed of two DC motors that are connected to a dual H bridge. I always used digital PWM pins with analogWrite() to change the speed, but since I'm making a project that will be using most of the digital pins, I wonder if I can use analog pins instead.

P.S. I know I can use analog pins as digital pins to free the digital PWM pins, but I'm not interested in doing that.

  • I'm voting to close this question as off-topic because this belongs on the Arduino Stack Exchange. – Nick Johnson Jun 27 '15 at 7:45
  • Well, I am confused. The Arduino reference page, the "Description" of analogWrite() also states: "Writes an analog value (PWM wave) to a pin. Can be used to light a LED at varying brightnesses or drive a motor at various speeds. " So can it or can it not? – evie May 16 '17 at 6:28
  • analogWrite() is for writing to the pins that support PWM. As far as I can tell you can't use it for writing to pins A0, A1, etc... Its a really confusing name for a function – Code Gorilla May 16 '17 at 7:35

You can't do it with the analogWrite function as that is reliant on physical binding beween certain pins and hardware timers inside the AVR. But you can always use SoftPWM because the the analog pins can generally be used as digital pins just as well.

| improve this answer | |
  • Yes, multiwii project does 'bit banging' software pwm on analog pins, but a library would be faster to implement – Hayden Thring Jul 19 '15 at 4:01

No, you can't. As it says in the Arduiuno reference page for analogWrite(): "The analogWrite function has nothing to do with the analog pins or the analogRead function."

You can use the "analog input pins" (A0..A5) of Arduino for analog input, or as digital inputs or outputs. They cannot be used for PWM(digital)-output. Only digital pins, and only those specifically stated for PWM-output, can actually be used for PWM output (unless you go down the path of software-PWM, though I would hesitate to do that for an H-bridge).

| improve this answer | |
  • depends on if the OP means an "H-bridge" with a control input stage that takes a single PWM input. I agree it would be a bit dicey to go that route with two inverted SoftPWM signals coming from the AVR. – vicatcu Jun 27 '15 at 0:41

You can use multiplexers if you are running out of digital pins or SPI PWM drivers for the motors. There are many multichannel drivers out there like TLC5940, put a power stage after them and you're done.

*Use Soft SPI on the analog pins to control the driver.

| improve this answer | |
  • The TLC5940 is designed for LEDs, would it be adequate for motor control? – Greenonline Aug 27 '15 at 23:29

Your Answer

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