I have been searching the internet about how to transfer data from multiple sensors from one Arduino to another using I2C, but no luck. All the code and tutorials online focus on multiple slaves with one sensor.
The attached Fritzing is not my project, but my project involves 22 pressure sensors and I need to log their readings in Excel using plx, so the picture is just for demonstrating the idea.
The main idea is to transfer the sensors' data from the slave to the master over I2C.
Is this doable?
-
1Do you know how to transfer one sensor's data over I2C?Majenko– Majenko08/15/2019 21:07:08Commented Aug 15, 2019 at 21:07
-
yes i did it for one sensortofi– tofi08/15/2019 21:07:35Commented Aug 15, 2019 at 21:07
-
1Then you can do it with more sensors. Just request (and send) more data.Majenko– Majenko08/15/2019 21:08:04Commented Aug 15, 2019 at 21:08
-
2Take your code. Where you request the data for 1 sensor, change that to request the data for more than 1 sensor. Where your code responds with the data for 1 sensor, change it to respond with the data for more than 1 sensor.Majenko– Majenko08/15/2019 21:10:06Commented Aug 15, 2019 at 21:10
-
2Your code is the example.Majenko– Majenko08/15/2019 21:10:13Commented Aug 15, 2019 at 21:10
1 Answer 1
There's two basic ways of doing it.
- Send a byte from the master to the slave first to select which sensor you want, then request the data for the currently selected sensor, or
- Request lots of bytes from the slave and fill them in one at a time as the requests arrive.
The first way is the slow, but "I2C" way of doing it - basically each sensor is its own register in the slave, and you select which register you want to read from.
The second way is faster, but you have to keep track internally of where you are.
A good compromise is to implement "auto increment" which does both scenarios: if you send a byte it sets the current sensor to read from. Then each read returns the current sensor and increments the sensor number internally in the slave, so the next read will get the next sensor.
That way you have a way to "reset" the sensor index that you are reading from, and it's reasonably quick to get all the sensors, since you just need to do multiple reads after a single write.
-
Wire.begin(9);//9 here is the address(Mentioned even in the master board code) Wire.onReceive(xxx); void xxx(int bytes) { l = Wire.read();//Receive value from master board Serial.print("Pressure 12= "); Serial.print(l); Serial.println(" PSI "); } this is the code i used to read data from slave for one sensor which is working fine how to add registory for each sensor?tofi– tofi08/15/2019 21:17:19Commented Aug 15, 2019 at 21:17
-
That looks like you're sending from the master to the slave, not from the slave to the master. You have it backwards.Majenko– Majenko08/15/2019 21:18:49Commented Aug 15, 2019 at 21:18
-
1will research on UART and how to use it Thank youtofi– tofi08/15/2019 21:26:48Commented Aug 15, 2019 at 21:26
-
1on the receiving end of ur code, how is it gonna look to store each sensor data?tofi– tofi08/15/2019 21:30:59Commented Aug 15, 2019 at 21:30
-
1majenko.co.uk/blog/reading-serial-arduino | majenko.co.uk/blog/splitting-text-cMajenko– Majenko08/15/2019 22:23:22Commented Aug 15, 2019 at 22:23