Arduino UNO; L298N Dual H-Bridge; Stepper motor rotation using a switch

Hi.

I have an Arduino Board connected to a L298N Dual H-Bridge which is connected to a stepper motor Nema 17 1,1kgf. I’m using a 9v 1200mA Source.

I need to program the motor to rotate 360 clockwise and 360 counter clockwise using a switch to invert rotation.
In other words → Press switch> Rotate 360 clockwise. Press Switch on other side > Rotate 360 counter clockwise.

I’ve tested this code and it works. It rotates 360º counter clockwise.

// Conexoes modulo - Arduino
int IN1 = 8;
int IN2 = 9;
int IN3 = 10;
int IN4 = 11;
int contador = 0;

void setup()
{
// Define os pinos como saida
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
}

int tempo = 10;

void loop() {
while( contador < 50 )
{
// Passo 1
digitalWrite(IN1, 1);
digitalWrite(IN2, 0);
digitalWrite(IN3, 1);
digitalWrite(IN4, 0);
delay(tempo);

// Passo 2
digitalWrite(IN1, 0);
digitalWrite(IN2, 1);
digitalWrite(IN3, 1);
digitalWrite(IN4, 0);
delay(tempo);

// Passo 3
digitalWrite(IN1, 0);
digitalWrite(IN2, 1);
digitalWrite(IN3, 0);
digitalWrite(IN4, 1);
delay(tempo);

// Passo 4
digitalWrite(IN1, 1);
digitalWrite(IN2, 0);
digitalWrite(IN3, 0);
digitalWrite(IN4, 1);
delay(tempo);

contador = contador + 1;
}
}

and this is the clockwise rotation

// Passo 1
digitalWrite(IN1, 1);
digitalWrite(IN2, 0);
digitalWrite(IN3, 0);
digitalWrite(IN4, 1);
delay(tempo);

// Passo 2
digitalWrite(IN1, 0);
digitalWrite(IN2, 1);
digitalWrite(IN3, 0);
digitalWrite(IN4, 1);
delay(tempo);

// Passo 3
digitalWrite(IN1, 0);
digitalWrite(IN2, 1);
digitalWrite(IN3, 1);
digitalWrite(IN4, 0);
delay(tempo);

// Passo 4
digitalWrite(IN1, 1);
digitalWrite(IN2, 0);
digitalWrite(IN3, 1);
digitalWrite(IN4, 0);
delay(tempo);

Sorry for the words in portuguese. I’m brazilian.

I attached the schematic i’m using right now and the switch i found (i don’t know if it works for this project). Please ignore the yellow arrow saying “manter jumper” in the schematics.

Thanks in advance!

What exactly is your question? Your program rotates clock-wise correctly and presumably also would work to rotate counter-clock-wise. Your program doesn't have anything in it yet to detect the Switch. Is that your question? How to detect the two Switches?

Save yourself a lot of trouble and use the Stepper or AccelStepper library to do the detailed motor control.

Save yourself even more trouble and use a specialized stepper motor driver. See Stepper Motor Basics

...R

guimcast:
I need to program the motor to rotate 360 clockwise and 360 counter clockwise using a switch to invert rotation.
In other words -> Press switch> Rotate 360 clockwise. Press Switch on other side > Rotate 360 counter clockwise.

Your switch is single pole, open or closed.
You need to detect when switch change state and move your stepper according to that.

Now closed, previous open -> move stepper clockwise.
Now open, previous closed -> move stepper counter clockwise.

You need to keep track of two switch states, current and previous.

JaBa:
What exactly is your question? Your program rotates clock-wise correctly and presumably also would work to rotate counter-clock-wise. Your program doesn't have anything in it yet to detect the Switch. Is that your question? How to detect the two Switches?

Sorry if I wasn't clear. Yes, that's the question, how to detect the switches.

Robin2:
Save yourself a lot of trouble and use the Stepper or AccelStepper library to do the detailed motor control.

Save yourself even more trouble and use a specialized stepper motor driver. See Stepper Motor Basics

...R

Thanks, i'm reading it right now and i just saw that what i chose, hardware wise, is poor as you said "These can be made to control a stepper motor but they are a very poor choice".

Gabriel_swe:
Your switch is single pole, open or closed.
You need to detect when switch change state and move your stepper according to that.

Now closed, previous open -> move stepper clockwise.
Now open, previous closed -> move stepper counter clockwise.

You need to keep track of two switch states, current and previous.

Good way to see it, thanks! I still need to know which pins to connect and how.

But anyway guys, thanks for the advices, I'll study a lil bit more and then post the results here!

Good start is Built-In Examples > 02.Digital > StateChangeDetection