# A refresher course in sensor design using microcontrollers: Part 3

May 27, 2007

In order to design the interface between a sensor and the MCU, we need to specify the performance of a linear amplifier which will translate the output of the sensor into a suitable input for the MCU, which is in the right range for the amplifier to handle and allows a convenient conversion factor to be used in the ADC.

For example, our standard temperature sensor with built-in signal conditioning produces an output of 10 mV per degrees Centigrade, with an output range of -55 to +150 degrees Centigrade. Let us assume it is to be interfaced to a PIC MCU to measure between 10 and 35 degrees Centigrade.

Gain
At 10 degrees Centigrade, the input will be 10 X 10 = 100 mV. At 35°C, it will be 35 X 10 = 350 mV. The gain is calculated as the required change in the output divided by the range of the input. Now if we are using a single supply op-amp package, such as the LM324, the output range is strictly limited. The output only goes down to about 50 mV and up to about 3.5 V. So if we assume an output swing of 2.5 V is available, the gain required = 2.5/(0.35"0.1) = 10.

Offset
As in this case, sensors often have a positive offset, so the output range has to be shifted down. With a gain of 10, the output at the lowest temperature will be 100 mV X 10 = 1.00 V, and at the highest 350 mV X 10 = 3.50 V. This can be shifted down by 1.00 V, so that the output range will be 0"2.50 V.

This also allows us to use a reference voltage of 2.56 V, just above the required maximum, which gives a convenient 8-bit conversion factor (10 mV/bit). Including 0 V in the output means that we will lose the lowest degree or two from the range.

If this is not acceptable, the offset can be adjusted to suit, so that the output ranges from 0.5 to 3.0 V. In this case, the negative reference voltage for the ADC should be modified to 0.5 V, and the positive reference to 3.06 V. If necessary, corrections can also be made in software.

Frequency Response
Since we are measuring direct voltages, it is sensible to restrict the frequency response of the amplifier to low frequencies, as any instability in operation tends to produce high-frequency oscillation and incorrect DC readings. A moderate value of capacitance across the feedback network will reduce the frequency response by reducing the feedback impedance at high frequency. However, too high a value will slow down the response time, so this needs to be considered if transient behaviour is a significant consideration.

Calibration
A circuit which meets these requirements is shown in Figure 10.3 below. It is based on a non-inverting amplifier configuration, with the offset added as a positive voltage at the reference input. The temperature sensor input is represented by three selected levels, corresponding to the minimum (100 mV), mid-range (225 mV) and maximum (350 mV) output voltage. The gain is notionally 10, but is adjusted by the reference input resistance. The offset input is notionally 100 mV, but is also adjustable.

Calibration of the amplifier normally consists of adjusting the gain and the offset at the minimum and maximum output levels, assuming that it is linear in between these values. However, there is a problem with the single supply case " the minimum output is not reached because the amplifier cannot reach the supply rail voltage (0.000 V).

In the simulation, the minimum reached is about 80 mV. The output gives a resolution of 100 mV/ degrees Centigrade, so readings from 10 to 11 degrees Centigrade will be affected, leaving an operating range of 11"35 degrees Centigrade. This will be accepted. If unacceptable, the operating range can be modified by adjusting the offset input voltage and recalibrating.

 Figure 10.3 Gain and offset adjustment

Therefore, in this circuit, we will calibrate the amplifier by adjusting the offset to approximately the correct value at the mid-output level (1.280 V), and then adjusting the gain to give the right output at the maximum level (2.500).

These steps are then repeated until the reading is correct at the mid and max values. This is usually necessary because the gain and offset interact, that is, adjusting one affects the other. In practice, multi-turn pre-set pots (typically 10 turns) are often used to give greater sensitivity or range to the adjustment.

In this circuit, a relatively small offset voltage is required, and it is obtained by taking the forward volt drop of a standard signal diode (about 0.7 V) and dividing it down to around 100 mV. A fine adjustment of this is then obtained by 'squeezing' the diode voltage via its current supply. A diode current of about 10 mA is used, dissipating about 10 X 0.7 = 7 mW in the diode.

It is possible that self-heating in the diode could cause some temperature instability. If necessary, a more stable reference circuit design should be used, or, at the very least, the circuit temperature should be allowed to reach a steady state before the calibration procedure is attempted.

The accuracy of the sensor is quoted as +/-0.5 degrees Centigrade, and the interface needs to match this. The output changes by 100 mV/degrees Centigrade, so 0.5 degrees Centigrade = 50 mV.

At midrange, 22.5 degrees Centigrade, the output is 1.28 V, and the allowed range is 1.23"1.33 V. The accuracy of the amplifier should in fact be better than +/-10 mV, and this is more than adequate. The ADC will be working at 2.56 V/256 = 10 mV/bit, the same resolution.

 Table 10.4 Weather station specification

Weather Station
To illustrate sensor interfacing, a weather station measuring temperature, light, pressure and humidity will be designed. These variables will be sampled at an interval of 5 minutes (12/hour) and data stored for a period of up to 10 days. The specification is detailed in Table 10.4 above.

The system will be based on a general purpose module using the PIC 16F877, an LCD and a serial memory, details of which will be provided in the next chapter. It has a 12-button keypad, 16 X 2 line backlit display and a 16 kb serial memory (Figure 10.4 below). Each variable will occupy eight characters on screen in run mode.

If sampled at 8-bit resolution, one sample for each sensor = 1 byte of data. Over 10 days, the system will store 10 X 24 X 12 X 4 = 11520 bytes of data. The user should be able to reset, run and read back data manually. Optionally, an RS232 link to host computer will allow the data to be downloaded for further analysis and long-term storage.

 Figure 10.4 Block diagram of weather station

The ADC inputs will be connected to this module via a 10-way ribbon cable, with the analogue interfaces built on a separate board. A sensor was selected for each weather variable, primarily based on the range required, ease of interfacing and low cost. An analogue interface was then developed to provide the gain and offset required for each.

Signal filtering was not considered in detail, but the possibility of controlling high-frequency interference and noise always needs to borne in mind. Typically, some low-pass filtering or decoupling may be included in the interface as a pre-caution when conditioning DC signals.

This may be in the form of a simple first-order CR network in the input, and an integrating capacitance connected across the feedback resistance in the amplifier stage. The maximum source resistance allowed at the PIC ADC input is 10 kohm; a low-pass filter with a 1 kohm series resistance and 100 nF decoupling capacitor will give a cut-off frequency of around 2 kHz.

Temperature Sensor Input
The default choice for this sensor is the LM35 type. The performance is adequate for this application, and it is possible to connect it direct to the ADC input. In this case, the LM35C is used which allows negative temperatures to be measured.

To provide these as a positive voltage with single supply, the sensor negative supply is connected to ground via a diode to lift the zero degrees output to around 0.7 V. This allows the actual output voltage to go below the zero level while remaining positive with respect to supply 0 V (Figure 10.5 below).

 Figure 10.5. Temperature sensor interface: (a) sensor connections; (b) interface simulation.

The interface uses a differential amplifier with two positive and two negative inputs, based on the universal amplifier described previously. The number of positive and negative inputs must always be equal to conform to this model. A reference diode provides a negative input to balance the positive offset on the sensor input.

The input from the sensor is simulated by a switch which provides the maximum and minimum voltage which would be seen at the input. A further positive input provides the offset at the amplifier output to give 0.00"2.00V corresponding to the input range of 100°C. The overall sensitivity is 20 mV/°C. A further negative input of 0 V is needed to match the offset input. The preset feedback resistance is adjusted for a gain of 2.00.

The circuit provides the following arithmetic sums at each end of the range (-25 and +75 degrees Centigrade).

2.000 x (443 + 247-693-0) = -6 mV @ -25 degrees Centigrade
2.000 x (1443 + 247-693-0) = -6 mV @ +75 degrees Centigrade

The 6 mV at the output (3 mV at the input) is the offset of the amplifier, which is allowed for in the external offset adjust (250"3 = 247 mV). Notice that in the simulation there is a residual offset at 2.000 V output, but this is less than 5 mV, which is acceptable (<0.5% at full scale).

The reference diode current may need to be adjusted in the real hardware by changing its 1k current feed resistor to a value that gives the same current as that provided by the sensor to its offset diode.

When converted with a 2.56 V reference, the temperature range will be represented by binary numbers equivalent to 0"200, with 50 representing 0 degrees Centigrade. This scaling offset can be corrected in software, prior to display.

Remember that the single supply amplifier output will not go all the way to zero, so the actual range starts at about -23 degrees Centigrade. In normal circumstances, this is acceptable, as this temperature is rarely experienced in temperate climates.

 Figure 10.6. Light sensor interface: (a) sensor connection; (b) LDR characteristic; (c) interface simulaiton

Light Sensor Input
The light sensor input is designed around the standard NORP12 cadmium disulphide-LDR. It has a spectral response which is similar to the human eye, and is sensitive to a wide range of values of light intensity and is relatively easy to interface. Its resistance is inversely proportional to light intensity, as shown in Figure 10.6 (b) above.

The light level is divided into five decades, from <1 lux (dark) to >10000 lux (direct sun), so the output levels are similarly divided. When the LDR is connected in series with a 4k7 resistor across the 5 V supply, a set of voltages is obtained which vary from 2.5 V (high resistance, dark) to 0 V (low resistance, light).

In the simulated interface, these values are represented by switched parallel resistances, with the sensor voltage simply buffered by a unity gain amplifier (Figure 10.6 (c) above). The software can then compare the input with any chosen set of limits, which correspond to the required light levels. The actual reading will be stored for further analysis.

Pressure Sensor Interface
Measurement of barometric pressure is not particularly straightforward, since pressure measurement in usually made relative to atmosphere (1 bar = 1000 mb). For example, it is straightforward to measure a low-pressure air supply for a pneumatic system operating at 5 bar.

One side of the gauge diaphragm is exposed to atmosphere, while the pressurised system is connected to the other side. Small deviations from atmosphere caused by meteorological variation are more difficult to measure accurately.

It is suggested here that one side of the gauge is connected to a closed tube representing 1 atmosphere, while the other is exposed to the varying meteorological pressure. Careful calibration will be required, with temperature compensation for its effect on the fixed volume of air. This temperature measurement is available from sensor input described above.

Low-cost pressure sensors use a strain gauge bridge made up of lasertrimmed piezo-resistive elements in a compact, robust package. A pressure in the range of 850"1106 mbar is proposed (range = 256 mbar), allowing an 8-bit conversion at 1 bit per mbar. Standard atmospheric pressure will then occur at a reading of 150.

The gauges investigated are rated in psi (pounds per square inch). 1 psi = 69 mbar, so the range required is 256/69 = 3.71 psi. A gauge is available which measures up to 5 psi with a 10 V supply. If the supply is +5 and 0 V, the output will be +/-2.5 psi, with a sensitivity of 5 mV/psi and offset of 2.5 V. This is equivalent to 5/69 = 0.0725 mV/mbar.

The range will then be 256 X 0.0725mV = 18.56 mV. The amplifier gain required is therefore 2.56 V/18.56 mV = 138. The output offset at 1000 mbar input will be 1.50 V. The low end will be curtailed by the output of the single supply amp not quite being reaching zero, but as this will be an extreme event, this is acceptable. The input and output voltages are then as follows:

Input Vinzero = 0 mV
Vinmin = 0.0725 X -140 = -10.15 mV (<140 not used)
Vinmax = 0.0725 X 100 = 7.25 mV
Output Voutzero = 1.50 V
Voutmin = 1.50"1.40 = 0.10 V
Voutmax = 1.50 + 1.00 = 2.50 V <> If the standard instrumentation amplifier is used, gain G = 1 + 2R2/R1, where R1 and R2 are the values in the input stage.Therefore

R2/R1 = (G-1)/2 = (138-1)/2 = 68.5

If R1 = 1k, R2 = 68k + 470R

 Figure 10.7. Pressure sensor interface: (a) sensor connections; (b) interface simulation.

The offset voltage (+1.50 V) will be input at the non-inverting reference input. This can also include some adjustable element to compensate for the amplifier input offset. Figure 10.7 (b) above shows the circuit simulation operating with the maximum input.

Humidity Sensor Interface
The humidity sensor selected has integrated signal conditioning so that an output between 0.8 and 3.9 V is produced, representing a change in relative humidity of 0"100%. A simple buffered attenuator is used to shift the signal range for input to the ADC. The output of 0 V from the single supply amplifier cannot be obtained, so the output is shifted up to the range 0.5"2.50 V, giving 20 mV/%. This offset must be removed in software, by subtracting 5010 from the 8-bit binary input.

Input range = 3.9-0.8 = 3.1 V
Output range = 2.50-0.50 = 2.00

Therefore required gain = 2.00/3.1 = 0.645 (attenuation)
Use unity gain + output attenuator

Output max = 3.9-0.645 = 2.516
Output min = 0.8-0.645 = 0.516

 Figure 10.8. Humidity sensor interface

The small residual offset is easier to eliminate in software, by adjusting the offset correction factor, and subtracting 52 instead of 50. This allows preferred values to be used in the attenuator, reducing component cost. Figure 10.8 above shows the simulated interface operating at 100% humidity.

The input and output buffering of the attenuator network simply reduces any error due to loading effects. However, the sensor is only specified about 4% accurate normally, so this may not be absolutely necessary.The sensor can be supplied with individual calibration data if a more accurate output is needed.

To read Part 1, go to An introduction to sensors and their characteristics
To read Part 2, go to A survey of sensor types

Used with the permission of the publisher, Newnes/Elsevier, this series of three articles is based on copyrighted material from "Interfacing PIC Microcontrollers: Embedded Design by Interactive Simulation," by Martin Bates. The book can be purchased on line.

Martin Bates is a lecturer in technology at the Hastings College of Arts and Technology, United Kingdom.

• 06.26.2007

• 12.30.2004

• 10.31.2013

• 12.04.2013