Driving Buttons, Leds and Relays

PIC users are always confronted with a number of very basic issues like those described in this article. The latter is meant for beginning PIC users, but of course can be of some interest also for others.

1. Driving a Led

This is easiest to do, the Led is simply connected to an output pin of the PIC via a series resistor. The Led has 2 leads: a long one, called "anode" ("a" in the Circuit Diagram) and a short one, called "kathode" ("k" in the Circuit Diagram). The anode is always facing the "+" of its current source, the kathode is facing the "-".
However, there are 2 different configurations possible: The choice between the two is rather arbitrary except in the case the output pin is a so called "open collector" (see the datasheet of the PIC you are using). In the latter case only the "active low" configuration can be used (unless the circuit diagram for active high is changed: the led connected directly to the PIC and the resistor between PIC and "+").
The value of the series resistor (R2 and R7 in the Circuit Diagram) determines how much light the Led will emit. A lower value gives a higher light output (and asks for more current). Do not make the value lower than e.g. 150 ohms (150R or 150E), otherwise the PIC will get into problems. 470R-1K is most common.

2. Reading a Button

Here also a minimum of electronic components is needed: the button or switch itself and a resistor. But, as with leds, 2 configurations are possible: The choice between the configurations is arbitrary except for one thing: on some PIC ports (usually PortB) a so called "Weak pull up" resistor can be enabled, see the datasheet of the PIC you are using). This weak pull up can be used in stead of the resistor in the active low configuration, so no external resistor needed when doing so.

Debouncing. One important issue: buttons and switches have the tendency to "bounce" when operated. "Bouncing" means that the contacts do not close or open at once: they close and open repeatedly before reaching their final state, resulting in multiple actions in software if nothing is done about it. So, "debouncing" is in order here. This is usually done in the software reading the PIC pin (can however also be done with extra hardware). If you do not want to write your own "debouncing", then use the MikroElektroniks's "Button" library routine. It has a parameter that makes it usable for both active low and active high configurations.
See here for a extra information about using buttons together with the "button" function.

3. Driving a Relay

A relay consits of a "coil" and some "contacts". Putting a certain voltage on its "coil" closes the contacts, removing the voltage opens the contacts again. In some cases relays have contacts that are normally closed. In this case, putting a voltage on the coil opens those contacts. Also a combination of normally open and normally closed contacts exists. In the Circuit Diagram the relay itself is RE1, its coil has the connections "1" and "2" and its (normally open) contacts have connections "P" and "S". As you can see, the working voltage of the relay coil is 12V.

Here more electronic parts are needed than were necessary for a led because The extra parts needed are: The transistor has 3 connections (leads) (see T1 in the Circuit Diagram): the "emitter", the "base" and the "collector". The transistor shown in the Circuit Diagram is the BC547c ,which will fullfill its purpose with most types of relays.
Its connections (seen on its bottom/leads) are shown in the image on the left.

The circuit diagram shows an "active high" configuration (relay is "on" when the PIC output pin is high). The active low configuration is also possible but somewhat more complicated (so, not included in this article).
If the PIC pin is a so called "open collector" output (= "open drain") (and only then, see again the datasheet of the PIC you are using), then the circuit diagram should be changed as follows: The resistor between PIC and the base of the transistor (R5) should be placed between the PIC and the +5V (R4) and the base of the transistor (T2) should be connected directly to the PIC.

Important: Do not forget the diode (D1 in the Circuit Diagram) across the relay's coil. If you do, it will kill the transistor. Be carefull: the diode is not polarized like a led: its kathode is facing the "+", its anode is facing the "-"!.

Also important: if the relay has to switch mains supply loads (as it is in the Circuit Diagram), then an EMI (Elecktro Magnetic Interference) suppression network (or "snubber" network) has to be present across the relay contacts. In the Circuit Diagram this network is C8 and R9. The capacitor must be of a type for mains voltage usage (250V~) and the resistor must be a "safety" type.
Failing to add an EMI suppression network will cause the PIC (and perhaps its environment also) to behave unpredictable and shorten the life time of the relay contacts.



Circuit Diagram
-------------------------------------------