PWM variable Width and Duration

OK, way out of my depth here but I need to have an Arduino and two rotary encoders.

I need a 3.3vdc PWM signal where I can adjust the Pulse-Width 1-to-1400 and the Pulse-Duration 10-to-40,000. I am probably not using the correct terminology but that's what has been described to me.

Can anyone please point me at some code to do this.

Thanks

I'll bet you've never had the same dream twice.

Pulse-Width 1-to-1400 and the Pulse-Duration 10-to-40,000 - what are your units? PWM as it comes off the shelf is about 490Hz and width ranges from 0 to 255 if that's any help.

DKWatson:
I'll bet you've never had the same dream twice.

Not sure, but will be having nightmares about this thing. :slight_smile:

DKWatson:
Pulse-Width 1-to-1400 and the Pulse-Duration 10-to-40,000 - what are your units? PWM as it comes off the shelf is about 490Hz and width ranges from 0 to 255 if that's any help.

I have an old controller for a high speed motor and I am trying to make sense of it. With an oscilloscope attached when I get things like the following outputs. Please bear in mind I am a neophyte with the 'scope too. Twist all knobs until you get something stable looking then press the "Measure" button. :slight_smile:
PWidth, PDuration = 'scope

20, 39999 = 35Hz, 1.8msec
147,2499 = 35Hz, 28.6msec
1400,10000 = 140Hz, 7.2msec
1000,20000 = 280Hz, 3.56msec

Not sure if that helps but I was trying to make some sense of it, but just confused myself even more. The old controller is made with wire-wrap and works about 40% of the time. I was hoping I could use an Arduino with a couple of potentiometers or encoders or something.

If you want to give me some PW and PD to test with I will see what I can get on the 'scope.

Maybe electronics isn't your thing. Try medicine. I hear brain surgery is a lot easier.

Back to reality. What specs do you have for the motor? The way that PWM works is that it gives full power for x% of the pulse period thereby 'faking' the motor into behaving as though it's getting x% of full power, which in turn controls the speed. There is no need really to change the frequency except that higher freqs tend to result in smoother rides. Some say as well that at the lower freqs they can hear a hum. Whatever, a value is then translated to 'on' time and 'off' time, this value in the case of an 8-bit timer being between 0 and 255. If you set you Vref to 3.3V, this means that you're theoretically getting a resolution 12.9 millivolts, or perhaps more to the point, if mac rpm is 20,000 each pulse will be +/- ~78 rpm. You want to increase resolution and/or frequency, get out the slide rule because before long you'll back into the minimum pulse rate that can be handled by a counter resets, loops, etc.

If you can stick to the default settings it also saves you having to understand the interior workings of the timers, prescalers, compare registers, blah, blah, blah. Your call.

Where do the PWidth and PDuration numbers come from?!?

PWidth PDuration Scope
Frequency
Scope
Period
20 39999 35 Hz 1.8 mSec
147 2499 35 Hz 28.6 mSec
1400 10000 140 Hz 7.2 mSec
1000 20000 280 Hz 3.56 mSec

The last three frequencies and periods match pretty closely but the first one is way off. Either the Period should be 28.6 mSec (like the second one) or the Frequency should be 555 Hz to match the Period.

Thanks guys,
@DKW, yup I am a brain surgeon and rocket scientist so this should be easy then? "Specs for motor?" What specs. {grin} A $5 Junk yard find and the only other thing I can tell you is it is painted brown. :wink:

@John, it has a control it 7-segment displays that I can set PW 1-1400 and PD 10-39999 with on/off buttons. As mentioned, it is all wire-wrap connected and terrible to get working consistently. Someone decided that wire-wrap would be good long term. :slight_smile:

Quite possible I wrote down the wrong thing for the first one. If you give me some PW/PD that would help figure this out I can set them and give you the 'scope readings.

The electronics are really noisy, no wire-wrap surprises there and I get a lot of peaks to 8+vdc, but it mostly stays within the 3-4vdc range. Pics may help -- or not. :slight_smile:

.p.s @John,

It may be ignoring the 20 for that errant result. If I try to enter anything higher than PW 1400 or PD 39999 it just sets it back to 1400/39999. That's how I have found the upper limits for the ranges. It is possible it is ignoring anything too low, just not correcting the visual number.

Did it occur to you the reason it was in the junk is because it never worked right?

Paul

Paul_KD7HB:
Did it occur to you the reason it was in the junk is because it never worked right?

Thanks for the help, but the motor works fine. And, I sincerely doubt it never worked right, it is just not reliable now.

It is the controller that is at issue and since the motor seems to like PWM for operation, surely replacing the controller with an Arduino would seem to be a sensible approach to having it ALL working reliably.

Ok. How about a picture of the motor. How big is it? How much does it weigh? Are you certain it can be driven with 5V? Do you have a wiring diagram?

If you set PD to 10 and run PW from 1 to 1400 what changes in the control signal? Draw a graph of the output pulse width and cycle period for each 100 units: 1, 100, 200, 300,... 1400. If you can determine the motor RPM at each point, that would be useful data, too.

If you set PW to 1 and run PD from 10 to 39999, what changes in the control signal? Draw a graph of the output pulse width and cycle period for each 2000 units: 10, 2000, 4000, 6000,... 38000, 39999. If you can determine the motor RPM at each point, that would be useful data, too.

johnwasser:
If you set PD to 10 and run PW from 1 to 1400 what changes in the control signal? Draw a

Thanks John, I have decided on the brute force approach. I am using a PWM library from a Julian Lett video he has a link in the description.

I am getting the trace in the pic, but a bit concerned about the overshoot as it is often almost 6v. Is there some way to prevent that overshoot on the rising edge? And, maybe the undershoot too, but not sure if that is important.

Preferably in code as Julian's traces don't have it. I tried using a Nano and Uno. The Uno is a little better than the Nano but both still almost at 6vdc. But the motor is running up fine with bench testing at PWM 5kHz.

Not sure what I am going to use it for, but 10K-rpm sounds cool, running up. I am guessing that the noise on the 'scope is because I have bare wires everywhere and when I touch one it gets better or worse. I'll use shielded in the final set up.

DKWatson:
Ok. How about a picture of the motor. How big is it? How much does it weigh? Are you certain it can be driven with 5V? Do you have a wiring diagram?

As mentioned, I have the controller it came with and it is showing 4.6-ish in the above 'scope pics so pretty sure 5v is OK. But, all moot as I have mentioned to John above. Brute force rules.

If it's old, it's probably a brushed DC motor (or brushless, doesn't really matter) and doesn't care about pulses. A stepper motor is never going to give you 10K RPM so that leaves us with the obvious. PWM replaces rheostat without all the losses. The only thing the motor cares about is a voltage between 0 and Vmax which translates to rotational velocity between 0 and max.

Standard freq for Arduino PWM is about 490Hz with a resolution of 256. Higher freqs will give a smoother ride at slower speeds and reduce what some will hear as audible hum. Otherwise there is no compelling reason to change to default settings.

As far as the overshoot you want to get rid of, attach a de-coupling capacitor between signal and ground as close to the MCU as possible.

DKWatson:
If it's old, it's probably a brushed DC motor (or brushless, doesn't really matter) and doesn't
... snip
As far as the overshoot you want to get rid of, attach a de-coupling capacitor between signal and ground as close to the MCU as possible.

Thanks, after some more digging it appears it my be from a high-speed centrifuge and the rpms may be in the 30K to 50K range when I get it cranking along. It appears that the PWM is not the direct driving force, just the control force.

What kind of value-range are you talking about for the capacitor? Neophyte in electronics here remember. I have some nano-Farad range of caps so will try some of those. As you have probably guessed brute force and trial and error are my two major go-to approaches.

The worst that can happen is I lose $5 of magic smoke and maybe a $9 Uno. :slight_smile:

Start with the ubiquitous 100uF and see what it does.

Also, to protect your $9 investment, you might want to through a diode in line with your output to protect against back EMF, always a good idea when connecting with any induction motor. All that stored energy has to dissipate when you power down or even reduce speed.