DC motor controlled by potentiometer using Arduino - Wired Connection


Circuit Diagram:


Required Components:


Arduino uno/nano - 1 Nos
Battery - 1 Nos
DC Motor - 2 Nos
L293D Motor Driver - 1 Nos
Potentiometer - 1 Nos

Program Code:

Type 1:

int Pot = A1;
int val;
void setup() {
  // put your setup code here, to run once:
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(A1, INPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
int val = analogRead(Pot);
if(val < 500 && val > 1){
val = map(val, 1, 525, 255, 0);

analogWrite(6,val);
}
else if(val > 526 && val < 1023){
val = map(val, 526, 1023, 0, 255);

analogWrite(5,val);
}

}

Type 2:

int pot = A1;
int pwm1 = 5;
int pwm2 = 6;

int motor_control;

void setup() {
  pinMode(pwm1, OUTPUT);
  pinMode(pwm2, OUTPUT);
}

void loop() {
  motor_control = analogRead(pot);
  motor_control >>= 1;
  if(motor_control > 255){
    digitalWrite(pwm2, 0);
    analogWrite(pwm1, (motor_control - 255));
  }
  else
    if(motor_control < 255){
      digitalWrite(pwm1, 0);
      analogWrite(pwm2, (255 - motor_control));
    }
    else{
      digitalWrite(pwm1, 0);
      digitalWrite(pwm2, 0);
    }
}

Video:



Comments

Popular posts from this blog

Servo motor controlled by Joystick using Arduino & Transceiver - Wireles...

DC motor controlled by Joystick using Arduino & L298N Motor driver - Wireless Connection

DC motor controlled by Gyro MPU 6050 using Arduino & L298N Motor driver - Wired Connection