0

Trinket MO PWM
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Trinket MO PWM

by davewithheld on Thu Mar 14, 2019 11:14 pm

I had very good results with CircuitPython and PWM on D2 and could control the frequency without (apparently) affecting system timing. Unfortunately, when I went to add an ISR, I found that CircuitPython does not support interrupts. (Bummer!) So, after learning the arduino IDE, I got things working well, except the PWM frequency. AnalogWrite() results in a frequency of about 750 hZ, which causes the motor I'm controlling to howl (seems to be the perfect frequency for the gearbox to resonate). I have found some (very) confusing and (very) complex code examples for arduino PWM programming, but each time I try to put the code into my sketch, all of the register variable names (like TCCR0A) are undefined. Is there an include file I need to add? Does anyone have an actual example of hardware PWM control via direct register programming, written specifically for the trinket MO? If not, Is there an arduino device that will translate directly into trinket MO code for direct register programming? Maybe some docs I could (should?) read to understand this stuff well enough to translate myself? Another (related) question: I'm using D2, whose PWM depends on TCC0_CH0. Will changing this timers prescaler affect millis() or micros()?

davewithheld
 
Posts: 2
Joined: Thu Mar 14, 2019 10:56 pm

Re: Trinket MO PWM

by oesterle on Thu Mar 14, 2019 11:30 pm

Hi, Dave!

What kind of motor are you driving? (servo, stepper, or plain ol’ DC motor?)
What are you making, and what does the motor do?

Can you also post your code? (Just paste it in, highlight it, and click the CODE button in the post editor here.)

Welcome to the Forums.

Cheers,

Eric

oesterle
 
Posts: 571
Joined: Tue Sep 17, 2013 11:32 pm

Re: Trinket MO PWM

by davewithheld on Fri Mar 15, 2019 1:14 pm

Hi, oesterle, thanks for the welcome. I'm using a DC motor with bi-directional, varying speed kind of like a servo. The trouble with a servo is that it needs power to hold it once it stops moving. Same with a stepper. By using a DC motor with a worm drive output and a position encoder (https://www.amazon.com/gp/product/B073S6DCRQ), I get a very low power device with relatively high torque and zero power (other than the Trinket) when at rest. No need to deal with the mass of the load causing it to overshoot or drift when power is removed. Just set PWM output to zero and it stops. I wrote some poor-man's version of deceleration, but the position is not critical. Just that it won't move after power is removed.

The Trinket MO is perfect for the task. I'm using an L298n motor driver (https://tronixlabs.com.au/news/tutorial-l298n-dual-motor-controller-module-2a-and-arduino/) which needs two inputs for direction and one input for PWM. That leaves two input pins on the Trinket for reading the encoder pulses.

As for what I'm building, it's a third generation of this first generation project: http://www.guiplot.com/mambali/crowcam/. That first gen was mostly a proof of concept, but it worked so well I built a second generation a couple of years ago with a water tank, compressor, two cameras and two valves, all controlled by a raspberry Pi zero. The pi serves a web page with two views, one east, one west, two fire buttons, tank pressure, temperature, and a bunch of other info. The third generation will have a turret so the damn crows can't get out of my sites. That's what the DC motor is for.

BUT, I don't want to scare them while I'm positioning the gun, so I need to control the frequency of my PWM. I'll post more info as I get farther along.

davewithheld
 
Posts: 2
Joined: Thu Mar 14, 2019 10:56 pm

Please be positive and constructive with your questions and comments.