Arduino DC Motor Control using L298N
We will now see a simple circuit where we control the speed and direction of a DC Motor using Arduino and L298N IC. You need few additional components for this project and the complete list is given below. In the Arduino DC Motor Control using L298N Project, we will control both the functionalities of a simple DC Motor i.e. speed and direction of rotation using a combination of PWM Signal and L298N (H-Bridge).
Code
| int mot1 = 8; |
| int mot2 = 9; |
| int en1 = 10; |
| int dir = 6; |
| bool state = true; |
| int nob = A0; |
| int val=0; |
|
|
|
|
|
|
| void setup() |
| { |
| pinMode(mot1,OUTPUT); |
| pinMode(mot2,OUTPUT); |
| pinMode(en1,OUTPUT); |
| pinMode(dir,INPUT_PULLUP); |
|
|
| } |
|
|
| void loop() |
| { |
|
|
| val = analogRead(nob); |
|
|
| analogWrite(en1, val / 4); |
|
|
| if(digitalRead(dir)==LOW) |
| { |
| state=!state; |
| while(dir==LOW); |
| delay(300); |
| } |
| if(state) |
| { |
| digitalWrite(mot1,HIGH); |
| digitalWrite(mot2,LOW); |
| } |
| else |
| { |
| digitalWrite(mot1,LOW); |
| digitalWrite(mot2,HIGH); |
| } |
|
|
|
|
| } |