AFMotor shield - PWM strangely getting interrupted by sketch code

After removing a lot of delays, i have still been unable to fix the PWM output. It is getting interrupted by something.

The problem could be anywhere. I modified the AFMotor library to use analog pins with the motor shield. The only other wires to the shield are PWM pin 5 and 5v + GND.

LCD Keypad shield is using LiquidCrystal lcd(10, 12, 6, 7, 8, 9); Motor shield is using pin 5 for PWM, 4 analog pins (15-18 or analog 1-4, CLK, ENABLE, DATA, LATCH) Temp sensor is using digital pin 2

Using a test sketch and the same AFMotor library, the motor runs fine at speed 50, but speed even as high as 196 in the dodgy sketch will not run the fan. The fan seems to be pulsating backwards and forwards, even after making forward the only possible option in the library, as well as 50 being minimum possible speed.

The library was tested after all modifications, with a minimal test sketch, and worked every time at speeds 50 and 255.

I would appreciate any advice.


You changed something a the library, but you don't show what you have changed. The pin numbers of the AFMotor library are hardcoded in the library, if you miss one of them, the library no longer works.

Although it is confusing that, using the same modified library and a new sketch, motor control works fine. Although it still gets interrupted with delays.

I am thinking the problem is PWM pin 5, since on the arduino website it states delays interrupt pins 5 and 6. I have decided to plan this project a bit better.

As it turns out, i should be able to use the standard library. That should be a more sensible approach, and would eliminate severe problems.

2 - Temp. sensor 3- D4 4- MOTORCLK 5- D5 6-D6 7-MOTORENABLE 8-MOTORDATA 9-D7 10-RPM 11-MOTOR1 PWM 12-MOTORLATCH 13 14-LCD Keypad 15-LCD RS 16-LCD Enable

I would like to avoid this situation. The Adafruit Motor shield uses a lot of pins, and the library is programmed for the internal timers, and also hard to understand.

The pins used by the library are pin 3, 4, 5, 6, 7, 8, 9 (only for servo), 10 (only for servo), 11 and 12. Pin 0 and 1 are for the serial port to the computer.

What LCD+keypad are you using ? There are LCD+keypad shield that use pin 4,5,6,7,8,9 and A0 for buttons. You can not use both shield. If you buy a I2C version of that LCD+keypad, your problems are gone.

Perhaps you can drop both shields and buy the new motor shield and a I2C LCD+keypad.

Or.... it is possible to use both your shields Keep the Adafruit Motor shield and it's library original. Rewire the LCD+keypad shield to use pin 2, 13, A0...A5. You can easily read the buttons from A0 (that is the original pin for the buttons), and the Arduino LCD library can select any pin (also analog pins).

The problem has gone after rewiring, using the pinout 2 posts above. I was wrong about the sketch and delays being the problem, because that only happened with the modified library.

Thankyou for assisting me :)