Change IR sensor status

Hello everyone,

I am trying to control two MG996R servos with a YWBL-WH IR sensor, all three connected on the same board.

I would like the sensor to change state when an object passes by, causing the servos to rotate a certain angle, but nothing happens: the sensor reads the passage of the object, as the LED lights up with the object in front and then goes back off when it moves on, but the servos don't move.

Maybe because the object is moving too fast (1.598rad/s)?
Or are the IR sensor not good for this application?

thank you all

Welcome to the forum

Please post a schematic of your project showing how the components are connected and powered and which Arduino board you have

Please post your sketch, using code tags when you do. This prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

So the sensor doesn't have any problem.
What about your wiring and code, you didn't post neither.

I apologise for the missing code, I was asking first if the sensor was a good brand, I am an arduino novice.
Anyway, I connected the two servos and the IR sensor to a NANO 33 IoT Board, powered by the PC's USB cable. (I also tried powering it with a 5.1V and 2.5A power supply).
Here is the code:

#include <Servo.h>
  Servo Serv1;
  Servo Serv2;

  int pinIR=4;
  int pinServo2=3;
  int pinServo1=2;
  int val=0;

  void setup(){
    Serv2.attach(3);
    Serv1.attach(2);
  }

  void loop(){
    val = digitalRead(4);
  
    if (val ==1){
      Serv1.write(8.4);
      delay(1200);
      Serv2.write(93.7);
      delay(2700);
    }
    else{
      Serv1.write(51.6);
      delay(600);
      Serv2.write(49.1);
      delay(100);
    }
  }``

You should post a link to that sensor, google finds too many products with YWBL-WH

certainly, here the link: https://amzn.eu/d/4CKLeK6

You should not power servos like that. Those servos have stall current of 2A, you need to use separate power supply.

Much better...
So you need to power this sensor at 3.3V, since that's your arduinos operating voltage. If you power it at 5V also the output to your arduino pin is going to be 5V. Not good.

Thank you very much for the information!
So, should i power the servos with a 2V power supply and get another board for the sensor and power it at 3.3V?

Sorry for the trivial question, but as I said I'm a beginner.

No, you can power that sensor from your arduino boards 3.3V pin.
You need 5V 2A(or more) power supply to power one servo.
Either two of them or one 5V (min)4A power supply. You need to connect power supply GNDs to arduino GND as well (common ground).

If you have doubts, you can draw a wiring scheme and post it here.

ok thank you so much, i think i understand. Now I buy a 5V (min) 4A power supply, and i try it.

Maybe, in case of help, can i write you again?

Of course.
Also I recommend you to run first a demo sketch for servo to verify everything works.
Then another demo sketch for your sensor.
After that you can start to play with your final code

To be clear, servo power supply voltage needs to be 5V (or max 6V) and current needs to be at least 4A. So for example 5V5A or 5V10A are fine.

Before you spend money on things you may not need why don't you try the servos with the power supply you have.
Power the nano via USB and connect the servos like this

hi! yes, the servo are connected like your sketch, and i power the nano via USB, and the movement is perfect, sometimes they bounce.
But, when I insert the IR sensor, the servo movement are wrong.

Can you explain better?

Servo uses integer numbers between 0 and 180.
Why do you have floating point numbers?

Does this do what you want


#include <Servo.h>
Servo Serv1;
Servo Serv2;

int pinIR = 4;
int pinServo2 = 3;
int pinServo1 = 2;
int val = 0;

void setup() {
  Serv2.attach(3);
  Serv1.attach(2);
  pinMode(pinIR, INPUT);
}

void loop() {
  val = digitalRead(pinIR);

  if (val == 1) {
    Serv1.write(8);
    delay(1200);
    Serv2.write(93);
    delay(2700);
  }
  else {
    Serv1.write(51);
    delay(600);
    Serv2.write(49);
    delay(100);
  }
}

if I want the servo moves like 8°, when I insert also the IR in the same board, the real servo movement isn't 8°, but for example 6-7°

physically insert? or in the code?

I have a question. The sensor works when I move the object slowly, but, when I move the object with his real velocity, the sensor led changes color, but doesn't change his status.

Maybe because it has a low signal sampling rate? If I use PIR motion sensor, is it better?