

If an external inverter is available, a single PWM can be used to drive the motor along with its inverted signal. The enable pin is tied to HIGH by a jumper. Lock Anti-phase mode drives each H-bridge input with a separate PWM pin, one of which is out of phase with the other.

Spd_bytes = speed & 0xFF // obtain speed value from lower bitsĭigitalWrite(PIN_IN1,spd_bytes) // directionĪnalogWrite(PIN_IN2,spd_bytes) // speed value for PWM If (speed > 8) & 0x01 // strip off direction bit (using 9th bit)

Set speed for left motor (looking from rear - speed range is -255 to 255) In Arduino, you don't need to use pinMode on pins that use PWM (pin 5). PinMode(PIN_IN2,OUTPUT) // Define pin 15 for output (IN1) PinMode(PIN_IN1,OUTPUT) // Define pin 14 for output (IN0) Void set_mtr_speed(int16_t speed) // function prototype #define PIN_ENA_PWM 5 // must be PWM capable pin define pins for the motor driver board (note: Enable pin not jumpered) Motor leads may need to be reversed to obtain proper direction. Routines use drive/coast mode to drive L298N H-Bridge module (common on small robots).ĭifferences in stop methods will be minimum for no or light load conditions. Motor runs in reverse direction for 2 seconds. Motor runs in forward direction for 2 seconds.ģ. Program cycles through the following sequence:ġ.
L298N MOTOR DRIVER SERVO MOTOR CODE
Using HIGH on both inputs and enable turns on transistors Q1 and Q3, shorting the motor through the upper transistors of the H-bridge.Ĭurrent path when Braking through the lower transistors.Īrduino code example code for Drive/Coast mode on L298N. Using LOW on both inputs, and setting enable HIGH, turns on transistors Q2 and Q4, shorting the motor through the lower transistors of the H-bridge. To correct for this, braking can be applied when the deacceleration is above a given value or if the controller calls for a zero speed. For a robot, a coast stop can cause the robot’s momentum to overshoot a desired stop location. It is possible to brake stop the motor in the Drive/Coast mode, by setting the two inputs to the same state (either LOW or HIGH) and having the enable pin high. When the Enable pin is low, the H-bridge state is the same as for the forward direction (i.e., all four H-bridge transistors off).Ĭurrent path when PWM on enable pin is HIGH in Reverse direction. The motor current flows as shown below when the Enable pin is high.

To reverse the motor’s direction the 2 input pin’s values are reversed. Below shows the current path for the two Enable pin states when moving forward.Ĭurrent path when PWM on enable pin is HIGH in Forward direction.Ĭurrent path when PWM on enable pin is LOW (all 4 H-bridge transistors are off). Motor speed is determined by the modulation value of the PWM pin. Since ENA is using a PWM signal, the L289N will alternate between Forward (or reverse) and Coast columns of the L298N logic table. It connects the two inputs of the H-bridge to General Purpose Input/Output pins (GPIO) and the enable pin to a pulse width modulated pin (PWM). This mode alternates between driving the motor and disenabling the H-bridge (coasting). It is a variation of sign-magnitude and is called by many names.
L298N MOTOR DRIVER SERVO MOTOR HOW TO
The Drive/Coast Mode is the most common found connection method for the L298N when searching the internet on how to use the L298N on small robots. The Inputs / Output logic table for the L298N: PIN The symbolic circuit for one of the L298N’s two H-bridges is shown below and will be used for the rest of the article. The L298N uses NPN bipolar transistors for the H-bridge. The L298N integrated circuit surrounds the four transistor H-bridge with control circuitry that makes it easy to use and protects the H-bridge from damage such as shoot-through. Each H-bridge can control one DC brushed motor. It has a maximum current specification of 2.0 A per channel with a peak current of 3.0 A per channel (from STI spec sheet). It is widely used on small inexpensive hobbyist robots. The L298N is a dual H-bridge chip often packaged nicely on a PCB module. Code examples will be for the Arduino Uno (or with small modifications the Mega2560). This article discusses how to connect the L298 motor driver in three modes: drive/coast, sign-magnitude, and lock anti-phase.
