Go Back Programming  Go to Contents

Mini PIC pin circuits.

  Here's a few basic circuits which will help the beginner to connect their PICs to the real world.


  
Lighting an LED

  Just about the simplest form of output from a PIC.



  The resistor serves to limit the current flow both through the LED and from the PIC pin which is driving it. PICs can source and sink a few milliamps but try to get more out of them and you might end up producing a puff of magic blue smoke instead. LEDs without current limitation will almost certainly die (although an IR one I connected directly yesterday glowed red hot but somehow managed to still function after the experience).

     

        SETB PORTB.0            ;turn an LED on
        CLRB PORTB.0            ;turn an LED off



  For this example only here's the complete circuit required to get the LED to light.




  The value for the current limiting resistor is found from Ohm's law.




  In this case, the maximum current (I) we want through our LED (and PIC pin) is 10mA. This equates to 0.01 Amps. (10 / 1000). Since our supply voltage is 4.5 Volts, Ohm's law tells us that we require a 450 Ohm resistor in series with the LED. The nearest standard resistor to that value is 470.





  
Simple oscillator

  It's worth mentioning here that without an oscilator circuit your PIC probably won't function at all!



  While the 12508/12509 has an internal 4Mz oscillator, other PICs require external circuitry before they'll spring to life. In situations where timing is non-crucial the simple Resistor-Capacitor oscillator above will suffice. In fact these two components are probably the simplest way of getting a 16F84 started. Using this circuit you can run all of the others on this page, even the ones which seem to rely on timing. For, while a R-C oscillator simply can't compete with a crystal, in these circuits we are using the PIC as a simple means of switching devices on and determining simple values from the outside world, accuracy is just not required as yet. Even fine motor control can be achived with appropriate sensors.





  
Simple switch input

  With the pull-up resisters internal to the PIC enabled, the following is all you need to get read the value of a switch.



  Note that switches are notoriously noisy, so that when changed their value will bounce a little between 1 and 0. This need not be a problem depending on how the software is written. At this stage it's probably best to deal with the problem when and if you come to it.

      

        SETB RP0                ;set register page 1
        MOV TRISB,#00000001B    ;define portb pins for i/o 1s=input, 0s=output
        CLR RBPU                ;turn on internal pullups (bit7 in option reg)
        CLRB RP0                ;set register page 0
        MOV PORTB,#0            ;clear PORTB

        ;test this switch
        JNB PORTB.0,HANDLEINPUT ;since switch pulls the pin down to 0V,
                                ;test for bit *not* set.







  
Switch with pull up resistor

  This circuit functions in exactly the same manner as the above switch circuit. The pull up resistor is merely external to the PIC.



  On the 16F84, only PORTB has internal pull up resistors. The above circuit would have to be used when switches are to be used as inputs on PORTA.

      

        SETB RP0                ;set register page 1
        MOV TRISA,#00000001B    ;define porta pins for i/o 1s=input, 0s=output
        CLRB RP0                ;set register page 0
        MOV PORTA,#0            ;clear PORTA

        ;test this switch
        JNB PORTA.0,HANDLEINPUT ;since switch pulls the pin down to 0V,
                                ;test for bit *not* set.







  
Transistor switch/amp

  Transistors are amplifiers, put a small current on their base and you can get a larger current across their collector/emitter pins. This fact also means that you can use them to drive components which your PIC alone could not power, ones which exceed the 20mA maximum output on a pin.



  

      

        SETB PORTB.0            ;turn external device on
        CLRB PORTB.0            ;turn external device off


  In the circuit above, you would need to output a square wave to make the speaker sound. This is achieved simply by switching the PIC's pin on and off a certain number of times a second.





  
MOSFET switch

  FETs are cool. Here's a single component which can bring your PIC from the small scale sublties of the logic level circuit straight into the real world. With simple connections you can drive motors, lamps, in short: big stuff!



  FETs are a type of transistor which operate on voltage levels rather than current. In short this means that you can switch a high current device using the meagre voltage from a PIC output pin *without* draining dangerous amounts of current from the PIC itself. You do not even need a current limiting resistor between the PIC pin and the FET.

  However. It's not entirely that simple. There are some important things to know about the usage of FETs, ignore the following advice and the devices can literally explode.

  Pulling the gate value down to 0v with a high value resistor (as in the above diagram) makes FETs behave a great deal more agreeably.

  An overheating FET is a warning sign. FETs can handle a great deal of current across their drain/source and so benefit from the addition of a heat sink.

  While a small gate voltage will switch the FET on, often anything less than 10V will not give maximum voltage across the device being driven. This is a pain, considering the PICs low maximum running voltage (6V for a 16F84). Consult the datasheet for the FET you want to use for more information.

  Driving motors is a very tempting application of FETs. However, motors by their nature are also very similar to dynamos. This means that as you drive a motor, it will induce a reverse EMF, this, combined with the spikes associated with starting and stopping will put a great deal of strain on the driver circuit (consisting in this case of just a simple FET!). While motor driver circuits can of course be created around FETs, you have to be careful if you want to drive large, high current motors.

  Having said all this, FETs are still cool, there's now a large number of useful external devices you can intelligently drive from your microcontroller, (buzzers, lamps, you name it). With motors less than 1 Amp you're probably going to be fine whatever, at currents greater than this, well, use your common sense: if you're trying to drive a huge motor with a single FET - take the time to wire in a few resistors, diodes and capacitors to help the component to do its job!

      

        SETB PORTB.0            ;turn external device on
        CLRB PORTB.0            ;turn external device off







  
LDR/Photo diode/transistor input

  A common input requirement for PICs is light level. This might be used for simple light (or the inverse, no-light) activated switches, or it might be for more complex beam break detection for counting, measuring rotational speed or security applications.

  The circuit is simple enough and the light sensitive components can pretty much be used interchangeably. The base connector on a photo transistor is usually unavailable for connection anyway.



  While the circuit looks simple, its action is quite clever. A light sensitive component can be looked upon as a resistor who's value varies with the light striking its surface. The aim of the circuit is to allow the PIC to measure the resistance value of the light sensitive component and therefore know the light level.

  In these examples we are looking at simple input and output examples which involve a single PIC pin. While some PICs have analogue to digital converters and these are considerably more acurate than the method here, this example is the simplest method of achieving a measurement of the unknown external resistance.

  The capacitor and light dependent resistor form a simple oscilator, (as described above). This effect can be utilised by the PIC to determine the value of the resistance. Instead of measuring it directly, we can indirectly measure it, by discharging the capacitor and timing how fast it is recharged. Since this recharge time depends on the value of the resistor, in software we can measure the resistance by measuring time.

  Once the circuit is set up, the software procedure is as follows. Set the pin to +5V output and wait a short time for the capacitor to discharge (both plates at +5V). Then, change the pin to act as an input. As soon as this is done the capacitor will be free to charge up again through the resistor (Ie it brings the capacitor plate back down to 0 Volts). As soon as the pin is changed to an input, either count the time it takes to change (therefore getting a pretty good idea of the resistance value), or, if using the LDR as a method of switching, wait for a certain amount of time and if it hasn't charged by then assume that the resistance is high and therefore the light levels are low.

  Note. If internal pull-up resistors have not enabled, or are unavailable on the chosen pin, you must insert a 10k Ohm value across the capacitor.

      

	;set up
        SETB RP0                ;set register page 1
        MOV TRISA,#00000000B    ;set porta i/o 0=output
        CLRB RP0                ;set register page 0

	;test the light value, and switch if the light levels are low (beam broken)
        SETB PORTA.0            ;discharge beam cap by setting pin high (+5V)
        CALL MINIPAUSE          ;give the capacitor a chance to *discharge*,
                                ;before we measure its *recharge* time!
        SETB RP0                ;change pin to input, therefore allowing cap to recharge
        MOV TRISA,#00000001B    ;be careful not to mess up the i/o settings
        CLR RP0                 ;on your other pins!

PAUSE                           ;wait for a bit (adjust wait time to your application)
        CLR C1                  ;set C1 as zero
PAUSELOOP
        DJNZ C1,PAUSELOOP       ;decrease c1 (-1) then loop until zero

        ;if beam cap not recharged by now then no light therefore beam broken
        JB PORTA.0,NOLIGHT        

        ;if light levels are high then...



  Remember that light sensitive resistors/diodes are not the only useful component you can use in this circuit. Thermistors would give a reading for temperature, a variable resistor could be tested and used as a basic form of speed/volume control input. However, remember also that the graph of Capacitance to resistance is not linear, it curves. So, unless you want to code a lookup table, or do some maths in software, the values you get from this circuit will not be consistent across the range of resistance. For the purposes above however it's fine to ignore this.





  
Voltage Regulator circuit

  Often in a project, especially those where MOSFETs or relays are being used to drive large loads, we want to use voltages greater than the 5v maximum for the PIC.

  While this can be simply achieved with two power supplies and a common ground, it is more convenient to design the circuit to give both the high and low level voltages from the same source.

  This can be achieved with the voltage regulator and decoupling capacitor circuit below.



  The use of a voltage regulator also enables us to run the PIC for longer, ie from a 9v battery.







[Go back for other PIC projects and info]


  A page from James David Chapman's website.
  Located at: http://www.users.globalnet.co.uk/~jchap/
  
Site mirrored here at: http://www.j.chap.btinternet.co.uk
Go back to the last page you viewed. Go to previous page on this website. Go on to the next page in this sub site. Go to the main contents list. Go to the help page. Please send me *your* home page address!. Go to the web form to simply and quickly send me your comments.
  This page last updated:
  
My rating for the page: How happy I am with this page...