Basic Microcontroller Interfacing

                     Figure 1: Basic Microcontroller Interfacing

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 commonly used devices like an LED, a switch, a transistor, a relay, a display, a keypad, a buzzer and so on to a PIC microcontroller.  

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. Interfacing a Light Emitting Diode (LED)

A Light Emitting Diode (LED) is a semiconductor light source, when forward biased, it emits light. 
LEDs are used mainly to indicate the status of electronic circuits, for example to indicate that power is on or off but nowadays they are used in many applications including lighting and beam detection.

An LED is similar to a diode, it has two legs: the longer leg is the anode (+) and the shorter leg the cathode (-). The cathode is also identified by a flat side on the body. 

The intensity of the light emitted by an LED depends on the amount of forward current passed through the device but we must take attention not to exceed the maximum allowable forward current or draw more current than the PIC output pin can handle. A PIC can source or sink 25mA of current per Input/Output pin.
When designing an LED circuit, we have to know the typical voltage drop, table 1 below lists some few characteristics of some LEDs.

Colour Typical Voltage Drop Typical Forward Current
Red 2.0V 20mA
Orange 2.0V 20mA
Yellow 2.1V 20mA
Green 2.2V 20mA
Blue 3.0V 20mA
Infrared 1.2V 50mA

Table 1: Typical LEDs Characteristics

Most LEDs have a typical forward voltage drop of about 2V, with a typical operating current of around 10 mA (it is always good not to operate a device at its high end current), it’s important to read the datasheet to get the correct values. 
An LED can be connected to a microcontroller in two different ways: in current sourcing mode (figure 3) or current sinking mode (figure 2).


  Figure 2: LED connected in current sinking mode     Figure 3: LED connected in current sourcing mode

In current sinking mode, a logic LOW (output 0) has to be applied to the connected pin for the LED to switch on while in current sourcing mode a logic HIGH (output 1) has to be applied to the pin for the LED to switch on.

The port output voltage can be assumed to be +5V when the port is at logic HIGH. Assuming that the LED is to be operated with 10mA forward current, and that it has a forward voltage drop of 2V, we can easily calculate the value of the current limiting resistor as:
                                          As the PIC can supply up to 25mA, the current can be increased for more brightness. We are going to choose a resistance of 220Ω (forward current of about 13.6mA) in our example but a resistance of 330 could be also do the job very well.

          >> To learn more on how to interface an LED, go to the Connecting Light Emitting Diodes (LED) to a PIC Micrcontroller using XC8 Compiler article.

Watch the Video Tutorial

 2. Interfacing a Switch

Figure 4: various forms of switches

Switches are digital inputs and are widely used in electronic projects as most systems need to respond to user commands or sensors. Reading a switch is very useful because a switch is widely used and can also represent a wide range of digital devices in real world like push buttons, limit sensors, level switches, proximity switches, keypads (a combination of switches) etc. 
Connecting a switch to a microcontroller is straight forward, all we need is a pull-up or pull-down resistor.


Figure 5: A switch with a Pull-up resistor        Figure 6: A switch with a Pull-down resistor

The pull-up or pull-down resistor is very important, if there is no resistor it will be difficult to determine the state of the pin, this is called floating.
let’s say the microcontroller pin is configured as an input. If there is nothing connected to the pin and the microcontroller program reads the state of the pin, will it be high (pulled to VCC) or low (pulled to ground)? It is difficult to tell. But with a resistor connected to VCC (pull-up) as in figure 5, or connected to ground (pull-down) as in figure 6, will ensure that the pin is in either a high or low state.  
Pull-up resistors are the more common so we will focus on them. 
In figure 3, if the switch is open, the input of the PIC will be high (+5V) and when the switch is closed, the input of the PIC will be low. If there was no resistor, then it could have been a short circuit. 
Internal pull-up resistors can also be enabled in software if external resistors are not going to be used, refer to the datasheet to find out more.
Now we know the reasons why we should use a pull-up or pull-down resistor, the next question is what should be the value of this resistor?
The larger the resistance of this pull-up resistor, the slower the pin is to respond to voltage changes, this is because the system that feeds the input pin is essentially a capacitor coupled with the pull-up resistor, thus this forms an RC filter, and RC filters take some time to charge and discharge. So if you have a very fast changing signal (like USB), a high value pull-up resistor can limit the speed at which the pin can reliably change state. 
And on the other hand if you select a lower resistance, when the switch is closed, more current will be routed to ground which is not a good idea especially if the circuit is battery powered. Generally a value of 10KΩ should do the job fine.

          >> To learn more on how to read a switch, go to the Reading Switches with PIC Microcontroller article

Watch the Video Tutorial

 3. Interfacing a Light Dependant Resistor (LDR)

A Light Dependant Resistor (LDR) is a resistor that changes its value according to the intensity of light falling on it. Generally, an LDR has a high resistance in the dark, and a low resistance in the light.


Figure 7: A Light Dependant Resistor   Figure 8: Interfacing a light dependant resistor

In figure 8, the LDR is connected as part of a voltage divider circuit. The output is connected to an analogue input of the microcontroller. The change in resistance of the LDR is thus translated into changes in output voltage that can be read by the analogue input of the PIC.

The Out Voltage (Vout) will be: (R1 / (LDR1 + R1)) X 5V

We should also note that the LDR output response is not linear, and so the readings will not change linearly as the light intensity changes, in general there is a larger resistance change at brighter light levels. This should be compensated for in the software by using a smaller range at darker light levels. Experiment to find the most appropriate settings for the circuit.

This same circuit can be used to interface a Thermistor to a PIC microcontroller, the LDR in figure 8 can be replaced by a thermistor.

4. Interfacing a 7-Segment display

The 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. 
It is not different from an LED in terms of interfacing, by turning the appropriate segments ON and OFF we can display easily the numbers 0 to 9 and optionally the decimal point (DP). 

  Figure 9: 7-Segment displaying 8 with decimal point         Figure 10: 7-Segment displaying 3

The segments of the displays are normally referred to by letters ‘a’ to ‘g’.
Figures 9 and 10 show how a 7-segment display can display digits. 
In figure 9, all the segments (LEDs) are switched on to display the digit “8” with the decimal point. On the other hand, in figure 10, segments a, b, c, d and g are switched on to display the digit “3”.  any combination can be used to display any desired digit.

Interfacing a 7-Segment display is basically like interfacing 7 light emitting diodes (LED) as each segment is an LED (we could count 8 if the decimal point segment is taken into consideration). All we need is to include an appropriate series resistor as learnt on how to interface an LED above in section 1.                Figure 11: 7-Segment display connected to PIC microcontroller with 7 x 220Ω series resistors

          >> To learn more on how to interface an LED, go to the Interfacing 7-Segment Display With PIC Microcontroller article.

Watch the Video Tutorial

5. Interfacing LCD Display

LCDs are alphanumeric (or graphical) displays. They are frequently used in microcontroller-based applications. There are many devices in the market which come in different shapes and sizes. In terms of interfacing technique, we can group them in two categories:  Parallel LCDs and serial LCDs.

Parallel LCDs like the popular Hitachi HD44780 series are connected to the microcontroller circuitry such that data is transferred to the LCD using more than one line and usually four data lines (4-bit mode) or eight data lines (8-bit mode) are used. 
Serial LCD is connected to a microcontroller using one data line only and data is transferred using the RS232 asynchronous data communications protocol. Serial LCDs are generally much easier to use, but they are more costly than the parallel ones. In this article we will discuss only the parallel LCDs, as they are cheaper and are used more commonly in microcontroller-based projects. 

This LCD display device generally has 14 pins which are marked on the PCB with some models have 16 pins if the the device has a back-light built in.

LCD connection to Port B of PIC Microcontroller

Figure 12: LCD connection to Port B of PIC Microcontroller

>> To learn more read the: Interfacing LCD Display With PIC Microcontroller using XC8 Compiler, Interfacing LCD Display With PIC Microcontroller using mikroC Compiler and Interfacing LCD Display With PIC Microcontroller using Flowcode articles.

Watch the Video Tutorial

 6. Interfacing a Piezo Sounder

A piezo sounder or buzzer is an electronic device commonly used to produce sound in an electronic device. it can be used to generate beep sounds for settings, alarms, warnings and so on. A piezo sounder is different from a normal buzzer as it can generate different sounds whereas, a buzzer produces a noise when power is applied to it. A piezo will require a pulsed signal as well to be able to generate sound. This can be done in software.

Figure 13: Interfacing a Piezo Sounder

Figure 13 shows how a Piezo Sounder can be interfaced to a PIC microcontroller. This circuit can also be used to interface a speaker to a PIC microcontroller, the piezo sounder on figure 13 can be replaced by a speaker. When a speaker is used, a small series capacitor (about 10uF) must be inserted between the PIC output pin and the speaker to block out any DC power.

7. Interfacing a Transistor

Many output devices require more current than the 20mA provided by the PIC, to achieve this a transistor employed as a switch can be used in this case. If more current is required, a darlington pair can be used. Some transistors contain the two darlington pair in a single package, this is a nice feature instead of using two separate transistors, one of them is the BCX38C shown on figure 14 below. This transistor can power a device that can consume up to 800mA.  


Figure 14: Interfacing a transistor to a PIC                  Figure 15: Interfacing a transistor darlington pair to a PIC

The back emf suppression diode D1 is used for protection against back emf if an inductive load is used at the output like a relay or a motor.

8. Interfacing a Darlington Driver IC 

When more than one devices are to be controlled, more transistors can be used. The best solution is to use a darlington driver IC which can contain a number of darlington transistors inside. One of those darlington ICs is theULN2803A which is an 18 pins device containing 8 darlington pairs with these features:

  • 500-mA-Rated Collector Current(Single Output)
  • High-Voltage Outputs: 50 V
  • Output Clamp Diodes for switching inductive loads  
  • Inputs Compatible With Various Types of Logic 
  • Relay-Driver Applications   
  • Compatible with ULN2800A Series


                             Figure 16: Schematic of each darlington pair

If even more current is required, two or more pairs can be connected in parallel as show on figure 17 below. Darlington pairs 1 and 2 are connected in parallel to control a DC motor. The ground pin of the ULN2803A is not shown here on the schematic, but it should be connected to the circuit ground.    

         Figure 17: Interfacing the ULN2803 Darlington Driver IC to PIC microcontroller

 9. Interfacing a Relay

A relay can be used to switch higher power devices such as motors, light bulbs and solenoids. If possible, the relay can be powered by a separate power supply to enable connection of relays requiring a different voltage like for example a 12V relay. The microcontroller will switch on the transistor which in turn will switch on the relay, anything connected to the contacts of the relay can thus be switched on or off. A normal Bipolar transistor used in switch mode like the BC108 or a Darlington pair like the BCX38 can do the job. Note the use of a back emf suppression diode across the relay contacts. This is to prevent damage to the transistor when the relay switches off. Diode type 1N4001 is suitable for this diode.

                             Figure 18: Interfacing a relay to a PIC microcontroller

Watch the Video Tutorial

10. Interfacing a Buzzer

Devices like a small light bulb, a buzzer and so on can be powered by the transistor that we learnt in section 7. The circuit on figure 18 below shows a buzzer interfaced to a PIC microcontroller. This buzzer can be replaced by a small light bulb if needed.                  

Figure 19: Interfacing a Buzzer to a PIC microcontroller