Low sensor value giving adjusted value < 0

I am using an Arduino Giga, and I wasn't sure whether this should go on the Giga section of the forum or this one, but I figured that I was playing with setting up sensors and this deals with sensor values.

I am working on coding my Arduino, and I am going to be using a 5 PSI pressure sensor. It runs from 0 PSI at 0.5 V to 5 PSI at 4.5 V. I am planning to use a logic level converter to drop this down to 3.3V for the Giga to read.

I was playing around to see how long it will take the Giga to run a float calculation using an equation (if you're wondering, it came out to anywhere from 22–211 microseconds, though I would estimate most came back around 50 microseconds, and I am appreciating the faster processor so that it doesn't take milliseconds), and I was using an empty pin because I will be using a pin to read sensor data anyways, using 102.3 as the low and 920.7 as the high. For kicks, I printed the output of the calculation, and I got a value other than 0.

I was also playing around with setting up the USB as a mass storage device, and one of the tutorials that I found is a data logger and it does an analogue read of an analogue pin. I was also playing around with using the WiFi to set the internal time and create file names using another set of code. I didn't think much of it at the time, but I didn't delete the logs and ended up with 900 data points.

When the data logging first started for each log, the value for the empty pin initially read an average of ~142 (range 139–148), then dropped to an average of ~89 (range 83–95) for the second read, and then to an average of ~65 (range 63–73) for the subsequent reads. The data logger code reads the value of the pin every 10 milliseconds, and it doesn't use pinMode to identify the pin as an input. These frequent reads are about 2/3 of the value that I was expecting.

I increased the read intervals to 100 milliseconds, I am now getting a value of between 1–3 from my conversion equation (the 1 is not unexpected, as I have a round() function as part of the equation), so I am wondering if my analogue minimum value is still slightly off.

This might just be the margin for error in the calculations, but does anybody have any ideas about what is going on here?

The formula is part of my research, so I don't want to share it just yet.I am more interested in knowing what is going on with the analogRead() values being this low and if I should be concerned than I am about whether or not I am doing the math right.

They might have, if you were to post code that demonstrates the problem (using code tags), example output, and explain what you have done to debug the problem. Also post a link to the sensor product page or data sheet.

Secret formulas are not welcomed by open source forum members, so if you can't share those details, you should not expect much help.

However, judging from the post title, if valid sensor measurements go through a black box and nonsense comes out, the black box is the very first suspect.

Using a digital converter to adjust an analog signal does not compute especially when two resistors would do it. If you need more help post an annotated schematic with links to technical information on all the hardware devices.

1 Like

That won't work, a logic level converter is a digital device, you need a voltage divider (analog).
An R1 of 3.9k and R2 of 7.5k would give you 3.29V out for 5V in.

It sounds like you first need to learn how to make valid measurements with the sensor. If you post a link to it, people can help. No secret formula need be disclosed for that.

This precision Sparkfun micropressure sensor has extremely wide absolute pressure range and designed to directly interface with 3.3V microprocessors.

They might have, if you were to post code that demonstrates the problem (using code tags), example output, and explain what you have done to debug the problem.

This is the code that I was using (minus the formula for what I am actually calculating).

  long startTime=micros();
  for(int i=0; i<50; i++) {
  int valueCalculated = round((float)[insert equation here]);
  Serial.print("time to calculate with analogRead: ");
  Serial.println(micros()-startTime);
  Serial.print("value calculated: ");
  Serial.println(valueCalculated);
  Serial.println("");

  delay(500);

Also post a link to the sensor product page or data sheet.

As I explained, I was reading a randomly selected unused pin using an example provided by Arduino for setting up a data logger based on reading an unused pin (the link was provided in the post). I was assuming that it would be a meaningful value, even if it wasn't getting direct sensor input.

Secret formulas are not welcomed by open source forum members, so if you can't share those details, you should not expect much help.

I'm planning to upload the code to GitHub for anyone to use once I have it finished, but it's part of my PhD research, which is why I can't share it until it's done—unfortunately, that's often how it goes with research.

The code you posted gives no insight into the problem you are having, since it doesn't read or input anything.

Post that code, minus the mystery equation.

In any case, you don't need a sensor or random input from a pin to test the math.

It is perfectly acceptable to simulate valid sensor measurements using the Arduino random() function. For example, use this to simulate valid 10 bit ADC readings:

data = random(0, 1024);

Okay, good to know. I was reading something else at the time that had suggested that it would work, but it doesn't appear that it's the best option. I'm going to use it for running some digital stuff like MOSFETS that are on/off, but I also need to send some signals from 3.3V up to 5 V, and resistors won't work for that.

Much appreciated, thanks. I was reading something else at the time that had suggested that it would work, but I looked into it further and it doesn't appear that it's the best option. I'm going to use it for running some MOSFETS for simple on/off components as well, but I also need to send some signals from 3.3V up to 5 V, and resistors won't work for that. Any suggestions for that?

It sounds like you first need to learn how to make valid measurements with the sensor.

As I mentioned, I am not reading from a sensor at this time, just a random, empty pin that doesn't have anything connected to it. So I don't have a link to share other than the Arduino Giga datasheet.

This precision Sparkfun micropressure sensor has extremely wide absolute pressure range and designed to directly interface with 3.3V microprocessors.

I appreciate the suggestions, but I'm going to be measuring the pressure of liquids in tubing, so I don't think that I can use the one from Sparkfun.

Like I said, I am doing an analogRead() on an empty pin, in this case A0, and using that to calculate a result. The analogRead is part of the int valueCalculated part (see update below).

  long startTime=micros();
  for(int i=0; i<50; i++) {
  int valueCalculated = round((float)analogRead(A0)[insert equation here);
  Serial.print("time to calculate with analogRead: ");
  Serial.println(micros()-startTime);
  Serial.print("Value calculated: ");
  Serial.println(valueCalculated);
  Serial.println("");

The numbers that I am asking specifically are just int value = analogRead(A0); though.

It is perfectly acceptable to simulate valid sensor measurements using the Arduino random() function.

I do appreciate the suggestion about using random(), but it's not helpful for what I am trying to figure out. The example from Arduino for creating a data logger uses an analogue read of A0, so I happened to have the data on hand from testing other code. When my results came back looking strange after seeing how quickly the equation could be solved with floats and an analogRead, I opted to go back and look at that data because I had thought when I first saw the output from the data logger example that it was strange that the reads of the pin had been low and I wondered if it might explain why my result was higher than expected (I was using the wrong low value for 0.33 V, and fixing that addressed most of the error that I had).

I'm trying to figure out if I need to adjust what my low is based on these low numbers from the data so that I get back a value of 0, or if they are artificially low as a result of reading the values from the pin in quick succession.

0.5volt to 4.5volt sounds like a 5volt-only ratiometric sensor. I would hesitate to use a 3.3volt MCU with that type of sensor. Your result will not only depend on pressure, but also on supply variations of the sensor and VCC of the processor. A $3 Nano clone could give you a more stable result here.
Try to find a sensor with a digital output (I2C). That would be a better match for a Giga.
Leo..

To clarify, I was looking at using this level shifter from Adafruit. The way that it is written, it sounded like it would work, but I guess that it's more for digital sensors:

Because the Arduino (and Basic Stamp) are 5V devices, and most modern sensors, displays, flash cards and modes are 3.3V-only, many makers find that they need to perform level shifting/conversion to protect the 3.3V device from 5V.

Although one can use resistors to make a divider, for high-speed transfers, the resistors can add a lot of slew and cause havoc that is tough to debug.

That's for digital signals only. 0.5-4.5volt indicates an analogue signal.
Leo..

1 Like

Yes, and I realize that now. I'm looking at setting up some voltage dividers instead.

That doesn't fix the fact that the output of the (ratiometric) sensor is now depending on three variables. A standard 10%-90% (0.5-4.5) sensor is designed to work with a 5volt processor with 5volt (VCC) Aref. You're trying to put diesel fuel in a petrol car. Expensive boards need expensive (digital) sensors. Good luck...
Leo..

That was quite clear. You don't seem to be paying much attention to any of the replies, so have fun and good luck!

You can achieve this by using a CMOS HCT gate. These gates typically have a 2V threshold when operating at 5V.

1 Like

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