Calculating a Value from a Voltage

Hello,

i have a basic question. I am using the analog Pins to read out the voltage from a sensor. I have to convert this voltage to a pressure value using this equation: p = 10^(U-c). C is a constant that definies the unit the pressure will be given in.
I want to display the pressure on a LCD display. I used this code:

#include <LiquidCrystal.h>
int rs=7;
int en=8;
int d4=9;
int d5=10;
int d6=11;
int d7=12;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);
int readPin=A3;
float V2=0;
int readVal;
int delayTime=500;
int pressure;
void setup() {
// put your setup code here, to run once:
lcd.begin(16,2);
pinMode (readPin,INPUT);
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
lcd.setCursor(0,0);
lcd.print(V2);
readVal=analogRead(readPin);
V2=(5./1023.)*readVal;
pressure=pow(10,(V2-5.5));
Serial.println(pressure);
delay(delayTime);

}

But the display still displays the Voltage, not the pressure. Can somebody tell me what i am doing wrong?
Since the Code is not displayed correctly here, i uploaded it.
Thank you alot!
Pressure.ino (585 Bytes)

And now with code tags, please.

(sp. "a lot")

Thank you for the tip! I changed it.

Some more information would help. Can you post a schematic of your project, not a frizzy picture. Be sure that schematic contains all power and ground connections and a reference to the power source. Post a link to the sensor showing technical details. What Arduino are you using?

Hi
try this way:

RV mineirin

#include <LiquidCrystal.h>
int rs = 7;
int en = 8;
int d4 = 9;
int d5 = 10;
int d6 = 11;
int d7 = 12;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int readPin = A3;
float V2 = 0;
int readVal;
int delayTime = 500;
int pressure;
//---------------------------------------------------------------------------
void setup() {
  // put your setup code here, to run once:
  lcd.begin(16, 2);
  pinMode (readPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  lcd.setCursor(0, 0);
  lcd.print(pressure);
  readVal = analogRead(readPin);
  V2 = (5. / 1023.) * readVal;
  pressure = pow(10, (V2 - 5.5));
  Serial.println(pressure);
  delay(delayTime);

}

Because that's all you send to the display.

I think int may be at bit rough for what could be very small values.

But i am sending "pressure" to the display: Serial.println(pressure)
And pressure is definied as the result of the equation:
pressure=pow(10,(V2-5.5))
So the display should display the result of this equation. I am pretty new to arduino, so bear with me, if i am a bit helpless.

@ruilviana That did not work. The display just shows 0.

That is not the Liquid Crystal Display (LCD) it is the serial monitor (your PC's display).

Have you a data sheet for the pressure sensor ?. If so, post a link to it.

Wow, now i feel a littlebit stupid :smiley:
Thank you alot! Now it works!

This is the datasheet: https://www.idealvac.com/files/ManualsII/Pfeiffer_PCR260_Operation_Instructions.pdf

The sensor outputs a signal between +2.2 and +8.68 V. I am planing to use a voltage divider, so i am able to use the analog pins of the arduino.

OK. Maybe two 10k resistors to halve the voltage. You'll probably need to adjust the formula for calculating pressure.

If I double the voltage before I use it to calculate the pressure, it should work.
My problem now is that I get a value on the screen (and serial monitor) that gets calculated by the software but there are other problems. If I connect A3 to other Voltages, I don’t get the value I should. I checked the calculation with a calculator and the values do not match.
If I switch pin A3 from 5V to ground, it displays the correct value (31622.7766) for a second, then it disappears and shows 0. When I connect A3 to 5V it shows 0.
Has anybody a suggestion what I am doing wrong?

#include <LiquidCrystal.h>
int rs=7;
int en=8;
int d4=9;
int d5=10;
int d6=11;
int d7=12;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);
int readPin=A3;
float V2=0;
int readVal;
int delayTime=1000;
int pressure;
void setup() {
// put your setup code here, to run once:
lcd.begin(16,2);
pinMode (readPin,INPUT);
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
lcd.setCursor(0,0);
lcd.print(pressure);
readVal=analogRead(readPin);
V2=2*((5./1023.)*readVal);
pressure=pow(10,(V2-5.5));
Serial.println(pressure);
delay(delayTime);
lcd.clear();

}

Thank you alot for your help!

You are printing pressure before you calculate it.
Move this line to where the equivalent Serial.println is.

Thank you. I changed it. But it still behaves strange.

Now, when it is connected to ground, it sometimes shows the correct value. This it what the serial monitor shows:

“0
0
0
31622
31622
0
0
0
133
31622
15
0
0
0
1
5979
2909
0
0
0
2
4563”

When A3 is connected to 5V, the output is 0.

#include <LiquidCrystal.h>
int rs=7;
int en=8;
int d4=9;
int d5=10;
int d6=11;
int d7=12;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);
int readPin=A3;
float V2=0;
int readVal;
int delayTime=1000;
int pressure;
void setup() {
// put your setup code here, to run once:
lcd.begin(16,2);
pinMode (readPin,INPUT);
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
lcd.setCursor(0,0);
readVal=analogRead(readPin);
V2=2*((5./1023.)*readVal);
pressure=pow(10,(V2-5.5));
Serial.println(pressure);
lcd.print(pressure);
delay(delayTime);
lcd.clear();

}

Hello, I'm still a novice, but thanks for the advice

Just to help isolate the problem, can you add the marked statement in your loop().
If it is behaving correctly for a value of 0 volts (simulating sensor output) then the problem will be with the sensor or your wiring so you should show a schematic.

void loop() {
  // put your main code here, to run repeatedly:
  lcd.setCursor(0, 0);
  readVal = analogRead(readPin);

  readVal = 0 ; // test <<<<<<<<<<<<<<<<<<<<<<
  
  V2 = 2 * ((5. / 1023.) * readVal);
  pressure = pow(10, (V2 - 5.5));
  Serial.println(pressure);
  lcd.print(pressure);
  delay(delayTime);
  lcd.clear();
}

Incidentally, you seem to be having problems with code tags. Highlight all your code in the edit window and use the </> button to wrap the highlighted text in code tags.

I was not using the sensor. I was using the 5V from the Arduino to test the code.
I got it to work! I dont know why, but when i connected an external power supply, the values are correct.
Thanks a lot for your help!
If someboy wants to know the code:

#include <LiquidCrystal.h>
int rs=7;
int en=8;
int d4=9;
int d5=10;
int d6=11;
int d7=12;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);
int readPin=A3;
float V2=0;
float readVal;
int delayTime=1000;
float pressure_mbar;
float pressure_Torr;
float VCC;
void setup() {
  // put your setup code here, to run once:
  lcd.begin(16,2);
  pinMode (readPin,INPUT);
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
readVal=analogRead(readPin);
V2=2*((5./1023.)*readVal);

lcd.setCursor(8,0);
lcd.print("mbar");
lcd.setCursor(0,0);
pressure_mbar=pow(10,(V2-5.5));
Serial.println(pressure_mbar);
lcd.print(pressure_mbar);

lcd.setCursor(8,1);
lcd.print("Torr");
pressure_Torr=pow(10,(V2-5.625));
lcd.setCursor(0,1);
lcd.print(pressure_Torr);


delay(delayTime);
lcd.clear();
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.