control the speed of a motor

Please can i get a help ! I want to know how to control the speed of a motor via a potentiometer via a H-Bridge . I get the code but i dont know where is the fault !

control_report.ino (2.35 KB)

Please can you describe what actually happens and what is spposed to happen.

…R

the motor rotates clock wise for 20 seconds , and i have to control the speed of the motor via the potentiometer and then rotates anticlockwise for 20 seconds and so on

What is the transistor connected to? That is not the way you control the speed of a motor you apply the PWM to one of the H-bridge pins and the other to control the direction. Then you use the H-bridge enable input to turn the motor on and off. There is no need to use a transistor.

do you have such a code for that please

I just want to control the speed of the motor with h bridge L298N and potentiometer

For a project like it, I use this Dual H-bridge by DFRobot:

http://www.dfrobot.com/index.php?route=product/product&path=156_51&product_id=66

Connect the bridge and motors like this:

The motor I use is a simple DC-motor 12V from a Carrera Racetrack car.

Connect the potmeter like this:

And finally write a program like this:

int i = 0;          //INITIALISING COUNTER
float t = 0;        //TIME

int analogPin = 3;  //ANALOG INPUT POTMETER
int fan1;           //ANALOGREAD analogPin3
int fan2;           //DIGITAL FANSPEED
int fan3;           //SERIALOUT POWER

int E1 = 12;        //DIGITALOUT FAN
int M1 = 22;        //DIGITALSET FAN



/*SETUP WITH SERIALBEGIN AND PINMODE*/

void setup() {
 Serial.begin(9600);
 pinMode(M1, OUTPUT);
}


/*LOOP*/

void loop() {
 
 /*INITIALISATION*/
 
 while(i==0){
   delay(2000);
   Serial.println("\t\t\t\tINITIALISING...");
   delay(1000);
   Serial.println("\t\t\t\t   TURN KNOB");
   i++;
 }
 
 
 /*POT READ*/
 
 fan1 = analogRead(analogPin);          //POT READ FOR WHILELOOP
 while(fan1 >= 80){                     //WHEN POT >= 80, BEGIN LOOP
   for(t = 0; t <= 9999999; t++){       //TIME RESET, TILL INFINITY
     fan1 = analogRead(analogPin);      //EVERY LOOP, READ POT
     if(fan1 <=80){                     //WHEN POT <= 80, STOP FAN
       fan2 = 0;
     }
   else                                 //WHEN POT > 80, RENDER FANSPEED
     fan2 = map(fan1, 0, 1023, 0, 255); //FANSPEED = POTREAD/1023*255
   
   
   /*SERIAL PRINT COMMANDS*/
   
   Serial.print("\tIN\t");
   Serial.print(fan1);                  //PRINT POTREAD
   Serial.print("\t\t|\tOUT\t");
   Serial.print(fan2);                  //PRINT FANSPEED
   Serial.print("\t\t|\tPOWER\t");
                                        //PRINT FANPOWER
   if(fan1 <= 80){                      //WHEN FAN = OFF, PRINT "OFF"
     Serial.println("OFF");
     analogWrite(E1, 0);
     break;
   }
   else{                                //WHEN FAN = ON, PRINT FANPOWER
     fan3 = map(fan1, 0, 1023, 0, 100); //FANPOWER = POTREAD/1023*100 (%)
     Serial.print(fan3);
     Serial.println("%");
   }
   
   digitalWrite(M1, HIGH);              //SET MOTORSHIELD HIGH
   analogWrite(E1, fan2);               //FANSPEED 0-255
   }
 }
}