# DC motor Speed control with Potentiometer and PWM using Arduino

Here we show an example of how we can use potentiometer to control speed of a DC motor using PWM signal from Arduino. When the potentiometer knob is rotated Arduino will read it via its ADC and generate corresponding PWM signal that is then fed to the DC motor. This example tutorial is extension of the earlier tutorial Speed control of DC motor with PWM using Arduino where we have explained in more details about the motor circuit used here and a basic PWM programming with Arduino. Here we explain briefly about PWM and how it is related to Potentiometer control, show and explain wiring diagram for interfacing potentiometer, Arduino and motor control circuit, then demonstrate with video the working of the DC speed control using potentiometer and at the end you will find programming code for potentiometer controlled PWM generation using Arduino. Hopefully this will be helpful in your work.

Pulse Width modulation(PWM) with Potentiometer & Arduino

PWM is a technique of generating High/Low pulse of different duty cylce. Duty cycle is the ratio of how long the signal stays high to the total pulse period. By controlling how long the pulse stays high and low during a period we can control how much energy(or power) is delivered to the motor. If for example, we have a PWM signal with 75% high and 25% low it means that in the total period the pulse stays high for 75% of the time and 25% of the time. The more time the pulse stays high the more energy or power is delivered to the motor. The more energy/power is delivered to the motor the faster it will rotate. In this way PWM is used to control the speed of a motor.

To use potentiometer to control speed, we connect potentiometer to the analog input pin of the Arduino. We then read in the analog signal samples using Arduino ADC and convert it to quantization level value between 0 to 1023. These level values called quantization level from 0 to maximum of 1023 is due to the fact that arduino ADC has 10-bit resolution which gives 2^10 = 1024 quantization level but since count starts from 0 the level is from 0 to 1023. The quantization level of 0 corresponds to 0V and 1023 corresponds to 5V.

Once quantization value is collected during one sample time, the value can be mapped to corresponding PWM level which can be from 0 to 255 because PWM is defined by 8-bit resolution and 2^8 = 256 and again taking 0 as count start the PWM level is from 0 to 255.

By mapping the ADC value range 0 to 1023 to PWM levels 0 to 255 we can then send the analog value collected to the PWM level converted value to the PWM pin. The mapping is simple and we just have to divide by 4.

Wiring diagram for Potentiometer Controlled DC motor control using PWM of Arduino

Below you can see the schematic diagram used to control the DC motor speed using potentiometer controlled PWM signal from Arduino. Here we have connected a 10KOhm potentiometer to the analog pin A0 of Arduino with the other two ends to 5V and ground. The PWM pin used here is pin 10. The PWM pin 10 of Arduino is connected to a 2N2222 transistor via 1KOhm resistor. A 10KOhm is also connected from the base of the transistor to ground to reduce chances of accidental signal into the sensitive base of the transistor. The DC motor is connected to the transistor collected. A noise filtering 0.1uF decoupling capcacitor and protection diode 1N4001 from possible back emf voltage spike from the motor are connected across the motor. Note the polarity of the diode. The motor is connected to a 5V power supply whose ground is shared with Arduino ground.

Note: we have used a small dc motor requiring 3V to 5V power supply. If you want to use higher rated voltage and current DC motor then this might not work. For higher rated voltage/current you need to use motor driver IC like L293D. See DC Motor Control using ATmega32 and L293D for example.

Video demonstration of Potentiometer controlled PWM signal generation using Arduino to control the Speed of a DC motor

The video below demonstrates how we can use a potentiometer to control speed of DC motor using PWM.

The following video demonstrates animation of DC motor Speed control with Potentiometer and PWM using Arduino.

Arduino program code for Potentiometer controlled PWM signal generation using Arduino to control the Speed of a DC motor

// Name pin 10 as motorPin for motor pin identification
int motorPin = 10;
// Name pin A0 as potPin for potentiometer pin identification
int potPin = A0;

int controlValue = 0;

void setup () {
pinMode(potPin, INPUT);
}

void loop() {