For those interested I thought I'd throw in some background info.....
Working with digital devices is a lot more difficult than just plugging an analogue sensor into an analogue gauge - with those the sensor takes a twelve volt feed and returns a voltage anywhere between 0 & 12v to the gauge.
12v will fry one of these microcontrollers, at most they can handle a 5v power supply, and most of the inputs need to be 3.3v maximum.
Fortunately, most of the sensors can be driven at 5v. A simple voltage divider made from 2 resistors will then allow the output to be scaled down to the 3.3v range. Typically I use a 10k ohm & 20k ohm pair then connect the ADC (Analogue to Digital Converter) input on the microcontroller between the 2.
So that's the easy part done, now you have to do some maths to figure out what that voltage actually means....
The ADC is 12 bit linear, so that means you get a value from 0 to 4093 depending on the input. 0 for 0v, and 4093 for 3.3v.
For the boost sensor that I'm using, the spec sheet says that at -1bar boost (0 bar absolute) the output is 0v, 0bar boost (atmospheric pressure) is 1v, 1bar boost is 2v, 2bar 3v, & 3bar is 4v, & 4bar is 5v.
We know that out voltage divider basically reduces all of these by 1 third.
Given that we know that the ADC will output 4093 for 4 bar and that the range of the sensors is 5 bar (-1 through to 4 bar), we can figure out how much pressure each graduation between 0 & 4093 is.
To make the numbers easier to work with, we'll use millibar instead of bar, 5 bar is 5000 millibar. If we divide 4093 by 5000, we get ~ 1.2195 mbar per adc graduation.
So if we multiply whatever the ADC is telling us by 1.2195, that will give us the absolute pressure from the boost sensor in millibar.
To get to PSI we then need to do a double conversion. Divide by 10 to get Kilo Pascals. Then multiply by 0.145038 to get PSI.
Finally, because Boost is measured relative to Atmospheric, we have to subtract 1 atmosphere of pressure in PSI from thsi figure (1 Atmosphere being ~14.5038 psi).
The code looks like this:
Code:
rawval = analogRead(sensor); // Read MAP sensor raw value on analog port 0
kpaval = (rawval * 1.2195)/10; // convert to kpa
boost = (kpaval * 0.145038) - 14.5038; // Convert to psi and subtract atmospheric (sensor is absolute pressure)
And that's just 1 sensor type of the 5 that will be used.