Having problems with CNY70 sensor.

Good evening

I am working on a project that needs to use infrared sensors, so I bought a pair of CNY70 sensors. I am pretty sure the wiring is right and the sensor has not gotten broken. I have used two different codes to try and use the sensor. I have been using a LCD screen for diagnosis and see what is wrong.

#include <LiquidCrystal.h>

int pinReceptor = A0; //Establecemos el pin a leer
int sensorVal; //Declaramos una variable para almacenar el valor de la lectura
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup(){
Serial.begin(9600); // Abrimos comunicación Serial
lcd.begin(16, 2);
}
 
void loop(){
sensorVal = analogRead(pinReceptor); //Guardamos la lectura del pin Analógico
Serial.println(sensorVal); //Sacamos la lectura por serial
delay(200);  //Pequeña pausa de medio segundo
if (sensorVal = 1)
{
  // Print a message to the LCD.
  lcd.print("1");
  
  delay(300);
  lcd.clear();

}
if (sensorVal = 0)
  {
  // Print a message to the LCD.
  lcd.print("0");

  delay(300);
  lcd.clear();

  }
}

This one just prints a 1 in an endless loop, regardless if there is something in front of the sensor or not.

#include <LiquidCrystal.h>

int stat;
int pinCNY70 = A0;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup(){
lcd.begin(16, 2);

    pinMode(pinCNY70, INPUT);
    Serial.begin(9600);

}
void loop(){

digitalRead(pinCNY70);
    {
 if (stat = 1);
  {
 lcd.print("1");
  
  delay(300);
  lcd.clear();
         }
    }
if (stat = 0);
    {
   lcd.print("0");
  
  delay(300);
  lcd.clear();
    }
}

This one prints 0 and 1 in an endless loop, regardless if there is something infront of the sensor or not. (0,1,0,1,0,1...)

I am starting to get desperate, I literally do not understand what is wrong and how I can get this sensor to work. Can anyone help?

if (sensorVal = 1)

isn't doing what you think it is. In C and C++ the operator that tests for equality is ==.

So you need to put

if (sensorVal ==1) etc

This is still a bad way to test an analog value. Analog values can be anywhere 0-1023 so very seldom will be exactly 1.

Something like this will work better

if (sensorVal > threshold)

You need to work out an appropriate value for threshold.

You have a Serial.println(sensorVal) statement in the first example. What values do you see in the serial monitor?

Hi,
Welcome to the forum.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.... :slight_smile:

GypsumFantastic:
if (sensorVal = 1)

isn't doing what you think it is. In C and C++ the operator that tests for equality is ==.

So you need to put

if (sensorVal ==1) etc

This is still a bad way to test an analog value. Analog values can be anywhere 0-1023 so very seldom will be exactly 1.

Something like this will work better

if (sensorVal > threshold)

You need to work out an appropriate value for threshold.

You have a Serial.println(sensorVal) statement in the first example. What values do you see in the serial monitor?

I see... I was warned about that == thing happening, but I never realized.
About the serial printIn, I really do not know what that does. But I instead get the values on the LCD board, and I stated above what was the result.

What do you mean by threshold?

TomGeorge:
Hi,
Welcome to the forum.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.... :slight_smile:

I tried...

Hi,
OPs circuit.

Sorry that is not a schematic.
Please draw one free hand and post a picture, labeling pins please.

This what your sensor really looks like.
CNY-70.jpg

Tom... :slight_smile:
If you use REPLY rather than Quick Reply ,you will find an attachment facility.

McFlyGuy:
About the serial printIn, I really do not know what that does.

What do you mean by threshold?

Serial.println(sensorVal) sends the number, sensorVal to your computer. If you open serial monitor (in the Tools menu of the IDE) you'll see the number. It's useful for debugging and problem-solving.

Threshold is a number that you choose. Once you've watched the serial monitor for a while, and seen how the number changes when something is in front of the sensor, you'll be able to choose the correct value.

For example, if sensorVal was about 200 when there's something in front of it, and about 600 with nothing in front of it, you would probably put threshold = 400

Hi,
If the OP looks at his fritzy diagram, he has not got any 5V bias on the transistor so will only get 0 analog input.
A suggested circuit is:

Tom... :slight_smile:

GypsumFantastic:
Serial.println(sensorVal) sends the number, sensorVal to your computer. If you open serial monitor (in the Tools menu of the IDE) you'll see the number. It's useful for debugging and problem-solving.

Threshold is a number that you choose. Once you've watched the serial monitor for a while, and seen how the number changes when something is in front of the sensor, you'll be able to choose the correct value.

For example, if sensorVal was about 200 when there's something in front of it, and about 600 with nothing in front of it, you would probably put threshold = 400

So I tried to use the Serial Monitor to see what I could do, and the numbers were around 960, but they suddenly went to 1023, then went back to 960. What makes it even werider is that if I disconnect the sensor, the numbers still reach 1023 for some reason...

That tells us (assuming you haven't changed your code) that something is wrong with the hardware.

Either the components aren't wired up properly, or there's a hardware problem with the arduino.

Unconnected analog inputs can float about strangely but it's a unusual for them to float up to 1023 and stay there, I think.