Bad IR reading when using analogWrite

Hi all,

I am making a project with arduino nano. I have connected two servos to pins 5 and 6 and I control them using analogWrite (they are continuous rotation so I don't need to use the Servo library or so). I have algo an IR sensor on pin 11. The problem is that the readings from the IR senses (using the IRRemote library) are good but when the servos are moving I can't get good readings from the IR sensor. While the servos are moving I get a lot of bad readings and only one or two good readings.

Why is happening this? Perhaps a conflict with timers? I also test to connect the IR sensor to pin 4 but this way the servos doesn't work well.

How can I get good reading from the IR when the servos are running?

Thanks in advance! --ugo

Hard to say anything without any code or even a photo of what you have made.

Are you powering the Servos from a separate power supply? If not, that would be the first thing to try.

marco_c:
Hard to say anything without any code or even a photo of what you have made.

Are you powering the Servos from a separate power supply? If not, that would be the first thing to try.

Hi marco_c, I am powering the servos directly from a 4xAA battery pack. This battery pack is connected to an stepper-up (because 4xAA=6V and seem too low to power an arduino) and the stepper-up powers the arduino.

I have made an example code. That is:

#include <IRremote.h>
//#include <NewPing.h>
//#include <toneAC.h>

#define TRIGGER_PIN  7
#define ECHO_PIN     8
#define MAX_DISTANCE 200
#define IR_PIN 11

#define REMOTE_FORWARD    3838214000
#define REMOTE_BACKWARD   4109411888
#define REMOTE_LEFT       2718175436
#define REMOTE_RIGHT      274564916
#define REMOTE_STOP       4291178960

//NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
IRrecv irrecv(IR_PIN);
decode_results results;

void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn();
  //analogWrite(5,1);
  //analogWrite(6,254);
  }

void loop()
{
  if (irrecv.decode(&results)) 
  {
    Serial.println(results.value);
    irrecv.resume(); // Receive the next value
  }
}

If I comment the “analogWrite” lines the codes I received from the IR are good. But when I turn on both motors I get a lot of bad readings until I get one good signal. If I run the code only with one motor it seems to get less glitches in the signal I received from the IR.

I think that it could be noise generated when the motors are running. Could be necessary some decoupling capacitors to filter these glitches?

Thanks again,
–ugo

I think you need a better power supply than you have. You can try some capacitors on the motor terminals but in the end I don't think that 4 AA batteries will do the job properly.

Are you powering the motors directly from the Arduino pins? This is also probably not a good idea and you should use the Arduino pin to turn on the power for the motors using a transistor as a switch.

Motor tend to inject a lot of noise into the power supply, and a "stepper upper" (a boost regulator) can't eliminate it.

IR sensors are very noise sensitive, so you need a good, quiet power supply for the Arduino and IR. A separate battery powered supply would be best.

Marco_c, I am not powering the motors from the arduino pins. My motors are servos (hacked to continuous rotation) and are powered directly from the AAx4 pack.

Jreminton, I think that a separate battery would be the best too. But my project is a little robot and I don’t have space inside the body to use another battery.

There is no way to eliminate the noise?

Thanks again!

We don't know its noise, it could easily be drop-outs due to inadequate power. That is solved by using a better power source or separate power sources.