■ FIGURE 7 ■ FIGURE 8 shows a
the picture can be numerous
objects. An incandescent light
bulb, a relay, a motor, a high
current bank of LEDs, a buzzer, a
solenoid, just about anything that
needs to be switched on or off
and draws less than 300 ma can
use this circuit. Bipolar transistors
are pretty tough so as long as you
don’t exceed the current limits or
heat limits, you should be fine.
Notice in Figure 6 that the relay has a diode across it.
This is to bleed off the excess current stored in these inductive devices when the transistor is switched off. Without
that diode, the transistor can see a large voltage spike at the
collector which could damage the transistor. That large
spike can also cause electro-magnetic interference to the
PIC if it’s big enough.
Also notice that the motor has a capacitor across it.
This helps to knock down the continuous electrical noise
generated by a spinning DC motor. That noise has reset
the PIC multiple times in cases where I used a large motor
without a cap. Took me a while to figure out why my code
wasn’t working only to later discover I had left off the cap.
Driving the transistor isn’t any different than driving a
high side LED. A HIGH command will turn the transistor on
and a LOW command will turn it off.
To read a resistive sensor, I go back to the setup I used to
read multiple switches and use an A/D port. A light-dependent
resistor (sometimes known as a CDS cell) can be used in this
method, as shown in Figure 7. I also show the same circuit with
a thermistor in place of the CDS cell. Basically, any resistive
style sensor can use this method. A series resistance of 100
ohms is always good to include just in case the sensor connections are shorted to power somehow. The PIC’s internal protection will prevent it from being damaged, but why not protect
the port from that high current with a cheap 100 ohm resistor?
Neither of these will be linear, so you have to use some
kind of look-up table in your code to react to the reading. If
it was linear like a straight line response, then just using an
equation would be simple. This is where compilers with
floating point math are handy. You can use a complex
equation rather than a lookup table to process the signal.
The Atom module software, despite being a free download,
offers floating point. This is one of the reasons I like using
this software for some of my development.
The code for reading one of these sensors is shown
using the Basic Atom code again, though this time I use the
LOOKDOWN command instead of a series of If-Then’s.
14 November 2006
Value var Byte
LED var Byte
Lookdown value, >,[10,50,100,200,400,600,800,1000], LED
The program reads the A/D value and stores it in variable “value.” Then the program uses the LOOKDOWN command to convert it to a single digit. The “greater than” sign
does a significant function in that it makes the LOOKDOWN
command search the list for the last value that is less than the
contents of “value” and records its position in variable LED.
For example, if the A/D reading of the sensor came back
with 460, then the value of LED would be 4 because it starts
counting at 10 and assigns it the location 0; 50 is 1, 100 is 2,
and so on. Since 460 is greater than 400 but less than 600, it
assigns LED the value 4. The HIGH command will then drive
the P4 pin of the Atom PIC chip (PortB bit4 in PIC terms) and
an LED would light if it was connected in a high side drive
arrangement. If you want to do this same function in PICBasic
Pro, then you need to use the LOOKDOWN2 command.
Taking the resistive sensor a step further, reading a potentiometer with an A/D port is quite easy. Feeding one side of
the pot to five volts and the other to ground lets you connect
the center wiper to the A/D port through a 100 ohm protection resistor, as seen in Figure 8. Now you can read any
position of the pot by monitoring the A/D value. This makes
for great position feedback in a robot or any control situation
that will allow you to tie the output to the shaft of a potentiometer. A slider potentiometer will work the same way with
this setup. If you are trying to control a position table, you can
use a long slider pot to give position feedback to the PIC so
it can drive the position table motor to the correct spot.
It’s pretty clear that I’ve only touched the surface of PIC
interface circuitry. In previous articles, I covered driving LCD
modules, LED displays, and RS232 level shifter circuits. With this
set of circuits, you should be able to build 90% of your first
projects and then do an Internet search for anything unique.
Once you have a list of these circuits, it makes it easy to go back
and reference them rather than re-invent the wheel every time.
I built my breadboard modules based on some of these circuits.
Once I used them more than 10 times, I would lay out a circuit
board and turn it into a new breadboard module. It makes
prototyping so much faster. You can do the same on your
workbench, building in one circuit at a time. Pretty soon you’ll
have a whole collection of PIC interface modules to build from.
Keep those emails coming to email@example.com. I’m
getting great ideas from readers and it lets me know I’m keeping people interested. If you are really experienced and find
these columns boring, I’m sorry, but I really want to help more
people get started with PICs. The number of emails keeps growing, so it seems I’m helping more and more every month. NV