First, we need to get a handle on the signals coming into our instruments. This chapter covers the techniques used to collect and condition the input signals so they can be connected to the microcontroller.
Before we can do useful work, we need to detect and manage the signal we are interested in. In our particular case, there is the added requirement that the signal either be an electrical signal or be such that it can easily be converted into an electrical signal. Though a Bourdon tube pressure gauge might read the pressure just fine for most engineering applications, we need a pressure sensor that provides us with an electrical signal we can feed into an electrical instrument. We are not so much concerned with the magnitude of the signal as we are with knowing it is in a range we can read, manipulate, and bring into our microcontrollers. A linear signal response is most desirable, but we can take care of the nonlinearity of a signal in our controller with software if that becomes necessary.
In the engineering laboratory and amateur engineer’s workshop, the electrical signals we encounter will most likely be somewhere between 0 and 120 volts ac and between 0 and 24 volts dc. No matter what the conditions, extreme caution and care should be exercised when dealing with any kind of electrical signal. Electricity can be lethal if not handled with care. When you are dealing with the higher currents and voltages, seek the help of an experienced electrician if you feel the least bit uncomfortable with undertaking that part of the task. Be informed that certain work must meet the requirements of the National Electrical Code in most jurisdictions, so help from an experienced person (electrician) can save you a lot of time and headaches.
Let’s take a look at the ac signals first, and then we will look at dc signals.
High voltage 120 volts (we will not consider anything over 120 volts)
Low voltage 24 volts
Electronic (computer circuitry signals) Less than 24 volts, and usually less than or equal to 5 volts
ALTERNATING CURRENT
The signals we are interested in as they relate to the electric grid and laboratory devices will most likely either be at 24 volts ac or at 120 volts ac. Chances are good that if we got it from anything connected to the national electrical grid, it will be in this voltage range. Everything under a half horsepower that can be plugged into an electrical socket is likely to be at 120 volts ac at 60 Hz. The control wiring most commonly used in the United States is at 24 volts ac (but sometimes higher voltages are used). Certain starters, relays, and coils will use 24 volts and will need a transformer to create this voltage. Look for this transformer and actually measure the voltages at the devices. Control wiring does not usually have to be in conduit if it is at 24 volts or below (check the National Electrical Code). If we want to interact with a control signal, we must extend these signals from the device to our controller, condition it, and feed it to the microcontroller. (Output to things needing 24 volts dc or 120 volts ac is discussed later in the Chapter 12.)
The other ac electrical property we are interested in is the frequency of the signal. A number of instruments we are interested in are based on how often a signal changes over a period of time, either at a fixed rate or a variable rate. Variable rates are usually read with counters over an extended period of time, while fixed rates can be read over a very short period. Oftentimes, the accurate measurement of one cycle will suffice. The measurement of pulse widths is a mixed bag in that we measure the time between changes of a fixed signal.
Computer Input If we are getting information from a computer, we will need an interface that matches the output of the computer both at the voltage level and as a communication protocol. These signals are almost always at or under 12 volts.
24 volts (we will not cover anything over 24 volts dc)
12 volts
5 volts
3.3 volts
DIRECT CURRENT
The signals we are interested in are under 24 volts, but before you do anything else you should check this for yourself. If lead acid batteries are involved, it will probably be at 12 volts. Most automotive voltages are currently at 12 volts (the measured voltage on a charged battery on a running car will be slightly higher). Heavy duty trucks often use twice this voltage. If you are considering electronic devices, the voltages will probably be between 3 and 9 volts. Conditioning dc signals for introduction into a microprocessor is usually a matter of making sure not too much current or too high a voltage goes into the electronics with appropriate resistances and buffers.
Note Safety is always “JOB 1.” Be cautioned that when working around lead acid batteries, extremely large currents (well over 500 amps instantaneously) are easily available at the battery terminals. These high currents can be dangerous and will easily melt wires and even wrenches. Be very careful when working around lead acid batteries. If you are not familiar with hazards associated with batteries (acids and hydrogen), you should take the time to become acquainted with this aspect of your work for your own safety, and the safety of those around you, before you start.
All the signals we collect must be conditioned and converted to a form that the microprocessor can safely accept. The conversion into digital format can take place in the microprocessor itself, so we do not have to worry about any external electronics to do that.
We have two interests in the incoming signal. With dc signals, we want to know if the signal is on or off, and we want to know its magnitude. (dc signals can also come in as a square wave or a frequency, which is covered under ac.)
On-off information is used as a means of detecting what the conditions downstream from us are so we can send control information to what is upstream from us. If a device that is interacting with us has come on, we need to respond to it in a specific manner, meaning that the information should be used as part of our control functions.
Magnitude information is used to determine the condition of the signal we are monitoring, to know if we are getting the results we want. If we are trying to maintain the temperature of an oven, we need to know the magnitude of the signal coming to us across the thermocouple so we can convert it into a temperature and then compare that temperature to what was needed, in order to decide whether to turn the heating elements either on or off. The instrument we design manipulates the thermocouple voltage data, to comprehend the information in the signal, and then makes the decision that controls the heaters.
KEYBOARDS
The reading of keyboards and other matrices of switches is covered in great detail in the earlier part of the tutorial under keyboards.
RELAYS
A relay can easily be used to provide a dry contact that can be read with the PIC. These contacts are quite noisy (electrically) and should be debounced either with hardware or software as they are read. If two pins are available for the signal, a latch can be set at one pin and cleared with the other. Latches have the advantage that a latched signal latches immediately and then does not need any more attention. We can come back to it when convenient. A signal latching and clearing circuit is shown in Figure 11.1.
RESISTANCES
Potentiometers can be read by placing them across a reference voltage and ground and using one of the analog inputs to read the position of the wiper. This technique is demonstrated in the chapter on reading inputs and in the final project where a potentiometer is used as the set point determining device.
Fixed resistances can be read either by measuring the time it takes to discharge a known value capacitor or by comparing them to a known resistor. The technique used will depend on the value of the resistor being read. The first technique is explained in the PBP manual under the POT command on page 116. The second technique places the two resistances in series and then reads the middle voltage as it represents the wiper in a potentiometer. The caution is that low resistances are likely to short across the power supply and ground. The total resistance of the two resistors needs to be well over 2k ohms and is better if over 5k ohms.
Figure 11.1 Signal latching and clearing. (Reading a relay or a pushbutton with a latch.)
24-VOLT SIGNALS
Signals between 12 and 24 volts dc can be introduced into a PIC by creating a voltage divider to reduce the signal to approximately 4.5 to 4.9 volts dc as shown in Figure 11.2. A current-limiting resistor of between 220 and 1000 ohms is placed in series with the signal as a safety precaution to limit the current into the PIC. The PIC inputs are high-impedance inputs but it does not hurt to add the resistance in an experimental situation where things might turn out to be not quite what you expected.
12-VOLT SIGNALS
Signals at 12 volts can be handled the same as the 24-volt signals above with appropriate resistances provided, or they can be passed through standard logic components to condition for use with a PIC. All signals should go though conditioning buffers or gates when there is any concern about the quality of the signal.
The PIC pins are in a high-impedance condition when programmed as inputs and will accept any TTL- or CMOS-level signal, so the task at hand is to convert the incoming signal into either a TTL-level signal or a CMOS-level signal. Floating signals coming
Figure 11.2 Wiring diagram for reading a 24-volt signal.
into floating inputs should have a pull-up resistor (10 to 100k ohms) at the pin to tie the pin high. Opto isolators can be used to isolate the noisy signal electrically from the PIC when necessary. If you have an isolator available, use it.
The diagrams in Figures 11.3 through 11.6 show various techniques that may be used.
Figure 11.3 Opto isolation of incoming 12-volt signal.
Figure 11.4 5-volt TTL-level signals.
Figure 11.5 15-volt logic.
Figure 11.6 3-volt signals.