EEVblog Electronics Community Forum

Electronics => Beginners => Topic started by: jbs on May 17, 2020, 09:55:08 pm

Title: Help: CNY70 photo sensor as a binary switch
Post by: jbs on May 17, 2020, 09:55:08 pm
Hi,

I'm trying to use a CNY70 as a binary switch. I have it connected to a LM339N comparator. The reference voltage (negative input) is given by a potentiometer. The output of the comparator is connected to an Arduino UNO.

What I want is for the CNY70 to give out HIGH whenever its output voltage surpasses the threshold reference voltage set by the potentiometer, and LOW whenever it goes below.

However the output fluctuates too much. At the threshold I get crazy readings on the Arduino of 0's and 1's before it settles.

How can I avoid this?

Thanks,
Joël
Title: Re: Help: CNY70 photo sensor as a binary switch
Post by: StillTrying on May 17, 2020, 11:31:47 pm
You'll have to add a diagram of how the CNY70 and LM339N are connected including the supply voltages.
Title: Re: Help: CNY70 photo sensor as a binary switch
Post by: Doctorandus_P on May 17, 2020, 11:56:53 pm
Use a schmitt trigger.

It's probably as simple as a resistor between the output of the LM339 and it's non-inverting input.
https://nl.wikipedia.org/wiki/Schmitt-trigger (https://nl.wikipedia.org/wiki/Schmitt-trigger)

Or, better: do it in software.
contact bounce is very common for mechanical switches, and usually it's taken care of by reading the input at regular intervals (for example between 1 to 10ms) and only accept an input change when multiple consecutive logic levels have the same value.
"State" is a uint8_t counter variable.
SWITCH_HIGH and SWITCH_LOW are defined constants. when "State" becomes equal to one of these, then the state of the switch has changed.

Code: [Select]
void TThreadSwitch::Debounce( void) {
// Debounce for the switch.
if( SWITCH_PIN & SWITCH_BIT) {
if( State < SWITCH_HIGH) {
++State;
}
}
else {
if( State > SWITCH_LOW) {
--State;
}
}
}
Title: Re: Help: CNY70 photo sensor as a binary switch
Post by: jbs on May 18, 2020, 09:17:10 pm
This is extremely helpful, Doctorandus. Both of your ideas.  :-+

Joël