IRreceiver library & Servo strange behaviour

Hello,

I am new to Arduino and IoT generally and right now I am trying to build a project consisting of a servo and an infrared receiver. I am also using Freaduino UNO with Mega326P, but as far as I know, there should not be big difference compared to Arduino UNO.

The whole thing works: I press a button on IR remote control, the servo moves. Problem is that the servo does not stay still, even if there is no load on it, makes irritating noises and just seems to be trying to correct its position all the time.

I’m using IRremote Arduino Library (GitHub link) version 2.1.0, Arduino IDE and included servo library.

What I found out (and how I know IRremote causes the problem) is that when I remove

//remoteCtrl is instance of IRrecv

remoteCtrl.enableIRIn(); //Start receiving signal via IR receiver

the problem goes away and servo finally “shuts up”.

As IRremote documentation says, I should connect the receiver to digital PWM pin 3 or 9 (pins with timer 2), which I tried both.

I am powering the whole project with power banks, one for Arduino and one for the servo (grounding connected, of course), each outputting 5V and 1A, which should be enough.

Only thing I already found is that someone else already had this issue (GitHub link) and not only with the servo, but the solution is missing, for my case.

What else should I try? What else do you need from me to help?

Google "Arduino IRRemote and Servo". You've hit a well known and well documented incompatibility between those two libraries and there's no need to type out all the potential ways to make it work when they are so well documented in so many places on the web. Just search and you'll see. They're both trying to use the same timer and they can't. You'll have to change one of the libraries. There are many options.

Thank you for advise, now I know timers are the problem.

However, I downloaded the latest IRreceiver library and tried to change its timer for my board from 2 to 1, but then the project won't even compile. I tried to look inside servo library but didn't found way to change its timer. Most of posts I found (when I'm getting the error) tell me to switch the timers, but that would bring me back to my first problem.

Ok, I have maybe a bit dirty solution:

Attach the servo; Do stuff with it; Detach it.

Of course it doesn't electronically hold position when detached, but it works fine for me.

However, the main problem with timers stays unsolved...

Well I can't see enough of your project to really guess anything other than a timer conflict.

Yeah, I had to work on it, here’s the code, without the attach/detach solution, IRreceiver using timer 2 and servo using timer 1:

#include <Arduino.h>
#include "IRremote.h"
#include <Servo.h>
Servo servo;

//IRremote sensor in PWM pin 9
#define pinIN 9

//Button (IRremote alternative) in analog pin 1
int button = A1;

IRrecv remoteCtrl(pinIN);
decode_results recieved;



void setup() {
  servo.attach(11);         //Servo in PWM pin 11
  servo.write(95);          
  delay(500);    
  remoteCtrl.enableIRIn();  //Line that causes the problem
}


//The whole project is controled with one click (either on the remote control or the button)
//Click - go to position
//Another click - go back to first position
boolean back = false;

void loop() {
  //If IRemote receives signal
  if(remoteCtrl.decode(&recieved) && recieved.value == 0xFD00FF) {
    move();    
  }
  //If button is pressed
  if(digitalRead(button)==LOW) {
    move();
  }
  remoteCtrl.resume();
  delay(200);
}

void move() {
      if(back) {
        //Go slowly to the position
        for(int d = 160; d > 76; d--) {
          servo.write(d);
          delay(40);
        }
        delay(500);
        //Go a bit back
        for(int d = 75; d < 96; d++) {
          servo.write(d);
          delay(60);
        }
        back = false;
      } else {
        //Go slowly to the position
        for(int d = 95; d < 180; d++) {
          servo.write(d);
          delay(40);
        }
        delay(500);
        //Go a bit back
        for(int d = 180; d > 159; d--) {
          servo.write(d);
          delay(60);
        }
        back = true;
      }
      delay(500);
}

See attachment - schematic (had to design (and mainly learn how to do it) white parts by myself, sorry if anything’s wrong)