Arduino Motor Speed Control Using a Potentiometer

 In this tutorial, we’ll learn how to control the speed of a DC motor using a potentiometer with an Arduino Uno. The motor’s speed is adjusted by varying the resistance of the potentiometer, which is then translated into a Pulse Width Modulation (PWM) signal to control the motor.


Components Needed:

  • Arduino Uno
  • DC Motor
  • Potentiometer (10k Ohm)
  • N-channel MOSFET
  • Diode (1N4007 or similar)
  • 9V battery with battery clip
  • Jumper wires
  • Breadboard

Circuit Diagram:

Refer to the diagram shown in the image above. Here’s a step-by-step guide for setting up the circuit:

  1. Motor and Power:

    • Connect the negative terminal of the 9V battery to the ground (GND) rail of the breadboard.
    • Connect the positive terminal of the 9V battery to one end of the motor.
    • Place a diode across the motor terminals to prevent back EMF from damaging the circuit. The diode's cathode should connect to the positive terminal of the motor.
  2. MOSFET:

    • Connect the drain of the MOSFET to the negative terminal of the motor.
    • Connect the source of the MOSFET to the ground (GND) rail of the breadboard.
    • Connect a wire from the gate of the MOSFET to Arduino digital pin 9, which will provide the PWM signal.
  3. Potentiometer:

    • Connect one terminal of the potentiometer to the 5V pin on the Arduino.
    • Connect the opposite terminal to the ground (GND) of the Arduino.
    • Connect the middle terminal (wiper) of the potentiometer to analog pin A0 of the Arduino.
  4. Arduino Power:

    • Connect the Arduino GND to the breadboard's ground rail.
    • Optionally, you can power the Arduino through USB or an external power source.



Arduino Code:


int sensorValue = 0; // Variable to store the analog value from the potentiometer int outputValue = 0; // Variable to store the PWM output void setup() { pinMode(A0, INPUT); // Set pin A0 as input for potentiometer pinMode(9, OUTPUT); // Set pin 9 as output for PWM signal to MOSFET Serial.begin(9600); // Begin serial communication for debugging } void loop() { // Read the analog value from the potentiometer (0-1023) sensorValue = analogRead(A0); // Map the potentiometer value (0-1023) to a PWM range (0-255) outputValue = map(sensorValue, 0, 1023, 0, 255); // Write the PWM value to the MOSFET to control motor speed analogWrite(9, outputValue); // Debugging: Print the sensor and output values to the serial monitor Serial.print("Sensor = "); Serial.print(sensorValue); Serial.print(" Output = "); Serial.println(outputValue); // Small delay for stability delay(100); }

Code Explanation:

  1. Reading the Potentiometer Value:
    The analogRead(A0) function reads the voltage from the potentiometer, giving a value between 0 and 1023. This value is proportional to the potentiometer’s position.

  2. Mapping the Value to PWM:
    The map() function converts the potentiometer value (0-1023) to the PWM range (0-255), which is used to control the motor speed via the MOSFET.

  3. Writing PWM to the Motor:
    The analogWrite(9, outputValue) sends a PWM signal to pin 9. This signal controls the MOSFET’s gate, which in turn regulates the motor’s speed based on the potentiometer's position.

  4. Serial Monitor Output:
    The Serial.print() functions allow you to monitor the values from the potentiometer and the PWM output in the Arduino IDE's Serial Monitor, which is helpful for debugging.


How It Works:

  • When you turn the potentiometer, the resistance changes, and this alters the voltage on the analog input pin (A0).
  • The Arduino reads this voltage as a value between 0 and 1023, which is then mapped to a PWM value between 0 and 255.
  • This PWM signal is sent to the MOSFET, which controls the current flowing to the motor, adjusting its speed.

Key Concepts:

  • PWM (Pulse Width Modulation): PWM is a technique where the width of the pulse is varied to simulate an analog signal. In this project, PWM is used to control the speed of the motor.
  • Analog Input: The potentiometer provides an analog input to the Arduino, which reads it as a digital value using the analogRead() function.

Customizing the Project:

  • Reverse the Motor: You can add an H-bridge to this circuit to allow for motor direction control.
  • LCD Display: You could display the motor speed or potentiometer value on an LCD screen.
  • Temperature Control: Integrate a temperature sensor to automatically adjust the motor speed based on the ambient temperature.

Conclusion:

In this project, you learned how to control the speed of a DC motor using a potentiometer and an Arduino. The motor’s speed is adjusted using PWM signals based on the potentiometer’s analog input. This concept can be applied to many applications, such as controlling fan speeds or dimming lights.

Comments

Popular Posts