Take the 2-minute tour ×

For an exhibition with some interactive installations, I have to create a system that reads data from three ultrasonic PING sensors and has a DMX dimmer connected at one output.

So I took my Arduino Mega board, built some hardware around it (signal inverter for DMX) and tested the DMX dimmer using the library DmXSimple. It works fine!

Next step: testing the PING sensors. I used the library NewPing, and connected the sensors. It works fine!

Now both together, in one program: Importing both libraries, using the same codes. It doesn't work!

Timer collision? Well...anyway - I have no idea on how to solve that. What should I do? If it's a timer issue (that is, both libraries call the same timer), I really don't know how to modify the libraries to get it working.

share|improve this question

1 Answer 1

There are two timers on the Arduino, and you may be able to make both works aside. DmxSimple explicitly says it uses the timer #2, whereas it is not clear for NewPing. But the NewPing library can work without using a timer. You can make the DmxSimple work using its timer (which is mandatory for close-to-real-time interaction with its bus), whereas you can handle the sonar ping in the loop function:

void loop() {
    int dst = sonar.ping_cm();
    DmxSimple.write(1, dst);

There you shouldn't have timers conflicting.

Looking at the source code of NewPing, it uses Timer2 if you use the timer_us()/timer_ms() functions, but no timer otherwise. But if you run it on an Arduino Leonardo (or Arduino Micro or any other ATmega32U4 based microcontroller), it will use Timer4 which is not conflicting with DmxSimple.

If you want to patch the NewPing library to use Timer3, you may want to replace this in NewPing.cpp:

OCR2A = 249;
TIMSK2 |= (1 << OCIE2A);


OCR3A = 640;
TIMSK3 |= (1 << OCIE3A);

and in stop_timer:

TIMSK2 &= ~(1 << OCIE2A);


TIMSK3 &= ~(1 << OCIE3A);
share|improve this answer
Thanks for your help, zmo! I figured out a way already, it seems. I changed in the NewPing library Timer2 to Timer4. Although I don't really know what I was doing I kept going on try and error, until I got some useful result. –  kilian232323 Jun 25 '13 at 21:20
Your suggestion, however, to use Delay in the Ping code, is not helpful for me, because I really have to avoid any kind of Delay traps, due to the rest of my program which requires a constant (and "uninterrupted") updating. –  kilian232323 Jun 25 '13 at 21:22
Maybe you wanna take a look at the modified NewPing library, where I tried to replace Timer 2 with Timer 4. It might be interesting to say that I'm working on an Arduino Mega 1280. –  kilian232323 Jun 25 '13 at 21:25
But how can I post that code here? It's too long by 9000 characters... –  kilian232323 Jun 25 '13 at 21:28

Your Answer


