Hello everyone, I would like to ask for your help if you dont mind please.
So I am trying to control the speed of a stepper motor while also showing the rpm in an lcd. However, the stepper motor wont seem run. Can anyone help me find what's wrong with my code? I am new to arduino, so I dont really know the problem. Thank You Very Much.
The code is as shown below.
float REV = 0;
int RPM_VALUE;
int PREVIOUS = 0;
int TIME;
#include <AccelStepper.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
//Define stepper motor connections
#define dirPin 3
#define stepPin 5
//Create stepper object
AccelStepper stepper(1,stepPin,dirPin); //motor interface type must be set to 1 when using a driver.
void INTERRUPT()
{
REV++;
}
void setup()
{
lcd.init();
lcd.init();
lcd.backlight();
Serial.begin(9600);
attachInterrupt(1, INTERRUPT, RISING);
stepper.setMaxSpeed(1200); //maximum steps per second
}
void loop()
{
stepper.setSpeed(-1200); //steps per second
stepper.runSpeed(); //step the motor with constant speed as set by setSpeed()
delay(1000);
detachInterrupt(0);
TIME = millis() - PREVIOUS;
RPM_VALUE = (REV/TIME) * 60000;
PREVIOUS = millis();
REV = 0;
Serial.print("RPM = ");
Serial.println(RPM_VALUE);
lcd.setCursor(0,0);
lcd.print(" Tachometer ");
lcd.setCursor(0,1);
lcd.print("RPM: ");
lcd.setCursor(5,1);
lcd.print(RPM_VALUE);
lcd.print(" ");
attachInterrupt(1, INTERRUPT, RISING);
}
to make sure to post your code in a more readable way you should use the "code tags" or copy the code from the Arduino IDE using the "copy for forum" menue entry ...
You'll find the code tags in the post editor menue:
It is this button:
float REV = 0;
int RPM_VALUE;
int PREVIOUS = 0;
int TIME;
#include <AccelStepper.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
//Define stepper motor connections
#define dirPin 3
#define stepPin 5
//Create stepper object
AccelStepper stepper(1, stepPin, dirPin); //motor interface type must be set to 1 when using a driver.
void INTERRUPT()
{
REV++;
}
void setup()
{
lcd.init();
lcd.init();
lcd.backlight();
Serial.begin(9600);
attachInterrupt(1, INTERRUPT, RISING);
stepper.setMaxSpeed(1200); //maximum steps per second
}
void loop()
{
stepper.setSpeed(-1200); //steps per second
stepper.runSpeed(); //step the motor with constant speed as set by setSpeed()
delay(1000);
detachInterrupt(0);
TIME = millis() - PREVIOUS;
RPM_VALUE = (REV / TIME) * 60000;
PREVIOUS = millis();
REV = 0;
Serial.print("RPM = ");
Serial.println(RPM_VALUE);
lcd.setCursor(0, 0);
lcd.print(" Tachometer ");
lcd.setCursor(0, 1);
lcd.print("RPM: ");
lcd.setCursor(5, 1);
lcd.print(RPM_VALUE);
lcd.print(" ");
attachInterrupt(1, INTERRUPT, RISING);
}
That's much better; I forgot to tell you also to properly format your code (that's done by ctrl T in the IDE) before providing the copy. That also improves readability (and avoids some comments by other forum members ).
Actually on a first glance some parts of your loop() code do belong into setup() as they have to be performed once, e.g. the attachInterrupt() and detachInterrupt() functions also the setSpeed/runSpeed functions ...
In general it looks as if the code was copied together from different sources?!?
Yeah, the code was copied from different sources, I just combined it together, ehhehe. For the setspeed and runspeed, i thought it must be in the void loop button to make it keep moving?
I understand ... You are in a trial 'n error phase without really trying to learn about Arduino and C++ first. I'd recommend to do this first because it improves successful coding
E.g. variables to store millis() data should (must) be of type unsigned long (int is just too small).
And it is always a good idea to try out existing code examples and then step by step move from there into unknown grounds. If the steps are too wide it is much harder to find the point where the error(s) come(s) into play ...
Thx for sharing your code. I will add the parts to read the millis() and rpm. I have tested the code that you sent, the stepper motor was running but it was very slow. I have increased the Serial.begin and stepper.setspeed but it was still slow. I used the IR proximity sensor and detected 0 revo since the speed was slow.
Introduced const byte and const int instead of #define (Reason: This way the compiler will not allow changes of their values inside the code and avoid possible problems.)
Introduces "StepsPerRevolution" which define how may steps arte required for one complete turn of the stepper axis. This is required if RPM is actually "Rounds Per Minute"!
Introduced StepsPerSecond to define the speed
Separated the calculation and printing of RPM in a single function. The calculation is done very Interval msecs to ensure a fixed (and hopefully long enough) measurement time.
Rounds and Rotation or Revolution per Minute is the the same (the abbreviation RPM = Rounds Per Minute comes from the "old" turntables if you know what I am talking about ).
The REV in your original code (or Steps in my) are counting the rising edges of pulses at steh stepPin.
A stepper motor has a certain angle per (full) step that it moves. Or the other way around a number of steps per full rotation.
NEMA 23 has 1.8° per step => 200 steps per one rotation (see const int StepsPerRevolution = 200).
So if you calculate RPM from the steps in a certain time interval it is
Interval => Time interval between measurements in [msec]
Steps => No of steps performed during Interval
StepsPerRevolution => Steps required for one complete 360° rotation
Thx for sharing the code. The code ran well. The last issues that I have is that even when I change the steps per second, the rpm shown in the lcd is still the same when it is 200.
Hi again, when i change the steps per second from 200 to 400, the steper axis doesnt rotate significantly quicker, however if I were to change it to 1000, the stepper does rotate quicker. Also what do you mean by removing the cable between pin 2 and 5? For the arduino board, I am using a arduino uno, for the driver I am using TB6560.