Servo Motor position control using l298n motor driver

Hello everyone,

I am trying to control the position of 2 servo motors (Servo A and Servo B) using l298n on proteus to see the signal waveform on the virtual oscilloscope. I wrote the code but I don't get what I am doing wrong.

this is the code

#include <Servo.h>

Servo A; 
Servo B; 
int IN1 = 9;
int IN2 = 10;
int IN3 = 11;
int IN4 = 12;

int EN1 = 2;
int EN2 = 3;
int EN3 = 4;
int EN4 = 5;

int D0 = 0;
int D1 = 1;
int pos1 = 0;
int pos2 = 0;
int pos3 = 90;
int pos4 = -90;
int i = 0;

void setup() 
{
 pinMode(IN1, OUTPUT);
 pinMode(IN2, OUTPUT);
 pinMode(IN3, OUTPUT);
 pinMode(IN4, OUTPUT); 
 
 pinMode(EN1, OUTPUT);
 pinMode(EN2, OUTPUT);
 pinMode(EN3, OUTPUT);
 pinMode(EN4, OUTPUT); 

 digitalWrite(IN1, LOW);
 digitalWrite(IN2, LOW);
 digitalWrite(IN3, LOW);
 digitalWrite(IN4, LOW);

 digitalWrite(EN1, HIGH);
 digitalWrite(EN2, HIGH);
 digitalWrite(EN3, HIGH);
 digitalWrite(EN4, LOW);
 
 A.attach(IN1);
 B.attach(IN2);
}

void loop() 
{
 for (pos1 = 0; pos1 >= -90; pos1 -= 1) 
 {
   A.write(pos1);
   delay(15);
 }
 
 delay(2000);
 
 for (pos2 = 0; pos2 <= 90; pos2 += 1)
 {
   B.write(pos2);
   delay(15);
 }

 delay(2000);

 if( pos1 == -90)
 {
   if( pos2 == 90)
   {
     for (i = 0; i<=90; i+=1)
     {
     A.write(pos4+=1);
     B.write(pos3-=1);
     delay(15);
     }
   }
 }
 delay(2000);
}

What do you mean by "wrong"?

Please remember to use code tags when posting code

What's all the EN and IN stuff?

What do servos have to do with an L298N? The servo library uses any standard digital pins.

Also write() takes a value 0 to 180. So negative numbers like -90 are invalid.

Steve

Sorry, it's my first time writing on this forum. from the picture (Annotation-2020-06-25-002819 hosted at ImgBB — ImgBB) I am trying to control the position of the servo motors A and B where A goes to -90 degrees with a delay of 2 seconds after it reaches -90 degrees, then Servo B starts moving to 90 degrees and after it reaches 90 degrees, there will also be a delay of 2 seconds. once both servos reach -90 and 90 degrees both will start moving to 0 degrees.

Offset the -90 degrees by +90 degrees - the Servo library doesn't manage negative ngkes, from what I remember

TheMemberFormerlyKnownAsAWOL:
Offset the -90 degrees by +90 degrees - the Servo library doesn't manage negative ngkes, from what I remember

would you mind showing me how to offset it to -90? I am quite new to Arduino myself.

The servo can't move from -90 to 90. It should move from 0 to 180 with 90 as the centre point. So your for loops will go from 90 to 0 or from 90 to 180.

Steve

Just add 90 to all your positions:

#include <Servo.h>

Servo A;
Servo B;
int IN1 = 9;
int IN2 = 10;
int IN3 = 11;
int IN4 = 12;

int EN1 = 2;
int EN2 = 3;
int EN3 = 4;
int EN4 = 5;

int D0 = 0;
int D1 = 1;
int pos1 = 90;
int pos2 = 90;
int pos3 = 180;
int pos4 = 0;
int i = 0;

void setup()
{
 pinMode(IN1, OUTPUT);
 pinMode(IN2, OUTPUT);
 pinMode(IN3, OUTPUT);
 pinMode(IN4, OUTPUT);
 
 pinMode(EN1, OUTPUT);
 pinMode(EN2, OUTPUT);
 pinMode(EN3, OUTPUT);
 pinMode(EN4, OUTPUT);

 digitalWrite(IN1, LOW);
 digitalWrite(IN2, LOW);
 digitalWrite(IN3, LOW);
 digitalWrite(IN4, LOW);

 digitalWrite(EN1, HIGH);
 digitalWrite(EN2, HIGH);
 digitalWrite(EN3, HIGH);
 digitalWrite(EN4, LOW);
 
 A.attach(IN1);
 B.attach(IN2);
}

void loop()
{
 for (pos1 = 90; pos1 >= 0; pos1--)
 {
   A.write(pos1);
   delay(15);
 }
 
 delay(2000);
 
 for (pos2 = 90; pos2 <= 180; pos2++)
 {
   B.write(pos2);
   delay(15);
 }

 delay(2000);

 if( pos1 == 0) // Never true.  After the loop, pos1 is -1;
 {
   if( pos2 == 180)  // Never true.  After the loop, pos2 is 181
   {
     for (i = 90; i<=180; i++)
     {
    // WARNING! The global variables pos3 and pos4 are not being re-initialized before 
    // these loops so the values soon go out of the allowed range of 0 to 180.
     A.write(++pos4);  // After each time through this loop pos4 will be 90, then 180, then 270...
     B.write(--pos3);  // After each time through this loop pos3 will be 90, then 0, then -90...
     delay(15);
     }
   }
 }
 delay(2000);
}
1 Like

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.

Have you looked in the IDE at the servo examples?
There is even a sweep example.

OPs picture.

Tom.... :slight_smile:
PS. How are you using the L298 to control the servo?

L298 is an H-bridge driver.
Servos need a 5V pulse, 1ms to 2ms wide, repeating every 20ms (50 Hz rate).
Why would you use a L298 for that?