Microcontroller Interfacing – Advanced
Microcontrollers have become very useful in embedded design as they can easily communicate with other devices, such as sensors, switches, LCD displays, keypads, motors and even other microcontrollers. A microcontroller is basically used as the brain or intelligent processing unit to control other devices connected (interfaced) to it in embedded systems just like a PLC in industrial automation.
To interface a device to a microcontroller simply means to Connect a device to a microcontroller. This article will make it easier to anybody with very limited experience in electronics to learn how to interface to a PIC Microcontroller some advanced components Graphical LCD, Quad 7-Segment Display, SD Card, DC Motor, GSM modem, GPS module, Real Time Clock and so on.
Many interface methods have been developed over the years to solve the complex problem of balancing circuit design criteria such as cost, size, weight, power consumption, reliability, availability.
1. Multiplexing of 7-Segment Displays
A 7-segment display is the earliest type of an electronic display that uses 7 LEDs bars arranged in a way that can be used show the numbers 0 – 9. (actually 8 segments if you count the decimal point, but the generic name adopted is 7-segment display.) These devices are commonly used in digital clocks, electronic meters, counters, signalling, and other equipment for displaying numeric only data.
Figure 2: Connecting a 2-digit 7-segment display to a PIC microcontroller
A 1-digit 7-segment display can only show numbers from 0 to 9, a 2-digit display can show numbers between 0 and 99, a 3-digit between 0 and 999, a 4-digit between 0 and 9999, and so on.
Figure 2 shows a 2-digit 7-segment display connected to a PIC microcontroller and figure 3 shows 2-digit and 4-digit seven segment displays.
Figure 3: 2-digit and 4-digit Seven segment displays
When more digits are required to be displayed, we need to come up with a better technique to connect more than 1-digit 7-segment displays to a microcontroller because if we connect them like the 1-digit display we will soon run out of input/output pins.
A 1-digit 7-segment display requires 7 output pins, a 2-digit would require 14 and a 4-digit would require 28, this is definitely not an efficient way of using a microcontroller.
The common widely used technique is to multiplex the digits to save input/output pins. All the digits share the same microcontroller pins plus few more pins to connect the digits to ground or to positive power depending on whether a common cathode or anode segments are used.
With multiplexing, a 2-digit display will require only 9 pins, a 3-digit display will require 10 pins, a 4-digit display will require 11 pins, and so on.
Another advantage of multiplexing 7-segment LEDs is to reduce the power consumption considerably.
In multiplexed applications, all the digit segments are driven in parallel at the same time, but only the common pin (e.g. anode or cathode) of the required digit is enabled. By enabling or disabling the digits so fast that it gives the impression to the eye that both displays are ON at the same time as the human eye cannot differentiate it when the speed is too high. This technique is based on the principle of Persistence of Vision of our eyes. If the frames change at a rate of 25 ( or more) frames per second, human eye can’t detect that visual change.
For example, let say we want to display the number ‘67’ on a 2-digit common cathode display. The steps are given below:
1. Send data to display ‘6’ on both digits.
2. Enable the left digit by grounding its cathode pin (send a high to the base of the transistor) and disable the right digit.
3. Wait for a while. (a short delay)
4. Send data to display ‘7’ on both digits.
5. Enable the right digit by grounding its cathode pin (send a high to the base of the transistor) and disable the left digit.
6. Wait for a while (a short delay).
7. Go back to step 1. By doing this rapidly, the eye won’t notice any fluctuation.
The common pins of each digit are usually controlled using transistors switches, almost any NPN transistor such as the BC108-Type transistor could be used for this purpose. A 1KΩ resistor can be used to limit the base current to about 4mA enough to saturate the transistor.
Figure 1 shows how a 2-digit display can be connected to a microcontroller using NPN transistors to control the segment lines. Notice that setting the base of a transistor to logic HIGH will turn the transistor ON and hence will enable the common cathode pin connected to it.
>> To learn more read the: Multiplexing of 7-Segment Displays with PIC Microcontroller using XC8 Compiler and Multiplexing of 7-Segment Displays with PIC Microcontroller using mikroC Compiler articles.
2. Interfacing a Graphical LCD Display
Graphics LCD displays (GLCDs) are commonly used in applications, where we may want to display not only basic characters but can graphical data as well such as a bar-chart or x-y line graph and some shapes like rectangles, circles and so on. GLCDs are also used in many consumer applications, such as mobile phones, GPS systems, but also in industrial automation and control, where various plant characteristics can easily be monitored or changed especially if a touch-screen facility is used. There are many types of Graphical LCD screens and controllers, for small applications, the 128 X 64 pixel monochrome GLCD with the KS0108 controller is one of the most
commonly used displays. For larger display requirements the 240 X 128 pixel monochrome GLCD screen with the T6963 (or RA6963) controller could be selected. For colour Graphical Display applications, The TFT displays seem to be the best choice currently. In this section we shall be looking at how the standard 128 X 64 GLCD can be interfaced with a PIC microcontroller.
The display is connected to the PIC Microcontroller through a 20-pin as shown on figure 4 below.
Figure 4: The 128 X 64 pixel monochrome GLCD with the KS0108 controller
3. Interfacing GSM/GPRS Modem with PIC Microcontroller
A GSM modem is a wireless modem that works with a GSM wireless network. GSM stands for Global System for Mobile communications, this architecture is used for mobile communication in most of the countries in the world.
A wireless modem acts basically like the traditional dial-up modem, the main difference is that a dial-up modem sends and receives data through a fixed telephone line while a wireless modem sends and receives data through radio waves. Besides the dial-up connection, GSM modems can also be used for sending and receiving SMS and some support the GPRS technology for data transmission.
It is very easy to interface a GSM Modem to a PIC Microcontroller as most GSM modems have a serial interface. The USART serial input pin RX and TX of the microcontroller are connected to the TXD and RXD pins of the GSM Modem. Some GSM modems have PCMCIA Type II or USB interfaces. Figure 5 below shows a block diagram of a GSM module connected to USART module of a PIC Microcontroller.
Figure 5: GSM module connected to a PIC Microcontroller
Depending from the type of serial port on the Microcontroller hardware, a level translator circuit may be needed to make the system work. If the Microcontroller USART voltage level is 5V as in most of the cases, most the GSM/GPRS modems USART voltage level is about 2.8V – 3V, you need a voltage level translator circuit. A simple diodes/resistors network could do the job as shown on figure 6 below.
Three diode in series are used to drop down voltage of TX pin of microcontroller to to 2.9 volt (each diode drops 0.7V) which is in acceptable range for RXD pin of gsm module. Similarly a diode, a resistor and 5 volt source is used to increase voltage of TXD pin of GSMmodule to 5 volt which is logic high for RX pin of pic microcontroller.
Figure 6: A simple diodes/resistors voltage level translator circuit
There are GSM board on the market that one can use to quickly interface to a PIC. The SmartGM862 Board from Mikroelekronika is one example of many boards. The SmartGM862 is a full-featured development tool for Telit GM862-QUAD GSM/GPRS module or the GM862-GPS version. It features GM862 module connector, voltage regulator, antenna holders, speaker and microphone screw terminals and more. DIP switch is provided for configuring UART communication lines with the target microcontroller. It can be connected to development boards via IDC10 connector.
Figure 7: Connecting the SmartGM862 Board to EasyPIC7 V7 Development Board
>> To learn more read the: Interfacing GSM/GPRS Modem with PIC Microcontroller using XC8 Compiler and Interfacing GSM/GPRS Modem with PIC Microcontroller using mikroC Compiler articles.
4. Interfacing ENC28J60 Ethernet Controller with PIC MicroController
Ethernet is the leading wired standard for networking as it enables to connect a very large number of computers, microcontrollers and other computer-based equipment to one another.
With just a network switch, many different devices can easily communicate with one another with Ethernet, allowing different devices and equipment to be accessed remotely and this also provides a cost-effective and reliable means of remote control and monitoring. Most of computers nowadays have an Ethernet port implemented on them so it is with many electronic devices. Many microcontrollers have built-in Ethernet peripheral, like the PIC18F97J60, this PIC18 Microcontroller has an integrated 10Mbps Ethernet communications peripheral but many other microcontrollers don’t have a built-in Ethernet peripheral.
When a microcontroller which does not have an integrated Ethernet peripheral is used, Microchip offer a serial Ethernet chip that can easily be used by any microcontroller with an SPI interface to provide Ethernet capability to the application. The ENC28J60 is a popular 28-pin serial Ethernet chip, 10BASE-T stand alone Ethernet Controller with SPI interface, on board MAC & PHY, 8 Kbytes of Buffer RAM and an SPI serial interface. With a small foot print package size the ENC28J60 minimizes complexity, board space and cost.
The interface between the microcontroller and the Ethernet chip is based on the SPI bus protocol, The SI, SO, and SCK pins of the Ethernet chip are connected to SPI pins (SDO, SDI and SCLK) of the microcontroller. The Ethernet controller chip operates at 3.3V, its output SO pin cannot drive the microcontroller input pin without a voltage translator if the microcontroller is operated at 5V. Figure 8 below shows how the ENC28J60 Ethernet controller can be interfaced to a PIC Microcontroller.
Figure 8: ENC28J60 Ethernet Controller Connections
To make the design of Ethernet applications easy, there are ready made boards that include the EC28J60 controller, voltage translation chip and an RJ45 connector. Figure 4 belows shows the the mikroElektronika Serial Ethernet Board. This is a small board that plugs in directly to PORTC of the EasyPI CV7 development board via a 10-way IDC plug simplifying the development of embedded Ethernet projects. This board is equipped with an EC28J60 Ethernet controller chip, a 74HCT245 voltage translation chip, three LEDs, a 5 to 3.3 voltage regulator and an RJ45 connector with an integrated transformer.
Figure 9: Connecting the Serial Ethernet Board to EasyPIC7 V7 development board
>> To learn more read the: Interfacing ENC28J60 Ethernet Controller with PIC MicroController using mikroC Compiler article.
5. Interfacing DC Motor
A DC Motor cannot be driven directly from a Microcontroller’s pin. Normally DC Motors require high current and high voltage than a Microcontroller can handle as Microcontrollers usually operates at +5 or +3.3V supply and it I/O pin can provide only up to 25mA current which on most cases is not enough for a motor. Typical small DC Motors require 12V supply and about 300mA current which way beyond what a Microcontroller can handle, however there are a couple of interfacing techniques that can be used.
The solution is to use an H-bridge circuit constructed from four MOSFET transistors, as shown on figure 10 below.
Figure 10: H-Bridge DC Motor Driving circuit
To Switch OFF/STOP the motor, a logic ‘0’ should be applied to RB0, RB1, RB2 and RB3. To Switch ON the Motor Clockwise, a logic ‘1’ should be applied to RB0 and RB2 while leaving RB1 and RB3 on logic ‘0’. To Reverse (Anticlockwise) the Motor, RB1 and RB3 should be set high (1) while RB0 and RB2 set low (0). Because a motor is an inductive load, a back emf could destroy the transistors when the motor switches OFF, the four Diodes are used to suppress the back emf.
Figure 11 shows a Motor Control Circuit using the L293D. We can drive two DC Motors with one L293D, in this example we are using only the first pair of drivers to drive one DC Motor. First pair of drivers are enabled by connecting EN1 to Logic HIGH. IN1 and IN2 are connected to RB0 and RB1 of PIC Microcontroller respectively which are used to provide control signal to the DC Motor. DC Motor is connected to OUT1 and OUT2 of the L293D.
Figure 11: L293D Motor Driving Chip Circuit
>> To learn more read the: Interfacing DC Motor with PIC Microcontroller – XC8 and Interfacing DC Motor with PIC Microcontroller – mikroC articles.
6. Interfacing SD Card
A memory card (also called a flash memory card) is a solid-state electronic data storage device used for storing digital information. They are commonly used in many electronic devices, including digital cameras, mobile phones, laptop computers, MP3 players and also in many applications where a large amount of data has to be stored either once or continuously like in data loggers.
Memory cards are small, rewritable and are able to retain data without power.
The card has nine pins, as shown in the figure 12 below, and a write-protect switch to enable/disable writing onto the card.
Figure 12: SD Card pins
A standard SD card can be operated in two modes: the SD Bus mode and the SPI Bus mode.
In SD Bus mode, all the pins of the card are used, data is transferred using four pins (D0–D3), a clock (CLK) pin, and a command line (CMD).
In SPI Bus mode using a chip select (CS) and a CLK line. The following pins are used in SPI Bus mode:
–> Chip select: Pin 1
–> Data in: Pin 2
–> Clock: Pin 5
–> Data out: Pin 7
–> Positive: Pin 4
–> Ground: Pin 3 and 6
The Card operates with 3.3V supply voltage and these are the logic levels:
Maximum logic 0 output voltage, VOL = 0.4125 V
Minimum required logic 1 input voltage, VIH = 2.0625 V
Maximum logic 1 input voltage = 3.6 V
Maximum required logic 0 input voltage, VIL = 0.825 V
When connected to a PIC microcontroller, the output voltage (2.475 V) of the SD card to the PIC is enough to drive the input circuit of the microcontroller, but the typical logic 1 output voltage of a PIC microcontroller pin is 4.3 V, and this is too to apply to the card, where the maximum voltage should not exceed 3.6 V. As a result of this, it is required to use resistors at the inputs of the SD card to lower the input voltage.
Figure 13 below shows a typical SD card interface to a PIC microcontroller in SPI mode. In this figure, 2.2K and 3.3K resistors are used as a potential divider circuit to lower the SD card input voltage to approximately 2.48 V, as shown below.
SD card input voltage = 4.3 V × 3.3 K / (2.2 K + 3.3 K) = 2.48 V.
Figure 13: SD card connected in SPI mode to Port C of PIC Microcontroller.
SD cards can consume up to 100–200 mA while reading or writing onto the card. This is usually a high current, and an appropriate voltage regulator capable of supplying the required current must be used in the design.
The card consumes approximately 150 μA in sleep (the card goes automatically in sleep mode if it doesn’t receive any command in 5ms).
Watch the video Tutorial:
>> To learn more read the articles: Interfacing an SD Card (FAT32 System) With PIC Microcontroller using mikroC compiler and Interfacing an SD Card With PIC Microcontroller using mikroC compiler and Interfacing an SD Card With PIC Microcontroller using XC8 Compiler.