Skip to main content

What outputs can I use with my Raspberry Pi? Part 3 - Motors

This article was written by James Macfarlane and Lucy Rogers

Parts 1 and Part 2 of this series of articles has talked about connecting LED's, relays and solenoids to your Raspberry Pi. This section focuses on motors.

Motors

Electric motors produce rotational motion from electricity. They are available in a massive variety of shapes and sizes from a few millimetres across to a few metres. Motors can do all kinds of useful work, turning pumps, fans, conveyor belts and all kinds of other mechanisms. There are many different types of motor. In this section, we will look at a few of the common ones and describe how to control them from the Pi. We start, though, with a quick review of how motors work.

DC Motor (Image credit: RS Components)

Figure 1: DC Motor (238-9721) - (Image credit: RS Components)

How Motors Work

Imagine you have a wheel that’s mounted on bearings so it can rotate freely, for example, the wheel of a skateboard. If you attach a magnet to the wheel, a second magnet moving nearby will cause the wheel to rotate a little bit, because the magnets attract or repel each other. We can swap the second magnet for a set of electromagnets, arranged evenly around the wheel. If they are switched on and off in the right order, the wheel can be made to rotate continuously. We could now couple some kind of mechanical load (e.g. a fan) to the wheel and make it do useful work.

What we’ve just described is exactly how most AC motors and brushless DC motors (including stepper motors) work. In the case of a brushless DC motor, an electronic circuit ensures the electromagnets are controlled in the most optimal way. In an AC motor, the alternating current creates a changing magnetic field so no electronics is needed, but this means the speed of the motor is now dictated by the frequency of the AC supply.

If we want a DC motor without electronics, we can swap our set-up around. If we now put the electromagnets on the wheel and the permanent magnet on the outside, we can arrange for the switching of the electromagnets to happen automatically by connecting them to a set of copper segments arranged in a ring that rotates with the wheel. The electricity is supplied via a pair of spring-loaded carbon blocks which rub against the segments. As the wheel rotates, the supply connects to each electromagnet in turn, ensuring maximum repulsion (or attraction) whatever the angle of the wheel. What we have described is, of course, a conventional (brushed) DC motor. The ring of copper segments is called the commutator and the carbon blocks are, of course, the brushes.

Motor Terminology

The part of the motor that rotates is called the rotor and the fixed part is called the stator. The coils to which the main electrical input is applied are called the armature - this might be on the rotor (as in a brushed DC motor) or the stator (as in a stepper motor or an AC induction motor.) The armature always makes a rotating magnetic field (as we have seen, this can be done in three ways: external circuitry, alternating current, or a commutator.) In order that the motor can move, the armature needs to react against another magnetic field. The part of the motor which produces this is called, simply, the field. The field is usually a set of permanent magnets but can also be an electromagnet or, in the case of an induction motor, an electric current induced in a piece of metal by the action of the armature’s alternating field.

Speed and Torque

For any particular motor, the speed of rotation is related to the voltage applied to the input terminals (with a slight caveat for AC motors, as we shall see.) The current drawn by a motor depends on the torque, i.e. the amount of mechanical load you apply to the output shaft.

Controlling Motors

Turning motors on and off with a Pi is just the same as switching any other medium to high current load. Simple driver circuits like the open collector driver can be used or, for bigger motors, an open collector driving a relay.

Things get more interesting when we want to control the speed of a motor.

DC Motors

Brushed DC motors are controlled by varying the applied voltage. This can be done simply and efficiently using PWM (pulse-width modulation). PWM can be used with an open-collector driver circuit but this doesn’t allow us to control the direction of the motor, though. To do this, a more sophisticated driver circuit is required called an H-bridge. It is called this because the circuit looks a bit like a letter ‘H’. It consists of four electronic switches (usually MOSFET transistors) arranged as two push-pull circuits (See the Inputs article.) If the top left and bottom right switches are on, the motor will turn one way. If the bottom left and top right switches are on, the motor will turn the other way. If no switches are on, the motor will free-wheel to a halt. The circuit must ensure that the top and bottom switches on one side can never be on together or they will short-circuit the supply. The H-bridge circuit can be fed with a PWM signal to allow speed and direction to be controlled. There are various HATs and add-on boards available that include H-bridges, such as Adafruit’s DC & Stepper Motor HAT. Make sure you check the datasheet to ensure the H-bridge is suitable for your motor.

Brushless DC Motors

Brushless DC motors (BLDCs) usually consist of a set of three armature coils which need to be driven in the correct sequence. This requires special driver circuits. In the world of drones and model radio control, these drivers are called speed controllers. They can control very powerful motors using only a 1-2ms PWM signal of the same format as an RC servo and cost relatively little. However, they are not suitable for applications needing precise control. For this, special driver circuits are available that usually require an analogue control signal, see the Analogue Output section below.

AC Motors

Large AC induction motors get used for all kinds of industrial applications but closer to home, small AC motors can be found in washing machines, fan ovens, central heating pumps, etc. Most applications that use AC motors just need them to turn on or off. To control the speed, the frequency and voltage of the incoming supply must be changed. This requires a special device called an inverter or VFD (Variable Frequency Drive.) VFDs are usually controlled by a low-voltage analogue control signal, again, see the Analogue Output section below.

Servo Motors

RC Servo (Image credit: RS Components)

Figure 2: RC Servo (781-3058) - (Image credit: RS Components)

A servo motor is a regular motor combined with a position sensing device and some drive electronics. The servo has two sets of electrical connections, one for power and one for the demand signal which tells it how to move. In small servos, these share a common ground. Unlike a normal motor, the output shaft of a servo doesn’t (usually) rotate continuously. Instead, it will move to an angle which corresponds to the demand signal. Servos are compact, fast, powerful and accurate. The most familiar kind of servo (Figure 2) are the ones used in radio-controlled (RC) cars, planes, etc, but they are also available in larger industrial versions and can be used in all kinds of applications where you want to accurately control the position of something, for example, in robotics.

Inside an RC servo is a motor, a set of gears, a potentiometer and an electronic driver circuit. When the demand signal changes, the driver circuit will cause the motor to turn until the feedback from the potentiometer tells it the shaft is in the desired position.

A servo will move to a position and hold that position even if an external force pushes against it. The maximum amount of force the servo can exert is called the torque rating. Most RC servos can only turn through about 170 degrees, but some servos can rotate a full turn or even multiple turns.

The great thing about RC servos is all of the electronics is built-in. You just provide DC power, usually in the range of 4-7V and a logic-level demand signal. With RC servos, the demand signal is pulse-width modulated. It takes the form of a train of a pulse which repeats every 20ms or so. The width of the pulse (the on-time) tells the servo what to do. A pulse width of 1.5ms will put the servo at its central position. Shorter (down to 1ms) or longer (up to 2ms) pulses will make it rotate up to around 90 degrees on either side of centre.

Note that the hardware and software for generating servo PWM pulses are usually slightly different from that used for controlling DC motor speed using PWM. Servo PWM has a very low duty factor and needs quite precise control of pulse on- time, whereas, for motor speed control, the PWM signal ranges from 0-100% DF.

There are a limited number of PWM pins on the Pi, and software PWM usually doesn’t cut it for accurately controlling servos. Instead, you can use an add-on board, such as Adafruit’s 16-Channel 12-bit PWM/Servo Driver. This is a breakout board for the PCA9685 (727-5649) chip and connects to the Pi using I2C (more on this in the Inputs article) which only needs two pins (specifically, pins 3 and 5). You can also purchase the NXP PCA9685PW (727-5649) chip separately.

Note: Powering Servos

When a servo motor starts moving it draws quite a lot of power. If the power supply is insufficient, the voltage may drop. Small RC servos can usually be run from the Pi’s 5V rail but bigger servos might cause the Pi to reset itself, especially if a lot of torque is demanded suddenly. If this happens you could try adding a high-value capacitor (470uF or greater) between GND and the 5V but it is best practice to use a separate power supply for servos.

Stepper Motors

Stepper Motor (Image credit: RS Components)

Figure 3: Stepper Motor (535-0372) - (Image credit: RS Components)

Stepper motors are a special kind of brushless DC motor. They move in highly accurate, repeatable steps. Stepper motors are used for many precision motion control applications, such as inkjet and 3D printers.

Stepper motors are generally much bulkier than DC motors of equivalent power and have lower efficiency (they use power even when not moving.) However, to get the same positional accuracy with DC motors, you need a rotary encoder (see Inputs) and a well-tuned digital feedback system. So, while servo systems can be smoother, quieter, more efficient and more compact than stepper motors, steppers nonetheless win on the grounds of cost and complexity in many applications.

Unlike RC servos, stepper motors don’t have any concept of absolute position. Limit switches or ‘home’ detectors are required for safety and to establish a reference position, usually each time you power-up the system.

Inside a stepper motor, there is a fixed set of coils organised in groups called “phases”. Inside the coils is a rotor made of permanent magnets. There are usually two phases and each can be energised in the forward or reverse direction.

As each phase is energised, the motor rotates one step at a time.

The two main varieties of stepper motor are bipolar and unipolar. The former brings out a pair of wires for each phase, giving a total of four wires. The later adds a “centre-tap” to each phase, giving six wires. This can make the drive electronics easier, at the cost of a reduction in the power-to-weight ratio.

Like any kind of motor, you can’t directly connect a stepper motor to a computer or microcontroller. A driver circuit is needed. Bipolar steppers need a pair of H-bridges to drive them. Unipolar motors can be driven by a set of four simple open collector circuits. Both types of driver are controlled by a set of four logic-level signals which must be sequenced correctly. You can also buy pre-packaged driver modules which take a power input and a pair of logic-level signals for “step” and “direction”. Some drivers are capable of “microstepping” which gives finer steps and smoother movement of the motor.

Whichever driver circuit you use, the generation of the stepping pulses using a Pi is easy if you don’t need much speed from the stepper motor and don’t mind if the movement is a bit “jerky”. Getting fast, smooth, movements from a stepper requires good hardware timers, though, putting it in the domain of microcontrollers or other external hardware.

If you want all the gory details, Atmel provides a really nice application note “AVR446: Linear speed control of stepper motors”, that goes into a lot of detail on this.

If you just want to get a bunch of stepper motors going with the Pi, Adafruit provides the DC & Stepper Motor HAT or you can buy the PCA9685 (727-5649) chip separately.

Like any motor, you can increase the torque of a stepper motor by using a gearbox. This is a trade-off, of course - gearboxes will cause a reduction in speed in direct proportion to the increase in torque.

NEMA Ratings

The National Electrical Manufacturers Association (NEMA) is a US Manufacturers Organization which promotes standardised product specifications for electrical parts. The NEMA size ratings on a stepper motor are the faceplate dimensions used for mounting the motor and not the motor characteristics.

Choosing a Motor

We’ve looked at several different types of motor in this section. We’ll end with a rough guide to selecting the best one for your application.

  • If you want continuous rotation, plenty of torque and good control over a wide range of speeds, brushed DC motors are probably the cheapest and best option.

  • For high efficiency, weight-critical battery-operated applications and/or very high power density, brushless DC motors are king - you can hold an 8kW BLDC motor in one hand (try doing that with an 8kW AC induction motor.)

  • If you want control of position rather than speed, and only need a small range of movement, RC servos are a cheap and compact solution that will save you all the bother of dealing with drive electronics.

  • If you need large amounts of movement without loss of precision, steppers are the answer, so long as you don’t need a lot of torque and speed at the same time.

  • If your requirement is for a super-smooth, precise, compact, energy-efficient position control, then this is the domain of servo systems based on DC (or synchronous AC) motors, coupled with digital encoders.

  • If the application is not weight/size critical and you need continuous rotation, lots of power (200W or more), only limited (if any) speed control, and have a mains supply available, then AC induction motors are the most cost-effective option.

Analogue Output

A lot of pre-packaged driver modules for motors, lamps, and other applications require an analogue control signal, often in the range 0-5V or 0-10V.

There are two ways of generating an analogue signal from the Pi.

DAC Digital to Analogue Converter

We met ADCs in the Inputs articles. A DAC does the opposite, it generates an analogue output in response to a digital input.

A number of DACs are available with SPI or I2C interfaces that will work with the Pi, for example, the >MCP4725 (669-6290) . Adafruit has a tutorial on using the MCP4725 with the Pi.

PWM and Filtering

A slightly less accurate but cheaper method of obtaining an analogue signal from the Pi or microcontroller is to generate a PWM waveform then low-pass filter it, leaving a smooth DC voltage. There are lots of different kinds of filter circuit, but a very simple RC (Resistor Capacitor) filter will usually do the job. More on this very shortly.

Voltage Range

DAC chips like the MCP4725 (669-6290) cannot generate an output voltage higher than their own supply voltage. When run off a 3.3V supply, this limits us to a range of 0-3.3V. The same applies to generating an analogue signal by filtering a PWM output. To generate control signals for external drivers, we often need a 0-5V signal or a 0-10V signal. One way of achieving this is to amplify the output of the DAC or filter using an op-amp. An op-amp (full name: operational amplifier) is a very useful chip for all kinds of analogue circuit tasks. Most good electronic books describe them in detail. We recommend The Art of Electronics by Horowitz and Hill (ISBN 0521809266). Figure 4 gives an example of using an op-amp circuit to generate a 0-10V output from a filtered PWM signal.

Generating a 0-10V analogue output from the Pi using a filtered PWM output and an op-amp.

Figure 4: Generating a 0-10V analogue output from the Pi using a filtered PWM output and an op-amp.

Resistor R1 and capacitor C1 are the filter. The values are chosen to give a “cut- off” frequency (the point when the filter cuts its output to 70% of the input) of about 48Hz. If the Pi is set to produce a PWM frequency of 100-1000 times the cut-off, the output of the filter will be a smooth voltage with almost no trace of the original PWM waveform. R2 and R3 are chosen so that the op-amp gives a gain of about 3. C2 is performing the role of a decoupling capacitor (also called a bypass capacitor.) It helps smooth the supply to the op-amp to ensure that it operates in a stable manner. The op-amp is a “single-supply” type. This means it can cope with inputs and outputs very near its negative supply (0 volts in our case.) The op-amp needs a higher supply voltage than the Pi - if you want 10V out, it obviously needs at least a 10V supply but not all op-amps can swing their output right up to the positive supply rail. The LM358, for example, needs a supply of 2-3 volts greater than the maximum output you want, hence a 12v supply. This supply shares a ground with the Pi.

Conclusion

These articles looked at how to control various outputs (loads) using a Pi through the GPIO pins. It also looked at how some of those outputs work. Other types of output can be connected to another microcontroller and controlled via the Pi through Serial, USB, Wifi, Bluetooth, etc., Monitors, screens and sound are also common outputs not covered here.

You should now be able to choose the required “thing” for your project.

The next part of this series of articles (Part 4), looks at how to protect your Pi, how to add additional power supplies safely and prevent ground loops.

I am an inventor, engineer, writer and presenter. Other stuff: Royal Academy of Engineering Visiting Professor of Engineering: Creativity and Communication at Brunel University London; Fellow of the Institution of Mechanical Engineers and have a PhD in bubbles; Judge on BBC Robot Wars.
DesignSpark Electrical Logolinkedin