PWM control of haptic driver

Adafruit's tiny microcontroller platform. Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
andrewggggggg
 
Posts: 4
Joined: Fri Oct 02, 2015 8:12 pm

PWM control of haptic driver

Post by andrewggggggg »

I have a similar question.. I am trying to control four vibrating motors using a ProTrinket interfaced to four DRV2605s. My code below (critique welcome) sets up two input pins that read data from my trigger source, two PWM (9&10) output pins that go to the IN connector of different DRV2605s. Every millisecond, i check if the trigger arrives. If it does, i start the motor using analogWrite() and start a timer. When timer passes 500, which hopefully is 500ms, i turn the motor "off". My issue is that i cant figure out how to turn the motors off. I tried analogWrite(0), analogWrite(255), digitalWrite(LOW), digitalWrite(HIGH). I found through experimentation that 72 provided the least buzzy state. Help Appreciated!

These are the motors i am using.
https://www.amazon.com/gp/product/B00PZ ... UTF8&psc=1

Code: Select all

#include <Wire.h>
#include "Adafruit_DRV2605.h"
int inPin1 = 8;     // StimBox input 1
int inPin2 = 4;    // StimBox input 2
int outPin1 = 9;    //Pins 3,5,6,9,10,11 are PWM compatible with analogWrite().
//Pins 9 & 10 operate at high speed on Pro Trinket
int outPin2 = 10;
int inVal1 = 0;        // variable to store the read value
int inVal2 = 0;
int outVal1 = 0;
int outVal2 = 0;


Adafruit_DRV2605 drv;

void setup() {
  Serial.begin(9600);
  Serial.println("PWM test");
  drv.begin();
  drv.selectLibrary(7);

  // PWM Input
  drv.setMode(DRV2605_MODE_PWMANALOG);

  // ac coupled input, puts in 0.9V bias
  //drv.writeRegister8(DRV2605_REG_CONTROL1, 0x20);

  // analog input
  drv.writeRegister8(DRV2605_REG_CONTROL3, 0xA3);

  pinMode(inPin1, INPUT_PULLUP);        // sets the digital input pin (pullup means connect to ground)
  pinMode(inPin2, INPUT_PULLUP);
  pinMode(outPin1, OUTPUT);   // sets the pin as output
  pinMode(outPin2, OUTPUT);
  analogWrite(outPin1, 72);// 72  was the closest i could get to "off"
  analogWrite(outPin2, 72);
}

int timer1 = 0;
int timer2 = 0;
int starttimer1 = 0;
int starttimer2 = 0;

void loop() {

  inVal1 = digitalRead(inPin1);     // read the input pin
  inVal2 = digitalRead(inPin2);     

  if (inVal1 > 0 && starttimer1 == 0)
  {
    analogWrite(outPin1, 1);
    starttimer1 = 1;
    timer1 = 0;
    Serial.println("Start motor 1");
  }
  if (inVal2 > 0 && starttimer2 == 0)
  {
    analogWrite(outPin2, 1);
    starttimer2 = 1;
    timer2 = 0;
    Serial.println("Start motor 2");

  }

  //PWM for motor off is somewhere around 65-70;  There must be a better way to shut the motors off.

 if (starttimer1 == 1)
 {
  if (timer1 > 500)
  {
    analogWrite(outPin1, 72);
    //digitalWrite(outPin1, LOW); //Tried this and did not work
    starttimer1 = 0;  
    Serial.println("Stop motor 1");
  }
  timer1++;
}
if (starttimer2 == 1)
{
    if (timer2 > 500)
  {
   analogWrite(outPin2, 72);
   // digitalWrite(outPin2, HIGH);
    starttimer2 = 0;
    Serial.println("Stop motor 2");
  }  
    timer2++;
}
    delay(1);
}

User avatar
adafruit_support_carter
 
Posts: 29457
Joined: Tue Nov 29, 2016 2:45 pm

Re: PWM control of haptic driver

Post by adafruit_support_carter »

Try wiring to I2C and see if you can get the demo sketch to work:
https://learn.adafruit.com/adafruit-drv ... g-and-test

User avatar
andrewggggggg
 
Posts: 4
Joined: Fri Oct 02, 2015 8:12 pm

Re: PWM control of haptic driver

Post by andrewggggggg »

I did initially start with the demo -- worked fine. I want to control four haptic drivers independently, based on inputs received by the Pro Trinket. I2C would not work for this, correct?

User avatar
adafruit_support_carter
 
Posts: 29457
Joined: Tue Nov 29, 2016 2:45 pm

Re: PWM control of haptic driver

Post by adafruit_support_carter »

Unfortunately that device has only one I2C address. However, you can still use I2C if you use something like this:
https://learn.adafruit.com/adafruit-tca ... t/overview

User avatar
andrewggggggg
 
Posts: 4
Joined: Fri Oct 02, 2015 8:12 pm

Re: PWM control of haptic driver

Post by andrewggggggg »

Thanks for the suggestion! So, no way to turn motors off using PWM at the IN port of the DRV2605?

User avatar
adafruit_support_carter
 
Posts: 29457
Joined: Tue Nov 29, 2016 2:45 pm

Re: PWM control of haptic driver

Post by adafruit_support_carter »

Kind of a chicken and egg thing. You can use it with a PWM input, but you need to configure it that way first over the I2C lines. So unfortunately you're still stuck with the I2C address conflict issue.

Locked
Please be positive and constructive with your questions and comments.

Return to “Trinket ATTiny, Trinket M0”