Stepper with 2 buttons code problem

I have a problem, I connected the M42SP-6NKA MITSOMI stepper motor. I connected it to the arduino via the L9110 motor driver on pins 8, 9, 10, 11. I connected buttons to pins 2 and 4. the code should work when one button is pressed the motor rotates 180° clockwise, and when the second button is pressed counterclockwise 180° counterclockwise. I took the already made code with the Arduino IDE from the example and added 2 buttons to it. When I test it, I press the button, the engine does not spin, but there is a buzzing sound in the engine while the button is pressed. The stepper and the driver are powered by 12v, as it says is allowed in the datasheet.

The code I tried


const int stepsPerRevolution = 48;  // Change this according to the number of steps per revolution of your motor

// Definition of button pins
const int buttonPinClockwise = 2;
const int buttonPinCounterClockwise = 3;

// Initialize Stepper object
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
  // Set button pins as inputs with pull-up resistors
  pinMode(buttonPinClockwise, INPUT_PULLUP);
  pinMode(buttonPinCounterClockwise, INPUT_PULLUP);

  // Initialize serial communication
  Serial.begin(9600);

  // Set motor speed
  myStepper.setSpeed(60);
}

void loop() {
  // Read button states
  int clockwiseButtonState = digitalRead(buttonPinClockwise);
  int counterClockwiseButtonState = digitalRead(buttonPinCounterClockwise);

  // If the clockwise button is pressed, rotate the motor clockwise
  if (clockwiseButtonState == LOW) {
    Serial.println("Rotating clockwise");
    myStepper.step(stepsPerRevolution);
    delay(500);  // Optional delay
  }

  // If the counter-clockwise button is pressed, rotate the motor counter-clockwise
  if (counterClockwiseButtonState == LOW) {
    Serial.println("Rotating counter-clockwise");
    myStepper.step(-stepsPerRevolution);
    delay(500);  // Optional delay
  }
}

```

Code fron the arduino IDE examples


/*
 Stepper Motor Control - one revolution

 This program drives a unipolar or bipolar stepper motor.
 The motor is attached to digital pins 8 - 11 of the Arduino.

 The motor should revolve one revolution in one direction, then
 one revolution in the other direction.


 Created 11 Mar. 2007
 Modified 30 Nov. 2009
 by Tom Igoe

 */

#include <Stepper.h>

const int stepsPerRevolution = 48;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
  // set the speed at 60 rpm:
  myStepper.setSpeed(60);
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {
  // step one revolution  in one direction:
  Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);
  delay(500);

  // step one revolution in the other direction:
  Serial.println("counterclockwise");
  myStepper.step(-stepsPerRevolution);
  delay(500);
}

Show your wiring diagram.

Your topic does not indicate a problem with the IDE and hence has been moved to a more suitable location on the forum.


Does the original example work as expected?

Do you have an Ohmmeter to check the motor wiring?
From the datasheet:

The datasheet claims the peak phase current is 400mA and resistance is 5Ω, if you put 12V across 5Ω, the current would be 2400mA. 400mA * 5Ω = 2V.
You probably need a "chopper" driver with adjustable current control like a DRV8825.
https://www.pololu.com/product/2133

Yes



+1 - according to the datasheet, the current of 400mA must be controlled by the stepper driver. You cannot use it with 12V or 24V directly.

Hi, @thegame007

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

What are you using as a power supply?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

@TomGeorge

Hi, @thegame007
Thanks for the diagram.

Have you got the gnd of the UNO connected to the gnd of the L9100 driver?

It isn't shown in your schematic.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Hi, @TomGeorge
Thank you for your reply

Yes, I connected gnd from arduino and gnd from l9110.
I think it's not a connection problem but a code problem. The code that does not use buttons works perfectly fine, but the one with buttons does not.

Thanks, David

Why does the stepper not work on 12 volts but only on 9 volts? In the data sheet, both the stepper and the driver say that they can work on 12 volts.
M42SP-6NK_MITSUMI.pdf (105.9 KB)
L9110.PDF (319.4 KB)

Not only the Voltage, but also the current is important. We already told you, that the driver is not suitable for your stepper.
See Post from @JCA34F

2.4A is too much for the stepper, and also for the L9110. Probably it simply shuts off.
Can you measure the current?

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