This chapter discusses motor control and shows 8051 interfacing with relays, optoisolators, stepper motors, and DC motors. In Section 17.1, the basics of relays and optoisolators are described. Then we show their interfacing with the 8051. In Section 17.2, stepper motor interfacing with the 8051 is shown. The characteristics of DC motors are discussed in Section 17.3, along with interfacing to the 8051. We will also discuss the topic of PWM (pulse width modulation). We use both Assembly and C in our programming examples.


This section begins with an overview of the basic operations of electromechanical relays, solid-state relays, reed switches, and optoisolators. Then we describe how to interface them to the 8051. We use both Assembly and C language programs to demonstrate their control.

Electromechanical relays

A relay is an electrically controllable switch widely used in industrial controls, automobiles, and appliances. It allows the isolation of two separate sections of a system with two different voltage sources. For example, a +5V system can be isolated from a 120V system by placing a relay between them. One such relay is called an electromechanical (or electromagnetic) relay (EMR) as shown in Figure 17-1. The EMRs have three components: the coil, spring, and contacts. In Figure 17-1, a digital +5V on the left side can control a 12V motor on the right side without any physical contact between them. When current flows through the coil, a magnetic field is created around the coil (the coil is energized), which causes the armature to be attracted to the coil. The armature’s contact acts like a switch and closes or opens the circuit. When the coil is not energized, a spring pulls the armature to its normal state of open or closed. In the block diagram for electomechan-ical relays (EMR) we do not show the spring, but it does exist internally. There are all types of relays for all kinds of applications. In choosing a relay the following characteristics need to be considered:

  1. The contacts can be normally open (NO) or normally closed (NC). In the NC
    type, the contacts are closed when the coil is not energized. In the NO, the
    contacts are open when the coil is unenergized.

  2. There can one or more contacts. For example, we can have SPST (single pole,
    single throw), SPDT (single pole, double throw), and DPDT (double pole, dou
    ble throw) relays.

  3. The voltage and current needed to energize the coil. The voltage can vary from
    a few volts to 50 volts, while the current can be from a few mA to 20 mA. The
    relay has a minimum voltage, below which the coil will not be energized. This
    minimum voltage is called the “pull-in” voltage. In the datasheet for relays we
    might not see current, but rather coil resistance. The V/R will give you the
    pull-in current. For example, if the coil voltage is 5V, and the coil resistance
    is 500 ohms, we need a minimum of 10 mA (5V/500 ohms = 10 mA) pull-in

The maximum DC/AC voltage and current that can be handled by the contacts.This is in the range of a few volts to hundreds of volts, while the current can be from a few amps to 40A or more, depending on the relay. Notice the difference between this voltage/current specification and the voltage/current needed for energizing the coil. The fact that one can use such a small amount of voltage/current on one side to handle a large amount of voltage/current on the other side is what makes relays so widely used in industrial controls. Examine Table 17-1 for some relay characteristics.

Figure 17-1. Relay Diagrams

Table 17-1: Selected DIP Relay Characteristics (

Driving a relay

Digital systems and microcontroller pins lack sufficient current to drive the relay. While the relay’s coil needs around 10 mA to be energized, the microcontroller’s pin can provide a maximum of 1-2 mA current. For this reason, we place a driver, such as the ULN2803, or a power transistor between the microcontroller and the relay as shown in Figure 17-2.

Figure 17-2. DS89C4xO Connection to Relay

The following program turns the lamp on and off shown in Figure 17-2 by energizing and de-energizing the relay every second.

Table 17-2: Selected Solid-State Relay Characteristics (

Solid-state relay

Another widely used relay is the solid-state relay. In this relay, there is no coil, spring, or mechanical contact switch. The entire relay is made out of semiconductor materials. Because no mechanical parts are involved in solid-state relays, their switching response time is much faster than that of electromechanical relays. Another problem with the electromechanical relay is its life expectancy. The life cycle for the electromechanical relay can vary from a few hundred thousands to few million operations. Wear and tear on the contact points can cause the relay to malfunction after a while. Solid-state relays have no such limitations. Extremely low input current and small packaging make solid-state relays ideal for microprocessor and logic control switching. They are widely used in controlling pumps, solenoids, alarms, and other power applications. Some solid-state relays have a phase control option, which is ideal for motor-speed control and light-dimming applications. Figure 17-3 shows control of a fan using a solid-state relay (SSR).

Figure 17-3. 8051 Connection to a Solid-State Relay

Reed switch

Another popular switch is the reed switch. When the reed switch is placed in a magnetic field, the contact is closed. When the magnetic field is removed, the contact is forced open by its spring. The reed switch is ideal for moist and marine environments where it can be submerged in fuel or water. They are also widely used in dirty and dusty atmospheres since they are tightly sealed.

Figure 17-4. Reed Switch and Magnet Combination


In some applications we use an optoisolator (also called optocoupler) to isolate two parts of a system. An example is driving a motor. Motors can produce what is called back EMF, a high voltage spike produced by a sudden change of current as indicated in the V = Ldi/dt formula. In situations such as printed circuit board design, we can reduce the effect of this unwanted voltage spike (called ground bounce) by using decoupling capacitors (see Appendix C). In systems that have inductors (coil winding), such as motors, decoupling capacitor or a diode will not do the job. In such cases we use optoisolators. An optoisolator has an LED (light-emitting diode) transmitter and a photosensor receiver, separated from each other by a gap. When current flows through the diode, it transmits a signal light across the gap and the receiver produces the same signal with the same phase but a different current and amplitude. See Figure 17-5. Optoisolators are also widely used in communication equipment such as modems. This allows a computer to be connected to a telephone line without risk of damage from power surges. The gap between the transmitter and receiver of optoisolators prevents the electrical current surge from reaching the system.

Figure 17-5. Optoisolator Package Examples

Interfacing an optoisolator

The optoisolator comes in a small 1C package with four or more pins. There are also packages that contain more than one optoisolator. When placing an optoisolator between two circuits, we must use two separate voltage sources, one for each side, as shown in Figure 17-6. Unlike relays, no drivers need to be placed between the microcontroller/digital output and the optoisolators.

Figure 17-6. Controlling a Lamp via Optoisolator

Next post:

Previous post: