0

PWM from Trinket M0
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

PWM from Trinket M0

by bythewater on Sat Apr 06, 2019 6:27 am

I'm attempting to drive this motor controller from pin 3 of an M0:
http://www.revrobotics.com/sparkmax-use ... tion-3-3-1

See screen capture showing specs on that device. It's not working and I believe that it could be a mis-match in pwm frequencies.
It appears the motor controller supports an input frequency of 50-200 while I'm finding from searching on the Trinket that it's PWM freq is 400?

Can what I need be done? I'd like to avoid bit-banging as it seems like a hack that may not be reliable. Thanks.
Code: Select all | TOGGLE FULL SIZE
int pwmPin = 4;
 
void setup() {
  pinMode(pwmPin, OUTPUT);
}

void loop() {
  analogWrite(pwmPin, 50);
}
Attachments
Screen Shot 2019-04-06 at 6.14.09 AM.png
Screen Shot 2019-04-06 at 6.14.09 AM.png (176.79 KiB) Viewed 183 times

bythewater
 
Posts: 23
Joined: Fri Oct 08, 2010 1:39 pm

Re: PWM from Trinket M0

by rcwilcox on Sat Apr 06, 2019 10:33 am

Have you looked at Curie Timer One ?
My TM0 is currently set up with a Circuit Python and I don't want to revert to Arduino (I've not yet tested swapping between languages) but you might see if that library is available for the SAM21 chip.

rcwilcox
 
Posts: 33
Joined: Thu Jun 06, 2013 4:33 pm

Re: PWM from Trinket M0

by rcwilcox on Sat Apr 06, 2019 10:51 am

Hmmmm, it seems that library may be for specific hardware on the Genuino. I would look at the SAM21 datasheet and see if the hardware can be programmed in a similar fashion.

Perhaps you can use this library Adafruit Zero Timer

rcwilcox
 
Posts: 33
Joined: Thu Jun 06, 2013 4:33 pm

Re: PWM from Trinket M0

by bythewater on Sat Apr 06, 2019 11:22 am

Thanks, I'll check it out.

I guess one option is the adafruit pwm board, which appears to allow greater degree of control. Overkill but would solve the problem. Or geez I revert to a 555 circuit, but that seems silly.

bythewater
 
Posts: 23
Joined: Fri Oct 08, 2010 1:39 pm

Re: PWM from Trinket M0

by XRAD on Sat Apr 06, 2019 12:26 pm

Using arduino IDE, servo control of motors is generally excellent. Trinket M0 outputs standard servo PWM 1-2ms position signals. the sparkmax has a 50ms update window which is pretty big. You need to include the servo library to control servos( or servo related objects such as a motor speed controller, ESC, etc...).

So try a simple servo.write() code block and see what happens. NOTE: for some motor controllers, only a few 1/10's msecs off midline makes the motor full speed. the servo lib converts this to easy to understand degrees 0-180. So the code may look max speed reverse = 60, and max speed forward = 110. You will have to find the best set-up.

Sometimes, you need to tie your motor esc ground and TM0 grounds together. obviously, make sure that the speed controller is not running off/through the M0 board power pins....

Code: Select all | TOGGLE FULL SIZE
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 90;    // I'm guessing that you should start in the middle (~1500), for zero motor speed

void setup() {
  myservo.attach(3);  // attaches the servo on pin PWM 3 to the servo object, on the M0
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // I would adjust this down a bit so that you do not full boat your motor, ie: 60;120)
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(500);                       // waits 1/2sec
  }
  for (pos = 180; pos >= 0; pos -= 1) {
    myservo.write(pos);               
    delay(500);                       
  }
}

XRAD
 
Posts: 532
Joined: Sat Nov 19, 2016 3:28 pm

Re: PWM from Trinket M0

by bythewater on Mon Apr 08, 2019 7:02 am

Thank you, will try, was wondering about whether ground loops are an issue.
I've also switched to CircuitPython to see if that will work.

bythewater
 
Posts: 23
Joined: Fri Oct 08, 2010 1:39 pm

Please be positive and constructive with your questions and comments.