Cannot use IR library and Motor library together, possible conflict ?

I am using a motor shield (I think is called L293D shield) with IR sensor and want to control the motors on/off with the remote.
I think there is a conflict between the motor library and the IR library.

These are the two libraries:

If I enable this line, the motors do not start anymore, the IR and servo plug on motor controller still work ok:


Any tips on how to get around this? Are there any other libraries available for the above purpose?
I noticed that the files of the infrared library refers to Pin 13 and other pins that I think are in use by the motor shield, so that might be the problem, but I am new at this.

If it helps, this is my full code:

// IR SETUP, leave it above motor INCLUDE to avoid an ERROR
#include <IRremote.h>
int RECV_PIN = 14;
IRrecv irrecv(RECV_PIN);
decode_results results;

#include <AFMotor.h>
AF_DCMotor left_motor(1, MOTOR12_64KHZ); // create motor #1, 64KHz pwm
AF_DCMotor right_motor(2, MOTOR12_64KHZ); // create motor #1, 64KHz pwm

#include <Servo.h> 
Servo servo1;


  String ir_code = "none";
  String ir_last_code = "none";

  // motors min and max speed, so we can easy accelerate with percent betwen these two
  int motor_min_speed = 200;
  int motor_max_sped = 200;
  String motor_status = "off";
  int servo_position = 0;
  int servo_min_position = 50;
  int servo_max_position = 125;
// ir string alues 
  String ir_code_stop = "3782930447";
  String ir_code_up = "3782871287";
  String ir_code_down = "3782891687";
  String ir_code_left = "3782897807"; 
  String ir_code_right = "3782914127";
  // 1,2,3 buttons on remote
  String ir_code_1 = "3782901887";
  String ir_code_2 = "3782885567";
  String ir_code_3 = "3782918207";
  // the repeat code, this value means that last button is kept pressed
  String ir_code_repeat = "4294967295";


// IR
void dump(decode_results *results) {
  int count = results->rawlen;
  if (results->decode_type == UNKNOWN) {
    Serial.println("Could not decode message");
  else {
    if (results->decode_type == NEC) {
      //Serial.print("Decoded NEC: ");
    else if (results->decode_type == SONY) {
     // Serial.print("Decoded SONY: ");
    else if (results->decode_type == RC5) {
     // Serial.print("Decoded RC5: ");
    else if (results->decode_type == RC6) {
     // Serial.print("Decoded RC6: ");
    Serial.print(results->value, HEX);

void setup() {
  // set up Serial library at 9600 bps
  // set the speed to 200/255

  // Start the IR receiver
  // this line seems to jam the motor start ... ?!

  // set the servo
  servo1.attach(10); // the 2 servos are on pin 9 and 10
  servo_position =;


void loop() {
  // start with motor on, just for testing;

 if (irrecv.decode(&results)) {
    // Serial.println(results.value, HEX);
    ir_code = results.value;
    if(ir_code == ir_code_repeat){
      ir_code = ir_last_code;

    // go LEFT
    if(ir_code == ir_code_left){
    // go RIGHT
    if(ir_code == ir_code_right){
    // go UP
    if(ir_code == ir_code_up && motor_status != "forward"){
      Serial.println("going up");;
      motor_status = "forward";      
    // go DOWN
    if(ir_code == ir_code_down && motor_status != "backward"){
      Serial.println("going down");
      motor_status = "backward"; 
    // STOP, maybe is best NOT to test if current status is "off" or not, just stop it whenever this button is pressed
    if(ir_code == ir_code_stop){
      motor_status = "off";
    // 1 (button)
    if(ir_code == ir_code_1){
      Serial.println("1 pressed");
    // 2 (button)
    if(ir_code == ir_code_2){
      Serial.println("2 pressed");
    // 3 (button)
    if(ir_code == ir_code_3){
      Serial.println("3 pressed");
    // save the last command, we will use this one if received command is "repeat"
    ir_last_code = ir_code;
    irrecv.resume(); // Receive the next value

Moderator edit: [code][/code] tags added. (Nick Gammon)

Please note that, at present, the String library has bugs as discussed here and here.

In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.

I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.).

I used the "String" code just when defining these:

  String ir_code_stop = "3782930447";
  String ir_code_up = "3782871287";
  String ir_code_down = "3782891687";

But the thing is... this part seems to work fine, because it prints in my serial monitor that I pressed button up/down/etc on my IR remote, so it reads IR fine, but it would just not start the motors anymore. Because the IR codes seem to work, I think it is not a string issue.
Are there any other things I can try?

I believe you may be correct about a conflict between the libraries.

It looks like they're both messing with the PWM timer(s), which isn't really surprising since both libraries have similar functionality.

It might be worth contacting the library authors to confirm which timers they take over. It would certainly be worth getting another confirmatory opinion on this in any case.

Another approach would be to search for similar projects using both libraries.

Good luck with your project,


I think it is not a string issue.

You are probably right, at present. However you may find it crashes after an hour, once you solve the other problems.

It's not uncommon for library authors to code as if they "own" the processor, using timers etc. to best make their library work. There may well be conflicts. A different library may solve your problems.