Skip to content

Instantly share code, notes, and snippets.

@john212
Last active February 7, 2016 04:55
Show Gist options
  • Save john212/0c06a234a9ea945e4d38 to your computer and use it in GitHub Desktop.
Save john212/0c06a234a9ea945e4d38 to your computer and use it in GitHub Desktop.
Udemy Arduino Step-by-Step Course Using the L298N H Bridge To Control Two Vex 393 DC Motors
/**************************************************
Udemy Arduino Step-by-Step Course
Section 5 DC Motors 46 Part 1 Using the L298N H Bridge
by J. Cantlin
February 6, 2016
***************************************************/
/**************************************************
Description of Program
A L298N DC Motor Controller is used to control two
DC Motors. The motors will rotate in opposite direction
while the speed ramps from high speed to low speed
over the full range
***************************************************/
/**************************************************
L298N DC Motor Controller
This controller, mounted on a breakout board, can control
one or two DC Motors or DC Stepper Motors.
A supply voltage of about 6vdc to a max of 35vdc is supplied
to the L298N board. The IC converts the low voltage signals
form the Arduino to the appropriate higher voltage signals to
control the voltage (speed) and direction or rotation (polarity,
positive or negative) of the two motors.
Summary of L298N H Bridge Contoller Pins/Terminals/Jumpers Used:
OUT1 = + Supply Voltage to "LEFT" DC Motor = DC Motor 1
OUT2 = - Supply Voltage to "LEFT" DC Motor = DC Motor 1
OUT3 = - Supply Voltage to "RIGHT" DC Motor = DC Motor 2
OUT4 = + Supply Voltage to "RIGHT" DC Motor = DC Motor 2
12V Jumper = On since supply volts = 7.2vdc, take off if supply > 12vdc
if off, the 5vdc supply pin is not used, not using this anyway.
+12V In = Supply is 7.2vdc for this project. About 6-35vdc OK.
GND = Ground Note: common ground to Arduino should be connected.
+5V Out = Not Used. Regulated 5vdc available if <=12vdc supplied.
ENA1 Jumper = On, Enable Motor 1
IN1 = Arduino Digital Pin 4 = HIGH/LOW = CW/CCW Rotation Motor 1
IN2 = Arduino Digital Pin 5 = PWM = Speed Motor 1
IN3 = Arduino Digital Pin 6 = PWM = Speed Motor 2
IN4 = Arduino Digital Pin 7 = HIGH/LOW = CW/CCW Rotation Motor 2
ENA2 Jumper = On, Enable Motor 2
Note: the built-in ports on the L298 boards ports reverses Motor 1 and 2's rotation.
They also reverse the PWM signal, 0 duty cycle becomes 100% and
255 duty cycle becomes 0% = 0 volts = 0 speed.
A good L298N tutorial referenced in the Udemy course is at:
http://tronixlabs.com/news/tutorial-l298n-dual-motor-controller-module-2a-and-arduino/
***************************************************/
/**************************************************
DC Motors
Two Vex 393 DC Motors are used in this project. These motors
can run past 12vdc but are designed for running at 7.2vdc which
was the voltage supplied to the L298N controller in this project.
More information on the 393 motors is at:
http://www.vexrobotics.com/wiki/2-Wire_Motor_393.
***************************************************/
/**************************************************
Summary of Arduino Uno Analog Pins Used:
A0 =
A1 =
A2 =
A3 =
A4 =
A5 =
***************************************************/
/**************************************************
Summary of Arduino Uno Digital Pins Used:
To use PWM call analogWrite(pin, dutyCycle), where dutyCycle
is a value from 0 to 255, and pin is one of the PWM pins
(3, 5, 6, 9, 10, or 11). A square wave is output, there is
no control over frequency but higher duty cycle = higher volts.
00 =
01 =
02 =
03 =
04 = Motor 1 = "LEFT" Motor Direction of Rotation
05 = Motor 1 = "LEFT" Motor Speed via PWM
06 = Motor 2 = "RIGHT" Motor Speed via PWM
07 = Motor 2 = "RIGHT" Motor Direction of Rotation
08 =
09 =
10 =
11 =
***************************************************/
//Variable assignments
int M1R = 4; //Motor 1 Rotation - High/Low changes direction
int M1S = 5; //Motor 1 Speed via PWM
int M2S = 6; //Motor 2 Speed via PWM
int M2R = 7; //Motor 2 Rotation - High/Low changes direction
void setup()
{//****start setup function****
//Serial.begin(9600); //set the serial port to 9600 baud if monitor is used.
pinMode(M1R, OUTPUT); //M1 rotation control
pinMode(M2R, OUTPUT); //M2 rotation control
}//****endof setup function****
void loop()
{//****start infinite loop****
int value;
for(value = 0 ; value <= 255; value+=1)
{
digitalWrite(M1R,HIGH); //Rotation Control
digitalWrite(M2R,HIGH); //Rotation Control
analogWrite(M1S, value); //PWM Speed Control
analogWrite(M2S, value); //PWM Speed Control
delay(30);
}
delay(5000); //let the motor/electronics rest/cool
for(value = 0 ; value <= 255; value+=1)
{
digitalWrite(M1R,LOW); //Rotation Control
digitalWrite(M2R,LOW); //Rotation Control
analogWrite(M1S, value); //PWM Speed Control
analogWrite(M2S, value); //PWM Speed Control
delay(30);
}
}//****endof infinite loop****
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment