Control DC motor speed using potentiometer + L298n + Arduino
Hello there,
Welcome to this tutorial where we see how to control DC motor speed using a potentiometer, the l298n motor driver and an Arduino board.
First If you’re not familiar with this module you can watch my previous tutorial here’s a video about how to use the L298n Motor driver with Arduino, where I explain step by step how to control your DC motor: Step by step on how to use the L298n dual H-bridge driver with Arduino
If you’re already familiar we can start from this:
First, I’m just gonna wire the potentiometer (I used 10k) with the Arduino board and calibrate it to get the values from 0-255 so it’s adapted to our module. I added this step so you know that your potentiometer is working fine and also you are using the same Analog-digital coding bits.
Wiring 1:
Code 1: Calibrating the potentiometer so it suits the values needed for the driver
This is the first code, it only uses the Arduino with a potentiometer wired as shown below, the serial monitor will constantly show the values (0-255), normally an analog input will give you values from (0-1023), but here we did a mapping of the values.
The code here could be cleaner if I used a “Map” function, but sometimes you forget about these things 😐
1 2 3 4 5 6 7 8 9 10 11 12 |
void setup() { Serial.begin(9600); } void loop() { int value = analogRead(A0); //declaring and reading value from the pin value = value*0.2492668622; // doing calibration to change range from 0-1023 to 0-255 the number //is obtained by 255/1023 Serial.println(value); } |
Wiring 2: Add the module and dc motor

I spoke before about power and usage of the module please check the other tutorial. Don’t forget !! The analog pin of the L298n (EnA/EnB) should be used with an Arduino PWM pin.
Code 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
//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 https://Surtrtech.com 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 } |
Categories
Yassine View All
Automation and Electrical Engineer, Electronics amateur trying to share my little projects.
Hello my friend.for one motor works perfectly.How about to control two motors?
You just add another potentiometer or keep only one, then add in the move functions other pins (3 and 4) for other motor
I want with two potentiometers to control the speed of one dc motor(one pot for forward and the other one for backward).How can i do this?
you don’t need to potentiometers. You can just use a h-bridge o reverse polarity.
If it’s you who commented on the video, you can contact me on facebook
Hello sir.I am the same guy.Can you please give me your mail.I dont have facebook.Thank you again.
Contact me here: “variety.tivi@gmail.com”
I have a motor driver (MD10-POT) is able to control the speed and direction of your DC motor without using a microcontroller or writing a single line of programming code. Very easy installation. No need wiring so much. The product link below :
https://www.cytron.io/p-md10-pot
Check it out.
Error in code that – redefinition of ‘void setup()’
How remove this?
Maybe you used ” void setup” more than once in the code, or maybe your sketch has multiple tabs if you drag sketch and put them in the IDE, you may need to create a sketch with one tab only
can you teach me how to control 240v motor using arduino uno?
If you want to control the Start/Stop and direction, you can do it using some relays and contactors, check how it’s done (Electrical wirings…) then when controlling the relays you can do it by Arduino.
If you want to control the speed too it can be a bit tricky, check how inverters work (generally AC-DC then DC-DC and DC-AC)
Thank you for this great tutorial, it was very clear and helpful!