Need help with Analog in

I am completely new to this Arduino stuff, so you will see some random postings from me.. I don't know what I am doing (yet), but hope to get some help here...

Here is a quote from the AnalogRead page.

int analogRead(pin) Description

Reads the value from a specified analog pin, the Arduino board makes a 10-bit analog to digital conversion. This means that it will map input voltages between 0 and 5 volts into integer values between 0 and 1024

I have connected an Motorola MPXA4115AP (http://www.freescale.com/files/sensors/doc/data_sheet/MPX4115A.pdf) pressure sensor. According to the datasheet this sensor gives a result of 0-5 volt. The sensor is connected to Analog Port 0 and the value is converted by the ADC to a 10-bit number (0-1024).

I have written some code to find out what the pressure sensor reads. Now, I am not sure how to use the data I get. If I just do the read and write it to serial port (reading with Hyperterminal), I get this value from the sensor: 833 (or something close to that number). Somewhere else, I saw some code from Tom Igoe and he used 2 expressions to get a value (high/low byte). When I do the same with that value, the low byte = 3 and the high byte = 66.

Obviously, both methods represents the voltage coming from the sensor... But, I don't know which to use..

Anybody that can give me some insight on this? Which number should I use to know the voltage coming from the pressure sensor?

Tom Igoe's code that I used can be found here: http://www.tigoe.net/pcomp/code/archives/arduino/000744.shtml

After that, I have to translate the voltage to a pressure value...

Thanks in advance!

Hmmm... In Tom's example, he used a value of 256 for getting the high and low byte. When I use the value of 1024 (10-bit), I get the same value (0 for high and ~833 for low) as I get by just showing the result of the Analogread...

According to the datasheet the sensor will produce a signal of about 4.8V at 1150mbar and approx. 0.2V at 150mbar.

5V / 1024 = 5mV
833 * ( 5V / 1024 ) = 4.067V <---- this is the voltage your sensor produces.

Sensor span is 4.8 - 0.2 = 4.6V.

( 4.067 / 4.6 ) * 1150 = 1016mbar <----- this is your current ambient air pressure

From the datasheet:

Vout = VS x (0.009 x P – 0.095) ± (Pressure Error x Temp. Factor x 0.009 x VS) VS = 5.1 ± 0.25 Vdc

Let's assume VS = 5V, P = 1016mbar = 101.6kPa and simplify this a bit to:

Vout = VS x (0.009 x P – 0.095) Vout = 4.097V

which is pretty close to what I calculated above, 4.067V. Certainly within the error margin, even though we rounded a few numbers (like sensor span).

Third post in a row. ::)

Sensor span is 4.8 - 0.2 = 4.6V. ( 4.067 / 4.6 ) * 1150 = 1016mbar

Actually this should be ( 4.067 / 4.6 ) * (1150-150)... or something like that

Thanks for your help Brainfart... It all seems to make sense...

Currently I am using this code

  val = analogRead(0); 
  Serial.print("Sensor signal: ");
  Serial.println(val);
  Serial.print("Voltage (val * (5.0 / 1024)): ");
  Serial.println((val * (5.0/1024))*10000, DEC);                      // value * 10000 to get more accurate display (4 decimals)
  Serial.print("Pressure (val * (5.0 / 1024)): ");
  Serial.println((((val * (5.0/1024)) / 4.6) * (1150)*100), DEC);     // value * 100 to get more accurate display (2 decimals)

Which results in this:

Sensor signal: 839 Voltage (val * (5.0 / 1024)): 40966 Pressure ((((val * (5.0/1024)) / 4.6) * (1150)*100)): 102416

That is 1024,16 hPa. In the data above, I didn't subtract the 150 that you posted about in your 3rd message, if I would change the last 'Serial.print...' line from the example above to:

Serial.println((((val * (5.0/1024)) / 4.6) * (1150-150)*100), DEC);     // value * 100 to get more accurate display (2 decimals)

I get the following result:

Sensor signal: 839 Voltage (val * (5.0 / 1024)): 40966 Pressure ((((val * (5.0/1024)) / 4.6) * (1150-150)*100)): 89058

This time, the result is 890.58 hPa. Now I happen to have a weather station (US$ 200 value, so not a cheapie one) that reads 1006.2 hPa. In both cases the result in hPa is quite a bit away from what I get at the same time from my weather station...

What could be wrong????

Thanks in advance for new suggestions

Did you declare val as an integer? Because you divide it by 4.6, and I don't know if that will work.

Yes, I did declare val as an integer. I also tried to declare it as float, but I got the same results (but, consuming a lot more memory).

JD

Oh, my next calculation will be about finding out altitude... if you have any insight on that, I would appreciate it very much!

Thanks for all your help!

JD

Ok, I found this formula for resolving altitude from pressure (this link):

Altitude = (10^(log(P/P_0)/5.2558797)-1)/-6.8755856*10^-6.

Where P is the pressure at an unknown altitude, and P_0 is the pressure at sea level (zero feet). P and P_0 can be expressed in any unit because they are computed as a ratio.

I changed my code to the following:

#include <math.h>

int val;            // outgoing ADC value
int p=0;
int p0=0;
int alt;

void setup()
{
  // start serial port at 9600 bps:
  beginSerial(9600);
}

void loop()
{
  // read analog input, divide by 4 to make the range 0-255:
  val = analogRead(0); 

  p = (((val * (5.0/1024)) / 4.6) * (1150)*100);     // value * 100 to get more accurate display (2 decimals)
  if (p0 = 0);
  {
    p0 = p;
  }
  alt = (10^(log((p/100)/(p0/100))/5.2558797)-1)/-6.8755856*10^-6;
  Serial.println(alt, DEC);

  delay(10000);                 
}

Of course ( :sunglasses: ) this code results in an error at compile time:

In function ‘void loop()’:
error: invalid operands of types ‘int’ and ‘double’ to binary ‘operator^’

I know this must be the line where ‘alt’ is calculated. I think I understand what is wrong, but don’t know how to resolve it.

I seem to remember that one of the moderators recently said that exponentiation (^) doesn't work. I don't think that declaring all these variables as integers and then performing such complicated calculations with them works either. I doubt the Arduino can handle 'log', maybe somebody else with more knowledge can comment on that?

Yep, brainfart's right. In Arduino/C, the ^ operator doesn't do exponentiation (it's actually bitwise xor, something you'll likely never need). And it doesn't handle log either.

Are you planning on communicating with a computer in your final setup? If so, you definitely want to do the calculation there instead of on the Arduino board.

Hmm, probably you are correct about that…

However, this (simple) code works:

#include <math.h>

int val;      

void setup()
{
}

void loop()
{
  val = 2 ^ 2;
  val = log(10);
}

I have not yet been able to ‘print’ the result to the serial port…

Yep, brainfart's right. In Arduino/C, the ^ operator doesn't do exponentiation (it's actually bitwise xor, something you'll likely never need). And it doesn't handle log either.

Are you planning on communicating with a computer in your final setup? If so, you definitely want to do the calculation there instead of on the Arduino board.

The final setup is not going to include communicating with a computer, I use the computer now (Hyperterminal) for debugging purpose... just to see what results I get.

I am working to make an altimeter... the formula above is needed to calculate altitude from Pressure.