Hello !!
I need some help with my DRV8835 :
I need to connect 2 DC motors with this h-bridge, but I can't found any working program, (I'm using the DRV8835MotorShield library).
The connections are :
MD -> +5V
IN1 PH A -> pin 51
IN2 EN A -> pin 3 (PWM)
IN1 PH B -> pin 50
IN2 EN B -> pin2(PWM)
VCC-> +5V
VIN-> +6V
GND -> pin (-) of the battery.
This is the DRV8835MotorShield.cpp :
#include "DRV8835MotorShield.h"
const unsigned char DRV8835MotorShield::_M1DIR = 50;
const unsigned char DRV8835MotorShield::_M2DIR = 51;
const unsigned char DRV8835MotorShield::_M1PWM = 2;
const unsigned char DRV8835MotorShield::_M2PWM = 3;
boolean DRV8835MotorShield::_flipM1 = false;
boolean DRV8835MotorShield::_flipM2 = false;
void DRV8835MotorShield::initPinsAndMaybeTimer()
{
// Initialize the pin states used by the motor driver shield
// digitalWrite is called before and after setting pinMode.
// It called before pinMode to handle the case where the board
// is using an ATmega AVR to avoid ever driving the pin high,
// even for a short time.
// It is called after pinMode to handle the case where the board
// is based on the Atmel SAM3X8E ARM Cortex-M3 CPU, like the Arduino
// Due. This is necessary because when pinMode is called for the Due
// it sets the output to high (or 3.3V) regardless of previous
// digitalWrite calls.
digitalWrite(_M1PWM, LOW);
pinMode(_M1PWM, OUTPUT);
digitalWrite(_M1PWM, LOW);
digitalWrite(_M2PWM, LOW);
pinMode(_M2PWM, OUTPUT);
digitalWrite(_M2PWM, LOW);
digitalWrite(_M1DIR, LOW);
pinMode(_M1DIR, OUTPUT);
digitalWrite(_M1DIR, LOW);
digitalWrite(_M2DIR, LOW);
pinMode(_M2DIR, OUTPUT);
digitalWrite(_M2DIR, LOW);
#ifdef DRV8835MOTORSHIELD_USE_20KHZ_PWM
// timer 1 configuration
// prescaler: clockI/O / 1
// outputs enabled
// phase-correct PWM
// top of 400
//
// PWM frequency calculation
// 16MHz / 1 (prescaler) / 2 (phase-correct) / 400 (top) = 20kHz
TCCR1A = 0b10100000;
TCCR1B = 0b00010001;
ICR1 = 400;
#endif
}
// speed should be a number between -400 and 400
void DRV8835MotorShield::setM1Speed(int speed)
{
init(); // initialize if necessary
boolean reverse = 0;
if (speed < 0)
{
speed = -speed; // make speed a positive quantity
reverse = 1; // preserve the direction
}
if (speed > 400) // max
speed = 400;
#ifdef DRV8835MOTORSHIELD_USE_20KHZ_PWM
OCR1A = speed;
#else
analogWrite(_M1PWM, speed * 51 / 80); // default to using analogWrite, mapping 400 to 255
#endif
if (reverse ^ _flipM1) // flip if speed was negative or _flipM1 setting is active, but not both
digitalWrite(_M1DIR, HIGH);
else
digitalWrite(_M1DIR, LOW);
}
// speed should be a number between -400 and 400
void DRV8835MotorShield::setM2Speed(int speed)
{
init(); // initialize if necessary
boolean reverse = 0;
if (speed < 0)
{
speed = -speed; // make speed a positive quantity
reverse = 1; // preserve the direction
}
if (speed > 400) // max PWM duty cycle
speed = 400;
#ifdef DRV8835MOTORSHIELD_USE_20KHZ_PWM
OCR1B = speed;
#else
analogWrite(_M2PWM, speed * 51 / 80); // default to using analogWrite, mapping 400 to 255
#endif
if (reverse ^ _flipM2) // flip if speed was negative or _flipM2 setting is active, but not both
digitalWrite(_M2DIR, HIGH);
else
digitalWrite(_M2DIR, LOW);
}
// set speed for both motors
// speed should be a number between -400 and 400
void DRV8835MotorShield::setSpeeds(int m1Speed, int m2Speed){
setM1Speed(m1Speed);
setM2Speed(m2Speed);
}
void DRV8835MotorShield::flipM1(boolean flip)
{
_flipM1 = flip;
}
void DRV8835MotorShield::flipM2(boolean flip)
{
_flipM2 = flip;
}
and my program...
#include <DRV8835MotorShield.h>
/*
* This example uses the DRV8835MotorShield library to drive each motor with the
* Pololu DRV8835 Dual Motor Driver Shield for Arduino forward, then backward.
* The yellow user LED is on when a motor is set to a positive speed and off when
* a motor is set to a negative speed.
*/
#define LED_PIN 11
DRV8835MotorShield motors;
void setup()
{
pinMode(LED_PIN, OUTPUT);
// uncomment one or both of the following lines if your motors' directions need to be flipped
//motors.flipM1(true);
//motors.flipM2(true);
}
void loop()
{
// run M1 motor with positive speed
digitalWrite(LED_PIN, HIGH);
motors.setM1Speed(400);
delay(2000);
motors.setM2Speed(400);
delay(2000);
motors.setM1Speed(-400);
delay(2000);
motors.setM2Speed(-400);
delay(2000);
}
I'm using an Arduino Mega
Thank you for your help !!!
Thierry