My IR sensor shows only '0' value when connected

I have a ESP8266 nodeMCU which is connected to a IR sensor at pin D2,
I do not get any error while compiling just it does not change its value when any object is detected, The serial moniter just shows the value 0 and builtin led is always at high. But the ir builtin led changes its state when objevt is detected.

Below is my code for the same:

// C++ code
//
int buttonState = 0;
//int freq = 100;


void setup()
{
  pinMode(2, INPUT);
  //pinMode(12, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(115200);
}

void loop()
{
  // read the state of the pushbutton
  buttonState = digitalRead(2);
  Serial.print(buttonState);
  // check if pushbutton is pressed. if it is, the
  // button state is HIGH
  if (buttonState == HIGH) {
    Serial.print(buttonState);
    digitalWrite(LED_BUILTIN, HIGH);
    //tone(12,freq);
  } else {
    Serial.print(buttonState);
    digitalWrite(LED_BUILTIN, LOW);
    //noTone(12);
  }
  // Delay a little bit to improve simulation performance
  delay(10);
}

And when I change this statement
if (buttonState == HIGH) to if (buttonState == LOW) serial moniter shows value 001100110011......

Show us how you wired your switch to the controller ?

Try wiring it similar to S2 below:

Your ESP8266 will of course be 3V3.

there is no switch its just a ir Sensor. Currently I am on testing phase of my project

What sensor ?

infrared sensor

Give us a link to the sensor.


Show use a good image of your wiring.

For IRsensor first pin is output, second gnd and third is vcc

If this is your sensor, does the output LED toggle with reflection ?

If not, adjust the potentiometer until the sensor starts working . . .

Yes.
As I said earlier builtin led of the sensor does toggle when anything is detected

If you have a DMM, measure the voltage on D2 when the sensor LED is ON and then when it is OFF.

You can also remove the wire on D2 then connect D2 to GND and then 3V3 to manually test D2.

1 Like

I do not have that....

Remove the sensor wire on D2 then connect D2 to GND and then 3V3 to manually test D2.

1 Like

what should I check for??

When the test wire connects to GND, your code should detect a 0 (LOW).

When the test wire connects to 3V3, your code should detect a 1 (HIGH).

issue is still the same

Try a different controller pin.

When I connected the pin to D0 instead of D2 then the if-else block is working properly. i.e. builtin led is high when its on gnd and low when its on 3.3v. But the serial moniter show all the value as 111111.....

Confirm what pin ledbuildin is ?

If ledbuildin is not one of these, D5, D6 or D7, try one for manual testing.

Hi,
Is that the code on the code screen in post #16 ?
If so it isn't the code in post #1.

Which code are you running?

If it is post#1 code, then you print the state twice.
So 0011001100110011 means that the input pin is regularly changing from LOW to HIGH.

Can you do @LarryD test again, but put the end of the jumper on the actual input solder joint, and then the other end to 3V3 then gnd solder joints?
That is don't use the protoboard connections.

Tom.. :smiley: :+1: :coffee: :australia:

Ledbuitin does not have any pin . We can call it directly using LED_BUILTIN