I have a couple of A/D converters and am reading them every 100 µs over SPI, using the timer interrupt routine with SPI clocked at 16 MHz.

Additionally, I have five digital temperature sensors using the I²C bus in a timer interrupt routine executed every second. With a 100 kHz I²C bus and two bytes per temperature sensor, it takes almost 1 ms to read the temperature.

Obviously while reading temperature values I cannot use the SPI bus, missing almost 10 A/D readouts while I²C is active.

How can I use slow and fast peripherals and have timely readouts?

Thanks, these are all great answers!

I am actually using Teensy 3.2 development board and would be nice to have a solution with ready-to-use Arduino IDE libraries. There is a library for Teensy 3.2 for I2C communication with DMA.

I think that the simplest solution is to read A/D converters using 100 us timer interrupt and increment a variable inside the interrupt routine to initiate I2C communication in the main code every second (Microchip TC74 temperature sensors).

I have never used DMA before and my question is what happens when I2C bus is active (reading), and 100us timer interrupt comes. Will it be accepted right away or uC will wait until the byte readout is finished on I2C, or interrupt will be accepted and I2c reading will continue independently?

  • 22
    \$\begingroup\$ Whether you can do that, and how, are going to depend a lot on exactly what microcontroller you are using. \$\endgroup\$
    – The Photon
    Commented Mar 14, 2021 at 21:00
  • 4
    \$\begingroup\$ What micro are you using and what clock frequency? \$\endgroup\$ Commented Mar 14, 2021 at 21:18
  • 8
    \$\begingroup\$ "Obviously while reading temperature values I cannot use SPI bus" Why not? \$\endgroup\$
    – glglgl
    Commented Mar 15, 2021 at 8:16

3 Answers 3


Obviously while reading temperature values I cannot use SPI bus, missing almost 10 A/D readouts while I2C is active.

Not at all - with many microcontrollers you can put the bytes you want sent into a buffer and return from the interrupt, then arrange to get another interrupt later when it's done. Interrupt handlers should never be waiting for something to complete.

  • 1
    \$\begingroup\$ Yup - and if DMA is available, it may be possible to do, for example, all of the SPI comms in a single transaction (I2C reads may require 2 transactions). \$\endgroup\$ Commented Mar 15, 2021 at 9:20
  • 3
    \$\begingroup\$ Note: It is possible for interrupts to wait for things if you are careful. I don't want anyone to get the impression it's not possible. It's a should not not a must not. If your SPI interrupt is a higher priority than your I2C interrupt, it could work. \$\endgroup\$ Commented Mar 15, 2021 at 9:42

It sounds like your I²C code is ‘blocking’, i.e.: sitting in a loop for something to happen.

I would suggest the I²C code is implemented as a state machine so that it gets called every 100 µs and tests the I²C state to see if it is ready. If it is, then issue the next step (write a byte, read a byte, etc.) then exit. Next 100 µs, check to see if it completed. If not exit. And so on. This assumes you are using a I²C or TWI peripheral in your microcontroller. Otherwise you can bit bang I²C at 100 µs intervals (10 kHz) as this is perfectly legal.

If your microcontroller has DMA, you could use this to manage the SPI transfers and free up the microcontroller for other tasks.

  • 1
    \$\begingroup\$ Many serial peripherals on microcontrollers will fire an interrupt when a transfer is completed, as well; that would be better than polling every 100 μs if you can use it. \$\endgroup\$
    – Hearth
    Commented Mar 15, 2021 at 14:38

Most of the microcontroller has Hardware SPI capabilities with DMA, which means you don't have to use cpu cycles to read it.

In addition, as the other answers point out, you have to design your I2C to be non blocking or use interrupt based logic.

This is highly dependent of your MCU.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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