Hi,
I'm trying to create an astro tracker with the Arduino.
I have successful built one using arduino UNO and a nema 17 with 100:1 planetary gearbox.
Now i want to improve the project changing the reduction ratio from 100:1 to 60:1.
In my code i use the timerOne library for set the timing between each step.
In the version with the 100:1 ratio i use 134.6ms but with the new 60:1 i need to use 224.4ms between each step.
This is my code for the 100:1 with 134.6ms:
#include <TimerOne.h>
#define M1 9
#define dirPin 8
#define enablePin 10
void setup()
{
Timer1.initialize(134600); // set Timer 1 for a cicle of 134600 us
pinMode(enablePin, OUTPUT);
digitalWrite(enablePin, HIGH);
pinMode(dirPin, OUTPUT);
digitalWrite(enablePin, LOW);
delayMicroseconds(2);
digitalWrite(dirPin, HIGH);
Timer1.pwm(M1, 128); // active the pin 9 for pwm, set duty at 12.5%
}
void loop()
{
}
i've studied the library here and here and i've changed the code in this way:
#include <TimerOne.h>
#define M1 9
#define dirPin 8
#define enablePin 10
void setup()
{
Timer1.initialize(224400); // set Timer 1 for a cicle of 224400 us
pinMode(enablePin, OUTPUT);
digitalWrite(enablePin, HIGH);
pinMode(dirPin, OUTPUT);
digitalWrite(enablePin, LOW);
delayMicroseconds(2);
digitalWrite(dirPin, HIGH);
Timer1.pwm(M1, 128) // active the pin 9 for pwm, set duty at 12.5%
}
void loop()
{
}
but the thing i can't understand is how i have to change the 128 and how i choose the percentage for the duty.
For a stepper motor it has to remain 12.5% and 128?
Any help in changing/choosing the right duty cycle will be greatly appreciated
Hi PaulS,
thanks for the reply, i've edited the code in the first post with the entire code.
I'm curious why you think you need to change it.
I'm not sure that i have to change it.
I don't understand how to choose the correct duty percentage and if it change with the changing of the timer.
if i understand correctly the 12.5% is the "part" of the chosen timer duration while the PWM is high.
So for 134.6ms is 16.825ms but with 224.4ms is 28.05ms.
I thought that i have to find a percentage of 224.4 that give me 16.825.
The duty cycle defines how long the pin is HIGH. Each HIGH causes the stepper to step once. If a value of 128 12,5% of 4096) causes a step when the timer is triggered every 134600 microseconds, then it will certainly cause a step when the timer is triggered every 224400 microseconds.
The longer interval between triggers, with the same duty cycle, will result in the PIN being HIGH for a longer interval.
Whether that is a problem, or not, can only be determined experimentally.
Going the other way, triggering the timer more often, would be more of a concern to me, where possibly the shorter HIGH time might not be long enough to trigger a step.
If i understood, every time the timer has reach the desired time it trig a single PWM signal, right?
In this tutorial https://www.arduino.cc/en/Tutorial/PWM i read that the Arduino PWM has the lenght of 500Hz=2ms so the duty cycle isn't affected by the duration of the timer and it has always the same duration?
Even if i change the Timer1.initialize(134600) to Timer1.initialize(224400) the duration of the signal recived to the stepper is the same? (in my case of 12.5% is 0.25ms)
Even if i change the Timer1.initialize(134600) to Timer1.initialize(224400) the duration of the signal recived to the stepper is the same? (in my case of 12.5% is 0.25ms)
Please use code tags ("</>" button), not quote tags, when posting code.
What stepper driver are you using? Most need just a few (e.g. 10) microseconds HIGH on the step pin in order to take a step, so the duty cycle is irrelevant.
i used the quote tab because with the code tabs i can't enlightened in[color=red][b] red[/b][/color] a piece of the code.
i'm using the DRV8825 and serching in his datasheet: http://www.ti.com/lit/ds/symlink/drv8825.pdf on page 7
i see for the "Pulse duration, STEP high" the min of 1.9us so even if i set the duty cycle at 1% i have 20us of HIGH signal. So as you say, i think i always trig a step.
All the arduino board (nano, micro, mega...) have a duty cycle of 2ms? Where i can find this information?