Control DC Motor Speed Using Potentiometer + L298n + Arduino
by AYassineLebouiha in Circuits > Arduino
31051 Views, 15 Favorites, 0 Comments
Control DC Motor Speed Using Potentiometer + L298n + Arduino
Hello there,
Here in this tutorial we gonna try to control a DC motor speed using a potentiometer, L298n Motor driver and an Arduino board, (we won't complicate things with directions or other functions).
If it's your first time using the L298n Driver with an Arduino board, you can check my previous Instructable, it shows step by step how to use it.
https://www.instructables.com/id/How-to-Use-L298n-...
For this we gonna need:
- Arduino Board (here I'm using UNO)
- 1k Potentiometer
- L298n Motor driver
- DC motor
- Jump wires, breadboard if you want, and something to power the module (here I'll use 9v battery).
Wiring
So to not make it long, this is the wiring I'm using:
Potentiometer : (from left to right) GND (A0) 5v
L298n: IN1 IN2 ENA with respectively D8,D9 and D10 From Arduino and the DC motor is in Out1 and Out2
The driver is powerd by 9v battery wired with +12v and GND ( don't forget to ground the Arduino too)
And don't forget if you're using another wiring that the ENA here wired with D10 should always be wired with a pin that can deliver PWM signal
Code
Here I'll leave you a written code or a .ino code.
I've added some comments so you can easily know what we are doing.
Hope you like it, and find it simplified to use in your own projects.
Code:
//This code is to control the speed of a DC motor by a potentiometer using l298n driver
//We read the value from the analog input, calibrate it then inject to the module //Refer to Surtrtech youtube channel for more information
int in1 = 8; //Declaring where our module is wired int in2 = 9; int ConA = 10;// Don't forget this is a PWM DI/DO int speed1;
void setup() { pinMode(8, OUTPUT); pinMode(9, OUTPUT); pinMode(10, OUTPUT); }
void TurnMotorA(){ //We create a function which control the direction and speed digitalWrite(in1, LOW); //Switch between this HIGH and LOW to change direction digitalWrite(in2, HIGH); speed1 = analogRead(A0); speed1 = speed1*0.2492668622; //We read thea analog value from the potentiometer calibrate it analogWrite(ConA,speed1);// Then inject it to our motor } void loop() { TurnMotorA(); //one function that keeps looping you can add another one with different direction or stop
}