Micro Controller Interfacing
A micro-controller can be a very versitile piece of control equipment, working to a set rountine, or set of conditions, at high speed - monitoring inputs and switching outputs. There are however a number of restrictions on the configuration of these inputs and outputs relating to either Voltage or Current. These applications look at microcontrollers in general terms rather than focusing on a specific brand or model.
NOTE: When considering these general interfaces check the data sheets to ensure that you do not exceed the maximum limits on your controller. Driving the micro controller beyond the ratings given in the data sheets can cause permanent damage. Similarly running the controller to it's maximum rating for extended periods may affect reliability.
Regulation of the controllers power supply voltage will be covered under in seperate application but requirements can generally be found on the data sheets as VDD and VSS or similar. The article Reading Data Sheets should help you decipher things if your new to electronics.
For these applications we are interested in the high and low voltage levels triggering a logic level on an input, eg. VIH or VIL, and the output voltages which may be described by VOH or VOL. We will also see limits placed upon our controller for current sink & supply as well as total power disipation. These limits will be shown for both pin and device such as ICC, PTotal, PDIS or simply listed as Absolute Maximum Ratings.
Simple Switch Input
A switch input can present one of the simplest interfaces but there are a number of considerations that are important. You can see we show an input with a resistor held to -ve by a 10KΩ resistor. This ensures that when the switch is open the input voltage can not "float" and give unreliable indications. Once the button is pressed a +ve is presented to the controller and the resistor limits the current flow between +ve and -ve supply.
Should the input be held high accidently, as an output for example, then the current from the microcontroller will be very small, consider V=IR when we use 5V as the positive voltage. The switch offers some protection against the high voltage and a pin programmed to be held low, better protection would see a second resistor on the connection between resistor/switch junction and the controller but this isn't a must have requirement.
Our push button switch, and most others, doesn't do it's job cleanly, it may take a moment to settle with the +ve voltage following a button push. This "bounce" usually goes unseen by us but can be picked up by our high speed controllers. Care must be taken when we are looking for short duration high followed by a low switching but other cases we may act on the first bounce and not see the problem. There are however a number of solutions that can be employed if this proves to be a problem.
While this type of input could be used for multiple push buttons on a keypad but it would be more usual to have some sort of multiplexing to reduce the number of inputs required. The simplest circuitry would use multiple switches connecting differing resistors as a potential divider to an analog input. We can calculate what values correspond to which button but some allowance may be required for variation in supply voltage.
You may wish to employ a multiplexing IC or special features on a specific controller, such as the PICaxe keyin function, these are beyond the scope of this application.
This input circuitry will also work with Micro Switches which are more suited to mechanical switching for bump detection.
Simple LED Output
Generally there is no problem driving an LED from the output but you will need to consider current limiting resistors to protect both the LED and microcontroller. The circuit pictured will light the LED when the output is held high, conversley the LED could be connected to the +ve supply and the LED would light on a low output.
When we first considering the limiting resistor for the LED we might consider IF at 20mA. If this is driving the controllers outputs to the limit of their supply then consideration should be given to a reduced current supply and reduced LED brightness, 15mA may be more suitable.
When calculating limiting resistor (R = V / I) for a high output on a 5v supply we may find :-
In a similar consideration for lighting the LED from a low output VOL may not be 0v but 0.6v
Transistor LED Output
One or two LEDs may not present a problem but if we are to drive numberous LEDs, in a seven segment display for example, then we may reach the maximum current supply for the controller. We need to think about the interface and how we can use a smaller current without detremental effect on our LED brightness. This application considers the use transistors to boost the current supplied as it leads into other high current outputs but we might use a display driver IC to reduce the output requirements in terms of current, pins and processing.
Here we use our output to supply a small current to the transistor on, this then carries the higher current load of our output device.
While this example still shows a relatively small load we can see how this helps us drive a much greater total load if several LEDs are to be in parallel.
The transistor isn't a straight forward switch, it's an amplifier that gives us a current gain with several additional design constraints to be considered in its use.
Pictured to the right is an NPN Transistor and an NPN Darlington Pair. C denotes the Collector connected to the load on the positice side of the circuit, B denotes the Base where we supply our switching signal and E denotes the Emitter connected to the negative side of our circuit. NPN transistors, such as the BC548 costing £0.03, may drive loads up to 100mA. While discrete transistor maybe used to construct a Darlington Pair they can be purchased as a package, such as the BCX38 costing £0.28, and driving loads upto 800mA. The MPSA13 darlington transistor costs £0.03, while it may drive loads up to 1.2A it's power pissipation is more limited. Note it's common for circuits to show darlington pairs as standard transistors despite the differences in characteristics.
The data sheets will help us with our design but it's worth a note that the same part from different manufacturers may differ on it's maximum values. We're looking at :-
Note that these figures are based on an ambient temperature of 25° C, other parameters become can become important as we push our designs but are not being considered at this time.
If we consider the limiting resistor for driving our LED we now need to consider the voltage drop both across the LED, VF, and the Transistor, VCE(sat).
We can see that our LED current, IF, is much lower than the collector current, IC, and can check the power dissipation:-
When turning our transistor on and off we need to consider the voltage on the base to give VBE and the current IB. As HFE is a ratio IC / IB we can see that we only need to supply a fraction of the current required by our load. While we might use a BC548 transistor and calculate IB as 0.2mA it will only just turn on, however doubling up to 1.0mA we still have a small load but the transisitor is driven hard on. The calcultaion for our base resistor would be based upon the High Voltage level (VOH) from our controller, the voltage drop on the transistor (VBE(on)) and the calculation V=IR once more.
If we now consider the Off voltage we can see that Transistors may prove problematic, the On voltage VBE(on) is only 0.66v and the controllers low voltage may be in the same region, out example gives VOL as 0.6V. This may mean that the transistor doesn't fully turn off, aside from our load still being driven it may be that there is a higher voltage across the transistor and problem with much higher than expected power dissapation.
We can resolve this problem in a number of ways. Firstly we could use add an additional resistor between the limiting resistor and the -ve supply rail. This gives us a potential divider, with the base driven from a point between out output voltage and -ve. The actual voltage is based upon the ratio of the two resistors, it will give us a lower voltage based on VOL but also VOH meaning that we would need to reconsider our base resistor.
However if we consider our Darlington Transistors we see that we have much higher VBE values and the potenial divider is not required, with some transistors and darlington transistors have similar prices many circuits become cheaper and similar with this solution. Note too that the voltage drop across the darlington transistor is higher, VCE, this may be significat in battery powered circuits.
Transistor Output Inductive Loads
There are a number of pros and cons when looking at relay contacts. In low voltage circuits switching does not drop the load voltage in the same way as a transistor but it can put a higher load on your battery supply. Where transistors do not have a limit to the number of times they are switched, mechanical relay contacts do. While the limit may be in the order of 1,000,000 operations this can be used very quickly if you attempt high frequency switching from your controller.
Relays cannot be driven directly from our controllers output, note that we have shown a transistor drive circuit once more but with an additional diode. Inductive loads such as relays and motors can produce high voltage, opposite to the supply voltage as they are turned off, this may distroy your transistor circuits. The diode offers protection against this voltage and should not be omitted from the circuit.
Final word on simple input and output
When switching larger loads MOSFET devices may be better suited to the task. These will be covered in another article as they have differing requirements, as will driver ICs and relay changeover circuits that would be better suited to Mini-Sumo motor drives and direction contol. There are more sensors and interfaces than can be listed here but their data sheets will give enough information for you to use them safely. Some of the more common one will be covered in an article.
Lastly, watch you component limitations, it can be easy slip up and blow it. Similarly, keep an eye on your power dissipation it's easy to burn your fingers on hot transistors.
If your looking for a little help, drop by the forum.