I am working on a project that involves me monitoring several pressures. I picked up 3 sensors on ebay, 2 that are 150 psi, and 1 500 psi. Trying to get the code to output correct is like pulling teeth at this point.
I can read the value, map it to the voltage range and when I compare the voltage output with my dmm they are the same, within a few mv. It's getting the voltage to map to the pressure range that I'm having trouble with.
With a bit of thought, you do that with just one map statement. Instead of first converting to voltage, convert to PSI instead. According to the plot on the ebay page, the conversion is quite linear. Hint: if 5V = 1024, 4.5V = 922.
jremington:
With a bit of thought, you do that with just one map statement. Instead of first converting to voltage, convert to PSI instead. According to the plot on the ebay page, the conversion is quite linear. Hint: if 5V = 1024, 4.5V = 921.
I wanted to be able to verify what I was seeing on my fluke so that is why I did the map for the voltage.
I get what you are saying, I’ll redo my mapping since I do know it is getting what I expect as far as voltage.
For someone who learned VB in the '90s and never dealt with sensor input this is a little on the fun side. Not just the code but being able to do something that isn’t restricted to a pc.
Be careful when using map(). It can misbehave when the input value is outside the defined input range. You may need to use the constrain function as well.
Well thank you to all who replied. After having this project on hold for a few months I decided to play around with it today. Getting pretty annoyed with the mapping results I decided to see exactly how the mapping was done. Well to my surprise the map() function sucks.
void setup( void )
{
Serial.begin( 9600 );
long i;
long m;
for ( i=0; i <=1023; ++i )
{
m = map( i, 0, 1023, 0, 1 );
Serial.print( i );
Serial.print( " " );
Serial.println( m );
}
}
void loop( void )
{
}
I actually just got out the calculator and messing in excel made my tables of raw values, and voltages then figuring where it would start and stop based on raw values did my own math and then mapped it based on what I got and it came to a better output than what I was getting before. I was actually thinking about something very similar to what you posted.
Wawa:
Maybe this example (for the 150psi sensor) will help.
Use pre-reads and smoothing for higher accuracy.
Leo..
int rawValue; // A/D readings
int offset = 51; // ~0.5volt zero pressure adjust (~30-72)
int fullScale = 819; // ~4.5volt max pressure (span) adjust (~798-840)
float pressure; // final pressure in psi
Just ran a test on this code and it's so much better. By having the actual matrix of the raw values and voltages then comparing what the map function gives to what I calculated made life much easier. I was trying to map based on voltages but didn't realize map didn't work that way. Once I wrapped my head around that I was able to sit down and figure out the start and end point and map it out based on raw values instead.
I’m aware it could be done with considerably less code. I needed a way to verify values were what I was expecting, it will be truncated before final version is sent.
I added the code using math rather than map() and while it averages right along with the map I’m not sure if I’m going to use it.
Wawa:
As said, pre-read and averaging/smoothing increases accuracy.
I will post an example later.
Leo…
From the code you posted to using the map() function the accuracy is about the same. In the application I’m using decimal point accuracy isn’t needed. I was just having trouble wrapping my head around mapping it.
That type of sensor has a range of 10% of the supply voltage (0.5V for 5 volt supply) for zero pressure, and 90% (4.5V) for full scale pressure. Post a link to the sensor you have. What kind of pressure readout do you want?