Arduino Nano sending junk ir codes when interfacing ir remote with servo motor for toggling servo sweep | servo sweep toggle using IR remote

Basically my requirement is to toggle the servo sweep (on/off) using IR remote.

Arduino Nano sending junk ir codes when interfacing ir remote with servo motor for toggling servo sweep.

Ir remote sends exact codes to serial output only until the servo motor start button is pressed.

Once the servo sweep motion starts serial output starts showing random hex codes. After the motor starts even if i press the stop button nothing happens.
The serial monitor just keeps on spitting random codes and the button for stopping servo mptor just dp not work.

Attaching the code.
My code goes haywire once the servo motor strats. Lot of junk codes is spitted in serial monitor.

Any help appreciated

#include <IRremote.h>
#include <Servo.h>

const int IR_RECEIVER_PIN = 5;
IRrecv irReceiver(IR_RECEIVER_PIN);
decode_results irResults;

Servo servoMotor;  // Create a servo object

bool isOn = false;

void setup() {
  Serial.begin(9600);
  servoMotor.attach(3);  // Attach the servo to pin 9
  irReceiver.enableIRIn(); // Start the IR receiver
}

void loop() {
  if (irReceiver.decode(&irResults))
  {
    Serial.println(irResults.value, HEX);
    if (irResults.value == 0xE318261B) { // IR remote button code for 'On'
      isOn = true;
    } else if (irResults.value == 0xEE886D7F) { // IR remote button code for 'Off'
      isOn = false;
    }
    irReceiver.resume(); // Receive the next value
  }
  if (isOn) {
    digitalWrite(IR_RECEIVER_PIN, LOW);
    delay (1000);
    // Sweep from 0 to 180 degrees
    for (int angle = 92; angle <= 180; angle += 1) {
      servoMotor.write(angle);  // Set the servo position
      delay(15);  // Small delay to allow the servo to reach the position
    }

delay (5000);

    // Sweep from 180 to 0 degrees
    for (int angle = 180; angle >= 92; angle -= 1) {
      servoMotor.write(angle);  // Set the servo position
      delay(15);  // Small delay to allow the servo to reach the position
    }
    digitalWrite(IR_RECEIVER_PIN, HIGH);
  }

  else {
    servoMotor.write(0); // Turn off the servo when the 'Off' button is pressed
  }
}

Help will come a lot faster if you show a block diagram with all the connections, including your power supplies.

Sounds like you are trying to power the servo from the Arduino, a very common mistake sometimes fatal to the Arduino.

I have tried with Arduino Uno and Arduino Nano..same issues

The Arduino is not a power supply. Power the servo separately, and connect the grounds. 4xAA batteries work well for one or two small servos.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.