Accéder au contenu principal
Publicités

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

}

 

Publicités

Yassine Tout afficher

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

13 commentaires sur “Control DC motor speed using potentiometer + L298n + Arduino Laisser un commentaire

    • 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

      J'aime

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

      J'aime

Répondre

Entrez vos coordonnées ci-dessous ou cliquez sur une icône pour vous connecter:

Logo WordPress.com

Vous commentez à l'aide de votre compte WordPress.com. Déconnexion /  Changer )

Photo Google

Vous commentez à l'aide de votre compte Google. Déconnexion /  Changer )

Image Twitter

Vous commentez à l'aide de votre compte Twitter. Déconnexion /  Changer )

Photo Facebook

Vous commentez à l'aide de votre compte Facebook. Déconnexion /  Changer )

Connexion à %s

%d blogueurs aiment cette page :