9. Motors: H-Bridge

Important

This activity may be completed in groups of 2 students.

9.1. Purpose

This activity aims to show how to control higher-current devices using a microcontroller.

9.2. Hardware and Tools

  • MSP-EXP432P401R Launchpad Development Board, OR

  • TI-RSLK Robotic Car

  • Breadboard

  • 4x PN2222 Transistors

  • 1x BiColor LED

  • 1x Small DC Motor (In classroom, do not take until last step)

  • Various Resistors (4x 220Ω, 1x 510Ω)

9.3. Description

As has been demonstrated in previous activities, the MSP432P401R can easily control some output devices via just digital outputs. This requires that the device being controlled can operate with a low voltage (e.g., 3V) and low current (e.g., <10mA). Considering these limitations, it is not feasible to use the microcontroller to directly interface with higher-power devices, such as actuators (motors, servors), high-brightness LEDs, wireless interfaces, battery charging, etc. Therefore, it is imperative to understand methods for interfacing with such components.

This activity will focus on control of DC Motors; that is, rotating motors that can operate on low-voltage DC power (e.g., 3-24 V). More specifically, we will use motors that will operate on 3.3V but require higher current than LEDs, etc.

A simple DC motor operates as you might expect: apply voltage/current and get rotation. This allows for a motor to be hooked up as shown:

../_images/simple_motor_2d.svg

A simple DC motor setup. Direction of operation is (left) forward, and (right) reverse.

Of course, in either instance above the motor will always be on and running at full speed in only one direction. A switch must be added to either the power or ground connection to the motor to be able to turn the motor on and off:

../_images/simple_motor_switch.svg

An On/Off controllable DC motor circuit.

This circuit allows a user to turn on and off the motor, but it isn’t an automated process. What is required is an electrically controlled switch. A transistor is one such device. For these purposes, we will consider an NPN BJT Transistor: the extremely common PN2222.

9.3.1. Transistors

Note

This discussion on transistors is highly simplified towards introducing them as electronically controlled switches. Their complete behavior is much more complicated. Further, any specifications of the transistors given are specific to the PN2222 and may change with different parts.

A transistor, as used in a digital circuit, is basically a controllable switch; where the device can allow current to flow through it depending on an control voltage. Many types of transistors exist (BJT, MOSFET, GaNFET, FinFET, etc.) for various purposes: fast switching, high power, low loss, etc. For our purposes, the BJT type works well as it can handle switching higher currents.

../_images/npn_bjt_good.svg

The schematic symbol (left) and pinout (right) of an NPN BJT. 1: Emitter, 2: Base, 3: Collector.

A BJT transistor consists of three pins: the Collector, Emitter, and Base. For an NPN type BJT (as opposed to the PNP type), the Collector pin should be connected towards the source voltage, the Emitter should be connected towards ground, and the Base connected to the control signal. The transistor is OFF (no current can flow) when \(V_{base} < V_{emitter}+0.6\). Likewise, the transistor is ON if \(V_{base} > V_{emitter}+0.6\).

A simple implementation of an NPN BJT is shown in the schematic below; where the manually operated pushbutton switch from the above circuit is replaced with the transistor.

../_images/bjt_motor.svg

A DC motor controlled via a BJT transistor.

Considering the description of the transistor functionality above, if the input GPIO is Low (0V), then Q1 (the transistor) is Off, forcing the motor to be off. If the input line GPIO is High (~3 V), then Q1 is On and the motor is on. Motor speed can then be controlled by turning Q1 on and off quickly and at different ratios of the on-time to the off-time.

9.3.2. H-Bridge

Unfortunately, the motor direction cannot be changed using the previous implementation. In order to accomplish this, circuitry must be added to enable switching between the source voltage and ground for each motor pin. Going back to the pushbutton implementation examples: this can be achieved by using four buttons, as shown below. This configuration is known as an “H-Bridge” as it clearly resembles the shape of the letter H. It may also be referred to as a “Full-Bridge”. Confusingly, a “Half-Bridge” is one half of an “H-Bridge” (either the left or right portions).

../_images/h-bridge_buttons.svg

H-Bridge motor control using pushbuttons.

Using an H-Bridge, the load (motor) can be run in both forward and reverse:

../_images/h-bridge_buttons_pressed.svg

Driving a motor in forward and reverse using an H-Bridge. The colored line represents current flow.

Of course, these switches can be replaced with transistors to produce the fully controllable H-Bridge:

../_images/h-bridge_bjt.svg

An H-Bridge implemented with NPN BJT transistors.

As shown, this requires 4 GPIO pins to fully control the bridge: two for each leg of the motor. In operating the motor in the forward direction, the control signals B1H and B2L would be high, with B1L and B2H low. Likewise, the motor would be driven in reverse with B1L and B2H high with B1H and B2L low. Therefore, it is possible to simplify the control to two GPIO pins: where PIN1=B1H=B2L and PIN2=B1L=B2H or PIN1=B1H=!B1L and PIN2=B2H=!B2L (requires inverters).

Warning

It is imperitive that the pairs of transistors for each leg not be on at the same time as this would create a short to ground; and could possibly damage the transistors. The two GPIO configurations mentioned above substantially reduce the possibility of direct shorts between power and ground by simplifying the control scheme; especially for the second case.

Note

The shown implementation for an H-Bridge is an inefficient one. This is because the high side transistors (Q1H and Q2H) will not allow the voltage applied to the motor to be 3.3V due to the requirement that \(V_{base} > V_{emitter}+0.6\) for the transistor to be on; therefore, the maximum applied voltage to the motor would be approximately 2.7 V. This limitation can be avoided by using PNP BJT transistors instead for Q1H and Q2H.

Note

There are many prebuilt single IC chip package implementations of H-Bridges for various uses. These prebuilt versions usually allow for higher voltages to be applied to the bridge as well while control is still provided by low-voltage signals (<=5V). A common one used is the L298N, which provides two independent H-Bridges capable of operating up to 46V.

9.4. Instructions

The lab has a limited amount of DC motors for this activity. Therefore, the initial development for the activity will use a BiLED to verify operation then the last step will be to add the DC motor and test.

Warning

This activity requires 4 GPIO pins: P3.2, P3.3, P6.6, P6.7. Depending on the RSLK or Launchpad Board used, some of these pins may be broken. You can check to see if the pins are failed using the IO_Test.zip project. If one or more of the necessary pins are found to be bad, you may select a different unused pin from RSLK_MAX_pin_map.pdf.

Warning

Be careful handling the transistors immediately after use!!

The transistors used in this activity will get hot during normal operation when connected to the DC motor. Transistors that are incorrectly wired or shorted may be much hotter, likely leading to component failure.

  1. Build the BiLED control with a single transistor as shown below on a separate breadboard. To test, you can take a plain wire and connect from the P3.2 connection to ground and 3.3V. It should light when attached to 3.3V. If this does not work, check the pinout and connections of the transistor.

    ../_images/bjt_BLED.svg

    BiColor LED controlled via one BJT Transistor.

  2. Import a new Template Project and name it appropriately.

  3. Add a GPIO_Init() function to initialize P3.2 to an output and ensure that its initial value is Low.

    Hint

    By now, you should see the repetitive nature of doing these initialization over and over again. Copying and modifying useful code from previous projects is usually a quicker way to build a new project.

  4. Add code in the main() functions while(1) loop to receive a character (getchar()) from the terminal and turn on the BiLED when the letter f is pressed. The BiLED should turn off if the letter s is pressed.

  5. Test and ensure the system works as intended.

  6. Modify the circuit to use a full H-Bridge to control the BiLED. Ensure that power is removed prior to doing this.

    ../_images/h-bridge_BLED.svg

    BiColor LED controlled via H-Bridge.

    Important

    If you are not confident that you wired the H-Bridge correctly, ask a staff member to check it for you. Incorrect wiring of the H-Bridge may lead to the burnout of transistors.

  7. Modify the program to initialize the required additional pins and ensure their initial values are Low.

  8. Modify the while(1) logic to turn the BiLED GREEN when f is pressed, RED when r is pressed, and OFF when s is pressed.

    Important

    As noted above, you must ensure that high and low transistors on one leg are NOT both turned on at once. A safe way to ensure this is to turn all control signals off and then turn the required ones on; therefore, a delay between turning signals off and then on should be added as well. A 10 μs delay should be more than enough: __delay_cycles(240).

    A framework for implementing this code is provided here:

    if(cmd=='f'){ // forward
        // Turn all transistors off
        __delay_cycles(240);
        // Turn the necessary transistors on
    }else if(cmd=='r'){ // reverse
        // Turn all transistors off
        __delay_cycles(240);
        // Turn the necessary transistors on
    }else if(cmd=='s'){ // stop
        // Turn all transistors off
    }
    
  9. Test the program. If the code resets or disconnects during testing, ensure that the outputs are set correctly when switching operation. This is a symptom of a short circuit; caused by both the high and low transistors being turned on at the same time. Resetting will not necessarily happen during the short circuit; it depends on the severity.

  10. If all is working, add the DC motor provided in the classroom in parallel to the 510Ω resistor and BiLED and test (alternatively, the resistor and BiLED may be removed). If the motor does not spin, check to ensure that the transistors are wired correctly with respect to their pinout. It is possible for the BiLED to light appropriately with the transistors mounted incorrectly due to it’s low power nature.

  11. Take a photo of the final circuit with the motor attached and submit to the corresponding Gradescope assignment. Submit the final code to the same assignment. Please submit as a group.