Jump to content

Recommended Posts

Posted

I am using one of the 28-BYJ-48 steppers with the ULN2003 driver board and the TinyStepper_28BYJ_48.h Library as it is geared it has a higher torque, but it has no positional feedback.

The position of the stepper is commanded by a toggle switch to go to either position A or B, with the position set by the sketch values. However the problem is that when you depower, whatever the position it is in when depowered becomes the default O position 

I want to use a microswitch for a simple home movement, does anyone have a snippet of code that would do that?

Here's the code I have, pretty simple

  


#include <TinyStepper_28BYJ_48.h>

bool buttonState1, buttonState2;
const int BUTTON_PIN1 = 5;  // the number of the pushbutton pin
const int BUTTON_PIN2 = 6;  // the number of the pushbutton pin
const int BUTTON_PIN3 = 7;  // the number of the pin for the homing switch

// and connect pins 8,9,10,11 to IN1,IN2,IN3,IN4 on ULN2003 board

const int MOTOR_IN1_PIN = 8;
const int MOTOR_IN2_PIN = 9;
const int MOTOR_IN3_PIN = 10;
const int MOTOR_IN4_PIN = 11;
const int STEPS_PER_REVOLUTION = 2048;
int pos = 0;  // variable to store the servo position
unsigned int closePos = 0; //Angle at closed position

// create the stepper motor object

TinyStepper_28BYJ_48 stepper;

void setup() {
  
 
  
  pinMode(BUTTON_PIN1, INPUT_PULLUP);
  pinMode(BUTTON_PIN2, INPUT_PULLUP);
  pinMode(BUTTON_PIN3, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);

  // connect and configure the stepper motor to its IO pins
 
  stepper.connectToPins(MOTOR_IN1_PIN, MOTOR_IN2_PIN, MOTOR_IN3_PIN, MOTOR_IN4_PIN);
}


void loop() {
  buttonState1 = digitalRead(BUTTON_PIN1);
  buttonState2 = digitalRead(BUTTON_PIN2);

  // set the speed and acceleration rates for the stepper motor
  
  stepper.setSpeedInStepsPerSecond(150);
  stepper.setAccelerationInStepsPerSecondPerSecond(500);

// set the position of the stepper for when switch 1 is connected, probably best left at zero as will be the starting position

  //if (buttonState1 == LOW) {
    if (buttonState1 == LOW && pos <= -0) {
    for (pos = 0; pos <= 20; pos += 1) {
    stepper.moveToPositionInSteps(0);
  }
    }
// set the position of the stepper for when switch 2 is connected, set as positive number between 10 and 200 depending on throw required

  //if (buttonState2 == LOW) {
      if (buttonState2 == LOW && pos >= 20) {
    for (pos = 20; pos >= 0; pos -= 1) {
    stepper.moveToPositionInSteps(200);
    //delay(1000);
    }
  }
}

Cheers


Les

Posted

What angles are involved?
Is a stepper the best option?

If you need to use a stepper, you will need to put in a means of detecting the zero point when you start up.  This could be something like an optical slot sensor, a microswitch, or you could even mimic the smaller motors and put a physical stop in and try not to over-drive the motor.
This might help with a zero position sensor:  


Can you use a servo instead?  A servo just needs to be told where to point - it has its own position feedback system built in.

Posted

it will be moving a lever, and a microswitch was what I was thinking as the limiting sensing device

I'm using these steppers 'cos I have a load of them!

Let me have a look at how he did it

 

Cheers

Les

Posted (edited)

@lesthegrngo

Hi Les,

I would try to bring a calibration routine into the Setup() section.

1) Check if an end switch is pressed:

a) If switch one is pressed, set the step counter to zero (closPos)

b) If switch two is pressed, set the step counter to 200 (stepsToGo)

2) While no switch is pressed, move forward (1 step) until switch two is pressed (you may run more steps than one per loop, just trial and error)

3) Set the step counter to 200 steps (You also may run the stepper backwards until switch one is pressed and then set the counter to zero steps)

4) Setup your calibrated buttonStates and leave the while loop

In the loop() section you that repeatedly ask which switch is pressed for the corresponding action

I think it's better not to use the buttonStates within the Setup() section of the sketch, because it only goes once though it, and therefore, especially in the While loop the pins directly are read out.

You then may use the buttonStates in the Loop() section as usual.

Spoiler
#include <TinyStepper_28BYJ_48.h>

bool buttonState1, buttonState2;
const int BUTTON_PIN1 = 5;  // the number of the pushbutton pin
const int BUTTON_PIN2 = 6;  // the number of the pushbutton pin
const int BUTTON_PIN3 = 7;  // the number of the pin for the homing switch

// and connect pins 8,9,10,11 to IN1,IN2,IN3,IN4 on ULN2003 board
const int MOTOR_IN1_PIN = 8;
const int MOTOR_IN2_PIN = 9;
const int MOTOR_IN3_PIN = 10;
const int MOTOR_IN4_PIN = 11;
const int STEPS_PER_REVOLUTION = 2048;
int closePos = 0; // Angle at closed position
int stepsToGo = 200;  // variable to store the servo end position
int calibrationSteps = 1; // Steps per loop within the calibration, trial & error

// create the stepper motor object
TinyStepper_28BYJ_48 stepper;

void setup() {
  pinMode(BUTTON_PIN1, INPUT_PULLUP);
  pinMode(BUTTON_PIN2, INPUT_PULLUP);
  pinMode(BUTTON_PIN3, INPUT_PULLUP);

  // connect and configure the stepper motor to its IO pins
  stepper.connectToPins(MOTOR_IN1_PIN, MOTOR_IN2_PIN, MOTOR_IN3_PIN, MOTOR_IN4_PIN);

  // set the speed and acceleration rates for the stepper motor
  stepper.setSpeedInStepsPerSecond(256);
  stepper.setAccelerationInStepsPerSecondPerSecond(512);
  
  // run the calibration routine
  calibration();
// stepper.moveToPositionInSteps(closePos); // alternatively run the stepper into closePos
  delay(500);  // wait for the stepper to go to target position before accepting the next actions
}

void loop() {
  buttonState1 = digitalRead(BUTTON_PIN1);
  buttonState2 = digitalRead(BUTTON_PIN2);

// your code here

// Vinc to test the sketch
  togglePointer(); // use BUTTON_PIN3 to toggle between closPos and stepsToGo
}

// ----- Supporting functions -----
void calibration()
{
 if ((digitalRead(BUTTON_PIN1) == LOW) && (digitalRead(BUTTON_PIN2) == HIGH)) // Check if switch 1 is pressed and switch 2 is released
  {
   stepper.setCurrentPositionInSteps(closePos); // if switch 1 is pressed, set stepper into start position (0 steps)
   buttonState1 = 1; // set status variables for the switches
   buttonState2 = 0;
  }
 if ((digitalRead(BUTTON_PIN1) == HIGH) && (digitalRead(BUTTON_PIN2) == LOW)) // Check if switch 2 is pressed and switch 1 is released
  {
   stepper.setCurrentPositionInSteps(stepsToGo); // if switch 2 is pressed, set stepper into end position (200 steps)
   buttonState1 = 0;
   buttonState2 = 1;
  }
 while ((digitalRead(BUTTON_PIN1) == HIGH) && (digitalRead(BUTTON_PIN2) == HIGH)) // While no switch is pressed, move forward until switch 2 is pressed
  {
   stepper.moveRelativeInSteps(calibrationSteps);
//   delay(50); // use a delay if necessary
   if (digitalRead(BUTTON_PIN2) == LOW) // if switch 2 is pressed
    {
     stepper.setCurrentPositionInSteps(stepsToGo); // set stepper into end position (200 steps)
     buttonState1 = 0; // set status variables for the switches
     buttonState2 = 1;
    }
  }
}

// ----- dirty test functions -----
void togglePointer()
{
 if (digitalRead(BUTTON_PIN3) == LOW && stepper.getCurrentPositionInSteps() == stepsToGo) stepper.moveToPositionInSteps(closePos);
 if (digitalRead(BUTTON_PIN3) == LOW && stepper.getCurrentPositionInSteps() == closePos) stepper.moveToPositionInSteps(stepsToGo);
}

 

Beware of, that the above sketch is not faile safe, e.g. if both buttons are pressed, and not debounced.

Regards, Vinc

Edited by Vinc_Vega

Regards, Vinc

real life: Royal Bavarian Airforce

online: VJS-GermanKnights.de

[sIGPIC][/sIGPIC]

Posted

Thanks Vinc, I'll give that a go

However, a word to the wise - never inadvertently plug a 9v AC adapter into a ULN2003 stepper motor driver instead of a 9v DC........

 

Les

Posted (edited)

Hi Vinc

I couldn't get your sketch to work exactly how I wanted, but studied it and wondered if the homing should be in the setup in order to not interfere with the switches

I ended up with this, which works exactly as I need it to - thanks for all your help as always!

#include "AccelStepper.h"

#define HALFSTEP 8

#define motorPin1 8    
#define motorPin2 9    
#define motorPin3 10  
#define motorPin4 11   

int endPoint = 1024;  // Move this many steps; 1024 = approx 1/4 turn

// NOTE: The sequence 1-3-2-4 is required for proper sequencing of 28BYJ-48
AccelStepper stepper(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);

bool buttonState1, buttonState2;
const int BUTTON_PIN1 = 5;  // the number of the pushbutton pin
const int BUTTON_PIN2 = 6;  // the number of the pushbutton pin

// Define the Pins used
#define home_switch 7  // Pin 9 connected to Home Switch (MicroSwitch)

// Stepper Travel Variables
int move_finished = 1;     // Used to check if move is completed
long initial_homing = -1;  // Used to Home Stepper at startup


void setup() {
  Serial.begin(9600);  // Start the Serial monitor with speed of 9600 Bauds
  pinMode(BUTTON_PIN1, INPUT_PULLUP);
  pinMode(BUTTON_PIN2, INPUT_PULLUP);
  pinMode(home_switch, INPUT_PULLUP);

  //  Set Max Speed and Acceleration of each Steppers at startup for homing
  stepper.setMaxSpeed(100.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

  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--;                 
    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 
  stepper.setAcceleration(100.0);  // Set Acceleration of Stepper
  initial_homing = 1;

  stepper.setCurrentPosition(0);
  Serial.println("Homing Completed");
  Serial.println("");
  stepper.setMaxSpeed(1000.0);      // Set Max Speed of Stepper (Faster for regular movements)
  stepper.setAcceleration(1000.0);  // Set Acceleration of Stepper
}

void loop() {
  buttonState1 = digitalRead(BUTTON_PIN1);
  buttonState2 = digitalRead(BUTTON_PIN2);

  stepper.setMaxSpeed(1000.0);
  stepper.setAcceleration(500.0);

  if (buttonState1 == LOW)  // if control switch is in position A move stepper to position defined on the next line
    stepper.moveTo(200);   // change number in brackets to suit the required throw
  stepper.run();

  if (buttonState2 == LOW)  // if control switch is in position B move stepper to zero position
    stepper.moveTo(0);     // this is the same position as the microswitch used for homing
}

 

Edited by lesthegrngo
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...