Code snippet: Arduino – Activate I2C broadcast reception

I2C is the bus protocol or technology via which a microcontroller can communicate with many sensors or actuators. Thanks to the right software, you can also connect and communicate with different microcontrollers. There is usually only one master per I2C bus that can request or send data from a slave via the corresponding address.

In one of my projects I wanted to (and still want to) connect several microcontrollers. Several bus technologies are suitable for this. But I2C is almost the only one that is supported directly by almost every modern microcontroller without additional hardware requirements. So the choice quickly fell on I2C.

Using I2C, several microcontrollers were now connected to one another. The master can easily send a command to each individual slave. To do this, the slave address and the desired command are simply sent via the bus. All slaves listen to what is going on on the bus, but only the slave with the corresponding address reacts to the command sent. So far so good.

But what if the master wants to send a command to all slaves? For example to start a command synchronously on all slaves? With two or three slaves, you can still inform each slave individually. The processing speed is usually so fast that you do not notice a large time difference between the executions on the individual slaves. However, if you use this procedure with 100 slaves, you will quickly find that the last notified slave starts much later than the first slave.

The solution to this problem is the I2C broadcast address. This is an address that every slave on the bus listens to. To activate this you only have to set a control bit. At least that’s how it works with Arduinos based on Atmel processors. 🙂

How you can set this bit is described in the following article.

Activate I2C broadcast for I2C slaves

The broadcast address “0” is available so that all participants on the I2C bus can be notified. The zero is therefore permanently defined as the address for broadcast messages and should not be used by any other slave.

By default, a microcontroller does not respond to this address. This must first be activated by setting the corresponding control bit. All you have to do is insert the following line into the “setup ()” function of your program code.

TWAR = (adress << 1) | 1;  // enable listening on broadcast messages

In a complete I2C initialization function, for example, this could look like this

void init_i2c()
  TWAR = (adress << 1) | 1;  // enable listening on broadcast messages
  Serial.print(F("I2C: I2C-Slave initialized at address: "));

General information about I2C and interrupts

First a few short words about the handling of I2C interrupts.

An event on the I2C bus always calls an interrupt, which interrupts the regular flow of your microcontroller. The program execution therefore jumps from the execution of the regular program into the interrupt routine. You should keep your stay in it as short as possible. Therefore it is "common practice" to set a "flag" within the interrupt routine. This "flag" is actually just a Boolean variable, which is set to "true" there. Back in the main program flow of your microcontroller, you can now regularly check whether this variable is set.

If so, you can now read out the received data or commands and react accordingly.

By the way: You shouldn't use any "Serial.print ..." or similar time-consuming commands within the interrupt routines.

Additional information

I hope everything worked as described. If not or you have any other questions or suggestions, please let me know in the comments. Also, ideas for new projects are always welcome. 🙂


P.S. Many of these projects - especially the hardware projects - cost a lot of time and money. Of course I do this because I enjoy it, but if you appreciate it that I share these information with you, I would be happy about a small donation to the coffee box. 🙂

Buy Me a Coffee at

Leave a Reply

Your email address will not be published. Required fields are marked *