Wiring and coding 5V IR Photoelectric Switch

Hello,

I've purchased this 5V IR Photoelectric Switch https://thepihut.com/products/5v-ir-photoelectric-switch-4m
I'm not sure how to install it correctly.

I would like to connect it to an Arduino in order to detect an object within the range of 0-25 cm.

Would someone be able to advice on how to do this?

Any help is much appreciated! Thank you

Have you tried asking The Pi Hut - they are pretty helpful:

Reading and understanding the data sheet helps from time to time.

1 Like

The first step might be finding the required data.

@fishmarcel: First click "+ View more" on the site, then scroll down to the cable colors.

It's not very helpful:

image

Very disappointing - I'd expect better from The Pi Hut.

:frowning_face:

What are you missing?

The green & blue wires are just marked as "floating" - but they must, surely, have a use?

That's not very clear, but "input" and "output" would not be much more clear. The signal connections are described in the library documentation.

Indeed not.

there should be a proper an complete description of what, exactly the wires do.

EDIT

I don't see any library linked from that page.

Next time search for a data sheet and Arduino library and example code before buying any module.

Yes, that is what I always say!

I would not buy this module for that very reason

(well, I would contact The Pi Hut first - see #2 - to ask if they did have any support)

Thanks for the suggestions.

I have been trying to find the required data without any luck and I'm still waiting to hear back from Pihut.

Lesson to learn: as @DrDiettrich said, always ensure that products have adequate documentation & support for you to use them before parting with any money!

The required amount of documentation decreases with experience. I'd connect a volt meter or scope to the output and connect the input to + or - PSU and find out what happens w/out an object in the beam.

1 Like

Indeed.

That's why I emphasised, " adequate documentation & support for you"

I managed to figure it out and it now works. This is the code:

const int SensorPin_Transmitter = 2;//Connect the green wire to digital pin 2
const int SensorPin_Receiver = 3;//Connect the blue wire to digital pin 3
const int LedDisp = 13;

void setup()
{
  Serial.begin(9600);
  Serial.println("Start!");
  pinMode(SensorPin_Transmitter,INPUT_PULLUP);
  pinMode(SensorPin_Receiver,INPUT);
  pinMode(LedDisp,OUTPUT);
  digitalWrite(LedDisp,LOW);
}

void loop()
{
  if(digitalRead(SensorPin_Receiver) == LOW)  digitalWrite(LedDisp,HIGH);

  else  digitalWrite(LedDisp,LOW);
  Serial.print("Status:");

  delay(50);
}

Does that pin actually need a pullup? Does it work if you just leave it unconnected?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.