[go: up one dir, main page]

0% found this document useful (0 votes)
198 views7 pages

Controlling Motor With Potentiometer

The document contains code for controlling DC motors using a potentiometer and switches. It defines pin assignments for motors, potentiometer and switches. In the setup, the pins are configured as inputs or outputs. In the loop, it reads the potentiometer and switches to control motor direction and speed based on potentiometer value, increasing or decreasing PWM output to control speed from 0-255. It prints the PWM value to serial monitor for monitoring.

Uploaded by

Patricia KC
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
198 views7 pages

Controlling Motor With Potentiometer

The document contains code for controlling DC motors using a potentiometer and switches. It defines pin assignments for motors, potentiometer and switches. In the setup, the pins are configured as inputs or outputs. In the loop, it reads the potentiometer and switches to control motor direction and speed based on potentiometer value, increasing or decreasing PWM output to control speed from 0-255. It prints the PWM value to serial monitor for monitoring.

Uploaded by

Patricia KC
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Controlling_Motor_with_potentiometer

#define pwm_2 4
#define pwm_1 7
#define dir_2 3
#define dir_1 6
#define pot A1
void setup() {
// define pin name
pinMode(dir_1, OUTPUT);
pinMode(dir_2, OUTPUT);
pinMode(pwm_1, OUTPUT);
pinMode(pwm_2, OUTPUT);
pinMode(pot,INPUT);
Serial.begin(9600);
}
void loop() {
int reading = 0;
int output=0;
int opposite_output=0;
digitalWrite(pwm_1,HIGH);
digitalWrite(pwm_2,HIGH);

for (int i=0;i<5;i++)


reading+= analogRead(pot);
reading/=5;
output=reading>>2; //convert 10-bit to 8-bit
opposite_output=255-output;
analogWrite(dir_2,output);
analogWrite(dir_1,opposite_output);
delay(30);
Serial.println("DIR");
Serial.println(output);
}
Controlling_Motor_with_program

// define pin name


#define pwm_2 4
#define pwm_1 7
#define dir_2 3
#define dir_1 6
void setup() {
pinMode(pwm_2, OUTPUT);
pinMode(pwm_1, OUTPUT);
pinMode(dir_1, OUTPUT);
pinMode(dir_2, OUTPUT);
Serial.begin(9600);
//Serial.println("DIR");
}
void loop() {
int pwm_value = 0;
int opposite_i;
digitalWrite(pwm_1,HIGH);
digitalWrite(pwm_2,HIGH);

for(int i=0;i<=255;i++){
analogWrite(dir_1,i);
opposite_i=255-i;
analogWrite(dir_2,(255-i));
delay(120);
Serial.println(i);
}
while(1)
continue;
}
Controlling_Motor_with_potentiometer

//define pin name


#define dir_1 7
#define pwm_1 6
#define switch_1 13
#define switch_2 12
#define dir_2 4
#define pwm_2 3
#define pot 1 //potentiometer
void setup() {
// put your setup code here, to run once:
pinMode(pwm_1,OUTPUT);
pinMode(dir_1,OUTPUT);
pinMode(pwm_2,OUTPUT);
pinMode(dir_2,OUTPUT);
pinMode(pot,INPUT);
pinMode(switch_1, INPUT);
pinMode(switch_2, INPUT);
Serial.begin(9600); //I am using Serial Monitor instead of LCD display
}
void loop() {
// put your main code here, to run repeatedly:
int pwm_value=0;
int reading=0;
int prev_reading=0;

//controls the direction the motor


digitalWrite(dir_1,digitalRead(switch_1));
digitalWrite(dir_2,!digitalRead(switch_2));
//controls the speed of the motor
for (int i=0;i<5;i++) //gets the average value of the pot value for better accuracy
reading+= analogRead(pot);
reading/=5;
pwm_value = reading>>2; //convert the 10-bit values to 8-bit values
analogWrite(pwm_1,pwm_value);
analogWrite(pwm_2,pwm_value);
Serial.println("PWM:");
Serial.println(pwm_value); //Display the value of PWM
delay(100);
}

Controlling_Motor_with_program
//define pin name
#define dir_1 7
#define pwm_1 6
#define switch_1 13
#define switch_2 12
#define dir_2 4
#define pwm_2 3
#define pot A1 //potentiometer
void setup(){
//declare pins as INPUT/OUTPUT
pinMode(pwm_1,OUTPUT);
pinMode(dir_1,OUTPUT);
pinMode(pwm_2,OUTPUT);
pinMode(dir_2,OUTPUT);
pinMode(pot,INPUT);
pinMode(switch_1, INPUT);
pinMode(switch_2, INPUT);
Serial.begin(9600); //I am using Serial Monitor instead of LCD display
}
void loop(){
int pwm_value;
for (pwm_value=0;pwm_value<=255;pwm_value++){
digitalWrite(dir_1,digitalRead(switch_1)); //controls the direction the motor
digitalWrite(dir_2,!digitalRead(switch_2));
analogWrite(pwm_2,pwm_value); //increase the speed of the motor from 0 to 255
analogWrite(pwm_1,(255-pwm_value)); //decrease the speed of the motor from 255 to 0
Serial.println("PWM:");
Serial.println(pwm_value); //Display the value of PWM
delay(100);
}
while(1) continue; //terminate the program
}

You might also like