{"id":5513,"date":"2019-03-02T16:52:05","date_gmt":"2019-03-02T21:52:05","guid":{"rendered":"https:\/\/dronebotworkshop.com\/?p=5513"},"modified":"2023-04-12T11:48:35","modified_gmt":"2023-04-12T15:48:35","slug":"dc-gearmotors-pwm","status":"publish","type":"post","link":"https:\/\/dronebotworkshop.com\/dc-gearmotors-pwm\/","title":{"rendered":"Control Large DC Gearmotors with PWM & Arduino"},"content":{"rendered":"\n
<\/a> <\/a> <\/a><\/p>\n I\u2019m very excited to announce a new project here in the workshop!<\/span><\/p>\n I\u2019m building a robot. This probably won\u2019t come as Earth-shattering news on a site called \u201cDroneBot Workshop\u201d, but this robot is different. \u00a0Unlike the small \u201crobot car\u201d bases I\u2019ve used in past projects, this will be a big robot. A capable robot.<\/span><\/p>\n A \u201creal\u201d robot!<\/span><\/p>\n Real robots need big motors and for my robot, I\u2019ve chosen a couple of large DC gearmotors to do the job. \u00a0These powerful motors consume a lot of current, which means I\u2019ll need to use a motor driver that can handle the current without burning up.<\/span><\/p>\n Today I\u2019ll show you how to do exactly that, control a large DC gearmotor and change its speed and direction. I\u2019ll be using an Arduino to create a Pulse Width Modulation (PWM) signal to regulate the motor speed and a Cytron MD10C motor driver to supply the power.<\/span><\/p>\n <\/p>\n Let\u2019s get started!<\/span><\/p>\n DC gearmotors are used in many electromechanical applications in appliances, industry, and robotics. \u00a0Their high torque and low cost make them popular components for experimenters and hobbyists. <\/span><\/p>\n A \u201cnormal \u201cDC motor spins at a very high speed, often several thousand RPM. \u00a0This is great for a high-speed drill but much too fast for spinning wheels to move a robot or car around. \u00a0<\/span><\/p>\n In a gearmotor there are, not surprisingly, a set of gears that reduce the motor speed to something more manageable, a few hundred RPM or even less. \u00a0The motor I\u2019m using has a full-speed shaft rotation of just 110 RPM.<\/span><\/p>\n <\/p>\n When you reduce speed using gears you also increase torque, this inverse relationship means the slower you gear the motor down the more torque it will have. \u00a0\u00a0<\/span><\/p>\n Most DC gearmotors, including the one I purchased for my robot, use brushed DC motors.<\/span><\/p>\n Pulse Width Modulation, or PWM, is a method of controlling the speed of a motor. It actually has many uses beyond that, controlling lights and LED\u2019s and data communications are a few other applications of PWM.<\/span><\/p>\n With PWM control the DC current applied to the motor is sent in square-wave pulses. The width of the pulses is changed to regulate motor speed, the wider the pulse the faster the motor will spin.<\/span><\/p>\n The pulse width is expressed as a percentage, at 50% the output is a perfect square wave. At 75% the pulse spends 75% of its time HIGH, the rest LOW. \u00a0At 25% it is HIGH 25% of the time and LOW for 75%. <\/span><\/p>\n This width is called the \u201cduty cycle\u201d.<\/span><\/p>\n At 100% width the pulse is constantly HIGH. The motor receives full power and spins at its rated output speed.<\/span><\/p>\n At 0% the signal is constantly LOW, essentially meaning no voltage at all. Obviously, this causes the motor to stop.<\/span><\/p>\n For a detailed explanation of PWM please see the article \u201c<\/span>Controlling DC Motors with the L298N Dual H-Bridge and an Arduino<\/span><\/a>\u201d. <\/span><\/p>\n A common method of controlling a DC motor is to use an \u201cH-Bridge\u201d. This type of controller allows you to change the polarity of the current sent to the motor.<\/span><\/p>\n An H-Bridge is an arrangement of switches that allows you to apply current to a DC motor and reverse the polarity to spin the motor in the opposite direction, as illustrated below:<\/span><\/p>\n <\/p>\n By turning on two of the switches the current is directed to the motor, In the following illustration, the motor spins clockwise when the switches are turned on:<\/span><\/p>\n <\/p>\n Turning those switches off and then turning on the remaining two switches will cause the polarity of the current to be reversed. As a result, the motor spins counterclockwise.<\/span><\/p>\n <\/p>\n Of course in real life, we seldom would use switches to create an H-Bridge.<\/span><\/p>\n Instead, we would use an active semiconductor switch, like a transistor, to do the switching for us.<\/span><\/p>\n In a traditional H-Bridge, like the L298N, the switching elements are built with bipolar transistors.<\/span><\/p>\n Bipolar transistors are the oldest and one of the most common types of transistor. They are inexpensive and readily available, making them ideal for use in circuits like H-Bridges.<\/span><\/p>\n <\/p>\n The problem with bipolar transistors is that in switching mode they act a lot like a \u201cswitched diode\u201d, and like any diode, they have a voltage drop. A voltage drop of 0.7 volts.<\/span><\/p>\n While that may not sound like a lot of voltage it actually does cause a lot of issues with H-Bridge circuits based upon bipolar transistors.<\/span><\/p>\n First, there are two transistors, so the total voltage drop is 1.4 volts. This is significant, especially for low-voltage motors like the 6-volt ones commonly used in small robot cars. A 6-volt power supply would only deliver 4.6 volts (6 – 1.4) to the motor, causing it to operate below its peak efficiency.<\/span><\/p>\n Second, that voltage doesn\u2019t just disappear. Instead, it is dissipated, mostly as heat. This is why many H-Bridge designs require big heatsinks.<\/span><\/p>\n There is actually a better component that can be used, one that doesn\u2019t exhibit the problems suffered by bipolar transistor designs.<\/span><\/p>\n A metal-oxide-semiconductor field-effect transistor<\/em>, or <\/span>MOSFET<\/span><\/a>, is a more advanced transistor that has become extremely popular in switching and amplifier circuits. <\/span><\/p>\n When used as switch a MOSFET acts more like a \u201cswitched resistor\u201d than a \u201cswitched diode\u201d. \u00a0\u00a0A very low-value resistor at that.<\/span><\/p>\n <\/p>\n This gives the MOSFET a very low voltage drop. \u00a0The 0.1-volt drop on the above illustration is actually just an estimate, as a MOSFET acts more like a resistor the actual voltage drop is really determined by the amount of current flowing through it.<\/span><\/p>\n Suffice to say that the voltage drop is extremely low.<\/span><\/p>\n Because the MOSFET doesn\u2019t drop the voltage as much as a bipolar transistor it is superior to the bipolar transistor in the following ways:<\/span><\/p>\n For the large gearmotor I\u2019m working with I\u2019ll be using a motor driver based upon MOSFETs.<\/span><\/p>\n When searching for a MOSFET-based H-Bridge that could power my motor I ran across several devices. Most of them were fairly expensive, often exceeding the cost of the motor itself.<\/span><\/p>\n There are, however, a few exceptions.<\/span><\/p>\n One of them is the Cytron MD10C driver board<\/a>. It\u2019s inexpensive and has some very impressive features.<\/span><\/p>\n The Cytron MD10C is a MOSFET-based motor driver that is very easy to use. It has the following specifications:<\/span><\/p>\n Another feature, of course, is its low price. It\u2019s currently available for less than 12 US dollars per unit, much less than drivers with similar specifications.<\/span><\/p>\n Hooking up the Cytron MD10C is very simple.<\/span><\/p>\n <\/p>\n On one side of the board there are four screw terminals, they are as follows:<\/span><\/p>\n On the opposite end of the board you will find the input connector. It has three pins, as follows:<\/span><\/p>\n There are also two push buttons on the board, labeled \u201cA\u201d and \u201cB\u201d. \u00a0\u00a0Pressing either one of these will spin the motor in one or the other direction, depending upon which one you press. It is a nice feature as it allows you to test your motor and power supply wiring without needing a PWM controller.<\/span><\/p>\n The motor direction is also indicated by two LEDs, also labeled \u201cA\u201d and \u201cB\u201d. <\/span><\/p>\n The Cytron MD10C has several advantages over the L298N.<\/span><\/p>\n The high-efficiency and low heat dissipation make the Cytron MD10C ideal for robotics applications, where every bit of energy needs to be used as efficiently as possible.<\/span><\/p>\n <\/p>\n Physically the L298N board is about half the size as the MD10C, and you should also note that the L298N can drive two motors whereas the MD10C is a single motor controller. <\/span><\/p>\n Cytron also manufactures the MD10A, a dual-channel version of the MD10C.<\/span><\/p>\n The board is packaged along with some plastic standoffs and a connector for the input, a nice touch.<\/span><\/p>\n We have used Pulse Width Modulation (PWM) with the Arduino in many of our designs, for DC and servo motor control as well as for regulating the intensity of LEDs. <\/span><\/p>\n In the Arduino IDE you can control PWM using the <\/span>analogWrite<\/span><\/i><\/a> command. \u00a0This command has two inputs:<\/span><\/p>\n Not every pin on the Arduino is capable of PWM. On many Arduino (and clone) boards you\u2019ll notice a small symbol beside the PWM-capable pins, usually a sine or square wave or tilde sign (i.e. \u00a0\u201c~\u201d ).<\/span><\/p>\n On the Arduino Uno pins 3, 5, 6, 9, 10, and 11 support pulse width modulation.<\/span><\/p>\n In our Arduino PWM controller we are going to make use of another component that I\u2019ve worked with before, the LCD Keypad Shield. \u00a0You\u2019ll find details for using this shield in the article <\/span>Using LCD Displays with Arduino<\/span><\/i><\/a>.<\/span><\/p>\n The keypad shield has a number of push buttons and they operate in a rather unique fashion. Instead of using digital I\/O pins these buttons are connected to a resistor array. The array acts as a voltage divider and every time you press a button you\u2019ll send a different voltage out. This is read on analog input A2 on the Arduino.<\/span><\/p>\n Hooking up all the components for our PWM test is pretty simple.<\/span><\/p>\n <\/p>\n You\u2019ll need to supply power that is suitable for your motor, I show a 12-volt power supply here but if you have a motor with a different voltage rating then you should change this to suit your motor. \u00a0Keep in mind that the MD10C has a voltage range of 5 to 30-volts.<\/span><\/p>\n Make sure that your power supply has a suitable current rating for powering your motor.<\/span><\/p>\n There are not many connections to the Arduino at all, just the ground and the DIR and PWM pins. I\u2019m using pin 3 as it is PWM-capable. \u00a0<\/span><\/p>\n <\/p>\n After you get it all wired up install your keypad shield.<\/span><\/p>\n In my test setup, I used an additional shield, a prototyping shield with screw terminals. This is a really handy shield as it sits in between the Arduino Uno (or Mega) and the shield you are working with. Screw terminals are provided for access to every Arduino pin. <\/span><\/p>\n <\/p>\n This arrangement allows me to connect to the Arduino while a shield is mounted, otherwise, you may need to do a bit of soldering to make the connections.<\/span><\/p>\n Cytron provides an Arduino sketch that can be obtained through the MD10C Wiki, unfortunately the sketch is not really usable in the form they provide it. \u00a0Two problems actually:<\/span><\/p>\n I\u2019ve resolved both those issues and fixed up a bit of the code. \u00a0Here is what it looks like now:<\/span><\/p>\nIntroduction<\/span><\/h2>\n
DC Gearmotors<\/span><\/h2>\n
Pulse Width Modulation<\/span><\/h2>\n
H-Bridges<\/span><\/h2>\n
H-Bridge Operation<\/span><\/h3>\n
H-Bridge with Bipolar Transistors<\/span><\/h3>\n
H-Bridge with MOSFETs<\/span><\/h3>\n
\n
Cytron MD10C Motor Driver<\/span><\/h2>\n
Cytron MD10C Specifications<\/span><\/h3>\n
\n
Cytron MD10C Connections<\/span><\/h3>\n
\n
\n
Cytron MD10C vs L298N H-Bridge<\/span><\/h3>\n
\n
Arduino PWM<\/span><\/h2>\n
\n
Arduino Hookup<\/span><\/h3>\n
Basic Arduino PWM Sketch<\/span><\/h3>\n
\n
\/*\r\n PWM Motor Test\r\n pwm_motortest.ino\r\n Uses LCD Keypad module\r\n Modified from Cytron example code\r\n \r\n DroneBot Workshop 2019\r\n
Welcome to the Workshop!<\/a><\/blockquote>