For a project I am working on I am using an IR sensor and a servo (I have other parts too but they aren't important to the problem I am having with the IR sensor) and have found that whenever the servo is set to a different position it triggers the IR sensor.
This is the code for the IR sensor:
int IRSensor = 22;
void setup(){
pinMode(IRSensor, INPUT);
}
void loop(){
int IRStatus = digitalRead(IRSensor);
if(IRStatus != 1){
//do stuff
}
}
For the servo I am just using the normal Servo library, have it connected to digital port 10, and for the code I just use:
myservo.write(180); // when opening it
or
myservo.write(0); // when closing it
I know it is the servo that is causing the IR sensor to trigger because it only happens when the servo is opened or closed. I don't believe it has anything to do with my code either because it doesn't happen if I do everything as normal but don't have the servo connected to 5v and ground.
I also don't believe it is due to the servo drawing power because I have my Arduino Mega board connected to a power supply and my computer.
Has anyone experienced this before or have any insight on what might cause this?