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

okay sorry it's flashing now i didn't realize it wasn't plugged into pin 12

the values pop up on the serial monitor now

  • What IR code do you want to use to make the servo to go to 120° ?

  • What IR code do you want to use to make the servo to go to 25° ?

for 120° i would like 46, and for 25° i would like 15

  • Try this test sketch to see if the servo moves when the IR remote buttons are pressed.
//================================================^================================================
//
//  Servo.ino
//
//  https://forum.arduino.cc/t/cant-figure-out-how-to-control-servo-with-ir-remote/1422403
//
//  LarryD
//
//  Version    YY/MM/DD    Comments
//  =======    ========    ========================================================================
//  1.00       25/12/29    Running code
//
//
//
//

#include <Servo.h>

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

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

#define DECODE_NEC

//================================================
#define LIGHTon                  0x46   //Up   Arrow
#define LIGHToff                 0x15   //Down Arrow 

#define SERVOmax                 120
#define SERVOmin                 25

//================================================
const byte IR_RECEIVE_PIN      = 12;
const byte heartbeatLED        = 13;  //Optional, toggles every 500ms

//================================================
//timing stuff
unsigned long heartbeatTime;
unsigned long checkRemoteTime;

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

  pinMode(heartbeatLED, OUTPUT);

  myservo.attach(8); // attaches the servo on pin 8 to the servo object

  //start the receiver
  IrReceiver.begin(IR_RECEIVE_PIN);

} //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();
  }


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


} //END of   loop()


//                                     c h e c k R e m o t e ( )
//================================================^================================================
void checkRemote()
{
  //========================================================================
  //have we received an IR code ?
  if (IrReceiver.decode())
  {
    //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("Code: ");

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

      Serial.println(byteRX, HEX);

      if(byteRX == LIGHTon)
      {
        myservo.write(SERVOmax);
      }

      else
      {
        myservo.write(SERVOmin);
      }

    } //END of   if (IrReceiver.decodedIRData.command != 0)

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

} //END of   checkRemote()


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

it moves when i press the buttons now!

  • Does this schematic make sense to you ?

  • Go though the sketch to see if you can decipher what is being done.

from what i can tell the 6v conection is connected to ground and has a switch that's connected to the positive on the servo

  • Common hobby servos can be powered from 4.5V to 6V (some can work at7V).
    As mentioned do not power a motor from the Arduino 5V pin.
    Doing so can damage your Arduino.

  • There must be a common GND connection between the Arduino and Servo power supply as seen in the schematic.

  • The Heartbeat LED should toggle every 1 second.
    It gives us a rough indication our sketch is running and our code is non-blocking.
    This LED is optional but I like using it.
    The on board "L" LED will also toggle at a 1 second rate.

So, I was able to get the code to work with a smaller servo motor, but I ended up having to get a 25g because the old one didn't have enough torque. When i plugged in the 25g servo it just kept turning without pressing any button. Do you have any ideas on how to fix that?


this is the new motor

  • Sounds like you purchased a continuous servo as discussed earlier in this thread.

so i'm assuming i can't use this servo for the purpose that i intended it for?

  • For an ON/OFF application like this you need a normal servo one that goes from 0° to 180°.

how can i differntiate the two before i purchase one? what should I be on the lookout for?

  • If you see something like Operating Speed : 0.13sec / 60 degrees (6.0V no load) in the description, it indicates a 0 to 180 degrees servo.

  • Most of the time, if the servo is continuous servo, it will state so.

  • An MG996R is a good choice, you need to confirm the torque and dimensions.

Example:

do you think an mg90s would be good? I feel like I would need a smaller form factor

never mind, I looked up the data sheet for both and I feel like i would run into the same issues as the sg90 i'm currently using

the hobby store i got it from didn't have anything that stated that it was continuous rotation on the packaging sadly, I should've done more research but I was just impatient

I ended up buying a 2 pack on amazon, I will keep you posted on that. Thank you so much for all of your help!