├── .DS_Store ├── H-Bridge with L298N.fzpz ├── keywords.txt ├── README.md ├── examples ├── test_drive │ └── test_drive.ino └── individual_motor_test │ └── individual_motor_test.ino ├── L298N.h └── L298N.cpp /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yohendry/arduino_L298N/HEAD/.DS_Store -------------------------------------------------------------------------------- /H-Bridge with L298N.fzpz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yohendry/arduino_L298N/HEAD/H-Bridge with L298N.fzpz -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | L298N KEYWORD1 2 | drive_motors KEYWORD2 3 | drive_motor KEYWORD2 4 | motors KEYWORD2 5 | setup_motors KEYWORD2 6 | setup_motor KEYWORD2 7 | full_stop KEYWORD2 8 | backward KEYWORD2 9 | turn_left KEYWORD2 10 | turn_right KEYWORD2 11 | forward KEYWORD2 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ARDUINO L298N 2 | ============= 3 | 4 | L298N Dual H-Bridge library for controlling via PWN 2 motors 5 | 6 | SCHEMATICS 7 | ========== 8 | designed using fritzing 9 | ![alt tag](http://oi62.tinypic.com/314pj6v.jpg) 10 | 11 | I take the L298N Fritzing part from this repo https://github.com/ComputacaoNaEscola/Fritzing 12 | and modify it, adding the 4 extra pings for ENA,ENB and 2 5V+ pings for ENA and ENB pins 13 | 14 | ![alt tag](http://oi62.tinypic.com/302cubn.jpg) 15 | -------------------------------------------------------------------------------- /examples/test_drive/test_drive.ino: -------------------------------------------------------------------------------- 1 | #include 2 | const int ENA = 6; 3 | const int IN1 = 8; 4 | const int IN2 = 7; 5 | const int IN3 = 2; 6 | const int IN4 = 4; 7 | const int ENB = 3; 8 | L298N driver(ENA,IN1,IN2,IN3,IN4,ENB); 9 | int time_delay = 500; 10 | int speed = 150; 11 | void setup() 12 | { 13 | } 14 | 15 | void loop() 16 | { 17 | driver.forward(speed,time_delay); 18 | driver.full_stop(time_delay); 19 | driver.turn_right(speed,time_delay); 20 | driver.full_stop(time_delay); 21 | driver.turn_left(speed,time_delay); 22 | driver.full_stop(time_delay); 23 | driver.backward(speed,time_delay); 24 | } 25 | -------------------------------------------------------------------------------- /examples/individual_motor_test/individual_motor_test.ino: -------------------------------------------------------------------------------- 1 | #include 2 | const int ENA = 6; 3 | const int IN1 = 8; 4 | const int IN2 = 7; 5 | const int IN3 = 2; 6 | const int IN4 = 4; 7 | const int ENB = 3; 8 | L298N driver(ENA,IN1,IN2,IN3,IN4,ENB); 9 | int time_delay = 500; 10 | int speed = 150; 11 | void setup() 12 | { 13 | } 14 | 15 | void loop() 16 | { 17 | drive(L298N::MOTOR_A,HIGH,LOW); 18 | driver.full_stop(10); 19 | drive(L298N::MOTOR_A,LOW,HIGH); 20 | driver.full_stop(10); 21 | drive(L298N::MOTOR_B,HIGH,LOW); 22 | driver.full_stop(10); 23 | drive(L298N::MOTOR_B,LOW,HIGH); 24 | driver.full_stop(10); 25 | drive(L298N::MOTOR_A,LOW,HIGH); 26 | drive(L298N::MOTOR_B,LOW,HIGH); 27 | driver.full_stop(10); 28 | drive(L298N::MOTOR_A,HIGH,LOW); 29 | drive(L298N::MOTOR_B,HIGH,LOW); 30 | driver.full_stop(10); 31 | } 32 | 33 | void drive(int motor,int state1,int state2) { 34 | driver.setup_motor(motor,state1,state2); 35 | driver.drive_motor(motor,speed); 36 | delay(time_delay); 37 | } -------------------------------------------------------------------------------- /L298N.h: -------------------------------------------------------------------------------- 1 | /* 2 | L298N.h - Library for L298N motor driver 3 | Created by Yohendry Hurtado, 28 dec 2014 4 | Released into the public domain. 5 | */ 6 | #ifndef L298N_h 7 | #define L298N_h 8 | 9 | #include "Arduino.h" 10 | 11 | class L298N 12 | { 13 | public: 14 | static const int MOTOR_A = 0; 15 | static const int MOTOR_B = 1; 16 | L298N(int ena, int in1, int in2, int in3, int in4, int enb); 17 | void drive_motors(int speed); 18 | void drive_motor(int motor_index, int speed); 19 | void setup_motors(int state1, int state2, int state3, int state4); 20 | void setup_motor(int motor_index, int state1, int state2); 21 | void forward(int speed, int delay_time); 22 | void full_stop(int delay_time); 23 | void turn_right(int speed, int delay_time); 24 | void turn_left(int speed, int delay_time); 25 | void backward(int speed, int delay_time); 26 | private: 27 | struct Motor; 28 | int _ena; 29 | int _in1; 30 | int _in2; 31 | int _in3; 32 | int _in4; 33 | int _enb; 34 | }; 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /L298N.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | L298N.cpp - Library for L298N motor driver 3 | Created by Yohendry Hurtado, 28 dec 2014 4 | Released into the public domain. 5 | */ 6 | 7 | #include "Arduino.h" 8 | #include "L298N.h" 9 | 10 | struct Motor { 11 | int in1; 12 | int in2; 13 | int pwn; 14 | }; 15 | 16 | Motor motors[2]; 17 | 18 | L298N::L298N(int ena, int in1, int in2, int in3, int in4, int enb) { 19 | pinMode (ena, OUTPUT); 20 | pinMode (in1, OUTPUT); 21 | pinMode (in2, OUTPUT); 22 | pinMode (in3, OUTPUT); 23 | pinMode (in4, OUTPUT); 24 | pinMode (enb, OUTPUT); 25 | 26 | motors[0].in1 = in1; 27 | motors[0].in2 = in2; 28 | motors[0].pwn = ena; 29 | 30 | motors[1].in1 = in3; 31 | motors[1].in2 = in4; 32 | motors[1].pwn = enb; 33 | } 34 | 35 | void L298N::drive_motors(int speed) { 36 | this->drive_motor(this->MOTOR_A,speed); 37 | this->drive_motor(this->MOTOR_B,speed); 38 | } 39 | 40 | void L298N::drive_motor(int motor_index, int speed) { 41 | analogWrite(motors[motor_index].pwn,speed); 42 | } 43 | 44 | void L298N::forward(int speed, int delay_time) { 45 | this->setup_motors(HIGH,LOW,HIGH,LOW); 46 | this->drive_motors(speed); 47 | delay(delay_time); 48 | } 49 | 50 | void L298N::turn_right(int speed, int delay_time) { 51 | this->setup_motors(LOW,HIGH,HIGH,LOW); 52 | this->drive_motors(speed); 53 | delay(delay_time); 54 | } 55 | 56 | void L298N::turn_left(int speed, int delay_time) { 57 | this->setup_motors(HIGH,LOW,LOW,HIGH); 58 | this->drive_motors(speed); 59 | delay(delay_time); 60 | } 61 | 62 | void L298N::backward(int speed, int delay_time) { 63 | this->setup_motors(LOW,HIGH,LOW,HIGH); 64 | this->drive_motors(speed); 65 | delay(delay_time); 66 | } 67 | 68 | void L298N::full_stop(int delay_time) { 69 | this->setup_motors(LOW,LOW,LOW,LOW); 70 | this->drive_motors(0); 71 | delay(delay_time); 72 | } 73 | 74 | 75 | void L298N::setup_motors(int state1, int state2, int state3, int state4) { 76 | L298N::setup_motor(this->MOTOR_A,state1,state2); 77 | L298N::setup_motor(this->MOTOR_B,state3,state4); 78 | } 79 | 80 | void L298N::setup_motor(int motor_index, int state1, int state2) { 81 | digitalWrite(motors[motor_index].in1, state1); 82 | digitalWrite(motors[motor_index].in2, state2); 83 | } 84 | --------------------------------------------------------------------------------