Infrared sensor

Need help setting up a infrared sensor. Not sure if i need to load a library. Just need an example to figure it out. The sensors i have are from amazon they have a ir emitter and receiver.

Hi, try gweimer/ir-remote

Are you just trying to detect if presence of IR signal, or actually transmit data? If you are just trying to detect when their is IR, you might be able to use digital input with button node; more likely you will need an analog input with PWM node and compare to a threshold value to determine if it is “on” or “off”.

If you are trying to transmit data, gweimer/ir-remote will help you read data from the IR receiver (if it is compatible), but will not help with transmitting data. I don’t know if there is anything to help with that. There is another current thread on RF transmitter/receiver code; it might provide a starting point if nothing else is available (unless they are just using an RF-specific library).

What I’m trying to do is use an IR sensor to detect reflected light to tell the difference between say between white and black

There are a lot of different IR sensors out there perhaps a bit more detail on which one you are using? Even the Amazon link would help.

That would be an on/off test. ir-remote code will not help you for this project.

How it gets hooked up depends on the specific sensor. Some have built-in circuit and output a digital signal for on/off; these usually have some sort of adjustment to set sensitivity. This just needs connected to power and a digital pin with a higher-value resistor (maybe 10K) between the digital pin and ground.

If you just have a bare emitter and detector, then you need to provide power to the emitter (probably through a resistor so you don’t burn it out) and wire in the detector to an analog port with a resistor to create a voltage divider, then determine what voltage (analog input level) you need to use to switch between on and off. That value will depend on your collector, the size of resistor used, and how well your surface reflects the different colors you are trying to detect. You can use trial-and-error, or create a temporary patch to watch the output value of a POT node and watch the output as you move the detector over different surfaces/colors.

Here is the link to the sensors i have. What i want to do is differentiate color from say white and black. An example would be a line follower robot.

Thanks for the help

OK this is an easy one just power the bored with your 5 volt positive and ground and attach the data pin to one of your digital pins .in xod just use the digital read block and adjust your sensitivity with the trim pot on the top of the board.

Thanks for the response, is there a way to visually test the threshold value.

If your part doesn’t include LED to indicate on/off, you can create a patch that uses digital-read and feeds the output to an LED, a watch node, or an attached LCD to see status and watch what happens as you move the sensor around.

According to the Amazon post there is a Green led on board to indicate the trigger.

I got the IR sensor to work what I like to do is use it to turn on a motor. I could get the motor to turn on but I wanted to act like a switch so when the IR sensors activated turns on the motor and when it’s off it turns off the motor right now all I
can get it to do is turn it on and will not turn off I’m sure it’s just using another node to do that but I’m not sure which one. Thanks

you can use a flip-flop with a delay

Digital-read outputs boolean. It depends what motor controller you are using how it can be used. For an h-bridge node, you want to input speed as a value between -1 & 1. If you only want stop and go, you can use 0 & some value between 0 and 1 to specify how fast you want to go forward.

Feeding digital-read output to an if-else, false should output 0 and true should output the value you want for “move” speed (could even be negative if you want to go backwards). This output would then feed to the h-bridge SPEED pin.

I can get the motor to turn on with the input from the ir sensor but it will not turn off when the ir sensor is triggered again and I can’t figure it out any ideas?


Here you have some idea


This probably isn’t really what dhall is looking for. This will start a timer when the line is seen, then start the motor after the timer finishes. The motor will continue to run until the line is seen again & timer expires again.

If you are using an h-bridge node, something like this might work (you may need to swap 0 & 1 on if-else, or use something smaller than 1 for “on”):


This will immediately change motor state when line state changes.

Note that if you are trying to follow a line with a robot, 2 sensors probably will not be enough. If the line is between the 2 sensors, you won’t see the line & you won’t know if it is between the sensors or outside of one of them. If you move the sensors close enough together that you won’t have that state, you will probably over-run the line before your motors stop (hence the need to go slower and use a # smaller than 1 in the if-else node). With just an h-bridge controlled motor, turning off the motor does not immediately stop the robot…it coasts to a stop, taking longer to stop the faster you are going.

I had trouble staying on the line with cheap motors and 3 sensors. The extra sensor allowed me to create additional logic to stop both motors if none of the 3 sensors could see the line (I chose to feed speed to this if-else from another if-else; if none of the sensors could see the line, “on” speed was 0; else it was a positive #). If the right sensor could see the line, stop the right motor so the left motor would turn the robot to center the line; same for left sensor/motor. The robot would stop if none of the sensors could see the line, or if both outside sensors saw the line. Setting the middle sensor very sensitive meant it saw the line if it was anywhere near. Setting the outside sensors less sensitive meant they didn’t see the line until it was fully under the sensor (but also meant it was easier to lose the line). I still had trouble with the robot coasting off the line after the motors had stop pushing. The sharper turns your line has or the faster the robot goes, the more trouble you will have with this. It was a balancing act between sensor sensitivity, sensor spacing, line thickness, and motor speed. Farther sensor spacing allows you to set them more sensitive so it was harder to lose the line, but they couldn’t be so far apart the line might hide between them. A thicker line would allow greater sensor spacing.

How do you adjust the speed when i set it up like the sample you provided i can’t figure out how to adjust the speed. Where you would enter the number it says linked and it grayed out.I am using this motor shield (
and this library nkrkv/af-motor. Thanks

Moderator note :exclamation: the message is not related to the original topic. Please, start a new thread.