I have a total of 15 sensors (analog and digital) connected to an arduino mega. For my real-time control application, it's really really important that all data be collected simultaneously at regular intervals in time. Is there any way to ensure that all data is collected at the same time? How can I minimize the total lag time between the first and last sensors?

  • What kind of interface (logical read, analog read, i2c, spi...) are your sensors using? – jfpoilpret Aug 20 '14 at 18:16
  • 3
    A microcontroller executes instructions one by one, and can do that really fast but most cannot do them simultanuously. You'll have to figure out what your minimum requirement is and if that requirement is faster than a microcontroller is able to, then you'll have to either find a better microcontroller or a way to distibute reading the inputs across different devices. Reading up to 8 digital inputs simultanuously is easy, but if you need more or you have lots of analog signals, then you have to rethink your architecture. – jippie Aug 20 '14 at 19:05
  • 2
    You really need to specify quantitatively what you mean by "simultaneously", is it in the same us, less, more? – jfpoilpret Aug 20 '14 at 19:49
  • 2
    Please edit your question to clearly state the max amount of time allowed to sample all inputs. Without this information you may get answers that are not in line with tour actual needs... – jfpoilpret Aug 21 '14 at 5:40
  • 1
    I suggest original poster may use the 'error budget' principle, listing error contribution percentage from all sources, to gauge a realistic time requirement. For example, in door robot move at speed of 1 meter per second. If laser ranger sensors error is 50mm. It may be sensible to set "time error budget" to, says, 5 times below sensor error, so that it does not significantly degrade the laser data quality. That is, during the time uncertainity, robot would moved 10mm at said speed. The acceptable time uncertaininty is 10ms. – EEd Aug 21 '14 at 12:23

I would suggest looking into threading.

To minimize the time from the first read to the final read, an interrupt and an interrupt timer would probably be good. Have the interrupt timer fire after some interval and place all of your sensor reads in the Interrupt Service Routine (ISR).

I would also suggest staying away from functions that the Arduino platform provides, like delay(), digitalRead(), digitalWrite(), analogRead(). Those functions will add extra instructions to your sensor reads and waste clock cycles. The Arduino platform sacrifices speed for usability in some cases.

You might also want to look into using a real-time OS if this is a real-time application


The arduino will not support 15 true threads. if you look into The TimedAction class you might see something that you can use. Basically, timed actions occur after an interval and a specified function is called. However, if you have 15 timed actions all with the same duration, they won't all read at the same time, they will still have to read in some order, one at a time.

If you look into the source code for many of the functions you can see how they work. The Arduino source code comes with the IDE. A good place to start would be Arduino.h. If speed is a concern, you want to reduce as many clock cycles as possible, which means removing as many lines of code as possible. If you can do something without calling digitalRead() for example, you will save yourself some time.

A real-time operating system is an OS that is used in real-time systems. I find it helpful to think of a RTOS as a timing library that will help you, opposed to installing Linux on your arduino. There is plenty of material around the web on what a real-time system is and what a real-time OS does if you want to learn more.

The important part of an RTOS is that is guarantees that an action be performed before a deadline. So in your case, sensors are read before a given time. There are a couple RTOS's for the Arduino that google will find for you.

| improve this answer | |
  • Can the arduino mega support 15 simultaneous threads? – Paul Aug 20 '14 at 19:10
  • Are there more efficient implementations of the digital/analog read/write functions within the arduino platform? – Paul Aug 20 '14 at 19:11
  • What do you mean by a "real-time OS"? Could you give an example? – Paul Aug 20 '14 at 19:12
  • Added an update section to answer some of your questions – Doowybbob Aug 20 '14 at 19:36
  • What do you really mean by "do something without calling digitalRead()"? I don't know of any other way to collect the data without calling a read function from one of the pins. Could you be more specific, possibly with an example of what can be done instead of digitalRead()? – Paul Aug 20 '14 at 20:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.