i'm using this serial topology to connect my Act ATmega328p (Arduino) slaves together to a master ESP32.

They are in total 10 ATmega328p slaves and one master:

serial topology

With this type of wiring i'm sure that only one slave per time will talk back to master (when requested), now here comes the problem, basically i need dynamic addressing of slaves: they can be many (up to 30) and i need to access in order the slave devices.

In order to solve the "dynamic addressing" problem i'm thinking about that:

The first slaves will be plugged in and will send to the master his default address (i.e.: 0xFF) The master will check if the address is used and then reply back to the device with the first available address (i.e.: 0x01) The slave will get back the response from the master and set an internal variable with the new address (maybe it's a good idea to store the variable in a EEPROM like M24C02: enter link description here) Starting from the next cpu cycle, the master will ask sequentially to all enumerated slaves to (for example) do an analog read using this workflow:

The master will send a message to the serial bus with address 0x01 asking to do an analog read The respective slave will handle the request, read the analog input and then reply back to the master I think that this can work, but i've not found any kind of this implementation...maybe i'm totally out of mind?

New idea and concept

Guys, what do you think about sequentially power up the circuits and using the same mechanism to assign dynamically the address?

This assumption will avoid me to save the assigned id to the eeprom and some other id software function defined functionalities like address reset, etc...

It can be a good (and dynamic) method to solve the problem, about that, how I can implement this sequential power up?

  • I would probably solve the problem using the I2C bus. Each I2C slave would have external inputs to determine the unique address. For 30 devices you would need 5 input pins on each STM board to determine the address. Maybe this is not what you are looking for, so look at my second proposal. The second solution could be to use the SPI bus, where ESP32 would communicate with the devices using a chip select signal (CS, SS). I would solve the problem with the lack of pins on the master development kit (ESP32), for example, using an I2C expander. – JSC May 29 '20 at 20:04
  • I forgot to mention that your solution, unlike mine, is non-standard. – JSC May 29 '20 at 20:11
  • I know man that is a non standard solution, but I have a constraint: from the ESP32 to the slave clients I can only have 4 wire cable: Vcc, gnd, RX and TX – Symon May 29 '20 at 20:19
  • In that case you can still use I2C. – JSC May 29 '20 at 20:22
  • How? Please give me an example: I need dynamic addressing of slaves and I can't place any dip switches on the PCB – Symon May 29 '20 at 20:29

The first slaves will be plugged in and will send to the master his default address

If this happens while the master is talking to one of the other slaves, you risk a collision.

I suggest a very small variation on your scheme. Each slave has a default address (0xff). When you turn it on, it doesn't say anything. The slaves only talk when they are addressed.

Every now and then, the master sends a “ping” message to 0xff. The newly installed slave responds, then the master gives it a real address, and goes back to its business of collecting sensor data. If no response is received for the ping within some specified timeout, the master assumes there is no unconfigured slave.

Since no slave ever sends unsolicited messages, this should avoid any kind of collision. You just have to make sure that new slaves are added at a slow rate: after connecting a slave, you wait for slightly more than the ping period before connecting another one.

  • Thanks Edgar, your answer is directly what I was looking for! I'll implement like that, I have two remaining question: 1.) how I can implement the "ping" code and manage the dynamic addressing, are you able to give me an example on your post? 2.) You think that a "conditional" power up of slaves (driving a MOSFET from gpio to give Vcc to relative cascading slave when the preceding slave will obtain the address) will help me to avoid to power up the system and then add manually the devices one by one? – Symon May 29 '20 at 20:24
  • You will probably have to invent your own protocol, where each message sent by the master has a slave address, a message type (ping, configure new address, data request), possibly a data field (the address being given) and maybe a checksum. – Edgar Bonet May 29 '20 at 20:28
  • Yes, sure! It can be feasible without so much effort...the only doubt that I have is about the serial ping: you suggest to use functions like serial write and serial.available? Or I have to implement some dumb loop that listen on serial for a specific amount of time? – Symon May 29 '20 at 20:35
  • 1
    For implementing the timeout, you can check Serial.available() for a specific amount of time. – Edgar Bonet May 29 '20 at 20:53
  • Upvote, this answer confirm me what I was thinking! – Symon May 29 '20 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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