I2C module + I2C communication between Arduinos

Hello everyone, this has been a question I sought for recently and I cannot find any similar problem and solution to this: using multiple i2c modules and sending data via i2c between 2 arduinos.

This is what I am working on, a
Master Arduino with

  • I2C module + LCD 20x4
  • 128x64 Graphics dot LCD running serial
  • I2C DS1307 RTC module

where 128x64 to display time and sensor value and i2c LCD on standby(printing hello world)

and a Slave Arduino with DHT22

The thing is I successfully send float values through wire i2c to the master device, let it run for some time (10 minutes i guess) and the display on 128x64 stops to display the time (blank) and the 20x4 lcd with i2c module starts to print weird characters instead of what it was printing before. I hook up the i2c modules (lcd and rtc) to the other troubleshooting arduino and no chance for the lcd, rtc is fixed. I assume that the i2c wire data from the slave device messed up my i2c modules.

2 weeks later after a rest from the mess, the 20x4 lcd with i2c module seems to working fine again after I tried to test the malfunctioning lcd once more.

Has anyone faced this kind of problem? Also is it possible that the data sent from the slave device is destroying the modules that runs on the same bus?

Image from Original Post so we don't have to download it. See this Simple Image Posting Guide

...R

seighton:
Has anyone faced this kind of problem? Also is it possible that the data sent from the slave device is destroying the modules that runs on the same bus?

Do you mean permanently damaging the modules so they can never be used again or do you just mean that the data is being corrupted?

Assuming the latter, you need to post both programs (master and slave) and describe in as much detail as possible what happens when your run them and what you want to happen that is different.

...R

Robin2:
Do you mean permanently damaging the modules so they can never be used again or do you just mean that the data is being corrupted?

Assuming the latter, you need to post both programs (master and slave) and describe in as much detail as possible what happens when your run them and what you want to happen that is different.

…R

The following code @master:

void i2c_comm(){
    Wire.requestFrom(SLAVE_ADDR,10);
    String datain;
    while (Wire.available()) {
      d = Wire.read();  
      datain += d;
    }
    
    dht22hum = datain.substring(0,5);
    dht22temp = datain.substring(5,10);
}

void setup()
{
  Wire.begin();
  Serial.begin(9600);

  lcd.createChar(1, jsun);
  lcd.createChar(2, jmon);
  lcd.createChar(3, jtue);
  lcd.createChar(4, jwed);
  lcd.createChar(5, jthu);
  lcd.createChar(6, jfri);
  lcd.createChar(7, jsat);

//dht.begin();
  
  lcd.init();         
  lcd.backlight();
  lcd.setCursor(3,0);
  lcd.print("Hello, world!");
  lcd.setCursor(2,1);
  
  u8g.setColorIndex(1);


}


void loop(){

  u8g.firstPage();
  do {   
   // i2c_comm();
    draw();
    timeprint();
    dht22();
  } while( u8g.nextPage() );

while the salve code:

void setup()
{
  Serial.begin(9600);
  dht.begin();
  Wire.begin(SLAVE_ADDR);
  Wire.onRequest(reqDat);
}

void loop()
{
    hum = dht.readHumidity();
    temp= dht.readTemperature();
 
  hdht = String(hum, 2);
}

void reqDat() {

  String hdht = String(hum, 2);
  String tdht = String(temp, 2);
  
  byte tresponse[ANSWERSIZE];
  byte hresponse[ANSWERSIZE];
  
  for (byte i=0;i<ANSWERSIZE;i++) {
    hresponse[i] = byte(hdht.charAt(i));
  }
  Wire.write(hresponse,sizeof(hresponse));


  for (byte i=0;i<ANSWERSIZE;i++) {
    tresponse[i] = (byte)tdht.charAt(i);
  }
  Wire.write(tresponse,sizeof(tresponse));
}

most of the code I did not post was basic display codes and my full code is really messy, or if you want the full code I will post below.

The whole thing was working fine, but just a matter of time before it goes wrong

MASTER

#include <RTClib.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include "U8glib.h"
#include <TimeLib.h>
#include <DS1307RTC.h>
#include <DHT.h>;

#define DHTPIN 4    
#define DHTTYPE DHT22
#define SLAVE_ADDR 1
#define ANSWERSIZE 10

DHT dht(DHTPIN, DHTTYPE);

LiquidCrystal_I2C lcd(0x27,20,4);

U8GLIB_ST7920_128X64_4X u8g(10);

char* monthval[] = {"","JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"};
char* dotw[] = {"SUN","MON","TUE","WED","THU","FRI","SAT"};
char d;

String dht22hum;
String dht22temp;



void i2c_comm(){
    Wire.requestFrom(SLAVE_ADDR,10);
    String datain;
    while (Wire.available()) {
      d = Wire.read();  
      datain += d;
    }
    
    dht22hum = datain.substring(0,5);
    dht22temp = datain.substring(5,10);
}

void timeprint(){
    tmElements_t tm;
   if (RTC.read(tm)) {
    u8g.setFont(u8g_font_9x18);
    u8g.drawStr(48,32,":  :");
    u8g.setPrintPos(30,32);
    if(tm.Hour<10){u8g.print("0");}
    u8g.print(tm.Hour); //hour
    u8g.setPrintPos(57,32); 
    if(tm.Minute<10){u8g.print("0");}
    u8g.print(tm.Minute); //min
    u8g.setPrintPos(84,32); 
    if(tm.Second<10){u8g.print("0");}
    u8g.print(tm.Second); //sec

    u8g.setFont(u8g_font_6x12);
    u8g.drawStr(45,45,"     ");
    u8g.setPrintPos(33,45);
    if(tm.Day<10){u8g.print("0");} 
    u8g.print(tm.Day);   //day
//    monthfunc();
    u8g.setPrintPos(51,45);
//    if(tm.Month<10){u8g.print("0");} 
//    u8g.print(tm.Month);   //month
   
    u8g.print(monthval[tm.Month]);   //month
    u8g.setPrintPos(75,45); 
    u8g.print(tmYearToCalendar(tm.Year)); //year

    u8g.setFont(u8g_font_4x6);
    u8g.setPrintPos(59,64);
    u8g.print(dotw[tm.Wday]);

    
Serial.print("dotw : ");
Serial.println(tm.Wday);
  }
}

void dht22() {
  
    u8g.setFont(u8g_font_4x6);
    u8g.drawStr(0,64,"Hu ");
    u8g.setPrintPos(12,64); 
    u8g.print(dht22hum);    //humidity
    u8g.drawStr(32,64," %");

    u8g.setFont(u8g_font_4x6);
    u8g.drawStr(0,5,"Ta");
    u8g.setPrintPos(12,5);
    u8g.print("00.00"); //average temp
    u8g.drawStr(32,5,"'c");

    u8g.drawStr(0,11,"T0 ");
    u8g.setPrintPos(12,11);
    u8g.print(dht22temp); //dht22 temp
    u8g.drawStr(32,11,"'c");
}

void draw() {
  /*
    u8g.setFont(u8g_font_4x6);
    u8g.drawStr(0,5,"Ta");
    u8g.drawStr(12,5,"00.00"); //average temp
    u8g.drawStr(32,5,"'c");*/
    u8g.drawStr(93,5,"Li");    
    u8g.drawStr(105,5,"00.00");//light intensity
    u8g.drawStr(125,5,"%");

    u8g.setFont(u8g_font_4x6);
    u8g.drawStr(0,29,"JAPN");
    u8g.drawStr(0,45,"CGAS");
    u8g.drawStr(113,29,"RAIN");
    u8g.drawStr(113,45,"MOTN");

    u8g.setFont(u8g_font_4x6);  
    u8g.drawStr(93,64,"AQ ");      //air quality
    u8g.drawStr(105,64,"00.00");
    u8g.drawStr(125,64,"%");

    u8g.drawStr(59,5,"-O-");
}



void setup()
{
  Wire.begin();
  Serial.begin(9600);
  
  lcd.init();         
  lcd.backlight();
  lcd.setCursor(3,0);
  lcd.print("Hello, world!");
  lcd.setCursor(2,1);
  
  u8g.setColorIndex(1);


}


void loop(){

  u8g.firstPage();
  do {   

    draw();
    timeprint();
    dht22();
  } while( u8g.nextPage() );


    lcd.setCursor(0,2);
    for(int i=1;i<8;i++){
    lcd.print(" ");
    lcd.write(i);}

}

SLAVE

#include <DHT.h>;
#include <Wire.h>
//Constants
#define DHTPIN 4     
#define DHTTYPE DHT22   
#define SLAVE_ADDR 1
#define ANSWERSIZE 5

DHT dht(DHTPIN, DHTTYPE); 

volatile byte* FloatPtr;

float hum;  
float temp;
char dhtdata[2]; // 0 is hum; 1 is temp 
bool trig;
String hdht;

void setup()
{
  Serial.begin(9600);
  dht.begin();
  Wire.begin(SLAVE_ADDR);
  Wire.onRequest(reqDat);
}

void loop()
{
    hum = dht.readHumidity();
    temp= dht.readTemperature();

  hdht = String(hum, 2);


    Serial.print("Humidity: ");
    Serial.print(String(hum, 2));
    Serial.print(" %, Temp: ");
    Serial.print(String(temp, 2));
    Serial.println(" Celsius");
    delay(100);

    
}

void reqDat() {


  String hdht = String(hum, 2);
  String tdht = String(temp, 2);
  
  byte tresponse[ANSWERSIZE];
  byte hresponse[ANSWERSIZE];
  
  for (byte i=0;i<ANSWERSIZE;i++) {
    hresponse[i] = byte(hdht.charAt(i));
  }
  Wire.write(hresponse,sizeof(hresponse));


  for (byte i=0;i<ANSWERSIZE;i++) {
    tresponse[i] = (byte)tdht.charAt(i);
  }
  Wire.write(tresponse,sizeof(tresponse));
}

seighton:
The whole thing was working fine, but just a matter of time before it goes wrong

You have not answered the most important question in my Reply #2 "Do you mean permanently damaging the modules so they can never be used again or do you just mean that the data is being corrupted?"

And what is the difference between the code in Reply #9 and in Reply #10 ?

...R

Robin2:
You have not answered the most important question in my Reply #2 "Do you mean permanently damaging the modules so they can never be used again or do you just mean that the data is being corrupted?"

And what is the difference between the code in Reply #9 and in Reply #10 ?

...R

I have no idea, the modules seems to be permanently damaged at first as I tried reset the arduino board, upload a new display test program, plug and unplug the power, nothing changed, but after 2 weeks not using it, when I use it back it just works as intended.

I do not know if its data corruption or permanent damage, the time not using the module is what contributes to the problem and it confuses me. Or probably I don't have enough knowledge of the module. I wish that there are also people out there encountered similar problem so there would be more idea on how it was caused.

The module is based on PCF8574T chip.

Regarding the code replies, you mean reply #3 and #4?
Codes in #3 are simplified codes where I suspect the problem sits while #4 codes are full codes.

The picture I attached here is the same lcd with module in use with my test display program running, after 2 weeks without usage.

lcd.PNG

lcd.PNG

seighton:
The picture I attached here is the same lcd with module in use with my test display program running, after 2 weeks without usage.

Does that mean that the problem appears to have gone away, or is that image produced by a different program?

...R

Robin2:
Does that mean that the problem appears to have gone away, or is that image produced by a different program?

...R

That is with another program, yes, at the same time when I hook it up on my project that I am currently doing(where the problem occurs 2 weeks ago) it also works, by displaying "Hello World" instead the weird characters. The problem seems to have gone away, but I'm afraid its just a matter of time before it go nuts again.

Note that the i2c RTC module was affected as well.

seighton:
That is with another program, yes, at the same time when I hook it up on my project that I am currently doing(where the problem occurs 2 weeks ago) it also works, by displaying "Hello World" instead the weird characters. The problem seems to have gone away, but I'm afraid its just a matter of time before it go nuts again.

I am not familiar with your display module (or indeed any display module). I suggest you run the "another program" for long enough to satisfy yourself that there is no problem with it. That should enable you to focus on the difference between it and problem program if the failure re-occurs.

I assume you are not using a 5v Arduino with a 3.3v display (or vice versa) without appropriate precautions.

...R