Stepper motor TB6600 and enable / disable function help

I have an elegoo uno (arduino clone) running a relay, a stepper motor with TB6600, and reading two thermocouples on my wood fireplace. The setup works - basically. The arduino watches the firebox temperature and turns the fan to high or low speed. The arduino watches the flue temperature and decides if it should close the air inlet to the fire at 550 degrees. I have three buttons, one to load wood, one to fully close the air valve, and one to move the air valve to the "running" or steady hot fire position.

My problem:
The stepper motor is always "on" and the noise off the motor interferes with the thermocouple measurement by about 50 degrees.

I tried using the TB6600 enable/disable pin with coding. But when I do this, I have to hold my function buttons the entire time I want the stepper to move. Desire is to push and release the button and the motor moves to position.

Do I need to incorporate a "while" function to keep the motor moving until it reaches position?

Interestingly - and what makes me wonder about the "while" function is that my "stepper home" and "load wood" functions will work. But then the motor pin is off and nothing else works...

I am open to all suggestions.

Thanks all!

#include "max6675.h"
#include "LiquidCrystal_I2C.h"
#include "Wire.h"
#include "AccelStepper.h"

//thermocouple 1 FOR FLUE
int ktcSO = 8;
int ktcCS = 9;
int ktcCLK = 10;
//thermocouple 2 FOR FIREBOX
int ktc2SO = 5;
int ktc2CS = 6;
int ktc2CLK = 7;
//blower relay pin 4
int relay = 4;

MAX6675 ktc(ktcCLK, ktcCS, ktcSO);
MAX6675 ktc2(ktc2CLK, ktc2CS, ktc2SO);
LiquidCrystal_I2C lcd(0x3F, A4, A5);
float temp;
float temp2;
float temp3;

// AccelStepper Setup
AccelStepper stepper(1, A1, A2); //AccelStepper(motorInterfaceType, stepPin, dirPin);
// 1 = Easy Driver type interface
//  Pin A1 connected to STEP pin
//  Pin A2 connected to DIR pin

const byte fullclose = 0; //fully close valve button
const byte load = 1;  //opens the valve to load wood
const byte runposition = 2; //this sets the valve to the run position quickly
const byte home_switch = 3; //stops the stepper at home
//const byte ftemp = 5; //fakes the thermocouple
int rpm; // new variable sets stepper speed
int location; //new variable for stepper location
int ENA = A3; //enables rotation of stepper

// Stepper
// Stepper Travel Variables
int move_finished = 1; // Used to check if move is completed
long initial_homing = -1; // Used to Home Stepper at startup
int runningposition = -3200; //steps for valve close to running position
int fullyclosed = -3550; //this is the steps to shut the valve fully
boolean alreadyRun = 0; //lock out valve from opening a second time w/o reset

unsigned long previousMillis = 0;
const long interval = 7000;

void setup() {
  Serial.begin(9600);
  // give the MAX a little time to settle
  delay(500);
  lcd.init();
  lcd.backlight();
  lcd.clear();
  lcd.print("BOOTING");
  pinMode(relay, OUTPUT);
  digitalWrite(relay, LOW);
  delay(1000);
  //the following lines control stepper motor
  // initialize the 8 pin as an output:
  pinMode(home_switch, INPUT_PULLUP);
  pinMode(fullclose, INPUT_PULLUP);
  pinMode(load, INPUT_PULLUP);
  pinMode(runposition, INPUT_PULLUP);
  pinMode(ENA, OUTPUT);
  // pinMode(ftemp, INPUT_PULLUP); //FAKES THE THERMOCOUPLE
  // initialize the load pushbutton pin as an input:
  // pinMode(temp, INPUT_PULLUP); //digitalWrite(load, HIGH); //turn on internal pullup resistor
  lcd.clear();
  lcd.print("Homing......");
  stepperHome();
  // Serial.println("Homed to Open (Drain) position");
  //Serial.println(" ");
}

void loop() {
  // basic readout test
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    temp = (ktc.readFahrenheit() + 5 );
    //delay(50);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Flue Temp");
    lcd.setCursor(10, 0);
    lcd.print(temp);
    lcd.print("F");
    temp2 = (ktc2.readFahrenheit() - 12 );
    //delay(50);
    lcd.setCursor(0, 1);
    lcd.print("Firebox Temp");
    lcd.setCursor(13, 1);
    lcd.print(temp2);
    lcd.print("F");
    lcd.setCursor(0, 2);
    lcd.print("Fan");
    lcd.setCursor(4, 2);
    //delay(75);
    if (temp2 < 190)
    {
      lcd.print("LOW");
      digitalWrite(relay, HIGH);//will switch the relay NO, Fan Low
    }

    else if (temp2 > 195)
    {
      lcd.print("HIGH");
      digitalWrite(relay, LOW); //will switch the relay NC, Fan High
    }
    // delay(75);
  }

  //if (temp2 < 190)
  {
    // digitalWrite(relay, HIGH); //will switch the relay NO, Fan Low
    //delay(2000);
  }

  //else if (temp2 > 195)
  {
    //digitalWrite(relay, LOW); //will switch the relay NC, Fan High
    //delay(2000); //Delay 2 sec.
  }

  //the following lines control stepper motor

  if (digitalRead(load) == LOW)
  {
    stepperHome();
    alreadyRun = 0;
  }
  if ((temp > 550) && (alreadyRun == 0))
  {
    alreadyRun = 1;
    location = runningposition;
    //one tooth forward
    //digitalWrite(ENA, LOW);
    stepper.setMaxSpeed(100);
    stepper.setAcceleration(5000);
    // Set the target position:
    stepper.moveTo(location);
    // Run to target position with set speed and acceleration/deceleration:
    //digitalWrite(ENA, HIGH);
    // delay(1000);
  }
  stepper.run();
  //digitalWrite(ENA, LOW);

  if (digitalRead(fullclose) == LOW)
  {
    alreadyRun = 1;
    // stepperHome();
    {
      location = fullyclosed;
      //digitalWrite(ENA, LOW);
      stepper.setMaxSpeed(500);
      //stepper.runSpeed();
      stepper.setAcceleration(200);
      // Set the target position:
      stepper.moveTo(location);
      // Run to target position with set speed and acceleration/deceleration:
      // delay(1000);
    }
  }
  stepper.run();
  //digitalWrite(ENA, HIGH);

  if (digitalRead(runposition) == LOW)
  {
    alreadyRun = 1;
    //stepperHome();
    {
      location = runningposition;
      //digitalWrite(ENA, LOW);
      stepper.setMaxSpeed(500);
      stepper.setAcceleration(200);
      // Set the target position:
      stepper.moveTo(location);
      // Run to target position with set speed and acceleration/deceleration:
      // delay(1000);
    }
  }
  stepper.run();
  //digitalWrite(ENA, HIGH);
}

void stepperHome()
{
  stepper.setMaxSpeed(400.0);      // Set Max Speed of Stepper (Slower to get better accuracy)
  stepper.setAcceleration(100.0);  // Set Acceleration of Stepper
  //digitalWrite(ENA, LOW);
  // Start Homing procedure of Stepper Motor at startup

  Serial.print("Stepper is Homing . . . . . . . . . . . ");

  while (digitalRead(home_switch))
  {
    // Make the Stepper move CCW until the switch is activated
    stepper.moveTo(initial_homing);  // Set the position to move to
    initial_homing++;  // Decrease by 1 for next move if needed
    stepper.run();  // Start moving the stepper
    delay(5);
  }

  stepper.setCurrentPosition(0);  // Set the current position as zero for now
  stepper.setMaxSpeed(100.0);      // Set Max Speed of Stepper (Slower to get better accuracy)
  stepper.setAcceleration(100.0);  // Set Acceleration of Stepper
  initial_homing = 1;

  while (!digitalRead(home_switch))
  {
    // Make the Stepper move CW until the switch is deactivated
    stepper.moveTo(initial_homing);
    stepper.run();
    initial_homing--;
    delay(5);
  }

  stepper.setCurrentPosition(0);
  Serial.println("Homing Completed");
  //digitalWrite(ENA, HIGH);

}

Wood_Stove_Blower_11_28_2020.ino (6.35 KB)

If you want to move the stepper to a position before doing anything else, you can use the runToPosition() function.
If you want to move to a position, but check other events, you can use the stepper.distanceToGo() function to see if you are there or not.

You may also want to look at the StateChangeExample (File->examples->02.Digital->StateChangeDetection) to see when a button gets presses vs. just knowing if it is pressed or not.

You need to enable the stepper driver before this line

stepper.moveTo(location);

and you need to disable it when the move is finished which can be determined with

if (stepper.distanceToGo == 0) {

...R

Robin - thank you for the help! This did exactly what I needed!

For those searching the forum in the future, here is my updated code.

#include "max6675.h"
#include "LiquidCrystal_I2C.h"
#include "Wire.h"
#include "AccelStepper.h"

//thermocouple 1 FOR FLUE
int ktcSO = 8;
int ktcCS = 9;
int ktcCLK = 10;
//thermocouple 2 FOR FIREBOX
int ktc2SO = 5;
int ktc2CS = 6;
int ktc2CLK = 7;
//blower relay pin 4
int relay = 4;

MAX6675 ktc(ktcCLK, ktcCS, ktcSO);
MAX6675 ktc2(ktc2CLK, ktc2CS, ktc2SO);
LiquidCrystal_I2C lcd(0x3F, A4, A5);
float temp;
float temp2;
float temp3;

// AccelStepper Setup
AccelStepper stepper(1, A1, A2); //AccelStepper(motorInterfaceType, stepPin, dirPin);
// 1 = Easy Driver type interface
//  Pin A1 connected to STEP pin
//  Pin A2 connected to DIR pin

const byte fullclose = 0; //fully close valve button
const byte load = 1;  //opens the valve to load wood
const byte runposition = 2; //this sets the valve to the run position quickly
const byte home_switch = 3; //stops the stepper at home
int rpm; // new variable sets stepper speed
int location; //new variable for stepper location
int ENA = A3; //enables rotation of stepper

// Stepper
// Stepper Travel Variables
int move_finished = 1; // Used to check if move is completed
long initial_homing = -1; // Used to Home Stepper at startup
int runningposition = -3200; //steps for valve close to running position
int fullyclosed = -3550; //this is the steps to shut the valve fully
boolean alreadyRun = 0; //lock out valve from opening a second time w/o reset

unsigned long previousMillis = 0;
const long interval = 7000;

void setup() {
  Serial.begin(9600);
  delay(500); // give the MAX a little time to settle
  lcd.init();
  lcd.backlight();
  lcd.clear();
  lcd.print("BOOTING");
  pinMode(relay, OUTPUT);
  digitalWrite(relay, LOW);
  delay(1000);
  //the following lines control stepper motor
  pinMode(home_switch, INPUT_PULLUP);
  pinMode(fullclose, INPUT_PULLUP);
  pinMode(load, INPUT_PULLUP);
  pinMode(runposition, INPUT_PULLUP);
  pinMode(ENA, OUTPUT);
  lcd.clear();
  lcd.print("Homing......");
  digitalWrite(ENA, LOW);
  stepperHome();
  digitalWrite(ENA, HIGH);

}

void loop() {
  // basic readout test
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    // save the last time the action happened
    previousMillis = currentMillis;
    temp = (ktc.readFahrenheit() + 5 );
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Flue Temp");
    lcd.setCursor(10, 0);
    lcd.print(temp);
    lcd.print("F");
    temp2 = (ktc2.readFahrenheit() - 12 );
    lcd.setCursor(0, 1);
    lcd.print("Firebox Temp");
    lcd.setCursor(13, 1);
    lcd.print(temp2);
    lcd.print("F");
    lcd.setCursor(0, 2);
    lcd.print("Fan");
    lcd.setCursor(4, 2);
    if (temp2 < 190)
    {
      lcd.print("LOW");
      digitalWrite(relay, HIGH);//will switch the relay NO, Fan Low
    }

    else if (temp2 > 195)
    {
      lcd.print("HIGH");
      digitalWrite(relay, LOW); //will switch the relay NC, Fan High
    }

  }


  //the following lines control stepper motor

  if (digitalRead(load) == LOW)
  {
    digitalWrite(ENA, LOW);
    stepperHome();
    alreadyRun = 0;
    digitalWrite(ENA, HIGH);
  }
  if ((temp > 550) && (alreadyRun == 0))
  {
    alreadyRun = 1;
    location = runningposition;
    digitalWrite(ENA, LOW);
    stepper.setMaxSpeed(100);
    stepper.setAcceleration(5000);// Set the target position:
    stepper.moveTo(location);// Run to target position with set speed and acceleration/deceleration
  }
  stepper.run();
  if (stepper.distanceToGo() == 0)
  {
    digitalWrite(ENA, HIGH);
  }

  if (digitalRead(fullclose) == LOW)
  {
    alreadyRun = 1;
    {
      location = fullyclosed;
      digitalWrite(ENA, LOW);
      stepper.setMaxSpeed(500);//stepper.runSpeed();
      stepper.setAcceleration(200);// Set the target position:
      stepper.moveTo(location);// Run to target position with set speed and acceleration/deceleration:
    }
  }
  stepper.run();
  if (stepper.distanceToGo() == 0)
  {
    digitalWrite(ENA, HIGH);
  }

  if (digitalRead(runposition) == LOW)
  {
    alreadyRun = 1;
    //stepperHome();
    {
      location = runningposition;
      digitalWrite(ENA, LOW);
      stepper.setMaxSpeed(500);
      stepper.setAcceleration(200);
      // Set the target position:
      stepper.moveTo(location);
      // Run to target position with set speed and acceleration/deceleration:
      // delay(1000);
    }
  }
  stepper.run();
  if (stepper.distanceToGo() == 0)
  {
    digitalWrite(ENA, HIGH);
  }
}

void stepperHome()
{
  stepper.setMaxSpeed(400.0);      // Set Max Speed of Stepper (Slower to get better accuracy)
  stepper.setAcceleration(100.0);  // Set Acceleration of Stepper
  // Start Homing procedure of Stepper Motor at startup

  Serial.print("Stepper is Homing . . . . . . . . . . . ");

  while (digitalRead(home_switch))
  {
    // Make the Stepper move CCW until the switch is activated
    stepper.moveTo(initial_homing);  // Set the position to move to
    initial_homing++;  // Decrease by 1 for next move if needed
    stepper.run();  // Start moving the stepper
    delay(5);
  }

  stepper.setCurrentPosition(0);  // Set the current position as zero for now
  stepper.setMaxSpeed(100.0);      // Set Max Speed of Stepper (Slower to get better accuracy)
  stepper.setAcceleration(100.0);  // Set Acceleration of Stepper
  initial_homing = 1;

  while (!digitalRead(home_switch))
  {
    // Make the Stepper move CW until the switch is deactivated
    stepper.moveTo(initial_homing);
    stepper.run();
    initial_homing--;
    delay(5);
  }

  stepper.setCurrentPosition(0);
  Serial.println("Homing Completed");

}

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