Stepper motor CNC Shield

Can someone please explain me the code below? I got this from the internet although I am not a hero in programming.
Basically I am trying to control a stepper motor with Arduino Uno, CNC Shield v3, stepper driver a4988 and joystick.

Further the stepper motor runs hot when not turning. For me the holding current is not important and this can be disabled. I have set V_REF using the potmeter on the stepper driver and used the formula V_REF = I_MOT8R_SENSE. R_SENSE is the resistor on the stepper driver.

Also it seems I can change the speed by changing the value after Serial.begin, e.g. the motor speed is increased when filling in 80000. Although when changing the number behind SMSpeed nothing happens.

Thanks for your help

// Stepper Motor X
  const int stepPin = 2; //X.STEP
  const int dirPin = 5; // X.DIR
 // joystick
 int button = A2;
 int vrx = A0; 
int vry = A1; 
int vrx_data = 0; 
int vry_data = 0; 
int buttonf = 0;
int led5 = 13; // indicator

int x = 0; 
int SMSpeed = 500; // Stepper Motor Speed 
 void setup() {
 // Sets the two pins as Outputs
 Serial.begin(9600); 
 pinMode(stepPin,OUTPUT); 
 pinMode(dirPin,OUTPUT);
 pinMode(vrx , INPUT); 
 pinMode(vry, INPUT); 
 pinMode(button, INPUT); 
 digitalWrite(button , HIGH);
 }

 
void loop() {
 
control(); 
 
if( buttonf == 1 )
{
 joystick();  
}
 
if( buttonf == 0 )
{
 delay(100);    
}
 
 
}
 
void control()
{
if (( digitalRead(button) == LOW ) && (buttonf == 0))
{
 
   Serial.println(" Started");
   digitalWrite(led5, HIGH);  
  buttonf = 1; 
  delay(1000);
 
 
}
 
if (( digitalRead(button) == LOW ) && (buttonf == 1))
{
  digitalWrite(led5, LOW); 
  Serial.println("ended"); 
  buttonf = 0; 
  delay(1000); 
}
 digitalWrite(button , HIGH);
}
 
 void joystick()
{
vrx_data = analogRead(vrx);
Serial.print("Vrx:"); 
Serial.println(vrx_data); 


// to stop the stepper motor
if ( (vrx_data > 490)  &&   (vrx_data < 510)   )
{

;
  
}


if ( vrx_data > 700  )
{

digitalWrite(dirPin,HIGH);
x = x + 1; 
 digitalWrite(stepPin,HIGH); 
 delayMicroseconds(SMSpeed); 
 digitalWrite(stepPin,LOW); 
 delayMicroseconds(SMSpeed); 
  
}




if ( vrx_data < 300   )
{
digitalWrite(dirPin,LOW);
x = x - 1; 

 digitalWrite(stepPin,HIGH); 
 delayMicroseconds(SMSpeed); 
 digitalWrite(stepPin,LOW); 
 delayMicroseconds(SMSpeed);  
}

}

First of all, you blasted past the forum software efforts to convince you to post using code tags. Please edit your post and fix that.

If you're just a beginner and struggling with a poorly written, poorly documented sketch, why not just dump it in the trash and find a better one?

What can be gained from having other people fix a sketch you can't understand?

I am trying to fix the code since the motor does rotate, although it runs very hot. I want to get rid of the holding state although I don't know how to.

You don't care that it runs hot? How long do you think it will last doing that? What about the load on the power supply? Effect of heat on nearby devices? etc. etc...

Perhaps an improperly adjusted coil current limit on the driver. Did you adjust it according to the instructions.

I take that to mean disable the driver output drivers. Use the enable pin on the driver (Uno pin 8 for the CNC shield V1.3).

Steppers do run pretty hot. Do you think yours is overly so?

I set the potentiometer on the stepper driver using the following formula:
V_REF=I_MOTx8xR_SEN
with I_MOT being the motor rated current 0.3A
R_SEN= the sensing resistor on the stepper driver 0.2Ohm.
Giving 0.48V. Then I subtracted a safety margin of 10%, so the actual V_REF I used is around 0.44V

Indeed what I try to do is lowering the temperature of the motor. Yes I want to disable the driver output while the motor is not moving. Although I tried that with the command
'digitalWrite(8, HIGH)'

Double check the values of the resistors on your board. I have not seen an A4988 with 0.2 Ohm sense resistors. The common values for the Chinese clones is 0.1 Ohms and others are 0.05 and 0.068 Ohms. The 0.1 resistors are usually marked R100. With 0.1 Rs the Vref is 0.24V. That may explain why the motor is too hot.

A4988 shunt resistors

That should work. Did you set pin 8 to pinMode OUTPUT first?

Dear groundFungus,

now I have also set the pinMode OUTPUT and is works! :slight_smile:

My next job is to properly adjust the speed of the motor. Or try to integrate with GRBL.

Best,

Rik

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.