IR sensor suddenly won't cooperate with Yun


I have an Infrared Proximity Sensor that has suddenly stopped working with my Yun. There could be many reasons for this, but I have tried three different IR sensors (same kind) with three different Yun’s, and the result is the same. I have also tried using different wires.

Everything used to work without problems, but after returning to my project after three months there is something wrong. The weird thing is, that everything works on my Leonardo, just as it used to on my Yun.

The only thing changed in the setup is that I got 4 gigs extra ram and upgraded to Win10 from Win7. I have tried all of my three USB ports, as I thought it could be some kind of a powering problem.

Do anyone know what could cause this? You can see my code below, but I am very confident that the error doesn’t lie in there as it as said works on the Leonardo.

// Debouncers
int IRDebounce1;
int IRDebounce2;
int IRDebounce3;

const int IRSensor = A1;
void setup() {
  pinMode(IRSensor, INPUT);

void loop() {

// Returns a color code based on the IR sensors reading
int IRToColor(int reading) {
  if (reading <= 600 && reading >= 98) { // If the reading are within the desired range
    int IRDebounceCheck;
    if (reading <= 600 && reading >= 327) { // [5-8] cm
      IRDebounceCheck = 1;
    else if (reading < 327 && reading >= 231) { // [9-12] cm
      IRDebounceCheck = 2;
    else if (reading < 231 && reading >= 171) { // [13-16] cm
      IRDebounceCheck = 3;
    else if (reading < 171 && reading >= 137) { // [17-20] cm
      IRDebounceCheck = 4;
    else if (reading < 137 && reading >= 116) { // [21-24] cm
      IRDebounceCheck = 5;
    else if (reading < 116 && reading >= 98) { // [25-28] cm
      IRDebounceCheck = 6;
    // Debounce the signal to filter out random values
    IRDebounce3 = IRDebounce2;
    IRDebounce2 = IRDebounce1;
    IRDebounce1 = IRDebounceCheck;
    if (IRDebounce3 == IRDebounceCheck && IRDebounce2 == IRDebounceCheck) {
      return IRDebounceCheck;
    else {
      return 255; // If the debouncers are not equal, return errorcode 255
  else {
    return 0; // If we are out of range

You say it isn't working, but what does that mean? It doesn't compile? Or doesn't load? Or doesn't output anything? Or outputs the wrong things? Or maybe something else? Having a hint as what you are experiencing will go a long way toward getting a useful answer.

What are you expecting to happen? (I can take a guess from looking at the code, but it's best to hear your explanation rather than making a wrong assumption.) And what is actually happening?

I don't see anything in the code that jumps out at me as being blatantly wrong. But I do question what is your intention with the line just before you return IRDebounceCheck:


It's syntactically valid, but has no run-time meaning, and shouldn't cause any problems. But what are you thinking this does?

I should of course have explained that.

The numbers are all off, and it bounces back and forth instead of being steady, as it is on the Leonardo. So if I measure e.g. 20 cm. away, it will bounce between 18 and 28 cm.

I guess (IRDebounceCheck); is a part of some old code that should have been deleted. Maybe a serial print - it should not be in there though.

The Yun has some internal power supply issues: it has some series diodes in the supply lines for reverse polarity protection, but this affects the voltages n various parts of the board. One of the known issues is with the analog inputs, where the default is to use the 5V VCC as the analog reference. Since that voltage is not precise/stable, analog readings can be affected. The Leonardo, while the same processor, does not share this power issue.

You may have better results by enabling the internal analog reference, which doesn't vary with voltage. Since it is less than 5V, you may have to divide down the IR sensor analog output voltage if it is too high.

Thanks, I will look into it :slight_smile:

But I don’t understand how it once worked. I developed the code to the Yun and have only been testing on the Yun, and everything worked back then. I only used the Leonardo yesterday to try and find the error.

It's a mystery... Are you using the same power supply as last time? (ie: USB vs VIN pin.)

I have been using USB the whole time. I will test with another computer tomorrow to see if it is my USB ports there is something wrong with, but I can't see how that could be, since they work with everything else.

This is embarrassing, but I guess I gotta post it anyway: It was the USB cable there was something wrong with. The only thing I didn't replace... I feel so stupid now, for wasting all these hours.

Anyway, for those who are reading: Remember to check your cables :)

Thanks for the help, ShapeShifter - I'm sorry that you wasted your time too!

So often, the problem turns out to be the simple things, the parts you don't bother to check when investigating a problem.

Thanks for posting the solution, and don't worry about wasting our time. I'm just glad you figured it out and got it working.