I2c master and slave in the same board

I need to use two i2c ports on the same arduino board, so that the arduino is master and slave at the same time. Is this possible?

Hi, @cesarp_ard
Welcome to the forum.

Please read the post at the start of any forum , entitled “How to use this Forum”.

To add code please click this link;

We will need a bit more info on your application, many I2C devices can exist on the one bus, can you explain the various master - slave combinations you want?

Thanks… Tom… :grinning: :+1: :coffee: :australia:

Some details of your project might help us to help you further.

From memory, there are boards that have two I2C interfaces; I think the Due is one of them. There is also a software I2C library; I’ve never used it.

I am making a linear network of sensors, so to communicate between adjacent nodes I need each board to be slave of one node and master of another for example:

  • node X [n] is: master node X [n-1] and slave of node X [n + 1]
  • node X [n-1] is: master node X [n-2] and slave of node X [n]

Thanks for the info.
How far apart are the nodes, not too far for I2C comms?

Tom… :grinning: :+1: :coffee: :australia:

1 Like

Using the broad definition of an arduino board the answer is yes! ESP32 has two I2C controllers (also referred to as ports) which are responsible for handling communications on two I2C buses. Each I2C controller can operate as master or slave. As an example, one controller can act as a master and the other as a slave at the same time. In your comment you state a network of sensors, this will be dodgy at best unless you use the correct hardware for teh I2C interface. The ESP32 gives you wifi, bluetooth etc.

That isn’t how a bus works. You have described a daisy-chain.

1 Like

It is even possible with Arduino Uno boards. Set the Uno as a Slave and use other pins with a Software I2C library to be a Master for the next Arduino board.

@cesarp_ard A “network” with I2C might not be the right choice. The I2C bus is to get data from a sensor.

The I2C bus was, and still is used for a lot more than sensors.
From the Embedded Systems Academy:

The I2C bus was developed in the early 1980’s by Philips Semiconductors. Its original purpose was to provide an easy way to connect a CPU to peripheral chips in a TV-set. … I2C is an acronym for Inter-IC bus. Its name literally explains its purpose: to provide a communication link between Integrated Circuits.

I use I2C between two processor boards (an Uno and a Wemos D1 Mini) in a recent project.

If you tell us what you really want to do, and why you think it has to be daisy-chained I2C, we may have a better idea. What you have described so far is a bit odd.

Can you draw a basic block diagram of what you have in mind please?

Thanks… Tom… :grinning: :+1: :coffee: :australia:

Untitled Diagram

Each node on the network has a sensor that captures data every minute. I am already testing with an esp8266 module, but the power consumption is high, therefore I wanted to try the i2c protocol to bring the data from the nodes to the gateway (node [1]) and how the distance between the nodes is maximum one meter there should be no problems.

right now I have only esp8266 and arduino uno, both only have an i2c port, which code could I use to implement i2c by software for these two boards.

Why can’t you have one master and the rest slaves?
That is how I2C is designed.
The master and the slaves each have unique addresses.
The master polls any slave by calling it on the I2C BUS.
The master and slaves ALL share the same I2C BUS.

Tom… :grinning: :+1: :coffee: :australia:

because the number of nodes is very high, I have at least 1000 nodes, and each node would be at an increasing distance assuming 1000 nodes the last one would be one kilometer away and i2c would not work in that scenario


In that case a 1km I2C would be impractical.
Daisy chaining would also be impractical.

You need to have a major communication backbone.
A group of sensors connected to their own masters using I2C, we will call these “remote”.
The “remote” masters then communicate with the “central” master via a comms bus suitable for your approx 1km system.

Can you tell us exactly what the application is?
What are the sensors and how many?
What environment your project will be operating in (essential for type of comms bus)?
What will you be doing/how displaying the sensor information at the end of your project?

Tom… :grinning: :+1: :coffee: :australia:

The project will be operating in underground pipes, therefore I have to operate with batteries. I am using json to save the data and send it between nodes. the gateway sends it to a platform through websocktes for processing.

Don’t you love it when the OP tells you about their project in drips.

Eventually he will tell us what his project is and we can then give practical advice.

1 Like

The high level of the I2C bus is made with a pullup resistors. Therefor the bus signals are weak. You don’t need the specific advantages of the I2C bus, so I suggest to use something with stronger signals. The Wire library also has blocking functions, that will be a real problem when using 1000 nodes.

The cascading will make it slow. I hope that is not a problem.
You could use RS-485, and use it for many (or all) the nodes.
Perhaps digital Serial/UART signals can be used, with cascading or not and perhaps with optocouplers.
If every node has its own battery, then using fiber optics can be an option.

Does every node has to be the same ? I hope not.
Perhaps the number of nodes can be reduced by applying 10 sensors to an Arduino board, instead of a Arduino board for each node.
Perhaps there can be a controller at the end or a few controllers along the route.

In my opinion, there should be two reliable ways to transfer the data. With 1000 nodes, the is risk is too high that a node or a wire will fail and that information from sensors will get lost.

RS-485 gets my vote too. There’s a feature in the Atmega328P UART called Multi-processor Communication Mode. Allows you to address UART slaves on a common bus (RS-485). It sorta becomes I2C like. It’s on page 195 of the Atmega328 datasheet

node[1] sends the address byte, and then go to receiver mode right away. When a slave is addressed, have it programmed to reply with requested data within a few millisecond (to give time the node[1] to transition to receiver mode), then go receiver mode after each transmission.

Since distance between nodes and “gateway” (up to 1 Km) and nodes number (up to 1000) can be very high, you might consider a CAN bus or several CAN buses.

A CAN Bus can have unlimited amount of “ECUs ~ nodes” on it if the system is designed to handle that amount. A CAN bus is a differential bus (such as RS 485) and as such is very efficient to avoid EMI you are certain to receive on any long distance bus.


One of my votes.

The other vote.

I am not familiar with the range of ethernet but power over ethernet as your backbone would make this easier power wise for each node.

Tom… :grinning: :+1: :coffee: :australia: