Interfacing microcontrollers to external devices

There can be any external device that may be connected(interfaced) to microcontroller. They may be for control input like buttons, keypads, touch or for information output eg. display, sound, motor, relay, modem. Also output devices may be further chips like transmitters, frequency generators, memory. Interfacing with chips is not that hard because most of all (especially digital ones) are TTL level compliant that allows to connect chips one to another directly. Other wise there are TTL level converter chips if needed – like RS232 to TTL converter MAX232 chip.

Lets take simpel device LED. Probably many of you are thinking that there is nothing more easier that connecting LED to microcontroller. Yes its true. Considering AVR microcontrollers Led can be connected directly to port pin via limiting resistor either as source either as drain because chip is capable to provide enough current to light diode without damaging port. But what if you need to connect like 10 LEDs when each require lets say 20mA. Total current would be 200mA. No tall microcontrollers can provide such power directly without damaging port. Simplest way to avoid this problem is to use LED via amplifying transistor.

transistor_key.JPG

Transistor acts as current amplifier which can provide LED with needed current without any damage to microcontroller. Such connection may be used in any digital circuit as transistor accept any control signal (TTL, CMOS) and amplifies it to level needed for LED or other device. R3 resistor is current limiting for LED and transistor itself of course. It is easy to calculate R3 value. Lets say diode requires ID=20mA transistor can handle 100mA, VDD=5V, then R3=VDD/ID=5/0.02=250Ω. It is good practice(especially for indication purposes) suggest to take a little bigger resistor value to ensure reliable work. R1 and R2 resistors act as voltage divider. But for 5V system you may exclude R2 as 5V is OK to connect to base via like 1kΩ resistor. For more compact solution there can be Darlington array driver (ULN2002) used instead of distinct transistors. Each Darlington transistor may source up to 100mA of current, that makes it ideal for interfacing LEDs, motors, relays compactly.

Lets say you want to interface more powerful device like light bulb, or other device powered from 110 or 220V. For this you need to modify previous circuit by adding relay instead of diode. This time you don’t have an option of connecting relay directly to MCU pin because of several reasons like relay is inductive load, that can generate voltage spikes during switching that may be strong enough to damage microcontroller port. Relays also require more current than port can handle.

MCU_relay_interface.JPG

 

It is important to add VD1 backward diode which protects transistor high voltage ‘spikes’ produced when the relay coil is switching.

Input devices can be connected also in many ways. I bet probably most of you know how to connect single button to port pin. Usually button is connected to ground with pull up resistor.

MCU_single_button.JPG

 

Some microcontroller types like AVR have pull-up resistors buil inside the chip, so you only need to connect button to ground and switch pull-up resistor in software. Reading of button state is simple. If button is pressed, port reads “0”, if not port pin value is “1”.

Lets take simple example of 4×4 button keypad input device. Keypad usually is organized as crossed rows and columns if button matrix. Such keypad may be read with half of pins configured as input and other half as output. Most microcontrollers allow to configure separate pins of single port as inputs and outputs.

keypad_matrix.JPG

 

Matrix keypad is a bit more complex. Keypad reading is done by scanning columns. So loop sends “0” to each column(or row-it doesn’t matter) and then microcontroller reads all rows by checking if any of pins have “0” level. If none of buttons were pressed, all input pins will be at “1” level.

keypad_scanning.JPG

 

If one of buttons were pressed, then on pin reads “0”. Because we know which column were set to “0” we can define which button was pressed by crossing row and column. This way it is possible to interface more buttons that there are free microcontroller pins. With one 8 bit port you can interface 16 buttons.

2 Comments:

  1. Pingback: Electronics-Lab.com Blog » Blog Archive » Interfacing microcontrollers to external devices

  2. There is actually an error here….
    R3=VDD/ID=5/0.02=250
    it should really be R3 = (VDD – Transistor junction voltage drop) / ID

Leave a Reply

Your email address will not be published. Required fields are marked *