Bi-polar stepper motor and arduino uno

Hi All
I did this project with a small stepper motor and what I need to know is as follows:
When stepper motor is idle it pulls ± 500ma but when running it only draws ± 150ma.
I’m using L298 stepper module, and an uno
Is there a way to let the 328 IC go sleep if no action is going on the motor IE if they not using it, lunch time etc.
If I can put the arduino to sleep please guide me how ?
Please give me marks on this sketch as I am a newbie and feel quite proud of what I have achieved bad mouthing also helps as I am learning.
thank you

//       Label Application Driver -  Using Arduino UNO to Program.
//  Control (L298N Driver) of a 4 wire Bipolar stepper motor with optical-sensor (TCST2103),
//  reading labels on a roll (28mm x 32mm) with a spacing of 3mm  between labels. A led confirms
//  sensor status and (JOG/INCH) delay. A pushbutton is used to (JOG/INCH) the motor with or without
//  a 2 sec (adjustable in program) delay between each push of the button, to feed the labels in the
//  machine till, the sensor can take over, thus starting the procedure of the labels being applied
//  to the product.
//  A 5K Potentiometer is added to adjust the speed of the motor.

#include <Stepper.h>

const int buttonPin = 2;               // pushbutton (pin 2 arduino - pin 4 ATmega 328P)
const int ledPin = 13;                 // led indicator (pin 13 arduino - pin 19 ATmega 328)
const int sensor = 3;                  // sensor (TCST2103)-(pin 3 arduino - pin 5 ATmega 328)
const int stepsPerRevolution = 200;    // 1.8 degree step motor
int stepCount = 0;                     // number of steps the motor has taken
int buttonState = 0;
int val = 0;                           // variable to store the sensor status (value)
//int state = LOW;                     // default, no motion detected
Stepper myStepper(stepsPerRevolution, 9, 10, 11, 12);  //(pin 9-12 arduino - pin 15-18 ATmega 328)
                 //Cable connections as follows:
                            // N1 - 16 brown
                            // N2 - 15 green
                            // N3 - 17 orange
                            // N4 - 18 purple
                 // Motor cable connections:        
                    // Red/Blue - Black/Green

void setup() {

 pinMode(ledPin, OUTPUT);            // initialize LED as an output
 pinMode(sensor, INPUT);             // initialize sensor as an input
 pinMode(buttonPin, INPUT);          // initialize pushbutton pin as an input
 //digitalWrite(buttonPin, HIGH);    // turn on pull-up

void loop() {

 int val = digitalRead(buttonPin);      // read button value
  if (val == HIGH)                      // button value high
   digitalWrite(ledPin, HIGH);          // turn LED on
  if (val == LOW);                      // button value low
   digitalWrite(ledPin, LOW);           // turn led off
  if (val == HIGH)    {                 // button value high
   digitalWrite(ledPin, HIGH);          // turn LED on
   int sensorReading = analogRead(A0);                     // pot to adjust motor speed (0v-AO-5v)
   int motorSpeed = map(sensorReading, 0, 1023, 100, 255); // map it to a range from 100 to 255:
   if (motorSpeed > 100)                                   // check motor status
     myStepper.setSpeed(motorSpeed);                       // set the motor speed:
   myStepper.step(stepsPerRevolution / 200);               // step 1/200 of a revolution:
   myStepper.step(200);                 // run motor minium 200 steps
  int val = digitalRead(buttonPin);     // read button value
  if (val == LOW)                       // button value low
     myStepper.step(0);                 // stop motor
  int val = digitalRead(sensor);       // read sensor value
   if (val == LOW)                     // sensor value low
   digitalWrite(ledPin, LOW);          // turn led off
    myStepper.step(0);                 // stop motor
 if (val == HIGH)     {                // sensor value high
   digitalWrite(ledPin, HIGH);         // turn led on
   myStepper.step(20);                 // run motor minium 20 steps
  int val = digitalRead(buttonPin);    // read button value
 if (val == LOW)                       // button value low
    digitalWrite(ledPin, LOW);         // turn led off
   myStepper.step(0);                  // stop motor

Stepper Mptor FL39ST34-0404A.pdf (106 KB)

I'm thinking, why bother? The power savings will be minimal compared to the 500mA the stepper motor is using while idling. If you must tho, read here about getting into Power Down Sleep Mode and waking up.

Please modify your post and use the code button </>

so your code looks like this

and is easy to copy to a text editor. See How to use the Forum Your code is too long for me to study quickly without copying to a text editor.

Putting the Arduino to sleep won’t save much energy, but disabling the motor will. IIRC the L298 has an enable pin (or two) that can be used to depower the motor.

However when you depower a stepper motor you lose control of the position and even if nothing external causes it to move it might jog one step in either direction when you re-power it. Maybe that does not matter.

It won’t do the motor or the Arduino any harm to leave them powered up.

Stepper Motor Basics
Simple Stepper Code

Why is the stepper using so much current when idle? Aren't the L298 outputs turned off when no stepper action is needed?

Hi What about the 7812 regulator will that last, feeding the whole lot. john

How do I turn the outputs of the driver off please ? enclosed is the driver pdf

l298n_dual_motor_controller_module.pdf (1020 KB)

Dedicated stepper drivers often have an automatic reduce-power-when-idle function - this takes advantage of the fact that a stepper's torque is larger at stationary than when running to reduce the current (50% is typical) when the motor hasn't moved for a while.

Dedicated stepper drivers are also chopper drivers, so that 50% current means 25% of the power consumed.

What current / resistance rating is your motor (your link describes many variations)?

Hi Motor as follows:

FL39ST34-0404A rated voltage current /phase res/phase induct/phase holding torque

12V 0.4 30 ohm 32mH 2100g-cm

OK, that's OK to drive from the L298. You could power it down completely by overriding what the Stepper library does and setting the output pins to all LOW of course.

Not all of your output pins are PWM so that complicates reducing the current on idle, but perhaps that can be done - you only have to set on of the pins on each winding to 50% duty cycle PWM to do this I think.