Pressure sensor mapping

I have a 0-10 Bar sensor, but I'm running at 8 bar max. I have analogRead values for 0 bar and 7 bar (I wouldn't want to push my pump to 10 bar and it may not even get there).

My question is, if I map using the above values, will my pressure output scale linearly if it exceeds those values?

Sure, if the sensor and the analog input are linear then it is linear.

The Arduino analog inputs are linear, but the analog input of the ESP8266 and ESP32 are not linear.
Read the datasheet of your sensor to check if it is linear. I should be linear.

Hi Koepel, the sensor is indeed linear. I'm using a D1 mini, I was noticing when printing the values sometimes it was flickering between 2 adjacent values (say 221 and 222) and other times it was flickering between values 3 apart (222 and 225). Is this the linearity you were talking about? Or just because the voltage is being stepped down for the chip it doesnt have the resolution?

Yes , if you are using the map function , values outside of its “ mapped “ range remain linear ( until the A/D Max’s out) .

Google “ Arduino map”

The D1 mini is a ESP8266, it's analog input is not as linear as, for example, a Arduino Uno board.
The input impedance of a Arduino Uno is also very high, and the D1 mini uses a resistor divider (220k + 100k) to lower the analog voltage for the ESP8266.

When you have some noise, you can take a number of samples and calculate the average. A little noise might even help to gain more bits. I have made a averageRead.ino for an Arduino Uno, that shows that the total is a 'unsigned long' and the result is a float for maximum accuracy.

Which sensor (link).
Common ratiometric sensors should be read with a 5volt ratiometric A/D.
Only industrial 4-20mA sensors can be read properly with an ESP8266 (absolute/voltage A/D).
Don't expect too much of the A/D of an ESP8266 though.
Leo..

Its an industrial puppy alright, here's the datasheet Huba 511
Mine is the 0-10bar 2-wire variant.

I've got the output going through a 160 ohm resistor with a 10k resistor spurred into A0. It is reading alright, as I say its just skipping values at times when it jumps between numbers. I initially calculated what my 0-1023 values should be using V=IR with 160 as the resistance, was a bit off though when compared to the actual reading at atmospheric pressure.
That's why I wanted to know if the map will go beyond 7 as I cant reliably take it to full scale.

Out of interest, I also got myself a stm32 nucleo-144 board which I believe uses 12-bit ADCs. Im guessing that would be better on the accuracy front?

trojanhawrs:
Mine is the 0-10bar 2-wire variant.

I've got the output going through a 160 ohm resistor with a 10k resistor spurred into A0.

Ok, the 2-wire 4-20mA version can be read with the WeMos/ESP8266.

160 (165) ohm is fine, but the 10k is not needed in this case,
because the WeMos already has a 220k resistor build-in.
Just connect directly to A0.

As said, don't expect too much of the A/D of an ESP8266.
An expensive sensor like that should be connected to a 15-bit ADS1115 (100 ohm sense resistor and PGA2),
before being connected to the WeMos with I2C.
Leo..

Edit:
Modules like this one use the ADS1115, and have a build-in voltage booster to power the sensor.
Note the 16-bit error. The ADS is 15-bit single-ended.

Will the 10k be a problem? It's already soldered in, I'd take it out if it was an issue of any significance, otherwise Id rather leave it.

I only paid £20 for the sensor (old stock) and I already have a 24v regulated supply , I'd be reluctant to buy an expensive module unless I really needed the precision.

I may try it out on my STM32 board, just need to figure out if it has a current limiting resistor first!

No, the 10k is not a problem.

There are also cheap ADS1115-only breakout boards.

Don't know if the STM32 has a ratiometric or absolute A/D, and if you can switch to the right one (absolute) in code.
You might come off worse than the ESP8266 if you choose the wrong one.
Leo..

Hi Wawa, your reply has caused me to do a bit of googling and wondering if you can clear anything up for me.

Am I right in saying that absolute uses 3.3V as the reference voltage, whereas ratiometric uses the ratio of "sensor supply : sensor output"?

Are you saying I need absolute in this case because its a 4-20ma sensor and it'll output that current regardless of input voltage (within reason)?

The datasheets are quite hard to get my head around but I assume as long as Vref is tied to my 3.3V it'll be in absolute mode if I'm not missing something!

Thanks for your time

No, the opposite.

Ratiometric behaviour is when sensor and A/D (reference) are powered from the same voltage source.
Take a potentiometer as example. Powered from 5volt, with the A/D (default) also powered from 5volt.

When input voltage goes up, A/D value should go up,
when reference goes up, A/D sensitivity drops with the same amount,
thus keeping A/D value constant.

A 4-20mA sensor is not powered from the reference voltage of the A/D, so has no relationship with it.
For this sensor you must use a stable reference.
You shouldn't use Arduino's potentially unstable supply for that.

Most MCUs have an option to switch to one or more internal reference voltages.
Leo..

Ok, I'm with you. They actually have a large article on ADC accuracy here, I've read through it and although it doesn't mention absolute or ratiometric, it harps on about stable references and its an Uno rev3 compatible board so I would assume its normally absolute. It mentions decoupling the VDDA and Vref pins with capacitors to potentially get a more stable voltage, I think thats without adding an external vref (although thats an option too).

This is the regulator on the board LD39xxxx

Ratiometric behaviour is not very well understood, and the document seems to completely ignore it.

The STM32 range seems to have a ratiometric A/D only, so in theory unsuitable for a 4-20mA sensor.
You can of course change that by adding an external reference.
A voltage regulator, which powers other things, is not exactly a reference.

A dedicated external A/D with build-in reference (ADS1115) could be the better option.
Especially when you want to keep the WiFi option (the Wemos).
Leo..

Thanks Wawa for the insights. At the moment I'm not using the sensor for anything where high precision is required - I was potentially thinking about using it to detect changes in flow rate (pressure drop over interval) which would probably need that kind of accuracy.

I'll experiment with my other board just to compare to the wemos for curiosity's sake, I'm tempted to use it anyway because my WiFi is a bit flakey and it has an ethernet port.