PDA

View Full Version : Scaling and offset of a sensor



Ioannis
- 13th March 2023, 12:46
I am a bit stuck in this offset and span measure of a pressure sensor.

I use the MPXV5004DP differential sensor and it gives and offsset around 1 volt at 0 differential pressure with max 4.92 volts for 39.22 mbar differential pressure.

So the span is 3.92 volts for 0-39.2 mbar, the offset depends on the device and may range from 0.75 up to 1.25 volts.

I would like to make an semi-automatic offset calculation on first program execution. Then arrange the 0-39.2 mbar to 0-5 volts but am confussed on the maths.

My A/D converter is 10bits. Any tip appreciated,

Ioannis

DaveP
- 13th March 2023, 13:11
Here is something I have used in the past.

'************************************************* ***************
'* Name : SCALE15W.BAS *
'* Notes : TO BE USED FOR SCALING 15 BIT VALUES (USING WORDS)*
' Input : INVALUE is the 15-bit value to scale
' : INMIN is the 15-bit lower bound of the value's current range
' : INMAX is the 15-bit upper bound of the value's current range
' : OUTMIN is the 15-bit lower bound of the value's target range
' : OUTMAX is the 15-bit upper bound of the value's target range
' Output : OUTVALUE holds the 16-bit result
'************************************************* ***************
INVALUE VAR WORD
OUTVALUE VAR WORD
INMIN VAR WORD
OUTMIN VAR WORD
INMAX VAR WORD
OUTMAX VAR WORD


SCLTEMP0 VAR WORD
SCLTEMP1 VAR WORD
SCLTEMP2 VAR WORD
SCLTEMP3 VAR WORD


'************************************************* ***************
SCALE_16W:
'************************************************* ***************
SCLTEMP1 = INVALUE - INMIN
SCLTEMP2 = OUTMAX - OUTMIN
SCLTEMP3 = INMAX - INMIN
SCLTEMP0 = SCLTEMP1 * SCLTEMP2
SCLTEMP2 = DIV32 SCLTEMP3
SCLTEMP0 = SCLTEMP2 + OUTMIN
OUTVALUE = SCLTEMP0

amgen
- 13th March 2023, 16:12
how wil you find the accurate 'offset' of the .75 to 1.25 volt. Do you need to know the mbar's presently at startup ?

Ioannis
- 13th March 2023, 16:28
Since there are some diferencies between sensor devices, on program startup or by user intervention, the offset will be stored in a variable. At this stage no other than the atmospheric pressure is applied, so this can be measured very easy.

Then in normal operation this will be the reference level.

So say that the offset is about 1.2 volts at 0 pressure. The max say it is 5 volts. The span is 3.8 volts for a pressure range of 0-39 mbar. I'd like to normalize this as 0 to 1023 counts.

In the above example I would first take the offset of 1.2 volts that is 246 count and store it in var offset (and EEPROM).

Then in normal operation say the A/D reads more then 1.2 volts, so it subtracts from the count the offset. But I would like to expand the count to match 0-1023 and not 1023-offset only. There is where I am confused.

I can ditch the floating point and just accept integers.

Ioannis

Ioannis
- 13th March 2023, 17:04
I think I got it.

My sensor is read at channel 0. So:



offset var word
sensor var word

offset_store: 'offset processing
adcin 0,offset
write 0,word offset
return

read_pressure:
adcin 0,sensor
sensor=sensor-offset
sensor=sensor*1023/(1023-offset)
return


the problem is how to manage the sensor*1023 since the result might be more than a word and I'd prefer not to use LONG.

But the 1023/(1023-offset) ends up in a small number like 1.62 or similar. It is not known untill the offset is measured and cannot be substituted as a constant and use */ operator.

Ioannis

Ioannis
- 13th March 2023, 20:10
RTFM once again.

Well, DIV32 is the magic trick.

I used a dummy var to make the sensor*1023 multiplication, saved the 1023-offset to range variable and did a div32 range.

Solved.

Ioannis