MOSFET H-Bridge for Motor Speed and Direction Control

This article presents a MOSFET-based H-bridge motor driver circuit for controlling motor speed and direction, ideal for robotics and motor-driven applications. It utilizes four N-channel MOSFETs (IRF540N) and two P-channel MOSFETs (IRF9540) with PWM control via Arduino pins D9 and D10. A potentiometer connected to the analog pin A1 allows precise control over motor speed and direction, with the motor turning off when the potentiometer is centered and running in either direction based on its position. The circuit supports a power supply range of 5V-30V for the H-bridge and 5V-12V for the Arduino, making it versatile for different applications. Additionally, the article explores the option to implement the H-bridge using BJT transistors as an alternative.

 I wanted to share a mosfet based circuit that is useful in robotics and anywhere motors are used. 

MOSFET H-Bridge for Motor Speed and Direction Control

The circuit is a H-bridge motor driver made using four N-channel MOSFET IRF540N,  and Two P-channel IRF9540 MOSFET. It has two four inputs and two outputs. The four inputs are the Vcc for motor, gnd, in1 and in2 and the outputs are the M+ and M- which are connected to the motor terminals. The in1 and in2 pins of the h-bridge are connected to two PWM capable pins like D10 and D9 of the Arduino or other microcontroller boards. The D10 and D9 are pins connected internally to Timer1. By controlling the in1 and in2 signals we can control both the speed of the motor and the rotation direction of the motor. This H-bridge is for one motor, so if you building four wheel robotic car then you would need to PCB fabricate four of these. By making adjustments in the signal connectivity then the wiring would be simpler. 

The H-bridge is connected to the Arduino D10 and D9 pins. A 10Kohm POT is connected to the analog input pin A1. By utilizing this potentiometer along with the arduino code below, we can adjust both the speed and direction of a DC motor. To verify the functionality of the H-bridge, so I connected them together and tested with a small DC motor.

int forward = 9; // use pin 9 for PWM forward
int reverse = 10; // use pin 10 for PWM reverse
int LED = 11; // use LED on pin 13 to indicate neutral
int potPin = A1;

int potentiometer_value = 0; // read the potentiometer to determine direction and speed
int deadband = 20; // determines deadband from center - a higher number equals a larger??neutral zone.
void setup(){
	Serial.begin(9600); // start serial monitor at 9600 bps
	// set PWM pins to be outputs
	pinMode(forward, OUTPUT);
	pinMode(reverse, OUTPUT);
	// set LED pin to be an output
	pinMode(LED, OUTPUT);
}
void loop(){
// read potentiometer (0-1023) and divide by 2 = (0-511).
// Now break that up into 255 either direction (0-255 = PWM value range).??
potentiometer_value = analogRead(potPin) / 2;
	if (potentiometer_value > 256 + deadband) {
		digitalWrite(reverse, LOW);
		analogWrite(forward, potentiometer_value - 256);
		digitalWrite(LED, LOW);
		Serial.print(potentiometer_value - 256);
		Serial.print(" ");
		Serial.print(potentiometer_value);
		Serial.print(" ");
		Serial.println(" Forward ");
	}
	else if (potentiometer_value < 255 - deadband) {
		digitalWrite(forward, LOW);
		analogWrite(reverse, 255 - potentiometer_value);
		digitalWrite(LED, LOW);
		Serial.print(255 - potentiometer_value);
		Serial.print(" ");
		Serial.print(potentiometer_value);
		Serial.print(" ");
		Serial.println(" Reverse ");
	}
	else {
		digitalWrite(forward, LOW);
		digitalWrite(reverse, LOW);
		digitalWrite(LED, HIGH);
		Serial.print(potentiometer_value);
		Serial.print(" ");
		Serial.println(" STOP ");
	}
}

When the potentiometer is in the neutral (center) position, the motor is turned off, and the red LED on pin D3 lights up. Moving the potentiometer to the right gradually increases the motor speed in the forward direction, with full right corresponding to maximum speed. On the other hand, shifting the potentiometer to the left causes the motor to spin in reverse, with the full left position providing maximum reverse speed. This approach uses a single input to manage both motor direction and speed, making it ideal for testing purposes.

Upload the code above and link the H-bridge inputs to the Arduino PWM pins: D9 for forward and D10 for reverse. The power supply for the H-bridge can range from 5V to 30V, while the Arduino can operate with a supply between 5V and 12V. The potentiometer controls the motor's direction and speed (any potentiometer will suffice). For testing, I assigned the green LED on pin D13 to turn on when the potentiometer is at the center position (neutral), helping indicate the stopping point for the motor.

The following video demonstrates how the actual h-bridge motor driver with arduino and POT controls the speed and direction of the motor.


This same mosfet based h-bridge driver circuit can also with implemented with bipolar transistors or BJT transistors. The guide BJT based h-bridge motor driver explains what is is needed,working principle which is the same as the MOSFET, the difference being that the MOSFET version requires more resistors compared to BJT h-bridge driver but MOSFET can handle more power than BJT. BJT h-bridge may be useful as drone motors driver because lesser part and less bulkly with simple BJT transistors like the 2N2222A and 2N2907N transistors. I have to test that.

Post a Comment

Previous Post Next Post