I would like to control speed of some steppers.
So I prepare below equipment.
- Arduino Due
- TB6600 Stepper controler
amazon.co.jp/gp/product/B06XSBB45M - Nema 23 Stepper
amazon.co.jp/gp/product/B07HQ77RWG
First I tried below code. It can rotate motor.
int PUL=35; //define Pulse pin
int DIR=33; //define Direction pin
int ENA=31; //define Enable Pin
void setup() {
pinMode (PUL, OUTPUT);
pinMode (DIR, OUTPUT);
pinMode (ENA, OUTPUT);
}
void loop() {
for (int i=0; i<6400; i++) //Forward 5000 steps
{
digitalWrite(DIR,LOW);
digitalWrite(ENA,HIGH);
digitalWrite(PUL,HIGH);
delayMicroseconds(50);
digitalWrite(PUL,LOW);
delayMicroseconds(50);
}
for (int i=0; i<6400; i++) //Backward 5000 steps
{
digitalWrite(DIR,HIGH);
digitalWrite(ENA,HIGH);
digitalWrite(PUL,HIGH);
delayMicroseconds(50);
digitalWrite(PUL,LOW);
delayMicroseconds(50);
}
}
To control speed, I must change frequency.
So I use below library because I can change frequency easily.
I have written code that works the same as the above code.
#include "pwm_lib.h"
using namespace arduino_due::pwm_lib;
#define PWM_PERIOD_PIN_35 10000000 // hundredth of usecs (1e-8 secs)
pwm<pwm_pin::PWMH0_PC3> pwm_pin35;
int PUL=35; //define Pulse pin
int DIR=33; //define Direction pin
int ENA=31; //define Enable Pin
void setup() {
// put your setup code here, to run once:
pinMode (PUL, OUTPUT);
pinMode (DIR, OUTPUT);
pinMode (ENA, OUTPUT);
// pwm_pin35.start(PWM_PERIOD_PIN_35,PWM_DUTY_PIN_35);
}
void loop() {
// put your main code here,to run repeatedly:
for (int i=0; i<6400; i++) //Forward 5000 steps
{
digitalWrite(DIR,LOW);
digitalWrite(ENA,HIGH);
digitalWrite(PUL,HIGH);
delayMicroseconds(50);
digitalWrite(PUL,LOW);
delayMicroseconds(50);
}
for (int i=0; i<6400; i++) //Backward 5000 steps
{
digitalWrite(DIR,HIGH);
digitalWrite(ENA,HIGH);
digitalWrite(PUL,HIGH);
delayMicroseconds(50);
digitalWrite(PUL,LOW);
delayMicroseconds(50);
}
}
When measured with an oscilloscope, the two programs output similar signals. However, the one using pwm_lib did not work.
Why I cannot control stepper by pwm?
Or can I control by other library?
Thanks.