Ir reciver gives false readings when a servo is conected

I'm a beginner with arduinos and i have a project where i want to control a servo with an infrared remote. The servo should move in one direction when i press the number one on the remote and in the other one if i press two.
I get clean readings without the servo attached, but as soon as i attach it there are a lot of wrong readings and the servo often doesn't react at all. (it moves, but for example it often doesn't change directions when i press the other button)

the circuit:

the code:
(it's not mine by the way)

// Include IR Remote Library by Ken Shirriff
#include <IRremote.h>

// Include Arduino Servo Library
#include <Servo.h>

// Define Sensor Pin
const int RECV_PIN = 11;

// Define Servo Pin
const int SERVO_PIN = 3;

// Define Variable for Servo position
// Start at 90 Degrees (Center position)
int pos = 90;         

// Define variable to store last code received
unsigned long lastCode; 

// Define IR Receiver and Results Objects
IRrecv irrecv(RECV_PIN);
decode_results results;

// Create servo object
Servo myservo;          

void setup()
{
  
  // Start the receiver
  irrecv.enableIRIn();
   
  // Attach the servo
  myservo.attach(SERVO_PIN); 

  // Start with Servo in Center
  myservo.write(pos); 
  Serial.begin(9600);
}

void loop() {
if(irrecv.decode(&results)) //this checks to see if a code has been received
{

Serial.println(results.value,HEX);

    if(results.value == 0xFFFFFFFF)   
    {
       // If Repeat then use last code received
       results.value = lastCode;        
    }

    if(results.value == 0xFF6897) //+    
    {
        // Left Button Pressed
        lastCode = results.value;
        // Move left 2 degrees     
        pos += 2; 
        // Prevent position above 180
        if(pos > 180){pos = 180;}                     
        myservo.write(pos);
              
    }
      
    if(results.value == 0xFF9867)  //-   
    {
       // Right Button Pressed
       lastCode = results.value;
       // Move Right 2 degrees  
       pos -= 2; 
       // Prevent position below 0
       if(pos < 0){pos = 0;}                   
       myservo.write(pos);
            
    }

    // Add delay to prevent false readings
    delay(1); 
    //receive the next value  
    irrecv.resume(); 
}
    
}

the components:
Infrarot Empfänger VS1838B
TS90A Servo - 3.3V
Arduino and remote from Fundoino

Thanks in advance for the help.

that's the picture of it:

Servo shouldn't be powered by Arduino 5 V.

It should be like this:

i tried it like this, but i still have the same problem

IR receivers are very noise sensitive. Your power needs to be SOLID, hopefully with some bypass and a short signal lead.

hi!

may I propose you to add a longer delay (like maybe 250 milliseconds) ? It could let more time to the servo to get its new position.

@jack4
I tried your setup and code. All I got was 0xFFFFFFFF for every button. I used some ?updated? library calls, and now this simulation Serial.print()s good values (not 0xFFFFFFFF)... you just need to map the "1" and "2" (as your post says) to servo movements AND fix the servo movements. Right now, your code has PLUS(+) and MINUS(-) mapped to move the servo, and the servo only wiggles when the Remote +/- are pressed.

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