Not sure where my problem is.

Hi all. The problem i need help solving is i added an asd1115 ADC and i cannot get my bits to turn into PSI. This is my code. Like i said i can read the bits but i cannot turn it into PSI with my formula. Hope someone can help.

#include <Wire.h>

#include <LiquidCrystal.h>

#include <Adafruit_ADS1015.h>

LiquidCrystal lcd(12,11,5,4,3,2);
Adafruit_ADS1115 ads1115(0x48);

void setup() {

ads1115.begin();
lcd.begin(16,2);

}

void loop() {

int adc0 = ads1115.readADC_SingleEnded(0);
int psi = (adc0-5366) * 20000/21353;

lcd.setCursor(0,0);
lcd.print(adc0);
delay(1000);

lcd.setCursor(0,1);
lcd.print(psi);
delay(1000);
}

I have no idea what values you are reading, but when I run the following:

void setup() {
  Serial.begin(9600);

  int psi_i = (5466 - 5366) * 20000 / 21353;
  Serial.print("int psi  ");
  Serial.println(psi_i);
  
  float psi_f = (5466 - 5366) * 20000.0 / 21353.0;
    Serial.print("float psi  ");
  Serial.println(psi_f);

  Serial.print("int division:  ");
  Serial.println(20000 / 21353);
  Serial.print("float division:  ");
  Serial.println(20000.0 / 21353.0);
  
}

void loop() {



}

the serial monitor shows this:

int psi  -1
float psi  93.66
int division:  0
float division:  0.94

First, read the sticky post, "How to use this forum - please read," posted at or near the top of each section of the forum. Please pay particular attention to item #7, "If you are posting code or error messages, use "code" tags," and item #11, "Tips for getting the most out of your post." Then consider editing telling us more.

One thing you could tell us is the source of your errant formula. Why do you think that this

 int psi = (adc0-5366) * 20000/21353;

is an appropriate formula to convert your ADC readings to PSI?

Blue eyes thank you for your response. Do I have to use float? I was not wanting to have decimal places in my read out. Anyway around this?

Tmd3 i am a little confused… Does the formula not work? or is it the integer? A little more help from your comment is needed please.

I can't tell if your formula works. You haven't told us anything about your setup, other than that an ADS1115 is somehow involved.

I'm not sure, but I think that you're using "PSI" as an abbreviation for, "pounds per square inch." That makes me think that you're trying to measure some sort of pressure. That suggests that you have some sort of pressure sensor connected to the ADC. If that's right, then that pressure sensor presumably delivers a voltage that's related in some way to the pressure that it senses. Maybe it delivers a millivolt per psi, or maybe it's a volt per psi, or maybe it's some nonlinear function of psi.

Or, maybe "PSI" means something else in this context. Maybe it's some angle that you hope to measure, and you're using the greek letter psi to indicate that angle. If that's what you're doing, then maybe you have some kind of detector that provides a voltage that's some function of the angle. Maybe it's a millivolt per radian, or a volt per degree, or a half-volt per sine of the angle, or something.

I'll ask again, and hope that you'll actually answer this time: why do you think that this:

int psi = (adc0-5366) * 20000/21353;

will tell you something meaningful about the mysterious quantity, "PSI?"

You can tell us these things, too: - What kind of thing is, "PSI?" - What detector are you using? Point us to a datasheet if you can. - Are you using an Arduino? - What kind of Arduino? - What have you done so far, other than type in the code, push the upload button, and hope?

Here is a simple way to emulate / verify your formula

void setup() {
  // put your setup code here, to run once:
  //int adc0 = ads1115.readADC_SingleEnded(0);
  //int psi = (adc0 - 5366) * 20000 / 21353;
  Serial.begin(115200); 
  for (int i = 0; i != 5000; i += 500)
  {
    int psi = (i - 5366) * 20000 / 21353;
    Serial.print(" psi : ");
    Serial.println(psi);
    delay(1000);
  }
  }
    void loop() {
    // put your main code here, to run repeatedly:
  }

Was that the OP question? Or did I missed something? Personally I think knowing the flavor of ice cream OP fancy is crucial for the formula to work properly.

Code like this - without explanations / comments using "MN" AKA magic numbers is a sure fire way for headache.

Jim

Not sure but possibly your multiplication overflows the size of the int. I can't test now but you can try 2000L to force calculations with long.

Ps: use serial print statements to debug your code.

Definitely overflows an int on the 8-bit Ardiunos which have 16 bit int type. Use long.

Thanks guys i got that figured out. I just ended up using long instead of int. Now for the next question if you dont mind. When i relief pressure from my transducer my psi reading and count reading go all kind of crazy. so lets say i have a 20,000 PSI pressure transdcuer and i relief the pressure from full scale my PSI reading goes to 0123 which is not right. If i reset my arduino the PSI reading reads the correct PSI which is zero. Any ideas? I hope this makes sense.

There's no count reading in your current code. Please post an updated code (and use code tags for that).

To post code and/or error messages:

  1. Use CTRL-T in the Arduino IDE to autoformat your complete code.
  2. Paste the complete autoformatted code between code tags (the </> button)
    so that we can easily see and deal with your code.
  3. Paste the complete error message between code tags (the </> button)
    so that we can easily see and deal with your messages.

Before posting again, you should read the three locked topics at the top of the Programming Questions forum, and any links to which these posts point.

If your project involves wiring, please provide a schematic and/or a wiring diagram and/or a photograph of the wiring.

Good Luck!

I am sorry. Not count..... i meant bit. So i am going to try to make better sense of this. I use the bit count of the ADC then change that to PSI. My code still from up there. The problem is when i come down from lets say a high pressure of 15,000 PSI for some reason something messes up and gives 0102 (This is with zero PSI on the transducer). Now the transducer is calibrated and is a high accuracy .1%. So i know its not that.

Let’s back up a little.
Fix the code first than work in wiring next.
1, what is the expected output from ads1115.readADC_SingleEnded(0)?
Likes 124 to 3 milions? Or - 345 to 1,2 ?
Just the numbers in human format.
2. how do these numbers relate to psi ?
124 = 19.4 psi 623 = 1500 psi?
and how are these numbers related to the formula?
3. what are the magic numbers in the formula?
Some conversion constants? Not that it matters, just curious.

What I am asking is - define this “bit count” we already now the objective is to convert it to psi.
So “1000” = 1 psi ?
10000 = 10 psi?
Just guessing, but The formula does not follow anything like that.

“I use the bit count of the ADC then change that to PSI.”

To eliminate “overflow” use floats in the formula calculation.
The computer will process the code irregardless if the formula is for real or just magic numbers.

You have been advised by couple of posts how to emulate the function so do the emulation first and when it does what is expected just plug it in - cut and paste - to your code.

void loop() {
  static long currentSample; // current sensor sample.
  static long currentValue;  // current sensor value.

  // current value works as a sum counter.
  currentValue = 0;

  // get sensor samples with delay and calculate the sum.
  for (int i = 0; i < 10; i++) {
    // get sensor sample.
    currentSample = ads1115.readADC_SingleEnded(0) ;

    // add sample to the sum counter.
    currentValue += currentSample;

    // delay some time for the next sample.
    delay(100);
  }

  // get the average sensor value (ignore the fraction).
  lcd.setCursor(0, 0);
  lcd.print(((currentValue / 10) - (5339)) * 20000 / 21350);

}

5339 is zero PSI
20,000 is max pressure
21350 is my span
to help explain my formula.

OK, is this what you are after? The analog (in V) level from your sensor will be converted to digital value (int) ranging from 0 to 1023 - that is the API / hardware spec on analog pin.. The range 0 to 1023 corresponds to your 5339 to 21350 - psi range from 0 to 20000. The range 0 to 20000 is mathematically debatable. It should be 5339 to 25339 to have such range if it is linear. But that is OT for now.

Map function will calculate / compare these two ranges so your "val" input (range 0 to 1023) will give you corresponding output "val" (psi) using range from 5339 to 21350

void setup() {}

void loop() { int val = analogRead(0); analog input from sensor val = map(val, 0, 1023, 5339 , 21350); map function outputs psi analogWrite(9, val); process new val from map function }

Julyjim thanks for the suggestion. If you dont mind i would like to ask a few questions. First will the map function fix my problem of not being able to read psi on the way back down. Next i am very new to the map fuction. I see you put an analogwrite( in there. Does that work in a way that it automatically goes to my lcd screen? You also have it compared to the arduino 10 bit read. Why is this i am using the ASD1115. I would have thought it would look something like this

val = map(val, 5339, 26689, 0, 20000); // 5339 represents my zero psi bit, 26689 is my 20,000 psi bits, 0 for zero psi, 20000 for 20000 psi

I know this is a lot to answer on one post. It is just questions i have. Once again thank you. This is helping me learn.

Yes, mapping using your numbers will actually work better - you get the 0 to 20000 result. To be honest , best way to prove it is just to write small emulation program. Try to write is yourself and if you get stuck let the forum know. Jim

Here is my test code , you will note that as coded the value is not what one would expect. There was a heated discussion why it works that way, I am not sure you and I want to get into the implementation of map function to answer that question. So if map(val, 0, 20000, 5338 , 26689); will work for you , let's call it good. If not will work on that later. Jim

void setup() { // put your setup code here, to run once: Serial.begin(115200); for (int val = 0; val != 20000; val += 2000) { int value = map(val,5339, 26689,0,20000 ); Serial.print("input value = "); Serial.println(val); Serial.print("output value = "); Serial.println(abs(value)); //delay(500); } while (1); }

void loop() { // put your main code here, to run repeatedly: }