ACS712 Current Sensor with Arduino -PWM Motor Speed Controller

Controlling the speed of a DC motor while simultaneously monitoring its current consumption is an essential aspect of many robotics projects, motorized systems, and energy-efficient applications. In this tutorial, we’ll walk you through how to control a DC motor using PWM (Pulse Width Modulation) and measure the current drawn by the motor using the ACS712 current sensor with an IRF540N MOSFET and an Arduino. We will also explore how to use these techniques for precise motor control, making your projects more efficient.

This setup allows you to dynamically adjust the motor speed based on the current usage, which helps to monitor and optimize power consumption.

Components You’ll Need:

  • Arduino (e.g., Arduino Uno, Nano)
  • ACS712 Current Sensor (5A version for general-purpose use)
  • IRF540N N-channel MOSFET (for motor control)
  • DC Motor
  • PWM Pin for controlling the MOSFET gate
  • Power Supply for the motor (usually 12V depending on your motor)
  • Arduino IDE for coding and uploading to the Arduino

What is PWM and Why is It Used?

Pulse Width Modulation (PWM) is a technique used to control the amount of power supplied to an electrical device, in this case, a DC motor. PWM works by switching the power on and off rapidly, varying the width of the "on" pulses. The duty cycle (the proportion of the time the signal is high versus low) determines how much power the motor receives. This is particularly useful for controlling the speed of motors in robotics, drives, and fan systems. By adjusting the duty cycle, you can easily regulate the speed of a DC motor. To learn more about motor speed control, check out this article on DC Motor Speed Control with Arduino PWM.

Why Use the ACS712 Current Sensor?

The ACS712 current sensor is a Hall-effect-based sensor that measures the current flowing through a conductor (like the motor's power supply). It provides an analog output that is directly proportional to the current. The 5A version of the sensor is perfect for low to moderate-power motors, providing a voltage output range between 0V and 5V that can be read using the Arduino's analog input pins.

ACS712 currrent sensor module
By integrating the ACS712 with your motor control circuit, you can track the current consumption in real-time, enabling you to monitor the load on the motor and take corrective action if necessary, such as reducing the speed or shutting down the system in case of an overcurrent condition.

For further insights into motor control and system protection, see this post on Understanding Low Side and High Side Motor Control.

How Does the IRF540N MOSFET Fit In?

The IRF540N is an N-channel MOSFET that acts as a switch to control the motor's power supply. The gate of the MOSFET is controlled by the PWM signal from the Arduino, allowing for smooth control of the motor's speed. When the MOSFET is on (gate voltage is high), current flows through the motor, and it spins. When the MOSFET is off (gate voltage is low), the current stops, and the motor halts. By adjusting the PWM signal, you control the speed at which the MOSFET turns on and off, which controls the motor's speed.

To understand different methods of motor control, including L298N Motor Driver for DC motors, take a look at this guide on L298N Motor Driver with Arduino.

The Circuit Setup

The following circuit diagram shows how to connect the ACS712 current sensor module with Arduino, motor and power supply circuit to measure the current flowing into a dc motor.

ACS712 Current Sensor with Arduino circuit diagram

ACS712 Current Sensor:

  • Connect the VCC pin to 5V on the Arduino.
  • Connect the GND pin to GND.
  • Connect the OUT pin to A0 on the Arduino (for current measurement).
  • Connect the +IP pin to the +ve terminal of the +12V power supply.
  • Connect the -IP pin to the one terminal of the dc motor.

IRF540N MOSFET:

  • Connect the Gate to a PWM pin via 1kOhm resistor to D9 pin on the Arduino.
  • Connect the Gate to Source with 10kOhm resistor.
  • Connect the Drain to the negative terminal of the motor.
  • Connect the Source to GND.

(Note: This is called low side switching of transistor: see Low-Side vs High-Side Switches - Arduino Robotics Control)

DC Motor:

  • Connect one terminal of the motor to the -IP pin of the ACS712 current sensor module.
  • Connect the other terminal of the motor to the drain of the IRF540N MOSFET

10k Potentiometer:

  • Connect one pin of the potentiometer to 5V and the other to GND.
  • Connect the wiper (middle pin) to A1 on the Arduino (for controlling PWM motor speed).

Arduino:

  • Use D9 (or another PWM pin) to control the MOSFET gate and adjust the motor speed based on the potentiometer input.
  • Use A0 to read the current from the ACS712.

For a more detailed guide on motor speed control with PWM, check out Speed and Direction Control of DC Motor with Arduino.

Arduino Code: Motor Control with Current Measurement

Here’s the updated code that reads the current from the ACS712, adjusts the motor speed using a 10k potentiometer for PWM control, and displays both the current and motor speed in the Serial Monitor.


// Pin definitions
const int pwmPin = 9;         // Pin for controlling motor speed (PWM)
const int currentPin = A0;    // Pin connected to ACS712 OUT
const int potPin = A1;        // Pin connected to potentiometer

const float VCC = 5.0;        // Arduino operating voltage
const float zeroCurrentVoltage = 2.5; // Voltage at no current flow (2.5V for ACS712)
const float sensitivity = 0.185; // ACS712 5A version sensitivity (V/A)

// Variables
float voltage = 0;
float current = 0;
int motorSpeed = 0;  // Motor speed (0 to 255 for PWM)
int potValue = 0;    // Value read from potentiometer

// Setup function
void setup() {
  Serial.begin(9600);      // Initialize serial communication
  pinMode(pwmPin, OUTPUT); // Set PWM pin as output
}

// Loop function
void loop() {
  // Read the potentiometer value (0 to 1023)
  potValue = analogRead(potPin);

  // Map the potentiometer value to PWM range (0 to 255)
  motorSpeed = map(potValue, 0, 1023, 0, 255);

  // Write the PWM signal to the motor (control speed)
  analogWrite(pwmPin, motorSpeed);

  // Read the current from the ACS712 sensor
  voltage = analogRead(currentPin) * (VCC / 1023.0); // Convert ADC value to voltage
  current = (voltage - zeroCurrentVoltage) / sensitivity;  // Calculate the current

  // Print the current and motor speed to the serial monitor
  Serial.print("Current: ");
  Serial.print(current);  // Current in Amperes
  Serial.print(" A\t");

  Serial.print("Motor Speed (PWM): ");
  Serial.println(motorSpeed);  // Motor speed in PWM value (0-255)

  delay(100); // Delay for stability and to update every 100ms
}

How This Code Works

The Arduino reads the voltage from the ACS712 current sensor connected to A0. This voltage is then converted into the current using the sensor's sensitivity (0.185V per Ampere for the 5A version). Meanwhile, the 10k potentiometer connected to A1 allows the user to adjust the motor speed by controlling the PWM signal sent to the IRF540N MOSFET on D9. As the potentiometer value changes, the PWM signal adjusts accordingly, controlling the motor speed.

Both the current drawn by the motor (measured by the ACS712) and the motor speed (represented by the PWM value) are displayed in real-time on the Serial Monitor, giving you continuous feedback on the motor's performance.

The following video demonstrates how to measure current drawn by a DC motor using ACS712 Current Sensor Module with Arduino and IRF540N MOSFET switch.

Why This Setup is Useful

This Arduino-based motor control system has several advantages:

  • Real-time current monitoring ensures you can track how much power your motor is drawing, preventing overloading or excessive power consumption.
  • PWM motor control allows for precise speed regulation, which is essential for many robotics projects, fan systems, or motorized devices.
  • This system can easily be expanded to include features like overcurrent protection or energy-saving modes based on the current measurement.

In this setup we have used a solid state switch which is the IRF540N MOSFET transistor which is quite suitable for this setup but if you have a load that draws lots of voltage and current or AC loads, consider using a optocoupler IC like 4N25 or relay switch. The Optocoupler with Arduino for Motor Control and Guide to Relay with Arduino tutorials gives you the basic of interfacing them with Arduino and program code to test the switches.

For more advanced PWM control with NodeMCU, check out this tutorial on PWM Controlled DC Motor with NodeMCU.


Conclusion

By using the ACS712 current sensor, IRF540N MOSFET, and Arduino, you can effectively control the speed of a DC motor while monitoring its current draw. This combination is ideal for various applications like robotics, energy management systems, and motorized projects where both speed control and energy consumption monitoring are necessary. For more information on related motor control techniques, check out this article on L298N Motor Driver with Arduino.

Post a Comment

Previous Post Next Post