analogRead returns analog values, but digitalRead only returns 0

I'm trying to use analog inputs to read digital values, but I'm obviously doing something wrong. I'm using some optical sensors that range from about 25 to 230 when I use analogRead(). However, when I use digitalRead() on the same inputs, I only get a return of 0 and never 1. I've tried labeling my analog inputs as A0-A5 as well as 14-19, but it doesn't seem to make a difference.

I'm guessing this is a simple fix, but I haven't been able to find anything on this issue. Any advice is much appreciated.

Post your code to allow us to see what you are doing.

Have you used pinMode(pin, INPUT); or pinMode(pin, INPUT_PULLUP); which is not needed for analogRead()

…R

An analogRead of 230 would surely be 0, since it's out of 1023.... perhaps you're thinking that they're out of 255 which would be a 1 but 230/1023 won't be.

Well it’s a pretty long code so I’ll just include the part I’m referring to.

#define lowLom2 A0

void setup()
{

pinMode (lowLom2, INPUT);

}

void loop()
{
Serial.println(digitalRead(lowLom2));
}

If you want the whole thing, I’ve added it as an attachment.

JimboZA: I definitely was thinking it was out of 255. When does analog go from 0 to 255 and when does it go from 0 to 1023?

Beta_Valve_test_141118.ino (14 KB)

An analogRead is 0-1023, and an analogWrite is 0-255.

Well...that's embarrassing.

So I guess now my problem is that my sensor isn't reading as high a value as I wanted. Is it possible to get a digital 1 from an analog value of around 230?

Perhaps there's some electronics that could step it up, but why don't you just read it as analog, and if it's over (say) 200 use an "if" to do something and under (say) 50 have an "else if" doing some other thing, with a no-mans-land between those two values?

theD_rob: So I guess now my problem is that my sensor isn't reading as high a value as I wanted. Is it possible to get a digital 1 from an analog value of around 230?

why do you need to. What's wrong with if ((analogRead(whatever)>100) ?

Well that was my initial thought, but using digital is just cleaner in my opinion. I also spent several hours adding pull-up resistors to make all of my analog sensors act as digital, and my pride wanted that to be justified. Another reason is that I will eventually need the analog inputs for other devices, so I'll have to switch to digital pins. For now, I can probably get by using analog.

theD_rob: So I guess now my problem is that my sensor isn't reading as high a value as I wanted. Is it possible to get a digital 1 from an analog value of around 230?

That is not really the correct question. What you really need to know is what values should register a 0 and what values should register a 1. And analogRead() may be the only way to make that distinction.

Have a look at the analog comparator in the Atmel datasheet for your MCU. It may do what you want, but I don't think there is a standard Arduino function to use it.

...R

What sensor is it? Do you have a link to it's datasheet.

This is the data sheet for my sensor:

http://pdf1.alldatasheet.com/datasheet-pdf/view/282900/SENSORTECHNICS/OLT01T0K3.html

I know I would like any analog value above 150 to be a 1, but I don't know how to make that correspond to a digital pin on my uno. I think .7V is the cutoff, but I'm not sure if that can be changed. I don't even know if I'm thinking about it correctly.

Which version of the sensor do you have? There are three of which one is already TTL compatible. Another is push pull which is essentially CMOS. Both of those two should work directly connected to a digital input.

The other one is a photo transistor which should basically only require a pull up resistor to VCC to be compatible with a digital pin (it is essentially open drain).

theD_rob: I think .7V is the cutoff, but I'm not sure if that can be changed. I don't even know if I'm thinking about it correctly.

You can't change the thresholds for digital 1s and 0s.

Have you read up about the analog comparator?

...R

Ideally, you would use digital inputs to monitor these devices (since they are liquid level "switches").

For the OLT01T0K3, try this connection scheme:

  • The output should be around 0.5V when LOW and 4V when HIGH.
  • If signal levels (voltages) are not satisfactory, change the 10K resistors to a lower value.
  • For example, with 1K resistors, the output will consume ±5mA drive current.

Just realized I never responded to this. If any of you ever see this, thanks for the help; I got it working.