Can't figure out how to control servo with IR remote

  • Do not use the Arduino to power inductive loads or motors.

  • Do you have a 4AA or AAA battery holder ?

  • Use 5v to power the IR receiver.

i don't have that what issue would that resolve?

  • A small servo motor like yours needs 1 or more amps.
    Your Arduino can be damaged if the 5v pin is required to supply this much current.

If your servo turns continuously, around and around without stopping, then it is the wrong type for what you want to do.

it's not turning around and around without stopping, it stops at 180 degrees

Good. I am assuming you want to swing back and forth about 60 ~ 90 degrees to work a wall switch toggle?

Yes that's exactly what I'm trying to achieve

OK, I'm not "up" on IR remotes but you came to the right place. :grin:

  • Tell us what your servo does when you upload this test sketch to the Arduino.
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup()
{
  myservo.attach(8); // attaches the servo on pin 8 to the servo object
  //myservo.attach(8, 400, 2600); // attaches the servo on pin 8 to the servo object
}

void loop()
{
  for (pos = 0; pos <= 180; pos += 1)   // goes from 0 degrees to 180 degrees
  {
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15 ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1)   // goes from 180 degrees to 0 degrees
  {
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15 ms for the servo to reach the position
  }
}

it gets bound up at the end of the mount, stops, goes to the other end of the mount, then repeats

https://youtube.com/shorts/pPF6ydxsm2E

  • Not sure what this means :roll_eyes:

  • If you try this version of the same sketch, the servo should go between 45° to 100° and back . . .

/* Sweep
  by BARRAGAN <http://barraganstudio.com>
  This example code is in the public domain.

  modified 8 Nov 2013
  by Scott Fitzgerald
  https://www.arduino.cc/en/Tutorial/LibraryExamples/Sweep
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup()
{
  myservo.attach(8); // attaches the servo on pin 8 to the servo object
  //myservo.attach(8, 400, 2600); // attaches the servo on pin 8 to the servo object
}

void loop()
{
  for (pos = 45; pos <= 100; pos += 1)   // goes from 0 degrees to 180 degrees
  {
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15 ms for the servo to reach the position
  }
  for (pos = 100; pos >= 45; pos -= 1)   // goes from 180 degrees to 0 degrees
  {
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15 ms for the servo to reach the position
  }
}

i changed it to 25 and 120 degrees and i feel like it's the perfect positions

  • Set the Baud rate of your serial monitor to 9600

  • Try this IR test sketch.

  • When you press an IR remote button, what happens on the serial monitor ?

//================================================^================================================
//
//  IR_Receiver_44.0_LMD.ino
//
//  URL
//
//  LarryD
//
//  Version    YY/MM/DD    Comments
//  =======    ========    ========================================================================
//  1.00       24/04/05    Running code
//  1.10       24/04/07    Added short/long button press 
//
//
//
//

//use IRremote library version 4.4.0 or newer
#include <IRremote.hpp>

#define DECODE_NEC

//================================================
#define LEDon                    HIGH   //PIN---[220R]---A[LED]K---GND
#define LEDoff                   LOW

#define PRESSED                  LOW    //+5V---[Internal 50k]---PIN---[Switch]---GND
#define RELEASED                 HIGH

#define CLOSED                   LOW    //+5V---[Internal 50k]---PIN---[Switch]---GND
#define OPENED                   HIGH

#define ENABLED                  true
#define DISABLED                 false


//================================================
const byte IR_RECEIVE_PIN      = 12;

const byte feedbackLED         = 11;  //Optional, indicates when an IR signal is received
const byte heartbeatLED        = 13;  //Optional, toggles every 500ms

//================================================
bool durationFlag              = DISABLED;
char lastButtonPressed;

//timing stuff
unsigned long shortPressTime   = 500ul;
unsigned long longPressTime    = shortPressTime + 1;
unsigned long durationInterval = 200ul;   //Note: a button hold sends a "Repeat Code" every ~100ms
unsigned long timePressed;
unsigned long durationTime;
unsigned long heartbeatTime;
unsigned long checkRemoteTime;

//                                           s e t u p ( )
//================================================^================================================
void setup()
{
  Serial.begin(9600);

  pinMode(heartbeatLED, OUTPUT);

  //start the receiver
  IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
  //setLEDFeedback(feedbackLED, true);
  setLEDFeedback(feedbackLED, false);

} //END of   setup()


//                                            l o o p ( )
//================================================^================================================
void loop()
{
  //========================================================================  T I M E R  heartbeatLED
  //is it time to toggle the heartbeat LED ?
  if (millis() - heartbeatTime >= 500ul)
  {
    //restart this TIMER
    heartbeatTime = millis();

    //toggle the heartbeat LED
    digitalWrite(heartbeatLED, digitalRead(heartbeatLED) == HIGH ? LOW : HIGH);
  }

  //========================================================================  T I M E R  checkRemote
  //is it time to check for a new IR remote character ?
  if (millis() - checkRemoteTime >= 10ul)
  {
    //restart this TIMER
    checkRemoteTime = millis();

    checkRemote();
  }

  //========================================================================  T I M E R  button duration
  //if this TIMER is enabled, has the IR button switch been released ?
  if (durationFlag == ENABLED && millis() - durationTime >= durationInterval)
  {
    //disable this TIMER
    durationFlag = DISABLED;

    serviceButton();
  }


  //================================================
  // other non blocking code goes here
  //================================================


} //END of   loop()


//                                    s e r v i c e B u t t o n ( )
//================================================^================================================
void serviceButton()
{
  //======================================================================== SHORT PRESS
  if (millis() - timePressed <= shortPressTime)
  {
    Serial.print("This was a SHORT Press on button ");
    Serial.println(lastButtonPressed);
  }

  //======================================================================== LONG PRESS
  else if (millis() - timePressed >= longPressTime)
  {
    Serial.print("This was a LONG Press on button ");
    Serial.println(lastButtonPressed);
  }

} //END of    serviceButton()


//                                     c h e c k R e m o t e ( )
//================================================^================================================
void checkRemote()
{
  //========================================================================
  //have we received an IR code ?
  if (IrReceiver.decode())
  {
    //if we are currently doing duration timing, was this the repeat code ?
    if (durationFlag == ENABLED && IrReceiver.decodedIRData.command == 0)
    {
      //restart the TIMER
      durationTime = millis();
    }

    //determine which button was pressed
    if (IrReceiver.decodedIRData.command != 0)
    {
      byte byteRX = IrReceiver.decodedIRData.command;

      //we will ignore repeat codes
      IrReceiver.decodedIRData.command = 0;

      Serial.print("\nCode: ");

      //should we add a 0 prefix ?
      if (byteRX <= 0xF)
      {
        Serial.print("0");
      }

      Serial.println(byteRX, HEX);

    }

    //restart IR code detection
    IrReceiver.resume();
  }

} //END of   checkRemote()


//================================================^================================================

nothing happens in the serial monitor when i press on my remote

  • Did you set the monitor to 9600 ?

yes i did

  • Does the TX LED on the Arduino come on then go off when the IR remote button is pressed ?

no it doesn't but the led on the ir receiver flashes

  • Which Arduino pin is your IR receiver module connected to ?