digitalRead set threshold

Hello,
I need to react on a signal. I assumed I could just read it via digitalRead() and use this as a boolean.
However when I write something like that:

Serial.print("Digital Read 1: ");
Serial.print(digitalRead(PIN));
Serial.println(analogRead(PIN));

// enable the pin

Serial.print("Digital Read 2: ");
Serial.print(digitalRead(PIN));
Serial.println(analogRead(PIN));

I see other results than I expected.

I can read something like that:

Digital Read 1: 0 20
Digital Read 2: 0 750

where both values are read as 0.

The question is now, can I set a certain threshold for the digital read function?

E.g. everything above 500 should be a 1 or so?

Thanks!

No that is not possible.

Why would you want to?

Try something like this

bool digitalAnalogRead( uint8_t pin )
{
	return analogRead( pin ) > 500;
}

For the standard Arduino with the ATmega chip -

Low = 1.5V or less (0.2Vcc)
High = 3V or more (0.6Vcc)

In-between is undefined and might read high or low.

And in case you don't know this, unconnected input pins are "floating" and they can (theoretically) float to any voltage and they can read high or low. In my limited experiments with analogRead() the voltage tended to float toward the middle and drift somewhere around about 500. But you can't count-on anything with a floating input.

In the analog world comparators do a great job at this. Simply feed a D/A or fix the refereed to voltage to the level you want to test, then a simple digital read give the answer.

Thanks for all the quick replies.

The issue is, I'm using one of these Arduino Soundboards

and there is a pin that goes low while a sound is played and "high" when it is finished.

The documentation states:

Act - this is the Activity pin, which goes low when an audio file is played

Sadly though the "high" value is at around 800 when reading analog (and thus still a 0).

But the suggested solution like

analogRead( pin ) > 500

seems fine to me. I just wondered whether I made something false since I understood the documentation like I could use a digitalRead function.

That makes little sense.

How are you powering this audio board? It should be with the same voltage as your Arduino is running at. If it is not then you need a logic level shifter to get your signal high enough to register.

The code in your first post is wrong. While an analogRead will, set up that pin to read the analogue port, it is not the same with a digitalRead. If you want to do is properly you will have to use a pinMode call before each digitalRead to make that pin a digital input. Then you will find you are not reading zero, like you think you are.

What is your arduino board?

If you want to do is properly you will have to use a pinMode call before each digitalRead to make that pin a digital input. Then you will find you are not reading zero, like you think you are.

So this is what I forgot?

What is your arduino board?

It's actually a XIAO nRF52840 but they are compatible with the arduino stuff (mostly). I plugged the chip to the USB power but the other devices are hanging on the 3V output from the nRF52840.

Why do you think I told you about it?

Drip drip feed of information is never a good idea, we can only know what you tell us.

Anyway it seems you have marked an answer as solving the problem so it makes little sense trying to help you get it right.

Default analogRead() resolution for your board is 10bit (0-4095 range). So if your code returns 750 from analog reading - it corresponds to LOW level and digitaRead() MUST returns zero!

wrong

10-bit is 0-1023 range. 12 bit would be 0-4095 range.

oops :)))))

For ATmega328P MCU of UNO Baord, the parameters are:
portIOElectCharac-2