Multiple I2C slaves


#1

Hello,

I am new to EV3 and Arduino. I have tried to connect an Arduino via I2C to the EV3. I am using the 1-byte read blocks and this is working fine.

The setup is as follows:

Arduino 1 is connected to a temperature/humidity sensor (DHT11).
Arduino 1 transmits temperature and humidity wireless to Arduino 2.
Arduino 2 receives data and sends temperature and humidity separately to the EV3. I use address 0x04 for temperature and 0x05 for humidity.
The EV3 receives data and sends it to the display.

This setup works as long as I use only one 1-byte read-block in the EV3. Thus I can only display temperature or humidity.
However, if I try to use multiple 1-byte read-blocks with two different slave addresses in order to display temperature and humidity at once, the EV3 program mixes the data of both blocks. As a result temperature and humidity is being displayed on the same spot on the display and flickers.
If I look into the “wires” between the blocks of the EV3 program while running, I can see that humidity and temperature are being transferred at once on every block. Thus it does not matter if I use the correct slave address on the read-blocks. Temperature and humidity are on the wires of every block at the same time .

See the attached code and the picture at the bottom.

I would be grateful if someone could help me figure out this problem.

Arduino 2 code:
#include <Wire.h>
#define SLAVE_ADDRESS_T 0x04
#define SLAVE_ADDRESS_H 0x05

#include <RH_ASK.h>
#include <SPI.h> // Not actualy used but needed to compile

RH_ASK driver;

int temp;
int hum;
char data[6];

void setup()
{
  pinMode(13,OUTPUT);
  Serial.begin(9600);
  if (!driver.init())
    Serial.println("Initialization failed");
}

void loop()
{
  // Receive data from Arduino 1 and split data
  
  memset(data, 0, sizeof(data));

  digitalWrite(13, HIGH);  
  
  uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
  uint8_t buflen = sizeof(buf);

  if (driver.recv(buf, &buflen))
  {
    int i;
    for (i=0;i<buflen;i++)
      {
        data[i] = char(buf[i]);
      }
    sscanf(data, "%d,%d", &temp, &hum);
  }
  
  // Transfer temp and hum to the EV3
  
  Wire.begin(SLAVE_ADDRESS_T);
  Wire.onRequest(sendtemp);
  delay(100);
  Wire.begin(SLAVE_ADDRESS_H);
  Wire.onRequest(sendhum);
  
  digitalWrite(13, LOW);
  delay(400);
  
  Serial.println(temp);
  Serial.println(hum);
}

void sendtemp ()
{
  Wire.write(temp);
}

void sendhum ()
{
  Wire.write(hum);
}

#2

Let me reformulate my problem in order to make it easier to understand.

This is how it should be:

Data from Sensor A --> Arduino 2 (Slave address 0x04) --> I2C --> EV3 read-block (0x04) --> provides only data from Sensor A
Data from Sensor B --> Arduino 2 (Slave address 0x05) --> I2C --> EV3 read-block (0x05) --> provides only data from Sensor B

Both processes happen at the same time and are printed on the same display on different spots.

Display output:

| A |

B

(A=temperature) (B=humidity)

This is how it is:

Data from Sensor A --> Arduino 2 (Slave address 0x04) --> I2C --> EV3 Dexter 1byte read-block (0x04) --> provides data from Sensor A and B at the same time
Data from Sensor B --> Arduino 2 (Slave address 0x05) --> I2C --> EV3 Dexter 1byte read-block (0x05) --> provides data from Sensor A and B at the same time

Display output: (flickering and changing from A and B and vice versa)

| A/B |

A/B

Hope this helps to better understand the problem.