Digital Pin - High & Low Voltages

According to the documentation a digital pin reads high when the voltage is at least 3V for a 5V board. It reads low when the voltage is less than 1.5V. What happens for voltages between 1.5V and 3V? Do you just get random results back?

As background information, I need to check if there is a 20VAC signal or just a 1VAC signal. I am planning to do this by using a diode, capacitor and voltage divider using resistors to get down to 5V (when the 20VAC is on). However, the capacitor needs some time to drop back down to ~0V so the voltage will be between that 1.5V and 3V for some time.

Assuming a digital imput, the reading will be the same as long as you are under the threshold for a logic 1.

// Per.

The result could be a 1or a zero - the output for the intermediate voltages is not defined.
Use an analog input and in your code look for the voltage to be above or below the values you want to see .

Or a 3-resistor voltage divider connected to an analogue pin, and peak/peak detecting in software.
No delay with that.
The third resistor to VCC (5volt) to bias the pin mid-voltage.

Could also use an opto coupler if grounds can't be shared.
Leo..

If it can only be 1VAC or 20VAC, your only concern would be the transition period - if you don’t need an instant response, you can test how long it will take to get below the low threshold, and require that the signal be low for at least that long before you react to it.

Using the ADC is another approach, and lets you determine the AC voltage, rather than just whether it’s above or below some threshold.

Be careful with how you set up that voltage divider; AC voltages are RMS, not peak voltage, so when you rectify them, you can end up with a higher voltage than you were expecting. If the voltage divider resistors are fairly large (10k+), you’ll be fine - the protection diodes will clamp the voltage at 5v (or 3.3v on a 3.3v board) - but they’re only good to a few mA max.

In between 1.5V and 3V the digital result is quite clearly defined. It will be whatever it was before.

The input must go outside of that range to change the result.

Note that the voltages are not that clearly defined. It depends on supply voltage and a few other factors. But usually you don't need to know that. In this case, yes, all those factors are important. Some cannot be measured. So use an analog input.

@MorganS That actually sounds pretty reasonable. Is there some documentation that describes this?

Wawa mentioned an opto coupler. It turns out that this is actually what I need because I don't have a ground reference for the Arduino. So now I ended up with something like this:

-->|--R1----R2------|
| |----
|--R3--OC--|

| is a diode that cuts out the negative part of the AC
R1/R2 is my voltage divider so that I have 5V over R2 (total over R1 and R2 is approx. 18V, so 15V is over R1)
R3 is just a regular resistor
OC is the input of an opto coupler (Sharp PC817).

I was going to use the other side of the opto coupler as an input the the Arduino. However, it turns out that the resistance of the output of the opto coupler is very dependent of the input voltage. When the input voltage is ~5V the resistance of the output is around 3kOhm. When I increase the voltage to like 15V the resistance drops to like 50 Ohm. Why is this opto coupler so dependent on voltage? Shouldn't it behave like an on/off switch. Go over 5V and resistance is close to 0 but be below ~3V and resistance is close to infinity.

mulu:
Is there some documentation that describes this?

mulu:
According to the documentation a digital pin reads high when the voltage is at least 3V for a 5V board. It reads low when the voltage is less than 1.5V.

That are the GUARANTEED levels. Levels <1.5V are guaranteed low; >3V are guaranteed high. Somewhere in between is the switch point, where the pin changes from one level to the other. There is usually a little hysteresis as well, but again this is not defined, the pins are not Schmitt triggers. There’s a good chance that a 2V level is detected as LOW and a 2.5V as HIGH but it is not guaranteed so you should never design your circuit that way, especially if that are your levels.

I’ve done lots of capacitor charge/discharge timing using digital pins, and experience tells me the pins have a very well defined switch point. When charging a cap, it will go from LOW to HIGH at a the same point every time (the charge times I measure this way are within 0.1% the same). The exact voltage it will switch does differ a bit between devices.

I have never seen any signs that the pin may be switching back and forth in that undefined range. As the voltage drops at some point the pin will switch from HIGH to LOW (or the other way around).

For your voltage divider calculations do remember that 20V AC after rectifying and smoothing gives you close to 30V DC!

mulu:
I was going to use the other side of the opto coupler as an input the the Arduino. However, it turns out that the resistance of the output of the opto coupler is very dependent of the input voltage. When the input voltage is ~5V the resistance of the output is around 3kOhm. When I increase the voltage to like 15V the resistance drops to like 50 Ohm. Why is this opto coupler so dependent on voltage? Shouldn't it behave like an on/off switch. Go over 5V and resistance is close to 0 but be below ~3V and resistance is close to infinity.

Most optocouplers like the one you named are like a bipolar transistor. Thry are CURRENT devices. The Amps output is a function of the Amps input. You cannot measure them with an ohm meter.

If you actually got 5V on the input terminals then you probably destroyed it. The input side is just an LED. You must give it a current-limiting resistor like every other LED.

The output function is a simple ratio. Sometimes this will be in the datasheet as "CTR" or Current Transfer Ratio. It can have other names. But the ratio can change over the life of the device. That change may or may not be in the datasheet. Allow at least 50% loss in your design.

mulu:
According to the documentation a digital pin reads high when the voltage is at least 3V for a 5V board. It reads low when the voltage is less than 1.5V. What happens for voltages between 1.5V and 3V? Do you just get random results back?

Undefined, unless the voltage is slewing rapidly between HIGH and LOW (logic signals are expected to slew at 1V/ns or so).

In practice the logic input may draw more power, or even oscillate, which is why many devices have some hysteresis circuitry on each input (the ATmega microcontrollers do). Whether it reads HIGH or LOW between 1.5V and 3.0V is not defined by the datasheet - you might need to experiment, and the threshold will vary between pins and chips.

The drawing more power thing will sting you if you try to run a microcontroller in sleep mode and expect the power drain to only be a few µA. You must enable pull-ups on all unconnected pins (or make them OUTPUTs)
to get minimum current drain. (Floating inputs often settle in the "forbidden" region).

If you are using pins as analog inputs then you may see the several mA of current draw from the pin's digital input section, a pain for micropower operation. This current is not on the pin, only betwen Vcc and ground.

As background information, I need to check if there is a 20VAC signal or just a 1VAC signal. I am planning to do this by using a diode, capacitor and voltage divider using resistors to get down to 5V (when the 20VAC is on). However, the capacitor needs some time to drop back down to ~0V so the voltage will be between that 1.5V and 3V for some time.