Go Down

Topic: Reprogramming to Bipolar stepper motor (Read 433 times) previous topic - next topic

Ellegaard

Hello

I need a little help on adjusting my code to a new stepper motor and driver.
My code runs a chicken coop.
Sensors/switches and light sensors works fine, but I need help to adjusting the code to the new hardware.

Arduino Uno

<script src="https://gist.github.com/anonymous/ce016245d9553669664b1a8dc52015f2.js"></script>


Robin2

I need a little help on adjusting my code to a new stepper motor and driver.
From what?
To what?

Post links to the datasheets for the motors and drivers.

Also post your Arduino program.

What is in the ZIP file? I (and others) don't like opening unknown ZIP files.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

MarkT

A photo of the underside of a stepper driver module isn't useful.  What is the module? What chip does it use?

I presume its something like a DRV8825 module, which takes step/direction pins to control and you
would normally start by trying AccelStepper library examples.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

TomGeorge

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

What is the spec of your old stepper?
What is the spec of your new stepper?

Thanks.. Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

Ellegaard

OK I will collect the missing data and repost.

Robin2

OK I will collect the missing data and repost.
Don't start a new Thread. Just continue with this one.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Ellegaard

#6
Mar 13, 2018, 12:02 am Last Edit: Mar 13, 2018, 12:10 am by Ellegaard
Here we go again:

Board: Uno

Attachments:
original Fritzing diagram
original driver L298n
picture of original 2 wire motor

I need my Arduino to work with the A4982 and new stepper motor.

wanted new driver A4982 (see link below...pdf too large to attach)
wanted new motor (picture attached)

Link to A4982: link

Code:

Code: [Select]

// libraries


#include <SimpleTimer.h>                     // load the SimpleTimer library
#include <LiquidCrystal.h>                   // load the liquid crystal library
#include <AccelStepper.h>                   // load the stepper library



// print debug messages or not to serial
const boolean SerialDisplay = true;

// pins assignments


const int photocellPin = A0;                 // photocell connected to analog 0
const int enableCoopDoorMotorB = 7;          // enable motor b - pin 7
const int directionCloseCoopDoorMotorB = 8;  // direction close motor b - pin 8
const int directionOpenCoopDoorMotorB = 9;   // direction open motor b - pin 9
const int bottomSwitchPin = 2;              // bottom switch is connected to pin 26
const int topSwitchPin = 3;                 // top switch is connected to pin 27
const int coopDoorOpenLed = 10;              // led set to digital pin 40
const int coopDoorClosedLed = 11;            // led set to digital pin 41

// variables

// photocell
int photocellReading;                            // analog reading of the photocel
int photocellReadingLevel;                       // photocel reading levels (dark, twilight, light)

// reed switches top and bottom of coop door

// top switch

int topSwitchPinVal;                   // top switch var for reading the pin status
int topSwitchPinVal2;                  // top switch var for reading the pin delay/debounce status
int topSwitchState;                    // top switch var for to hold the switch state

// bottom switch

int bottomSwitchPinVal;                // bottom switch var for reading the pin status
int bottomSwitchPinVal2;               // bottom switch var for reading the pin delay/debounce status
int bottomSwitchState;                 // bottom switch var for to hold the switch state

// SimpleTimer objects
SimpleTimer coopPhotoCellTimer;


// debounce delay
long lastDebounceTime = 0;
long debounceDelay = 100;

// interior lights twighlight delay
long lastTwilightTime = 0;
long TwilightDelay = 300000;           // 5 minutes



// ************************************** the setup **************************************

void setup(void) {

  Serial.begin(9600); // initialize serial port hardware


  // welcome message
  if (SerialDisplay) {
    Serial.println(" Procesor arbejder:");
    Serial.println(" Timer readPhotoCell hvert 10 minut - lys niveau: åbn eller luk låge");
  }

  // coop door

  // coop door motor
  pinMode (enableCoopDoorMotorB, OUTPUT);           // enable motor pin = output
  pinMode (directionCloseCoopDoorMotorB, OUTPUT);   // motor close direction pin = output
  pinMode (directionOpenCoopDoorMotorB, OUTPUT);    // motor open direction pin = output

  // coop door leds
  pinMode (coopDoorOpenLed, OUTPUT);                // enable coopDoorOpenLed = output
  pinMode (coopDoorClosedLed, OUTPUT);              // enable coopDoorClosedLed = output

  // coop door switches
  // bottom switch
  pinMode(bottomSwitchPin, INPUT);                  // set bottom switch pin as input
  digitalWrite(bottomSwitchPin, HIGH);              // activate bottom switch resistor

  // top switch
  pinMode(topSwitchPin, INPUT);                     // set top switch pin as input
  digitalWrite(topSwitchPin, HIGH);                 // activate top switch resistor

  // timed actions setup
  coopPhotoCellTimer.setInterval(6000, readPhotoCell);   // read the photocell every 10 minutes 600000




}

// ************************************** functions **************************************

// operate the coop door

// photocel to read levels of exterior light

void readPhotoCell() { // function to be called repeatedly - per coopPhotoCellTimer set in setup

  photocellReading = analogRead(photocellPin);
  if (SerialDisplay) {
    Serial.print(" Fotocelle Analog Værdi = ");
    Serial.println(photocellReading);
  }


  //  set photocel threshholds
  if (photocellReading >= 0 && photocellReading <= 60) {
    photocellReadingLevel = '1';

    if (SerialDisplay) {
      Serial.print(" Fotocelle Analog Værdi:");
      Serial.println(" - Mørkt");
    }
  }
  else if (photocellReading  >= 61 && photocellReading <= 120) {
    photocellReadingLevel = '2';
    if (SerialDisplay) {
      Serial.print(" Fotocelle Analog Værdi:");
      Serial.println(" - Tusmørke");
    }
  }
  else if (photocellReading  >= 125 ) {
    photocellReadingLevel = '3';
    if (SerialDisplay) {
      Serial.print(" Fotocelle Analog Værdi:");
      Serial.println(" - Dagslys");
    }
  }
}



// stop the coop door motor
void stopCoopDoorMotorB() {
  digitalWrite (directionCloseCoopDoorMotorB, LOW);      // turn off motor close direction
  digitalWrite (directionOpenCoopDoorMotorB, LOW);       // turn on motor open direction
  analogWrite (enableCoopDoorMotorB, 0);                 // enable motor, 0 speed
}



// close the coop door motor (motor dir close = clockwise)
void closeCoopDoorMotorB() {
  digitalWrite (directionCloseCoopDoorMotorB, HIGH);     // turn on motor close direction
  digitalWrite (directionOpenCoopDoorMotorB, LOW);       // turn off motor open direction
  analogWrite (enableCoopDoorMotorB, 255);               // enable motor, full speed
  if (bottomSwitchPinVal == 0) {                         // if bottom reed switch circuit is closed
    stopCoopDoorMotorB();
    if (SerialDisplay) {
      Serial.print(" Lågen er lukket - ingen fare");
    }
  }
}



// open the coop door (motor dir open = counter-clockwise)
void openCoopDoorMotorB() {
  digitalWrite(directionCloseCoopDoorMotorB, LOW);       // turn off motor close direction
  digitalWrite(directionOpenCoopDoorMotorB, HIGH);       // turn on motor open direction
  analogWrite(enableCoopDoorMotorB, 255);                // enable motor, full speed
  if (topSwitchPinVal == 0) {                            // if top reed switch circuit is closed
    stopCoopDoorMotorB();
    if (SerialDisplay) {
      Serial.print(" Lågen er åben - vær opmærksom!");
    }
  }
}


// do the coop door
void doCoopDoor() {
  if (photocellReadingLevel  == '1') {              // if it's dark
    if (photocellReadingLevel != '2') {             // if it's not twilight
      if (photocellReadingLevel != '3') {           // if it's not light
        debounceTopReedSwitch();                    // read and debounce the switches
        debounceBottomReedSwitch();
        closeCoopDoorMotorB();                      // close the door
      }
    }
  }
  if (photocellReadingLevel  == '3') {              // if it's light
    if (photocellReadingLevel != '2') {             // if it's not twilight
      if (photocellReadingLevel != '1') {           // if it's not dark
        debounceTopReedSwitch();                    // read and debounce the switches
        debounceBottomReedSwitch();
        openCoopDoorMotorB();                       // Open the door
      }
    }
  }
}




// ************************************** the loop **************************************

void loop() {
  coopPhotoCellTimer.run();      // timer for readPhotoCell
  doCoopDoor();
  doCoopDoorLed();
  doLcdMsg();

}

Robin2

Images from Reply #6 so we don't have to download them. See this  Simple Image Guide











...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Robin2

You need to post a link to the datasheet for your stepper motor. Without that it is impossible to know if the A4982 is suitable.

I thought from your Original Post
Quote
help on adjusting my code to a new stepper motor and driver.
that you are trying to change from one stepper motor to a different stepper motor but you seem to be changing from a simple DC motor to a stepper motor.

Stepper motors are very different.

Start by having a look at these links
Stepper Motor Basics
Simple Stepper Code


...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Ellegaard

I'm struggling to find the data sheet for the motor. But I have had it running with a simple code. So I believe it works.

Nema 17 Stepper motor Specification:
Model No.:17HD48002H-22B
Step Angle: 1.8 deg.
Holding Torque: 59Ncm(84 oz.in)
Rated Current/phase: 1.7A
Frame Size: 41 x 41mm
Body Length: 47mm
Shaft Diameter: 5mm
Shaft Length: 22mm
Weight: 380g

MarkT

#10
Mar 13, 2018, 02:50 pm Last Edit: Mar 13, 2018, 02:51 pm by MarkT
The key electrical specification for a stepper motor is current, here 1.7A, which is only just within the
range the A4982 can handle - this means it will need a heatsink and a small fan for definite if you
want reliable operation (if it overheats it will cut-out automatically without warning).

AccelStepper library will handle such a step/direction interface to a stepper controller.  I'd suggest
trying the various examples in that library.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

zeus2kx

Or use higher current drivers like TB6600..

Ellegaard

Motor will only be running 2 x 45 seconds per day. I don't think heat will be an issue.


Ellegaard

Now I also have the specs for the motor.


MarkT

#14
Mar 15, 2018, 12:03 pm Last Edit: Mar 15, 2018, 12:05 pm by MarkT
Motor will only be running 2 x 45 seconds per day. I don't think heat will be an issue.


Steppers are hot all the time, they consume power just as much when stationary.  If you power
them down, they can lose position.

If you don't care about losing position you'll need some high-side switch to power down the
driver when the motor is idle, if you do care about position you have to keep it powered up and
you will have an issue with heat.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up