Potentiometer
The potentiometer provides an analog voltage to the Arduino A0 pin.
The Arduino's ADC reads a value between:
0 to 1023
The program converts this value into a PWM value between:
0 to 255
The mapping used by the program is:
speed = map(potVal, 0, 1023, 0, 255)
Therefore, turning the potentiometer changes the PWM value supplied to the motor driver.
Motor Direction
The motor direction is fixed by the following logic:
A1 = HIGH
A2 = LOW
This causes the motor to rotate in one direction.
The direction does not change when the potentiometer is rotated. The potentiometer is used only to control the motor speed.
PWM Speed Control
The Arduino generates a PWM signal on D9.
The PWM value is determined from the potentiometer reading:
PWM = 0–255
A low potentiometer value produces a low PWM value, while a high potentiometer value produces a high PWM value.
The PWM signal is applied to the motor driver's Enable A pin.
Working
The Arduino continuously reads the potentiometer and controls the motor according to the following sequence:
- The potentiometer value is read using A0.
- The Arduino obtains an ADC value between 0 and 1023.
- The value is mapped from 0–1023 to 0–255.
- The motor direction is set using A1 = HIGH and A2 = LOW.
- The mapped value is applied as PWM to the motor driver's Enable A pin.
- The potentiometer value and PWM speed value are displayed on the Serial Monitor.
- The program waits for 200 ms before repeating the process.
The control relationship is:
Potentiometer → Analog Reading → PWM Mapping → Motor Speed
Serial Monitor Output
The Serial Monitor is set to 9600 baud.
The program displays both the potentiometer ADC value and the corresponding PWM value.
Example output:
Potentiometer Value: 0 | Motor Speed (PWM): 0
Potentiometer Value: 512 | Motor Speed (PWM): 127
Potentiometer Value: 1023 | Motor Speed (PWM): 255
The exact values may vary depending on the potentiometer position.
Arduino Code
// © 2026, Cognifront. Member of The IoT Kits Family.
// Controlling DC Motor with potentiometer and display output on serial monitor
// Define analog input pin connected to the potentiometer
const int Pot = A0);
// Define motor control pins
const int motorA1 = 5); // Motor driver input A1 (direction control)
const int motorA2 = 6); // Motor driver input A2 (direction control)
const int enableA = 9); // Motor driver enable pin (PWM speed control)
void setup() {
// Start Serial communication for debugging and monitoring
Serial.begin(9600);
// Configure potentiometer pin as input (optional for analogRead)
pinMode(Pot, INPUT);
// Configure motor control pins as outputs
pinMode(motorA1, OUTPUT);
pinMode(motorA2, OUTPUT);
pinMode(enableA, OUTPUT);
}
void loop() {
// Read potentiometer value (range 0–1023 from ADC)
int potVal = analogRead(Pot);
// Map potentiometer range to motor speed range (0–255 for PWM)
int speed = map(potVal, 0, 1023, 0, 255);
// Set motor direction: A1 HIGH, A2 LOW → rotate in one direction
digitalWrite(motorA1, HIGH);
digitalWrite(motorA2, LOW);
// Control motor speed using PWM signal on enable pin
analogWrite(enableA, speed);
// Display potentiometer value and motor speed on Serial Monitor
Serial.print("Potentiometer Value: ");
Serial.print(potVal);
Serial.print(" | Motor Speed (PWM): ");
Serial.println(speed);
// Small delay for readability in Serial Monitor
delay(200);
}
Observation
The Serial Monitor is set to 9600 baud.
As the potentiometer is rotated, the Potentiometer Value changes from approximately 0 to 1023.
The corresponding Motor Speed (PWM) value changes from approximately 0 to 255.
The motor rotates in the fixed direction specified by:
A1 = HIGH
A2 = LOW
Result
The Arduino successfully reads the potentiometer value, converts it into a PWM speed value, and controls the speed of the DC motor through the motor driver's enable pin.
The motor direction remains fixed while the potentiometer controls the motor speed.
|