Notifications
Clear all

Stepper Motor Arduino Use  

  RSS

rf19
 rf19
(@rf19)
New Member
Joined: 5 months ago
Posts: 3
2020-05-10 8:06 pm  

Hi guys, 

I've created the Arduino bipolar stepper motor setup as seen in this tutorial, using essentially the same parts. ( https://dronebotworkshop.com/stepper-motors-with-arduino/#Resources Demo 3 - Bipolar Stepper with L298 H-Bridge)). I've been trying to edit the Arduino code so I can have the motor go in both directions, depending on the angle of the potentiometer. I have had no success so far, but I would love help if you guys could figure out a way to edit the code to make it work. 

Thanks 🙂


Quote
byron
(@byron)
Reputable Member
Joined: 1 year ago
Posts: 353
2020-05-10 9:10 pm  

@rf19

In the article Bill indicates that a negative number in the setspeed will drive the stepper in the reverse direction.   The potentiometer reading of 0 to 1024 is mapped to a range 0 to 100.  I think you need to map that to a range of -100 to 100 or something like that.  There are other bits of the code to change as well such as 'if motorspeed > 0'.  But caveats, I take no responsibility for a smoking circuit board 😎 


ReplyQuote
itsCaseyDambit
(@itscaseydambit)
Eminent Member
Joined: 6 months ago
Posts: 46
2020-05-10 9:31 pm  

Bi-directional should be no problem.

Using the speed-pot angle as the delimiter for forward/reverse is another issue.

I'm not certain if this trick can be pulled off with the L298 H-bridge, but to change rotational direction on a project I was working on with a uni-polar motor, I simply changed a multiplicand used in the speed settings from (+) positive to (-) negative, and the motor spun reverse.

I'm really gnu to the forum, too, but one thing is certain, if you need help with code, please attach a copy of the code you need help with, or present the code in some manner (like a code snippet) so we can play with it.

I realize this is a DroneBot Workshop project, but we may find the wrong sketch if we go hunting.

Peace, always, my new friend! 🧐 


ReplyQuote
rf19
 rf19
(@rf19)
New Member
Joined: 5 months ago
Posts: 3
2020-05-10 9:33 pm  

@byron @itscaseydambit Would you mind taking a look at what I have below and letting me know what you think. I'm relatively new to Arduino and need all the help I can get.

/*
  Stepper Motor Demonstration 3
  Stepper-Demo3.ino
  Demonstrates NEMA 17 Bipolar Stepper with L298N Driver
  Uses Potentiometer on Analog Input A0
  Uses Arduino Stepper Library

  DroneBot Workshop 2018
  https://dronebotworkshop.com
*/

// Include the Arduino Stepper Library
#include <Stepper.h>

// Define Constants

// Number of steps per output rotation
const int STEPS_PER_REV = 200;
const int SPEED_CONTROL = A0;

// Create Instance of Stepper Class
// Specify Pins used for motor coils
// The pins used are 8,9,10,11
// Connected to L298N Motor Driver In1, In2, In3, In4
// Pins entered in sequence 1-2-3-4 for proper step sequencing

Stepper stepper_NEMA17(STEPS_PER_REV, 8, 9, 10, 11);

void setup() {
  // nothing to do inside the setup
}

void loop() {
  // read the sensor value:
  int sensorReading = analogRead(SPEED_CONTROL);
  // map it to a range from 0 to 100:
  int motorSpeed = map(sensorReading, 0, 1023, -100, 100);
  // set the motor speed:
  if (sensorReading > 0) {
    stepper_NEMA17.setSpeed(motorSpeed);
    // step 1/100 of a revolution:
    stepper_NEMA17.step(STEPS_PER_REV / 100);
  }
  if (sensorReading < 0) {
    stepper_NEMA17.setSpeed(motorSpeed);
    // step 1/100 of a revolution:
    stepper_NEMA17.step(-STEPS_PER_REV / 100);
  }
  if (sensorReading = 0) {
    stepper_NEMA17.setSpeed(motorSpeed);
    // step 1/100 of a revolution:
    stepper_NEMA17.step(0);
  }
}


ReplyQuote
itsCaseyDambit
(@itscaseydambit)
Eminent Member
Joined: 6 months ago
Posts: 46
2020-05-10 10:01 pm  

Hello rtf19:

Attached is sketch I rewrote a while back. The documentation pretty much explains it all (I hope).

I think it's the sketch you're working on, except this sketch uses LEDs to indicate which direction the motor is spinning. It's been a while, and I'm old lol.

I've also modified things ever-so-slightly to change the motor direction, or at least, I've laid things out so they are easy(ier) to understand. 

It's well documented. Please give it a look over. It reads like a book.

 


ReplyQuote
itsCaseyDambit
(@itscaseydambit)
Eminent Member
Joined: 6 months ago
Posts: 46
2020-05-11 12:45 am  

LOL sorry I got your name wrong @rf19 old age does that to a man.

So, I read through the code I left, and I guess I added remote control for F/R and speed control with a speed pot, but the code is clean, so, it's kinda cool 'n' stuff for beginners and advanced, alike.

The following code segment is two things: important and unnecessary. Wrap your head around that...

const float CYCLE_STEPS  = 0x20;    // <<- the internal motor steps per 360º armature rotation cycle 
const float GEAR_REDUCT = 0x40;   // <<- the gear ratio (reduction) of this model of 28BYJ-48

const float TOTAL_STEPS =    // to get the TOTAL_STEPS to acheive 360º outshaft rotation 
                CYCLE_STEPS *    // <<- multiply CYCLE_STEPS by GEAR_REDUCT ( *0x40 = *64 ) 
              GEAR_REDUCT;      // <<- to find the TOTAL_STEPS per 360º of geared outshaft rotation 

The value of TOTAL_STEPS is equal to 0x80 in this sketch, and the values of CYCLE_STEPS and GEAR_REDUCT are used to find the value of TOTAL_STEPS and nothing else.

We could simply state: TOTAL_STEPS = 0x80; but the thing is, IF we want to give this code a life beyond this sketch, we use CYCLE_STEPS and GEAR_REDUCT in the equation, so that if we want to use a motor with different CYCLE_STEPS and GEAR_REDUCT values, we plug the values in, and voilà! The sketch works with a different motor than originally intended.

Peace always! 🧐 


ReplyQuote
itsCaseyDambit
(@itscaseydambit)
Eminent Member
Joined: 6 months ago
Posts: 46
2020-05-11 3:46 am  

and, I see I need to learn how to plug code into a forum message cuz dats messed up

 


ReplyQuote