PIC Projects

Digital barometer

This project is built around Freescale MPXHG6115 silicon pressure sensor. The sensor itself provides at its output voltage which is proportional to the air pressure. The working range overlaps the atmospheric pressure (90 - 110kPa) at the sea level. The minimum working air pressure of the sensor is 15kPa, which allows to use it in the mountains area. For this, however, three resistors on the sensor board must be recomputed according to the formulas below.

For the atmospheric pressure in my area (close to the sea level) the range of the sensor output voltages is 3.625 - 4.55V. The analog circuit (shadowed on the schematic) linearly maps this range to the range 0 - 5V, which is the normal range of PIC's ADC (I assume that PIC is powered from 5V). The mapping is performed by two Op Amps. The left one (on schematic) provides an optimal load resistance for the sensor (51K) and inverts the voltage about the 2.5V reference. The reference voltage is obtained by using a voltage divider consisting of two resistors 11.5K (1%). The right Op Amp provides necessary scaling of the voltage and bootstrapping it to 0. I use a rail-to-rail double Op Amp OPA2374 manufactured by Texas Instruments.

Schematic Test circuit

The sensor scaling analog amplifier is assembled on a small PCB, see below. It connects to the main board by using 3 wire headers. The test circuit consist of a microcontroller and an LCD module with a home-made interface circuit mounted on its back side. The interface board establishes all communication with PIC by using only two wires and its software implements a simplified version of the standard I2C interface. The PIC's program assigns its input pin RC3 to the ADC input. It simply computes the pressure based on the input voltage according to the formula below, converts it into BCD and sends to the display. The measurements are done in approx. 700msec intervals. The sensor voltage is very stable and repeatable from one measurement to another, so no averaging of successive measurements is needed.

Sensor board Board layout

The Math part

Deriving the formula to compute for PIC

I do all calculations in symbolic form in order it would be easy to adjust them to the required conditions. From the sensor data-sheet we get that the voltage V at its output is a linear function of the atmospheric pressure P, i.e.

V(v)= k·P(kPa) + b

where k and b are some constants. Specifically, k = 0.045 and b = -0.475 ± Error, according to the data-sheet. Inverting this equation results in

P = (V - b) / k

We assume that V is in some range corresponding to the interested pressure range, that is, v1 < V < v2 (for example, for the range of pressures 90 - 110 kPa we have v1 = 3.625V and v2 = 4.55V, according to the data-sheet). We also assume that we want to scale this range [v1 .. v2] to the rage [0 - R] of voltages corresponding to the DAC's range (in my circuit R = 5). This will be arranged by an analog circuit, whose output voltage Y is related to the sensor output voltage V as

Y = (V - v1)·R / (v2 - v1)

The DAC assigns a digital code to the voltage Y. I assume that we use an 8-bit DAC with 256 voltage levels and whose reference voltage is R. Denoting the numeric value of the DAC's output by Z (0 < Z < 255) we have

Y = Z·R / 255

Combining the last three formulas, we get

V = (Z·(v2 - v1) / 255) + v1

and, finally,

P = (((Z·(v2 - v1) / 255) + v1) - b) / k = (Z·(v2 - v1) / (255·k)) + (v1 - b) / k

Plugging into this formula the values of the involved parameters choose above, we have

P(kPa) = 0.08·Z + 91.1

This is the right moment to ask yourself in what units do you want to measure the atmospheric pressure. Needless to say that the units have to be mostly useful and understandable for you. I personally prefer to measure the pressure in mmHg. The conversion P(mmHg) = 7.5·P(kPa) leads to the final formula

P(mmHg) = 0.6·Z + 683.25

The wonderful thing with this formula is that if you multiply the both parts by 10 and round off the free term, the equation becomes

10·P(mmHg) = 6·Z + 6832

Thus, instead of computing the value of P we compute ten times this value by using the simple expression in the RHS of the above formula. When this is done, we convert the value of 10·P in BCD and simply ignore the last (rightmost) digit.

Calculating the resistors on the sensor board

As it is mentioned above, the output Y of the sensor board should be related with the output V of the sensor as

Y = (V - v1)·R / (v2 - v1) = V·[R / (v2 - v1)] - [v1·R / (v2 - v1)]

Let us see what do we have in the analog circuit. The output voltage U of the first Op Amp is just the input one (V - (R/2)) reflected about R/2:

(R/2) - U = (V - (R/2))

The output of the second Op Amp is then

Y - (R/2) = -(U - (R/2))· (R2/R1) - (R/2)· (R2/R3) = (V - (R/2))· (R2/R1) - (R/2)· (R2/R3)

In other terms.

Y = V·(R2/R1) - (R/2)[(R2/R1) + (R2/R3) - 1]

Hence, to have a match between the first and the last formula, we have to provide:

R2/R1 = R/(v2 - v1) and (R/2)[(R2/R1) + (R2/R3) - 1] = v1·R / (v2 - v1)

Using into those formulas the values R = 5V, v1 = 3.625V and v2 = 4.55V, we get

R2/R1 = 5.4 and R2/R3 = 3.43

This system of two equations wight three unknowns can be solved by fixing one of them. I choose R2 = 62K, which implies R1 = 11.5K and R3 = 18K.

Accuracy consideration

It has to be mentioned that the coefficient b in the above formulas is known only up to some unknown and part specific Error term. This results in a linear (constant) error term to be added to the final equation. This error term can be found by comparing the pressure shown by your unit with a calibrated one. In my case the additive term turns out to be 186, so the test program computes the formula

10·P(mmHg) = 6·Z + 6832 + 186

If you measure P in mmHg, it makes no sense to show the decimal digit appearing after the decimal point, as one cannot trust to is. I showed it on the test circuit just for an example.

The most important thing is to provide the exact values for the voltage divider (11.5K resistors), R1, R2, and R3. However, the software compensation is needed anyway to compensate the Error term in the sensor equation.

Downloads


Last modified:Mon, Jan 23, 2023.

12068