H-bridge with P-channel and N-channel MOSFETs for Arduino Robotics

Motor and motor driving circuits are important in robotics. As discussed earlier, the H-bridge is a basic motor driving circuit build with switches that controls the direction of current flow through the motor. Now there are different types of switches used to control motors used in robotics. There are relay switches, optocoupler switches, mechanical switches. These were discussed in earlier robotics with arduino tutorials which are again listed below.

Another solution is to use solid state devices that is transistors, BJT, JFET, MOSFET. The circuit application and factors such as current, voltage, power rating determines which transistor should be deployed. Application area like power supply for robotics uses power MOSFET, while on the other hand for lighting a light bulb purpose bipolar transistors(BJT) are generally sufficient. Here we specifically show MOSFET based H-bridge circuit because they are the most commonly used type of transistor for robotics motor application.

When it comes to robotics and motor control using Arduino, the H-bridge circuit is an indispensable tool. By leveraging P-channel MOSFETs for high-side switching and N-channel MOSFETs for low-side switching, you can create a robust, solid-state H-bridge that’s both cost-effective and reliable. This design eliminates mechanical components, offering superior longevity and scalability for driving motors, relays, or other high-power loads in robotic applications. H-bridge can also be constructed with BJT transistors which was illustrated in the tutorials DC Motor with BJT H-bridge motor driver and also in the guide driving DC motor with BJT based H-bridge. In this article, we’ll explore how to build and optimize this H-bridge, discuss its advantages and limitations, and provide practical tips for seamless integration into your Arduino projects.

Why Choose a MOSFET-Based H-Bridge for Robotics?

The H-bridge configuration discussed here uses P-channel MOSFETs on the high side and N-channel MOSFETs on the low side, making it ideal for bidirectional motor control. Unlike relay-based solutions, this fully solid-state design ensures smooth operation without the wear and tear of mechanical parts. It’s also highly scalable—by adding MOSFETs in parallel, you can increase current-handling capacity to meet the demands of larger motors or multiple actuators.

The following is circuit diagram of H-bridge built with two P-channel(IRF9540N) and two N-channel(IRF540N) MOSFETs for Arduino Robotics. 

circuit diagram of H-bridge built with two P-channel(IRF9540N) and two N-channel(IRF540N) MOSFETs

In the circuit diagram, Q1 and Q2 are p-channel MOSFETs IRF9540N and the Q3 and Q4 are n-channel MOSFETs IRF540N. IRF8540N and IRF540N are two complementary transistors with similar electrical characteristics which is essential for smooth operation of the circuit. The p-channel MOSFETs Q1 and Q2 are called high side mosfet switches, while the n-channel MOSFETs Q3 and Q3 are called low side mosfet switches. For a deeper dive into high-side and low-side switching concepts, check out our guide on understanding low-side and high-side configurations . Notice that in order not to float the gate of the transistors 10kOhm pullup resistors for the p-channel mosfets and 10kOhm pulldown resistors for then-channel MOSFET have been used. The resistors R5 and R6 are current limiting resistors. These four mosfets are arranged in a H-bridge style to control the flow of current through the motor. As can be seen the gates of the p-channel mosfet Q1 and the n-channel mosfet Q3 are tied together, similarly on the opposite side between Q2 and Q4. These two bases terminals are applied complementary signals with a pulse generator or microcontroller board like Arduino used here. There is also a push button. The Arduino code is such that in the first button press, the motor rotates in ani-clockwise direction, in the 2nd press the motor rotates in clockwise direction and in the third press, the motor stops. If you interested below is the arduino sketch.

Arduino code for MOSFET motor drivers

const int pinC = 6;                  // PWM output for C (controls high-side P-MOSFET)
const int pinD = 5;                  // PWM output for D (controls low-side N-MOSFET)
const int buttonPin = 2;             // Pin connected to the momentary button

int motorState = 0;                  // Tracks motor state: 0 = Stop, 1 = Forward, 2 = Backward
int lastButtonState = HIGH;          // Last button state for debouncing
int currentButtonState;              // Current debounced button state
unsigned long lastDebounceTime = 0;  // Tracks when the button state last changed
unsigned long debounceDelay = 50;    // Debounce delay in milliseconds

void setup() {
  pinMode(pinC, OUTPUT);             // Configure pinC as output
  pinMode(pinD, OUTPUT);             // Configure pinD as output
  pinMode(buttonPin, INPUT_PULLUP);  // Configure button pin with pull-up resistor

  // Initialize both pins to LOW (motor off)
  digitalWrite(pinC, LOW);
  digitalWrite(pinD, LOW);

  Serial.begin(9600);
  Serial.println("Program started");
}

void loop() {
  int reading = digitalRead(buttonPin);

  // Reset the debouncing timer if the button state changed
  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  }

  // Check if button state is stable
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // If the button state has changed:
    if (reading != currentButtonState) {
      currentButtonState = reading;

      // Only toggle the motor state if the new button state is LOW (pressed)
      if (currentButtonState == LOW) {
        motorState = (motorState + 1) % 3; // Cycle through states: 0 -> 1 -> 2 -> 0
        updateMotorState();               // Update motor based on the new state
        Serial.print("Motor State: ");
        Serial.println(motorState == 0 ? "Stopped" : (motorState == 1 ? "Forward" : "Backward"));
      }
    }
  }

  lastButtonState = reading;
  delay(10); // Small delay to stabilize readings
}

// Function to update the motor state based on the current motorState value
void updateMotorState() {
  switch (motorState) {
    case 0: // Stop the motor
      analogWrite(pinC, 0); // C = 0V (P-MOSFET off)
      analogWrite(pinD, 0); // D = 0V (N-MOSFET off)
      break;

    case 1: // Rotate motor forward (A = 0V, B = 9V)
      analogWrite(pinC, 0);   // C = 0V (P-MOSFET off)
      analogWrite(pinD, 255); // D = 5V (N-MOSFET on)
      break;

    case 2: // Rotate motor backward (A = 9V, B = 0V)
      analogWrite(pinC, 255); // C = 5V (P-MOSFET on)
      analogWrite(pinD, 0);   // D = 0V (N-MOSFET off)
      break;
  }
}

This Arduino code controls a DC motor's direction and stop state using a push button and an H-bridge circuit driven by P-channel and N-channel MOSFETs. The button cycles through three states—stopped, forward rotation, and backward rotation—using debouncing logic to ensure stable input readings. The motorState variable tracks the current mode, and the updateMotorState() function adjusts the PWM signals on pins 5 and 6 to control the MOSFETs accordingly. When the motor is stopped, both MOSFETs are off; for forward rotation, pin 5 is set to 5V and pin 6 to 0V; for backward rotation, pin 5 is set to 0V and pin 6 to 5V. Serial output provides real-time feedback on the motor's state, aiding debugging and monitoring. This setup ensures smooth toggling of motor states while preventing issues like shoot-through with proper hardware considerations.

Below is video demonstration of how P-channel/N-channel based H-bridge motor driver circuit works with PWM signals from Arduino.

However, one critical consideration is avoiding shoot-through conditions , which occur when both switches on the same side of the bridge are open simultaneously. This can lead to excessive current flow and damage the MOSFETs. To prevent such issues, precise control logic is essential, especially when driving the H-bridge with an Arduino. Learn more about implementing this setup in our tutorial on driving a relay H-bridge with Arduino .

Advantages of Using P-Channel and N-Channel MOSFETs

This H-bridge design offers several compelling benefits for Arduino robotics:

  1. Cost Efficiency : The circuit requires only two P-channel power MOSFETs, two N-channel power MOSFETs, two N-channel signal MOSFETs, and a few resistors, making it budget-friendly for DIY projects.
  2. Durability : With no moving parts, this solid-state solution outlasts mechanical alternatives like relays, provided it operates within the MOSFETs’ voltage and current limits.
  3. Customizability : By paralleling additional MOSFETs, you can tailor the circuit to handle higher currents, making it versatile for various robotic applications.

Despite these advantages, the design has limitations. It is not optimized for high PWM frequencies or high-voltage scenarios , which may require more advanced motor driver ICs or IGBT-based solutions. For insights into alternative switching methods, refer to our relay vs. optocoupler comparison for Arduino users .

Practical Tips for Building and Optimizing Your H-Bridge

Constructing this H-bridge on a prototyping PCB is straightforward, but attention to detail is key to ensuring optimal performance:

  • Thermal Management : MOSFETs can generate significant heat under load. Use heatsinks, thermal vias, or fans to dissipate excess energy and prevent overheating.
  • Gate Drive Circuitry : Properly biasing the MOSFET gates is crucial for efficient switching. A transistor configured as a diode can simplify gate control; learn more in our article on how to use a transistor as a diode .
  • Current Scaling : If your robot’s motors demand higher current, add MOSFETs in parallel while ensuring balanced current distribution across all devices.

Limitations and When to Explore Alternatives

While this H-bridge design is well-suited for many Arduino robotics projects, it may not be ideal for all scenarios. For example:

  • High-Frequency PWM : Applications requiring rapid switching may benefit from dedicated motor driver ICs with optimized gate drive circuits.
  • High-Voltage Loads : For voltages exceeding the MOSFETs’ ratings, consider using IGBTs or other high-voltage components.
  • Isolation Needs : If electrical isolation between the control circuit and the load is critical, optocouplers may be a better choice than direct MOSFET switching.

For a detailed comparison of relay and optocoupler solutions, explore our relay vs. optocoupler guide for Arduino users .

Conclusion

An H-bridge built with P-channel and N-channel MOSFETs is a versatile and reliable solution for Arduino robotics, offering a perfect blend of simplicity, cost-effectiveness, and performance. By understanding its strengths and limitations, you can design circuits that meet the unique demands of your robotic projects while avoiding common pitfalls like shoot-through conditions. For hands-on guidance, check out our tutorials on driving a relay H-bridge with Arduino and optimizing low-side and high-side configurations .

Whether you’re building a simple rover or a complex robotic arm, mastering this H-bridge design will empower you to take full control of your Arduino-powered creations. Start experimenting today and bring your robotics projects to life!

Post a Comment

Previous Post Next Post