How do you program an Arduino Uno to request data over I2C, sending parameters defining the request?

I'm trying to program Arduino A to read a sensor value from another Arduino B, and it first needs to tell B which sensor to report, but I can't find any easy way to do this with Arduino's Wire library. There doesn't seem to be anything like Wire.requestFrom(addr, 1235, 2) which would then call a handler on the slave the looks like onRequest(sensor_id).

I found this page on how to push data one way to a slave, and I found this page on how to request data from a slave without specifying any input, but I can't find anything in between.

Is the solution to use the first method to set the parameters, and then the second to get the data? If so, that seems very circuitous. Is there a more direct method?


Its the way the protocol is designed. From the very first byte that is sent, you already indicate if you want to read or write data. You can't do both. The protocol is a lot more straightforward this way, one transaction at a time. Plus it gives the feel of a data frame, with START and STOP bits delimiting the frame (not regarding repeated STARTs).

But even for a full-duplex protocol like SPI, you've already said that the master must first send certain parameters to the slave. Nothing can be sent by the slave until it has received these parameters and processed them, full-duplex comms or not. Only after this, can it reply with the requested data.

The Wire methods are designed to send data in one direction at a time, as it should be. You could, of course, create a function like the one in your question, but it would inevitably involve the same process of first writing to the slave and then reading.

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.