Uploaded by atul narkhede

abc1

advertisement
Here’s a table that summarizes the pins and corresponding motor direction. These assumes
you are following the same Fritzing diagram above.
IN1
IN2
IN3
IN4
Direction
0
0
0
0
Stop
1
0
1
0
Forward
0
1
0
1
Reverse
1
0
0
1
Left
0
1
1
0
Right
Speed Control?
Speed control is also possible with the L298N motor driver. All you need is feed PWM
signals to the motor enable pins. The speed of the motor will vary according to the width of
the pulses. The wider the pulses, the faster the motor rotates. How fast the motor rotates for
a given pulse width will vary from motor to motor even if they look exactly the same. Thus,
the actual pulse width must be derived through experiment.
Using the L298N with Arduino
[the_ad id="3059"]
An example diagram for connecting the L298N motor controller board to an Arduino is
shown:
Here’s an example Arduino sketch utilizing the diagram above:
//Motor Connections
//Change this if you wish to use another diagram
#define EnA 10
#define EnB 5
#define In1 9
#define In2 8
#define In3 7
#define In4 6
void setup()
{
// All motor control pins are outputs
pinMode(EnA, OUTPUT);
pinMode(EnB, OUTPUT);
pinMode(In1, OUTPUT);
pinMode(In2, OUTPUT);
pinMode(In3, OUTPUT);
pinMode(In4, OUTPUT);
}
void goStraight()
{
//run both motors in the same direction
// turn on motor A
digitalWrite(In1, HIGH);
digitalWrite(In2, LOW);
// set speed to 150 out 255
analogWrite(EnA, 200);
// turn on motor B
digitalWrite(In3, HIGH);
digitalWrite(In4, LOW);
// set speed to 150 out 255
analogWrite(EnB, 200);
delay(2000);
// now turn off motors
digitalWrite(In1, LOW);
digitalWrite(In2, LOW);
digitalWrite(In3, LOW);
digitalWrite(In4, LOW);
}
void loop()
{
goStraight();
delay(1000);
}
You can modify this sketch to include a function for going backwards, turning left and
turning right. Just follow the table above.
Download