Shop

Using Motors with Arduino

In general, you will encounter three different kinds of motors in your Arduino projects. Each has its own uses, advantages, and disadvantages.

  • DC Motors – simple to connect and use (just two leads), driven via voltage, not very precise
  • Servo Motors – relatively simple to connect and use (three leads), driven to a particular position or at a particular speed via one or two control commands
  • Stepper Motors – complex to connect and use (four or more leads), driven to a particular position via special sequencing of control commands, can move very precise distances at very precise speeds

The Vernier Digital Control Unit (DCU) provides an easy way to control many types of motors with an Arduino. Many Arduino boards have at least 16 digital output lines, but these lines can only be used directly to turn on low-current electronics, such as LEDs and buzzers. They cannot drive enough current for most motors. Dedicated Arduino “motor shields” are often sold for this purpose, but another option is to use the Vernier DCU.

DC Motor Control with PWM

DC motors are simple electronic components. Apply a voltage across the two leads and the motor spins. Apply a greater voltage and it spins faster. Switch the leads and it spins in the opposite direction.

One limitation with many microcontrollers, like the Arduino, is that it only offers digital lines for output. You can set an output line HIGH (max voltage) or LOW (zero voltage), but you can’t directly output a voltage in between. There is an indirect method however, that allows you to output a voltage some place between max and zero. This process is called pulse-width modulation (PWM). In order to output a voltage midrange between max and zero, a microcontroller can alternate between the HIGH and LOW output settings very quickly. If it switches equally between max and zero, the output voltage is effectively one-half the max. If it is HIGH twice as long as it is LOW, then it is effectively outputting a voltage two-thirds max.

The sample sketch, VernierLibTutorialDCUPWM, uses PWM to gradually speed up and slow down a DC motor. Connect the DC motor to lines D4 and GND on a Vernier DCU. Plug the DCU into the Digital 2 port on the Vernier Arduino Interface Shield or a Digital Protoboard Adapter wired to Arduino pins 6, 7, 8, and 9 as explained in the Connecting Vernier Sensors to the Arduino Using a Breadboard section.

Servo Motors

Servo motors are popular with many Arduino users, because they allow for precise control of angular position. A standard servo is small, but strong, and energy-efficient, because it draws power proportional to its load.

The sample sketch, VernierTutorialDCUServo, controls the position of a servo arm in response to raw voltage from any Vernier Analog (BTA) sensor. You will need to include the servo.h library at the beginning of your sketch. If you are using a servo motor with a standard 3-pin plug, you can connect it directly to the 3 pins on the Vernier DCU labeled “Servo.” Be sure to verify that the orientation of the servo connector is correct (black lead to GND and white or yellow lead to 4). If your servo motor does not have a 3-pin plug, you can wire it to the DCU screw terminal using the wiring diagram below.

  • Servo Control line (white or yellow wire) to DCU line D4
  • Servo 5V (red wire) to DCU line XP (power)
  • Servo GND (black wire) to DUC line GND

Plug the sensor and DCU into the Analog 1 and Digital 2 ports, respectively, on the Vernier Arduino Interface Shield or into Analog and Digital Protoboard Adapters wired as explained in the Connecting Vernier Sensors to the Arduino Using a Breadboard section. An external power supply, such as the LabQuest Power Supply, is required to power the servo motor.

Stepper Motors

There are two basic types of small, permanent-magnet stepper motors: bipolar and unipolar. Bipolar stepper motors have four wires that connect to the ends of two motor coils. Unipolar motors have five, six, or eight wires, four of which will connect to the ends of four motor coils. The easiest way to control a stepper motor with an Arduino is to connect it to the DCU, but the tricky part is identifying how to connect the wires.

If you are using a bipolar stepper motor, the first step is to identify which two wires go to the same coil. You can do this either by looking at a wiring diagram on the manufacturer’s website or by measuring the resistance between a pair of wires with a multimeter. The motor coil will have only a few ohms of resistance (often about 20 ohms on small motors), so find the pairs of wires with low resistance between them. When you have identified the two pairs, wire one pair to DCU lines D1 and D2; wire the other pair to DCU lines D3 and D4. Plug the DCU into the Digital 2 port on the Vernier Arduino Interface Shield or a Digital Protoboard Adapter wired to Arduino pins 6, 7, 8, and 9 as explained in the Connecting Vernier Sensors to the Arduino Using a Breadboard section. Connect an external power supply, such as the LabQuest Power Supply, to the DCU.

If you are using a unipolar stepper, you need to identify which four wires are “live.” The “live” wires will go to the four motor coils; the other wires are ground wires. Use a multimeter to measure the resistance between pairs of wires. The resistance between one “live” wire and a ground wire will be small (about 20 ohms), while the resistance between two ground wires will be negligible (zero). When you have identified the four “live” wires, connect them to DCU lines D1, D2, D3, and D4.

The VernierLib library contains a function to control a stepper motor:

Vernier.DCUStep(stepCount, stepDirection, stepDelay);

where stepCount is the number of steps the motor turns, stepDirection specifies whether the motor rotates clockwise or counterclockwise, and stepDelay is the pause between each step measured in milliseconds (the shorter the delay, the faster the motor turns). Note that the number of degrees the motor turns per step is determined by the manufacturer.

The sample sketch, VernierLibTutorialDCUStepper, will make the stepper motor rotate one complete rotation clockwise, stop for one second, and then rotate one complete rotation counterclockwise. Note if your motor does not rotate as expected, try switching the wires in the DCU screw terminal. Finding the correct wiring pattern is basically a trial-and-error situation.