ad space

DC motor speed control with Arduino PWM and Potentiometer

Controlling the speed of a DC motor is crucial in various applications such as RC toy cars, DC controlled fans, and more. The speed of a DC motor is determined by the power it receives, which is the product of current and voltage. By applying varying voltage or current, or both, the power and consequently the speed of the motor can be controlled. Microcontrollers or microcontroller boards like Arduino are capable of applying varying voltage to the DC motor using Pulse Width Modulation (PWM) signals.

PWM signals are signals that remain high for a certain duration over a period. The duration for which the signal remains high directly relates to the power being applied. A longer duration means more power is being generated. The duty cycle of a PWM signal is used to indicate the percentage of time the signal remains high over the signal period. By varying the duty cycle, the speed of the motor can be adjusted. To allow users to control the duty cycle, a potentiometer can be used.

The potentiometer is connected to an analog input pin of the microcontroller, such as an Arduino. The voltage from the potentiometer is divided using a voltage divider circuit and acquired by the Arduino's internal Analog-to-Digital Converter (ADC). The ADC converts the analog voltage to a digital representation ranging from 0 to 1023, as the Arduino has a 10-bit ADC. When the voltage read by the Arduino is 0V, it is converted and saved as 0, and when the voltage is 5V, it is converted and saved as 1023. Intermediate voltages between 0 and 1023 represent different levels.

Once the voltage level is read, the PWM signal with a duty cycle corresponding to the voltage level can be generated on the pin connected to the DC motor. This allows the potentiometer to control the speed of the DC motor. In this tutorial, we will demonstrate how to control the speed of a DC motor using a potentiometer connected to an analog pin of an Arduino. The read voltage will be converted to a duty cycle, and a PWM signal will be generated on the pin connected to the DC motor. This will enable precise control of the motor's speed.

Tutorials such as "DC motor speed control with Arduino PWM" and "Joystick controlled DC motor with Arduino and TIP122" have covered similar topics and provided additional insights into controlling DC motor speed using different techniques or components.

DC motor speed control with Arduino PWM and Potentiometer

Circuit drawing for DC motor speed control with Arduino PWM and Potentiometer

The following is the circuit diagram to control DC motor speed with Arduino, L298N motor driver and a potentiometer.

DC motor speed control with Arduino PWM and Potentiometer Circuit Diagram

In the above circuit diagram, the 10KOhm potentiometer is connected to the analog input pin A0 of Arduino. The L298N motor driver integrated circuit(IC) is used to control the DC motor. The pin 7, pin 6 and pin 5 of Arduino is connected to IN2, IN1 and ENA pins of the L298N integrated circuit. The PWM signal is generated by Arduino and appears at pin 5. Note that PWM capable pin has to be used. The L298N Vcc pin is connected to +5V while the Vs power pin is connected to a +12V power supply. The SenseA and SenseB pin of L298N IC are connected to common ground. The OUT1 and OUT2 output pins of L298N integrated circuit are connected to the two terminals of the DC motor. Four diodes are used for protection and connected in bridge configuration as shown in the circuit drawing above.

The following video shows simulation how the DC motor speed control with Arduino PWM and Potentiometer works .


Program code for DC motor control with Arduino PWM and Potentiometer

The following is the Arduino sketch for the DC motor control with potentiometer adjusted PWM.

const int ENA = 5;
const int IN1 = 6;
const int IN2 = 7;


void setup() {
  Serial.begin(9600);
  pinMode(IN1,OUTPUT);
  pinMode(IN2,OUTPUT);
  pinMode(ENA,OUTPUT);
}

void loop(){
  int pot = analogRead(A0);
  int PWMDC = map(pot,0,1023,0,255);
  digitalWrite(IN1,HIGH);
  digitalWrite(IN2,LOW);
  analogWrite(ENA,PWMDC);
  Serial.println(PWMDC);
}

In the above code, we have used alias name for the pin 5, 6 and 7 as ENA, IN1 and IN2. In the setup() we have made the pins are output. We do not need to set up the analog pin A0 as input because the analogRead() when used will automatically make it as input pin. We have initialized serial communication using the Serial.begin() with 9600bps baud rate. In the loop() function we read in the voltage input from the potentiometer using the analogRead() function and store that discrete level representation of the voltage in the integer variable called pot. We need to convert the voltage discrete level representation range 0 to 1023 to the PWM range which is from 0 to 255. Then we make the IN1 pin high and IN2 pin low for setting forward direction rotation using the digitalWrite() function. Then we send the PWM signal to the ENA pin using the analogWrite() function.

The following video shows how the DC motor speed control with PWM from Arduino and Potentiomter.


In the above video demonstration, we have used L298N breakout board whose design was shown in the tutorial Custom PCB design for L298N breakout board.

Here we have used potentiometer to vary the PWM signal applied to the DC motor via motor driver IC L298N. We can also control the speed of the DC by using commands. The Arduino L298N DC Motor Speed control with PWM tutorial shows how we can control the DC motor speed and direction using command from serial terminal.

Post a Comment

Previous Post Next Post