# How to use motor drivers with H-bridge and PWM input, to control direction and speed of DC motors?

I'm running series of experiments using a 6V DC motor. I'm using desktop DC power supply to power the motor and regulate its speed and everything works just fine, the setup has been running for months.

Now I need to use Arduino motor shield to power the motor and run a particular control sequence (involving motor speed, direction, brake). Arduino is powered by 9V DC supply brick (from an 110V outlet), and delivers approx. 8.5V on its outlet power pins. Once I connect the motor the voltage I read on the pins is approx. 6.8V using my multi-meter. I cannot measure the current, once I connect the multi-meter the RPM drops dramatically, I guess I'm affecting the circuit somehow.

So far i have been using Arduino's PWM control successfully but only for motors rated for voltage equal or greater than that of the power supply. This time I'm conscious of the max. voltage/current on/through the motor. I understand that PWM will reduce the average voltage but my understanding is that the motor will see the full voltage only at shorter intervals. Will this damage the motor? Playing with different PWM values I was able to get the same voltage as previously from bench DC power supply (say, 3V) but the motor noise is quite different. Motor is much louder and it sound as if it's struggling to work. I'm still measuring the same average voltage of 3V on the motor's power pins. I'm afraid this will reduce motor life or burn it completely if I continue letting 6.8V (8.5V?) directly onto it. How safe is to continue this operation?

I also saw a good discussion on regulators here on StackExchange. If PWM is not suitable, should I try a voltage regulator? Ultimately, what I'm trying to achieve is for motor to change the speed/RPM but also the direction - not sure if I can pull this off with the regulators. What would be your advice for this application?

Please don't mind if I used inappropriate terminology - I'm just a daft mechanical guy trying to figure the electrical part of how I can run this safely. I'd appreciate any input. Thanks.

EDIT: This is to provide info on PWM frequencies/mark/space ratios. I hope a chart is OK, it might be easier to explain.

The way Arduino works is you set PWM via a byte, so sending a value between 0 and 255. 0 will let no marks through, 255 will have no spaces.

I plotted here the voltage measurements across motor contacts when: a) no motor is connected, b) when motor was connected and rotation was set to CW, and c) when motor was connected and rotating CCW. CCW rotation voltage should have "-" sign in front but I plotted it this way to be easier to read and compare. Motor had no mechanical load on it.

For the motor, I stopped plotting PWM at point when I almost reached the rated voltage. I'd like to run the motor at minimum of 2.5V, and I'm interested to know how high I can go. Also if this minimum is OK or not (so far it worked just fine off of a bench DC supply).

I hope this makes sense and please let me know if there's anything else I should check and provide.

• A warm welcome to the site. Please can you edit your question and add PWM frequencies and some mark/space ratios you've been using as that essential data's missing. Commented Jul 16, 2020 at 19:46
• There is a lot to read up on with respect to bucking and pwm for driving motors. Not all of it is software, either.
– jonk
Commented Jul 16, 2020 at 21:46
• Thank you TonyM, jonk for replies and the greeting. I have updated the original message to provide more info on PWM. I understand that there is lot more to this, I hope I can figure some of it here. Commented Jul 17, 2020 at 1:49
• Hi @mk1138, Welcome and nice to meet you. Ah, let me see. Your question is basically on DC motor speed (and direction) control, using PWM or "regulator". Let me introduce myself. Ages ago I studied in a trade school, and later worked in mechanical workshops as a technician for a couple of years. I also have a very rusty diploma in electronic engineering and then worked in electronic factories for another couple of years. Fast forward a bit, I have 5 hobbyist years in Arduino, and another 5 years in Raspberry Pi. I have 100+ hours playing with DC motors, sometimes using PWM, / to continue, ... Commented Jul 17, 2020 at 4:38
• Your question asks about DC motor speed control using two methods: (1) PWM, (2) Regulator. You also ask about direction control which I know needs to switch voltage power polarity using switches like double pole double throw relay, or motor drivers like H-Bridge which are basically also switches of motor voltage polarity. / to continue, ... Commented Jul 17, 2020 at 4:44

You can use PWM control under the following two conditions:

• The average voltage applied does not exceed the rated voltage of the motor. This ensures that the average power rating of the motor is not exceeded.
• The duration of the PWM pulse does not cause the winding current to saturate. The rotor windings act like an inductor, which smooths the current, but if the PWM frequency is too low, you won't get a smooth current but rather high current pulses during each PWM pulse.

To do a quick and dirty test, just set the PWM frequency to 20kHz, and set the duty cycle so that the effective voltage applied to the motor, d*Vin, = Vrated.

• Thanks for the reply mr_js. Regarding your second point, is there a rule of thumb for minimum frequency that would cause current pulses that are too high? I'm using a simple micro gear motor and I've been running it for months now at 2.5V with no issues. How can I check if this average voltage via PWM control would damage it? Also, I updated my originating post with more information on how PWM control works with this particular controller, and some measurements. Commented Jul 17, 2020 at 1:54
• @mk1138 for that type of motor (iron-cored armature with many turns) anything above ~3kHz should be high enough. electronics.stackexchange.com/questions/287046/… Commented Jul 17, 2020 at 4:16
• Thank you very much Bruce. I will look into it. There is an Arduino tutorial page discussing the frequency in context of PWM so I'll try to figure it out from there. Motor also comes with an encoder, but without a data sheet and it's giving some non-sensical values. So I'll try to figure it out in a different way. Commented Jul 17, 2020 at 12:26
• @mk1138 If you have an oscilloscope you can measure the current flowing into the DC motor. As a rough rule, tune the PWM frequency so that the ripple is small, e.g. +-5% of the mean value. Keep in mind that if the PWM frequency is less that 20kHz, you may hear it, it will sound like an annoying squeal. Commented Jul 18, 2020 at 9:47
• I don't have any but I will try and get it from someone and check the current that way. Thanks. Commented Jul 18, 2020 at 15:25

Question

The OP wishes to control the speed of a DC motor using PWM or regulator. He also wishes to change the direction of the motor.

This answer is in two parts: Short and Long.

The short answer is kind of an introduction to the long answer.

Part A - Clarifications

A.1 - AC to DC Switching Power Supply and DC-DC Step down voltage regulator

The OP's Q&A on using resistor divider or Zener diode (Ref 5, 6) is not appropriate to step down a power supply for the DC motor.

The following instead are recommended: (1) 110VAC to 12V switching power supply (Ref 4) to get 12VDC, (2) A LM2596 Voltage regulator module is then used to get 3~9VDC for the DC motor.

A.2 - The OP's Average Voltage vs PWM Measurements and Chart

The average voltage measurement used by the OP is not very useful for controlling DC motors for many reasons: (1) DC Motor speed is not linearly proportional to input voltage or current (Appendix B). (2) The DC motor's Voltage, Current, Torque, Efficiency relationship (Appendix B) is complicated.

Part B - Suggestion to run a particular control sequence

The OP would like to run a particular control sequence, involving motor speed, direction, brake. I would suggest to first use the following offline, table top hardware setup to experiment with speed and torque etc, before doing Arduino or Raspberry Pi programming.

1.  GA12-N20 geared motor with Hall effect encoder (TT130, GB37)

2.  Manual UART XY Signal Generator (Arduino/Rpi PWM, NE555, PCA9685)

3.  L298N H-bridge motor driver (L293D, TB6612FNG, BTN7971B)


/ to continue, ...

Part C - Discussion, Conclusion, and Recommendation to Newbies

/ to continue, ...

1. Scope

This answer is in general applicable to 6~12V DC motors, using the L298N H-bridge motor driver, and Arduino C++ or Raspberry Pi python programming.

2. Focus

The focus will be on PWM speed control on DC geared motor, using a power MOSFET and a PWM signal generator.

Once speed control problems are solved, direction control is relatively easy, by just using a H-bridge motor driver, such as L298N to switch power polarity and thus direction. The Arduino or Raspberry Pi programming part is also relatively easy.

So, as recommended by EE StackExchange, we will focus on only one topic:

DC Motor speed control using PWM


/ to continue, ...

References

Part A - Switching Power Supply and Voltage Regulator

Part B - PWM Signal Generator

Part C - PWM H-bridge Motor Driver

Part D - DC Motor Characteristics

Part D - Automation, Control, and Robotics Newbie Reading List

(26) Rotary Encoder (Hall Effect Quadrature Encoder) - Wikipedia https://en.wikipedia.org/wiki/Rotary_encoder

(27) Hall Effect - Wikipedia https://en.wikipedia.org/wiki/Hall_effect

(28) PID Controller - Wikipedia https://en.wikipedia.org/wiki/PID_controller

(29) Root Mean Square - Wikipedia https://en.wikipedia.org/wiki/Root_mean_square

/ to continue, ...

Appendices

Appendix A - L298N Schematic and Operation

Appendix B - PWM Controlling Speed of JB37 Geared Motor

Appendix C - GA12 N20 Gear Motor with Hall Effect Encoder Specification

Appendix D - GA12 N20 Gear Motor Spec

Appendix D - Pololu Hall Effect Encoder

Appendix E - BTN7971B Datasheet Summary

Appendix F - BTN7971B Module

Appendix G - N20 DC Motor PWM and Hall Effect Quadrature Encoder Test

Appendix H - NA20 Motor PWM and Encoder Test Preparation Notes

Appendix I - TT130 6V Quadrature Encoder Test Results

Motor TT130 6V ~= 48 rpm

Appendix J - N20 Quadrature Encoder Test

Appendix K - ZonRi Tech BTN7971B Module Schematic

Appendix L - Calibration of PWM BTN7971B Motor Module with N20 Motor

Appendix M - BTN7971B H-bridge Motor Driver Voltage Drop Measurement

(a) I set 1kHz PWM signal to 100% duty, and also direct 6V PSU to check the motor speed. I found in both cases, the encoder output C1 is

600us, or 1000000/600 = 1666Hz, or 27rpm.

In other words, the motor driver does not seem to have any volt drop causing a reduction of motor speed.

(b) I then measure the voltage drop of the motor driver. What I did was the following.

(b.1) Use a multi-meter to measure the voltage of the power connector at the motor driver board. I found it 6.35V.

(b.2) Use the same multi-meter to measure the voltage across the motor terminals (ie, after voltage drop across the two on resistances of the two MOSFET switches conducting the current through the motor. I found it 6.13V.

(c) Total voltage of the two FET switches

So the voltage drop is 6.35V - 6.13V = 0.22V

(d) The PSU's current meter is not very accurate, shows very roughly under 20mA. So the the

Total on resistance of the two FET switches = 0.22V / 20mA ~= 0.01mΩ (Note 1).

Note 1 - My measurement are not at all precise. Perhaps I should use a L298N motor driver to compare the voltage drop and speed.

Appendix O - MT1208/MX1508 Flyback diode discussion notes

Note: the following article has a discussion on whether it is necessary to us flyback diodes on MX1508.

/ to continue, ...