Two Arduinos get out of sync after a few seconds I built a small PCB that has some LEDs (bare NeoPixel 5050) and an IR receiver on it. The microcontroller is an ATtiny85, and the PCB is powered by a LiPoly battery (3.7V). The circuit normally stays in ULP sleep mode until a pin change interrupt wakes the circuit. The pin change interrupt is tied to the IR receiver so when the receiver gets a signal from a remote (38kHz modulated IR burst) a lighting sequence is triggered. I have multiple of these PCBs running the exact same code (shown below). When the PCBs receive an IR signal they all start at the same time, but then after a few seconds they start to drift apart. The lighting needs to be synchronized. What is the reason for this drift and how can I eliminate it? Is it due to different battery voltages? That would be the only physical difference between any two PCBs. Thanks for your help.

#define IR_PIN      4
#define LIGHT_PIN   0
#define DELAY_TIME  200

#include &lt;Adafruit_NeoPixel.h&gt;
#define NUM_PIXELS  2
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_PIXELS, LIGHT_PIN, NEO_GRB + NEO_KHZ800);

#include "avr/interrupt.h"
#include "avr/sleep.h"

void setup() {
  pinMode(IR_PIN, INPUT);
  strip.begin();
  pixelsOff();
}

void loop() {
  sleep();

  for (int i = 0; i &lt; 5; i++) {
    pixelsOnRed();
    delay(DELAY_TIME);
    pixelsOff();
    delay(DELAY_TIME);
    pixelsOnGreen();
    delay(DELAY_TIME);
    pixelsOff();
    delay(DELAY_TIME);
  }

  for (int i = 0; i &lt; 5; i++) {
    pixelsOnBlue();
    delay(DELAY_TIME);
    pixelsOff();
    delay(DELAY_TIME);
    pixelsOnRed();
    delay(DELAY_TIME);
    pixelsOff();
    delay(DELAY_TIME);
  }

  for (int i = 0; i &lt; 5; i++) {
    pixelsOnGreen();
    delay(DELAY_TIME);
    pixelsOff();
    delay(DELAY_TIME);
    pixelsOnRed();
    delay(DELAY_TIME);
    pixelsOff();
    delay(DELAY_TIME);
  }
}

void pixelsOff() {
  strip.setPixelColor(0, 0, 0, 0);
  strip.setPixelColor(1, 0, 0, 0);
  strip.show();
}

void pixelsOnRed() {
  strip.setPixelColor(0, 255, 0, 0);
  strip.setPixelColor(1, 255, 0, 0);
  strip.show();
}

void pixelsOnGreen() {
  strip.setPixelColor(0, 0, 255, 0);
  strip.setPixelColor(1, 0, 255, 0);
  strip.show();
}

void pixelsOnBlue() {
  strip.setPixelColor(0, 0, 0, 255);
  strip.setPixelColor(1, 0, 0, 255);
  strip.show();
}

ISR(PCINT0_vect)
{
    //This is not needed for the current sketch
}

void sleep() {
    GIMSK |= _BV(PCIE);                     // Enable Pin Change Interrupts
    PCMSK |= _BV(PCINT4);                   // Use PB3 as interrupt pin
    ADCSRA &amp;= ~_BV(ADEN);                   // ADC off
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);    // replaces above statement

    sleep_enable();                         // Sets the Sleep Enable bit in the MCUCR Register (SE BIT)
    sei();                                  // Enable interrupts
    sleep_cpu();                            // sleep

    cli();                                  // Disable interrupts
    PCMSK &amp;= ~_BV(PCINT3);                  // Turn off PB3 as interrupt pin
    sleep_disable();                        // Clear SE bit
    ADCSRA |= _BV(ADEN);                    // ADC on

    sei();                                  // Enable interrupts
}
What is the reason for this drift and how can I eliminate it?


two solutions:

1) try using crystal oscillators;

2) try driving on arduino with the other arduino's clock output.