Im working on a project where i have motor controller(arduino nano) being i2c slave. Its job is to get RF24 data and control motor and at same time send data to master about motor... Currently i have 2 ultrasonic sensors for distance sensing and if they are too close it will stop motor. I want to add 2-4 more but it adds a lot of delay because of


command. I want to use attiny as sensor reader and just dump current data when requested.

Is it possible for arduino nano to be i2c slave (hardware i2c) and at same time i2c master (software i2c/please library without using timers... im short on them) or there is way to dump data in less than 2ms via serial or some other protocol? SPI is already used for RF24 and i need RF24 to be up all the time.

  • What is the length of the data to be transmitted? In any case using a UART you can send, at 115200, around 230 bits (23 bytes) in 2 milliseconds (count 8 bits/byte + start and stop bits). At 9600 you can send sligthly less than 2 bytes. Is this enough? Since the attiny just has to check it asynchronously an asynchronous transmission (UART) is better if it has enough bandwidth)
    – frarugi87
    Mar 6, 2018 at 10:28
  • I would need around 6 bytes, struct of 6 bytes (6 ultrasonic sensor readings), is there Serial_Anything like I2C_anything?
    – Nicky
    Mar 7, 2018 at 19:02
  • What do you mean? Serial.write(buf, len) and, well, the reading has to done byte by byte using the Serial.read() function
    – frarugi87
    Mar 9, 2018 at 7:55
  • Oh thanks, i didnt know that i could use (buf, len).
    – Nicky
    Mar 11, 2018 at 18:26

1 Answer 1


On simultaneous master/slave: Yes and no. You can initiate the Wire library as slave and despite of than using Wire.beginTransmission() and it's siblings to send as a master. The library will configure the hardware as master only for the time it needs to send. But there are 2 problems:

  1. When you have multiple masters on the same bus, they have to continuously watch the bus, so that they are only starting a transmission, when the bus is free. The Wire library doesn't seem to do that.
  2. If two masters start their transmission at the same time, the data will be good, as long all transmitted bits are equal. For other cases the I2C protocol has a rule: When 2 masters start a transmission at the same time, the first one to write a 1 to the bus (HIGH value) has to immediately back of to slave mode. (This is because to write a 1 to the data line the pullup resistors are used. When another master tries to write 0 to the bus (LOW value on SDA) he wins against the pullups, effectively writing 0 to the port, and not 1)

As mentioned on this Github issue there are some workaround, though I haven't tested them myself until now. All in all I2C multi-master may be to complex at this point.

As another option, you can use an extra line, connecting master and slaves (use the internal pullup on the masters side). Any slave can at any time write a LOW to this line, triggering the master to request data from all slaves (maybe via external interrupt or pin change interrupt).

Note: I don't know, for what purposes you used all your timers, but maybe there is a way to share one of them with other hardware, so that you don't have to use the blocking pulseIn() function. Or maybe you can tweak the timeout value of the function to a fitting value.

  • One timer is used for servo, other is used for millis and delay and pwm for controlling motors, third one is used by a some library i think.... Anyways in loop i have to read 5 ultrasonic sensors and it has to finish whole loop in less than 10ms, im scared that if i add too many ultrasonic sensors(max 6) that it will lag(be delayed because of 6x pulsein, i have timeout set to 60cm).
    – Nicky
    Mar 7, 2018 at 19:01

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.