I have a weird problem with my Arduino MotorShield Rev3 which is connected to a Arduino Uno WiFi Rev2.
I have 2 DC motors connected to the shield channels A and B and an external power supply which consists of 8 AA batteries that is connected to the Vin and GND of the shield.
Both motors are working fine, except that I can't control the speed or PWM of the channel B motor. The B motor will only run if the PWM is set to 255 whereas motor A works fine with different PWM values.
The code I'm using:
const int pwmA = 3;
const int pwmB = 11;
const int directionA = 12;
const int directionB = 13;
const int brakeA = 9;
const int brakeB = 8;
void setup() {
//Setup Channel A
pinMode(directionA, OUTPUT); //Initiates Direction Channel A pin
pinMode(brakeA, OUTPUT); //Initiates Brake Channel A pin
pinMode(pwmA, OUTPUT); //Initiates PWM Channel A pin
//Setup Channel B
pinMode(directionB, OUTPUT); //Initiates Direction Channel B pin
pinMode(brakeB, OUTPUT); //Initiates Brake Channel B pin
pinMode(pwmB, OUTPUT); //Initiates PWM Channel B pin
}
void loop(){
//Motor A forward
digitalWrite(directionA, HIGH);
digitalWrite(brakeA, LOW);
analogWrite(pwmA, 75);
//Motor B forward
digitalWrite(directionB, HIGH);
digitalWrite(brakeB, LOW);
analogWrite(pwmB, 75);
delay(3000);
digitalWrite(brakeA, HIGH); //Engage the Brake for Channel A
digitalWrite(brakeB, HIGH); //Engage the Brake for Channel B
delay(1000);
}
When I run the code only the motor A will run. If I change the value of analogWrite(pwmB, value) to 255 then the motor B will run also, but at max speed.
I'm thinking that it might have something to do with the internal connections of the pin 11. I have tested it with another MotorShield Rev3 and the same problem occurs. I have also changed the motors to opposite channels and the motor that is connected to channel B can't run with lower PWM than 255.
The pin 11 is marked as PWM on the Uno WiFi Rev2 board.
There is an error on the silkscreen of some Uno WiFi Rev2 boards that incorrectly marks pin 11 as having PWM functionality, but it does not, as explained on the "Tech Specs" tab of the product page:
PLEASE NOTE: Pin 11 is not PWM enabled and the connector is wrongly marked on some batches. Available PWM pins are: 3, 5, 6, 9, and 10.
Thanks for the help. What would be the best solution to resolve this type of problem? Is there any other way to remap the MotorShield pin11 to a different pin than wiring it with jumpers somehow?
That is really unfortunate that the Uno WiFi Rev2 is not compatible with the Motor Shield Rev3 out of the box. I don't any way you could remap the pin.
It is possible to use software PWM to create a PWM signal on any pin on the Arduino board. Unfortunately, the existing software PWM libraries for Arduino use low level code, which means they only work for the specific architectures the author intended to support. The Uno WiFi Rev2 uses a fairly new microcontroller, which has significant differences at a low level, so it requires library authors to write dedicated low level code for things like software PWM. Since the Uno WiFi Rev2 is fairly new, not many 3rd party library authors have gotten around to adding support for this board. Hopefully now that the low priced Nano Every (which uses the same microcontroller as the Uno WiFi Rev2) is on the market, we will see more users of this architecture and the 3rd party library authors will take notice and provide support. It is certainly possible to write your own software PWM implementation. All you need to do is switch the pin on and off as fast as possible, with a specific proportion of time in the HIGH state vs time in the LOW state (duty cycle). You could do this using digitalWrite() and micros(). The trouble with that is that digitalWrite() is very slow so you won't be able to achieve a very high PWM frequency (and it will also depend on how long the rest of your code takes to run). So the question is whether the maximum software PWM frequency you can achieve using digitalWrite() is higher than the minimum PWM frequency that can be used with the motor driver (and I don't know what that minimum frequency is).
If you decide to go the jumper route, you can remove the Motor Shield Rev3 from your Uno WiFi Rev2 and then bend pin 11 on the shield just enough so that when you replace the shield on the board pin 11 won't go into the female header on the Uno WiFi Rev2. Then you can use a jumper wire between pin 11 on the shield and a free PWM pin and update your code accordingly.