Skip to content

Control DC motor speed using potentiometer + L298n + Arduino

 

 

Hello everyone,

Welcome to this tutorial where I show 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

 

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

 

Code 2:

//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

}

 

Yassine View All

Electronics, Automation engineering student... I try to share my little experience with other electronics amateur.

13 thoughts on “Control DC motor speed using potentiometer + L298n + Arduino Leave a comment

  1. 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?

    Like

    • 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

      Like

    • 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)

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: