CNY70 photo sensor too sensitive, too many alternating readings...

Hi,

So I have connected a CNY70 photo sensor and a resistor to a LM339N comparator.

Whenever the voltage across the CNY70 exceeds the voltage across the resistor, the comparator sends out a HIGH, else a LOW.

The problem that I'm having is, that when I move VERY SLOWLY (but constantly in one and the same direction) over the threshold voltage, I get lots of alternating HIGHs and LOWs ...

Is this bouncing? How can I avoid this without using delays ???

Thanks,
Joël

you can add a feedback resistor to the comparator to add hysterisis, shifting the reference voltage depending on the state of the output.

Thanks, I will read into that.

You need a divider to set the reference voltage, like this:

R2/R3 set the reference point, and the much larger resistor R4 provides a bit of hysteresis.
Note that a comparator, like any other logic chip, requires 100nF ceramic decoupling capacitor right
close to the supply pin.

A stiff pullup of a few k ohms provides faster response than built-in pullups should this matter
(normally it won't and internal pullup is sufficient, but note that its only the pullup that drives
the feedback - Rpullup is a good idea so you can test the circuit without the Arduino connected)

Almost all comparators have open-collector/open-drain outputs, so they can be paralleled for
the window comparator circuit (and so they can have different supply voltage), hence the need for pullups on the outputs.

Thanks Mark. It works. Now, I'll have to figure out why :grinning:

btw, why does the cap have to be ceramic, and not electrolytic for example?

Joël

Well, it doesn't seem to work when I try it on my own build.

Here's a drawing of it:

The CNY70 is represented by the photocell and the LM339N comparator is represented by the L293D IC, as I didn't find the correct parts in the Fritzing app.

So, in my project, the 2 potentiometer set 2 reference voltages (e.g. 500 Volt for potentiometer at the top, which is connected to negative input 1 and 1500 Volt for potentiometer at the bottom, which is connected to negative input 2). The CNY70 connects to pos. input 1 and pos input 2. Output 1 of the comparator then goes to digital pin 2 and output 3 goes to pin 3.

Here's the code

unsigned long start, finished=0, elapsed;
bool sema1,sema2=false;
unsigned long vitesse;
 
 
void setup() {
  Serial.begin(115200);
  pinMode(2, INPUT); // start INPUT
  pinMode(3, INPUT); // stop INPUT
}



void loop() {
  
  if (digitalRead(2)==LOW && sema1==false) {
    start=millis();
    Serial.println("start>>>>");
    sema1=true;
  }  
  
  if (digitalRead(2)==HIGH && sema1==true) {
    sema1=false; 
    
  }
  
  if (digitalRead(3)==LOW && sema2==false) {
    finished=millis();
    Serial.println("<<<<finish");  
    sema2=true;
  }
  
  if (digitalRead(3)==HIGH && sema2==true) {
    sema2=false;
  }

}

I want to measure the velocity between the 2 thresholds.

Here's the results I'm getting when I approach my finger to the CNY70:

start>>>>
start>>>>
start>>>>
start>>>>
start>>>>
start>>>>
start>>>>
start>>>>
<<<<finish
<<<<finish
<<<<finish
<<<<finish
start>>>>
start>>>>
start>>>>
start>>>>
start>>>>
start>>>>
start>>>>
start>>>>
start>>>>
start>>>>
start>>>>
start>>>>
<<<<finish
<<<<finish
<<<<finish
<<<<finish
<<<<finish
start>>>>
start>>>>
start>>>>
start>>>>
start>>>>
start>>>>
start>>>>
start>>>>
start>>>>
start>>>>

The problem: Too many consecutive starts and finishes.... I need only 1 of each, whenever I pass the thresholds!
And I don't want to use delays to achieve this!

Thanks,
Joël

Can it be resolved with hardware, or is it a software problem?

maybe you should just put the sensor in series with a resistor and do an analogRead(). you can have separate thresholds for on and off

Can someone please check if I have done the circuit right ?