IR sensor project just reading nonsense :/

I can’t figure out what I’ve done wrong here. It’s my first time touching an arduino in nearly a decade so please be go gently on me XD

All I’m wanting is to be able to read from my IR sensor (VS1838B) a simple boolean value of whether it detects IR being shone at it or not. Ideally detecting when IR starts and when it stops.

I think what I’ve written makes sense, and I think my circuit makes sense too.

I also have an IR LED hooked to PIN 13 with the basic Blink code running on it, just for some lazy testing (though I’ve tried jabbing it with my TV remote too).

The plan is to ultimately use this as a counter, when something interupts the IR light, count++.

Anywho, all I’m getting is constant HIGH reads, or constant LOW reads, regardless of whether there is actually any IR being emitted (I can literally just unplug the LED and it still just reports randomness), I never see any of my RISING or FALLING being triggered either.

Here’s my code:

#define SENSOR_PIN 12

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(SENSOR_PIN, INPUT);
  Serial.begin(9600);
  attachInterrupt(digitalPinToInterrupt(SENSOR_PIN), irDetected, RISING);
  attachInterrupt(digitalPinToInterrupt(SENSOR_PIN), irLost, FALLING);
  Serial.println(digitalRead(SENSOR_PIN));
}

void irDetected() {
  Serial.println("Detected");
}

void irLost() {
  Serial.println("Lost");
}

// the loop function runs over and over again forever
void loop() {
  Serial.println(digitalRead(SENSOR_PIN));
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  Serial.println(digitalRead(SENSOR_PIN));
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
}

I’ve attached images of the setup itself. It really is extremely simple. LED wired directly to pin 13 and ground. The IR sensor has 5v directly to the right pin (when looking at the sensor side), middle to ground, left to pin 12.

What silly mistake am I making? :slight_smile:




Here's a screenshot of my console... terminal... errr... serial... thingy!

I believe that that IR senor is to receive modulated (38KHz) signals from a remote. It will not work with an unmodulated signal. There are IR detectors made for light gate use. Most of them will use modulated IR, though, to discriminate from ambient IR.

Interesting... well I've used this same LED to control a robot hoover before in another project using a RaspberryPi, so, could I just create one long, never ending, modulating signal and have the Arduino watch out for it? :slight_smile:

I'm in a zero budget make the thing work kind of mood :slight_smile:

Other options, I have a bunch of tiny strong magnets that I could use instead (I'm counting rotations of a drill), but I don't think I have any kind of magnet detecting sensor thingy... I do have a couple of those little switchy things you get on 3D printers and behind bumpers on robot hoovers that you maybe shouldn't have tried hacking...

could I just create one long, never ending, modulating signal and have the Arduino watch out for it?

I just tried that with an IR decoder that I have that is probably very like the VS1838B that you have. I just get solid 1’s when the IR is blocked or not.

My test code:

const byte IRinPin = 4;
const byte IRledPin = 7;

void setup()
{
   Serial.begin(115200);
   tone(IRledPin,38000);  // constant 38KHz signal to LED
}

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 100;
   if (millis() - timer >= interval)
   {
      timer = millis();
      Serial.println(digitalRead(IRinPin));
   }
}

couple of those little switchy things

Post a photo, I don’t know what you mean, exactly.
How to post images so we don’t have to download them and everyone can see them.
How to post an image.
Another page on posting images.

And that's me found a solution: Detecting an IR Beam Break with the Arduino IR Library

Thank you muchly for your help in making me understand that this wasn't just a detector of any old IR but specifically IR remote signals :slight_smile:

And for anyone else wondering, "light gate" was a very useful keyword I didn't know :slight_smile: