Go Down

Topic: newbie: how to count pwm pulses (Read 3495 times) previous topic - next topic


Dec 28, 2011, 04:50 pm Last Edit: Jan 01, 2012, 02:01 pm by andreacuozzo Reason: 1

I've got a probably stupid question about steppers, it's my first time with motors so bear with me, here's where I'm at:

I've got a pcb with some stepper motors out of a printer external feeder module: it's got several Sanyo STK672-600 2-phase stepping motor driver chips on it, here's the chip datasheet:


I've read it and thought it looked easy to solder some wires to the most important pins of the chip (actually I soldered to some convenient test holes in the pcb that were directly connected to the pins), and then I connected:

STK pin 12, clock, to arduino mega pwm pin 7
STK pin 13, cwb (direction), to arduino mega digital pin 22
STK pin 15, enable, to arduino mega digital pin 23

Setting clock (via analogWrite) and enable to high the motors run, setting cwb to low/high they change direction, very nice:


After a couple of tries I've understood that motor speed is linked to the pwm frequency, I've found that out by moving the STK clock wire from arduino pin 7 (lower frequency pin) to pin 4 (higher frequency pin) and the motors ran at double speed, now my question is: how do I tell the stepper motor to move an exact number of steps in any given direction ? the motors I'm going to use are Shinano 1.8 degree/step, so they'll take 200 steps to one complete revolution, one step should be one pulse (if I got it right), so is there a way for me to tell Arduino to send an exact number of pulses from the pwm pin to the clock pin on the Sanyo chip ? A forum search got me to this answer, if that's the way to go a link to an example of how to use the "Output Compare Match Interrupt Enable" would be great.




You don't need a "PWM" output pin to send pulses.  The PWM pins are useful for sending a stream of pulses with an adjustable duty cycle to simulate analog output.

For any I/O pin you can send a pulse with:

Code: [Select]

digitalWrite(pin, LOW);

Don't forget pinMode(pin, OUTPUT) in setup().

Send pulses in a for-loop to send a specific number of pulses.  If the pulses are too fast for your motors to keep up with, add delay() or delayMicroseconds() calls to slow things down.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp


Thanks a lot! I've just tested your suggestion and It works like a charm, and it's way easier to control the motors speed playing with the delayMicroseconds. Thanks!



I've understood that motor speed is linked to the pwm frequency

No this is wrong, it is the pulse width ratio.

see this:-


Um Grumpy_Mike, did you read the post?  Stepper motors are speed controlled by the frequency of pulses to their pins, and the driver uses a clock input to control the speed based on frequency.

If you read the first line under features (on the first page) in the pdf datasheet, you might have realized this.

You are thinking of RC servos.  Totally different animals, which are indeed controlled with variable pulse width, at a (normally) fixed frequency.  They were not mentioned, nor are they relevant.

To andreacuozzo:

Congrats on getting your steppers working so easily!

You can use timer interrupts to continuously run the motor(s) in the background.  There is a library on the playground for using timer1.
What you want is an interrupt which toggles the clock pin.  You can then control the speed using setPeriod().  Just remember not to exceed the driver's max frequency.


Jan 01, 2012, 01:10 am Last Edit: Jan 01, 2012, 02:01 pm by andreacuozzo Reason: 1
Thanks Darth Maker, I'll take a look at that library, it might help me with the fact that the steppers speed has dramatically slowed down now that I've added some DigitalRead (did I press left/right/up/down button ?) and if sentences (if I did then act accordingly) to the code loop().

I've added a most needed user interface to the project, which by the way is about building a tennis ball machine, reusing a broken HP ScanJet front panel for both lcd (standard hitachi with the "16x1 being 8x2 (or 40x2) thing" described here) and push buttons (lots o them), and here's where I am now:


The idea is to use three steppers for (1) left/right movement, throwing the ball on the forehand and backend, (2) up/down movement, throwing the ball higher and lower on the net, and (3) driving the "carousel" that filters a single ball out of the ball basket and sends it to the two main motors, 12v heat blowers from a car, running at some 2000 rpms in opposite direction, that compress the ball between their wheels (that is, between their fans that have been reinforced inside and outside to serve as wheels) and spit it out at (hopefully) high speed to the open court. The throwing element of the machine (basculating and rotating wooden case with throwing wheels inside) is almost ready to be tested on the tennis court, hopefully next week I'll know if the thing can work as expected.

Go Up