Code snippet: Arduino - Enable I2C broadcast receive

I2C is the bus protocol or technology through which a microcontroller can communicate with many sensors or actuators. Thanks to the right software you can also connect different microcontrollers with each other and let them communicate. Normally there is only one master per I2C bus which can request data from or send data to a slave via the corresponding address.

In one of my projects I wanted or want to connect several microcontrollers. Several bus technologies are suitable for this. But I2C is almost the only one which is directly supported by almost every modern microcontroller without additional hardware requirements. So the choice was quickly made for I2C.

So by means of I2C several microcontrollers were or are now connected with each other. The master can easily send a command to each individual slave. To do this, simply send the slave address and the desired command over the bus. All slaves listen to what is going on on the bus, but only the slave with the corresponding address reacts to the sent command. 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 don't notice a big time difference between the executions on the individual slaves. However, if you use this method with 100 slaves, you will quickly notice that the last notified slave starts much later than the first slave.

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

How to set this bit is described in the following article.

Enable I2C broadcast for I2C slaves

Damit alle Teilnehmer auf dem I2C-Bus pauschal benachrichtigt werden können, gibt es die Broadcast-Adresse “0”. Die Null ist also fest definiert als die Adresse für Broadcast-Nachrichten und sollte auch von keinem anderen Slave genutzt werden.

Standardmäßig reagiert ein Mikrocontroller aber nicht auf diese Adresse. Dies muss erst aktiviert werden indem Ihr das entsprechende Kontroll-Bit setzt. Dazu müsst Ihr lediglich folgende Zeile in die “setup()” Funktion Eures Programmcodes einfügen.

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

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

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

General information about I2C and interrupts

Ein Ereignis auf dem I2C-Bus ruft immer einen Interrupt auf, welcher den regulären Ablauf Eures Mikrocontrollers unterbricht. Die Programmausführung hüpft also kurz aus dem Ablauf des regulären Programms in die Interrupt-Routine. Den Aufenthalt darin solltet Ihr aber so kurz wie möglich halten. Deswegen ist es “gängige Praxis” innerhalb der Interrupt-Routine eine “Flag” zu setzen. Diese “Flag” ist eigentlich nur eine Boolsche Variable, welche dort auf “true” gesetzt wird. Zurück in dem Haupt-Programmablauf Eures Mikrocontrollers könnt Ihr nun regelmäßig prüfen ob diese Variable gesetzt ist.

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

Übrigens: Ihr solltet innerhalb der Interrupt-Routinen auch keine “Serial.print…” oder ähnliche Zeit-fressenden Befehle nutzen.

Further information

Have fun with the project

I hope everything worked as described for you. If not or you have questions or suggestions please let me know in the comments. I will then add this to the article if necessary.
Ideas for new projects are always welcome. 🙂

PS 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 think it's cool that I share the information with you, I would be happy about a small donation to the coffee fund. 🙂

Buy Me a Coffee at       

Kommentar hinterlassen

Your email address will not be published. Erforderliche Felder sind mit * markiert

The reCAPTCHA verification period has expired. Please reload the page.