A refresher course in sensor design using microcontrollers: Part 3 - Embedded.com

A refresher course in sensor design using microcontrollers: Part 3

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

For example, ourstandard temperature sensor with built-in signal conditioning producesan output of 10 mV per degrees Centigrade, with an output range of -55to +150 degrees Centigrade. Let us assume it is to be interfaced to aPIC MCU to measurebetween 10 and 35 degrees Centigrade.

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

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

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

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

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

A circuit which meets these requirements is shown in Figure 10.3 below. It isbased on a non-inverting amplifier configuration, with the offset addedas a positive voltage at the reference input. The temperature sensorinput is represented by three selected levels, corresponding to theminimum (100 mV), mid-range (225 mV) and maximum (350 mV) outputvoltage. The gain is notionally 10, but is adjusted by the referenceinput resistance. The offset input is notionally 100 mV, but is alsoadjustable.

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

In the simulation, the minimum reached is about 80mV. Theoutput gives a resolution of 100 mV/ degrees Centigrade, so readingsfrom 10 to 11 degrees Centigrade will be affected, leaving an operatingrange of 11″35 degrees Centigrade. This will be accepted. Ifunacceptable, the operating range can be modified by adjusting theoffset input voltage and recalibrating.

Figure10.3 Gain and offset adjustment

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

These steps are thenrepeated until the reading is correct at the mid and max values. Thisis 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 orrange to theadjustment.

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

It ispossiblethat self-heating in the diode could cause some temperatureinstability. If necessary,a more stable reference circuit design should be used, or, at the veryleast, the circuit temperature should be allowed to reach a steadystate beforethe calibration procedure is attempted.

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

At midrange, 22.5 degrees Centigrade, the output is1.28 V, and theallowed range is 1.23″1.33 V. The accuracy of the amplifier should infact be better than +/-10 mV, and this is more than adequate. The ADCwill be working at 2.56 V/256 = 10 mV/bit, the same resolution.

Table10.4 Weather station specification

Weather Station
To illustrate sensor interfacing, a weather stationmeasuring 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 isdetailed in Table 10.4 above.

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

If sampled at 8-bitresolution, 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 usershould be able to reset, run and read back data manually. Optionally,an RS232 link to host computer will allow the data to be downloaded forfurther analysis and long-term storage.

Figure10.4 Block diagram of weather station

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

Signal filtering was not considered in detail, butthepossibility of controlling high-frequency interference and noise alwaysneeds to borne in mind. Typically, some low-pass filtering ordecoupling maybe included in the interface as a pre-caution when conditioning DCsignals.

This may be in the form of a simple first-order CRnetwork in the input, and an integrating capacitance connected acrossthe feedback resistance in the amplifier stage. The maximum sourceresistance allowed at the PIC ADC input is 10 kohm; a low-pass filterwith a 1 kohm series resistance and 100 nF decoupling capacitor willgive a cut-off frequency of around 2 kHz.

Temperature SensorInput
The default choice for this sensor is the LM35 type. The performance isadequate for this application, and it is possible to connect it directto the ADC input. In this case, the LM35C is used which allows negativetemperatures to be measured.

To provide these as a positive voltagewith single supply, the sensor negative supply is connected to groundvia a diode to lift the zero degrees output to around 0.7 V. Thisallows the actual output voltage to go below the zero level whileremaining positive with respect to supply 0 V (Figure 10.5 below ).

Figure10.5. Temperature sensor interface: (a) sensor connections; (b)interface simulation.

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

Theinput from the sensor is simulated by a switch which provides themaximum and minimum voltage which would be seen at the input. A furtherpositive input provides the offset at the amplifier output to give0.00″2.00V corresponding to the input range of 100°C. The overallsensitivity is 20 mV/°C. A further negative input of 0 V is neededto match the offset input. The preset feedback resistance is adjustedfor a gain of 2.00.

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

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

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

The reference diode current mayneed to be adjusted in the real hardware by changing its 1k currentfeed resistor to a value that gives the same current as that providedby the sensor to its offset diode.

When converted with a 2.56 V reference, thetemperature range will be represented by binary numbers equivalent to0″200, with 50 representing 0 degrees Centigrade. This scaling offsetcan be corrected in software, prior to display.

Remember that thesingle supply amplifier output will not go all the way to zero, so theactual range starts at about -23 degrees Centigrade. In normalcircumstances, this is acceptable, as this temperature is rarelyexperienced in temperate climates.

Figure10.6. Light sensor interface: (a) sensor connection; (b) LDRcharacteristic; (c) interface simulaiton

Light Sensor Input
The light sensor input is designed around the standard NORP12 cadmiumdisulphide-LDR. It has a spectral response which is similar to thehuman eye, and is sensitive to a wide range of values of lightintensity and is relatively easy to interface. Its resistance isinversely 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 levelsare similarly divided. When the LDR is connected in series with a 4k7resistor across the 5 V supply, a set of voltages is obtained whichvary from 2.5 V (high resistance, dark) to 0 V (low resistance, light).

In the simulated interface, these values arerepresented by switchedparallel resistances, with the sensor voltage simply buffered by aunity gain amplifier (Figure 10.6 (c)above ). The software can then comparethe input with any chosen set of limits, which correspond to therequired light levels. The actual reading will be stored for furtheranalysis.

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

Oneside of the gauge diaphragm is exposed to atmosphere, while thepressurised system is connected to the other side. Small deviationsfrom atmosphere caused by meteorological variation are more difficultto measure accurately.

It is suggested here that one side of the gaugeisconnected to a closed tube representing 1 atmosphere, while the otheris exposed to the varying meteorological pressure. Careful calibrationwill be required, with temperature compensation for its effect on thefixed volume of air. This temperature measurement is available fromsensor input described above.

Low-cost pressure sensors use a strain gaugebridgemade up of lasertrimmed piezo-resistive elements in a compact, robustpackage. A pressure in the range of 850″1106 mbar is proposed (range =256 mbar), allowing an 8-bit conversion at 1 bit per mbar. Standardatmospheric pressure will then occur at a reading of 150.

The gauges investigated are rated in psi (poundspersquare inch). 1 psi = 69 mbar, so the range required is 256/69 = 3.71psi. A gauge is available which measures up to 5 psi with a 10 Vsupply. 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 equivalentto 5/69 = 0.0725 mV/mbar.

The range will then be 256 X 0.0725mV = 18.56mV. The amplifier gain required is therefore 2.56 V/18.56 mV = 138. Theoutput offset at 1000 mbar input will be 1.50 V. The low end will becurtailed by the output of the single supply amp not quite beingreaching zero, but as this will be an extreme event, this isacceptable. 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.0725X 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 isused,gain G = 1 + 2R2/R1, where R1 and R2 are the values in the inputstage.Therefore

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

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

Figure10.7. Pressure sensor interface: (a) sensor connections; (b) interfacesimulation.

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

Humidity SensorInterface
The humidity sensor selected has integrated signal conditioning so thatan output between 0.8 and 3.9 V is produced, representing a change inrelative humidity of 0″100%. A simple buffered attenuator is used toshift the signal range for input to the ADC. The output of 0 V from thesingle supply amplifier cannot be obtained, so the output is shifted upto the range 0.5″2.50 V, giving 20 mV/%. This offset must be removed insoftware, 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

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

Figure10.8. Humidity sensor interface

The small residual offset is easier to eliminateinsoftware, by adjusting the offset correction factor, and subtracting 52instead of 50. This allows preferred values to be used in theattenuator, reducing component cost. Figure10.8 above shows the simulated interfaceoperating at 100% humidity.

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

To read Part 1, go to Anintroduction to sensors and their characteristics
To read Part 2, go to Asurvey of sensor types

Usedwith the permission of the publisher, Newnes/Elsevier, this series ofthree articles is based on copyrighted material from “ InterfacingPIC Microcontrollers: Embedded Design by Interactive Simulation,” by Martin Bates. The book can bepurchased on line.

Martin Bates is a lecturer intechnology at the Hastings College of Arts and Technology, UnitedKingdom.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.