Help with TB6612FNG H Bridge and PWM

I'm very new to all this (so please be nice) I'm attempting to build an Arduino controlled tank as a base for future hardware and coding projects (add arm, sensing etc) but for now it would be really great just to be able to get the movement bit sorted.

I've bought a Pololu md08a motor controller (has a Toshiba TB6612FNG chip on it) which seems to be working great. However i can't work out how to implement PWM to gain control over the speed.

To really cut out the variables I've hard coded a set of values that should have the motors turning slowly, once i understand how to control it, i can then look at making it more usable.

  const int motorAPin1 = 8;   // AIN1
  const int motorAPin2 = 9;   // AIN2
  const int motorBPin1 = 10;  // BIN1
  const int motorBPin2 = 11;  // BIN2
  const int enablePin = 2;    // H-bridge enable pin
  const int ledPin = 13;      // LED 
  const int PWMA = 3;         // Pulse Width Modulation Motor A
  const int PWMB = 5;         // Pulse Width Modulation Motor B

  void setup() {
    // set outputs:
    pinMode(motorAPin1, OUTPUT); 
    pinMode(motorAPin2, OUTPUT); 
    pinMode(motorBPin1, OUTPUT);
    pinMode(motorBPin2, OUTPUT);
    pinMode(enablePin, OUTPUT);
    pinMode(ledPin, OUTPUT);
    pinMode(PWMA, OUTPUT);
    pinMode(PWMB, OUTPUT);

    // set enablePin high so that motor can turn on:
    digitalWrite(enablePin, HIGH); 

  void loop() {
    digitalWrite(motorAPin1, LOW);
    digitalWrite(motorAPin2, HIGH);
    digitalWrite(PWMA, 60);

With motorAPin1 Low and motorAPin2 High, if i understand correctly, i should be able to use PWMA to control the speed moving the motor counter clockwise.

To save knackering a motor i've got a multimeter connected over AO1 and A02 (motor voltage output on H Bridge) and with this code i see the 9V i'm putting into the Motor voltage Pin. Hurrah!

Thing is, if i change the PWMA value (if i understand PWM it should be a value between 0 and 255. 0 being 0 duty and 255 being full duty) i'm still getting the same 9v.

What i was hoping for an incremental increase in the output voltage as i changed the PWMA value from 0 to 255.

Where am i going wrong!

Many thanks in advance!


PWM doesn't change the voltage. What it changes is how often that voltage is on and off. Your multi-meter isn't seeing that off and on change. since it happens so fast. But, the motor will only be on a percentage of the time, so it will move slower than if it were on all the time.

Ah okay, i'd have known this if i'd tested it properly with the motor.

I'll wire it all fully and give it a go.

Thanks for the prompt response!



Having fully wired and tested all parts, it still seems like the i'm getting little/no speed modulation with my PWM value.

From testing the motor appears to be going the same speed 1 through 255 the only difference is with a value of 0 whereby there is no movement.

How much modulation could i expect doing this ? Could it be that it is working, albeit far to subtle for me to see. Or could there still be a fault in the system?



Having put the project down - unable to work it out. Its finally come to me - you 'analogwrite' PWM not 'digitalwrite'

Newbie Mistake

The multi meter would have shown the voltage change because it measures the mean voltage for example if you measure the wall voltage it will be aprx 120 but in reality its fluctuating up to 170.