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)
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?
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.
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();
#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();
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();
}