Interfacing Devices with Microcontroller
This section briefly describes the interfacing of some common external peripheral devices with the microcontroller. The peripheral devices discussed in this section include LEDs, electromechanical relays, seven-segment displays, keypads, LCD displays and analogue-to-digital and digital-to-analogue converters. Only the basic fundamentals are discussed here. A detailed description of the software routines is beyond the scope of this book.
Interfacing LEDs with Microcontroller
The commonly used configuration to connect an LED to a microcontroller is shown in Fig. 1(a). The LED glows when the microcontroller pin is driven LOW and is OFF when the pin is set HIGH.
The LEDs are connected in this fashion as the current-sinking capability of microcontrollers is of the order of a few tens of milliamperes and the current-sourcing capability is of the order of microamperes. The resistor is used to limit the current through the LED.
The value of the resistance is chosen according to the equation
R = (VCC –VLED )/I
Where VLED is the voltage across the LED and I is the current.
Typical values of VLED and I are 1.5 V and 20 mA respectively. If the current-sourcing capability of the microcontroller is sufficient to drive the LED directly, then the LED is connected to the microcontroller as shown in Fig.1(b). The LED in this case glows when the microcontroller pin is set HIGH.
Interfacing Relay with Microcontroller
Figure 2 shows the typical connection diagram for interfacing an electromechanical relay to a microcontroller. The NPN transistor is used to provide the desired current to the relay coil as the microcontroller cannot drive the relay directly. The freewheeling diode is required as the current through the inductor cannot be instantaneously reduced to zero.
When the microcontroller pin is set HIGH, the transistor is switched on. Current flows through the relay coil and the contact is closed.
When the microcontroller pin is LOW, the transistor is switched off and the inductor current now flows through the freewheeling diode and slowly decays to zero value.
Interfacing Keyboard with Microcontroller
Keyboards are used to enter data, values, etc., into the microcontroller system. They are generally available in three configurations, namely the lead-per-key keyboard, the matrix keyboard and the coded keyboard.
Lead-per-key or linear keyboards are used when very few keys have to be sensed. Coded keypads are generally used in telephonic applications. They are high-quality durable keyboards and permit a multiple key press to be detected easily. They are used when the number of keys is 16 or less, as they are very expensive.
The most commonly used keyboard is the matrix keyboard where the keys are arranged in a matrix, with keys in the same row and column sharing the same access lines. When the number of keys exceeds 10, more often than not matrix keyboards are used.
Interfacing a matrix keyboard with the microcontroller is discussed in the following paragraphs. When the keyboards are connected to a microcontroller, following factors must be considered:
Contact bounce. Contact bounce refers to multiple ‘make’ and ‘break’ oscillations of contact during the key-pressing operation (Fig. 3).
Good-quality keyboards have bounce periods of 1–5 ms, whereas low-cost keyboards have bounce periods of tens of milliseconds. If the bounce is not taken into consideration, the microprocessor responds as if the key has been pressed and released several times when in fact it has been pressed only once. Contact debouncing through either a hardware or a software routine is done to avoid the undesirable multiple-contact effects during a key closure, so that it appears as a single ON or OFF operation.
Hardware debouncing is done using an RC circuit [Fig. 4(a)] or a Schmitt trigger circuit [Fig. 4(b)]. If debouncing is done by a software routine, a delay of 20–50 ms is given after a key press before the routine for that key is executed.
Multiple keys. If more than one key is pressed, then only routines corresponding to valid multiple-key presses should be executed. Also, the first valid key press pattern is executed.
Key hold. There are two types of keyboard actuation, namely the two-key lock-out and the N-key rollover. The two-key lock-out takes into account only one key pressed. An additional key pressed and released does not generate any codes. The system is simple to implement and most often used. The N-key rollover will ignore all keys pressed until only one remains down.
Figure 5 shows the connection of a 16-key matrix keypad with a microcontroller. Here, each column and row access line is connected to the microcontroller pin. The columns are generally at a HIGH level.
The row lines are configured as output lines and the column lines are used as scan lines. The key actuation is sensed by sending a LOW to each row one at a time through a software routine via the row 1, row 2, row 3 and row 4 lines.
The column lines are checked for each row to see whether any of the normally HIGH column lines are pulled LOW. If a column is driven LOW, then, by determining which row and column line is LOW, the key is identified and the routine corresponding to that key press is executed.
Interfacing Seven-Segment Display with Microcontroller
Seven-segment displays commonly contain LED segments arranged as a figure-of-eight pattern, with one common lead (anode or cathode) and seven individual leads for each segment. When the common lead is the anode it is referred to as the common anode (CA), and when the common lead is the cathode it is referred to as the common cathode (CC).
Figure 6 shows one of the possible configurations of interfacing a CC display with the microcontroller. The IC CD4511 is a BCD to seven-segment decoder/driver. The microcontroller feeds the BCD equivalent of the digit to be displayed to the 4511 IC.
Seven-segment displays can also be connected directly without the use of a BCD to seven-segment decoder. In this case the seven-segment code of the digit is generated by the microcontroller program itself. Figure 7 shows the direct circuit connection for CA display.
If more than one display is to be used, the displays are time multiplexed. The human eye cannot detect the blinking display if each display is relit every 10 ms or so. The 10 ms time is divided by the number of displays used to find the interval between updating each display.
In the case of CC displays the display is selected by driving the common cathode to logic LOW, and in the case of CA displays the display is selected by driving the common anode to logic HIGH.
Figure 8 shows the multiplexed circuit for two CC displays. The IC 74138 is a 3-to-8 line decoder used for selecting the display. Figure 9 shows the multiplexing in the case of CA displays for direct connection without the use of a BCD to seven-segment driver.
Interfacing LCD Displays with Microcontroller
Liquid crystal displays allow a better user interface compared with LED displays as it is much easier to display text messages in LCD displays. They also consume much less power than LED displays. However, LED displays have better intensity than LCD displays.
LCD displays are available typically in 8 × 2, 16 × 2, 20 × 2 or 20 × 4 formats. 20 × 2 means two lines of 20 characters each. These displays come with an LCD controller that drives the display.
Figure 10 shows the interface of an LCD display with a microcontroller. There are three control lines, namely EN (enable), RS (register select) and RW (read/write). The EN line is used to instruct the LCD that the microcontroller is sending the data.
- When the RS line is HIGH, the data comprise text data to be displayed on the LCD.
- When the RS is LOW, the data are treated as a command or instruction to the LCD module.
- When the RW line is LOW, the instruction on the data bus is written on the LCD.
- When the RW line is HIGH, the data are being read from the LCD.
The software routine initializes the LCD firstly by setting the width of the data bus, selecting the character, font, etc., clearing the LCD, turning on the LCD module and the cursor, setting the cursor position and so on. Then the data to be displayed are sent on the data lines, and the three control signals are made use of to ensure proper LCD operation.
Interfacing A/D Converters with Microcontroller
A/D converters are used to interface the microcontroller with the analogue world. Figure 11 shows the interface of A/D converter type number AD571 with the microcontroller. AD571 is an eight bit A/D converter. As can be seen from the figure, the data output lines and the control lines of the A/D converter are connected to the microcontroller I/O pins.
The microcontroller sends commands such as the start of conversion, selection of the input channel if the A/D converter has more than one input channel, etc. It also senses signals from the A/D converter such as the end of conversion to store the digital bits.
In the present case, the microcontroller sends a LOW on the BLANK/DRbar line to start the conversion process. It then waits for the data ready (DRbar) signal to go to LOW. After that, the digital output bits are received by the microcontroller and processed according to the software routine.
Interfacing D/A Converters with Microcontroller
When interfacing a D/A converter to the microcontroller, the digital data lines and the control lines, such as the start of conversion and chip select lines, are connected to the microcontroller I/O pins. The software routine generates the required signals to start the conversion process.
Figure 12 shows the interface of D/A converter type number DAC 809 with the microcontroller. DAC-809 is a eight-bit D/A converter. Here, the output is current, so a current-to-voltage converter is required at the output