i2c project; started on UNO and switched to MEGA; not working anymore

Hi all,

I started an i2C project on an UNO (reading Sensirion senors). In parallel I had a project running on a MEGA (with Ethernet and WLan-router, LCD on I2c). After the Sensor reading was functioning on the UNO I wanted to transfer the code to the MEGA. It didn´t work… Then I removed all SW on the MEGA and flashed the UNO SW into (still having the Ethernet shield connected). After some time I detached the i2C-Display lines to avoid problems.
Still it´s not possible to read the sensor…
A skope reveals, that the MEGA stops after writing the “trigger measurement”. This will be followed by a read-Sensor command (on the UNO) but doesn´t happen on the MEGA.
For reference I attached a screenshot. Any Ideas why the track right of the red line is different (although the SW & Sensor is the same)???

Any Ideas?!?

Do you have the code that produced this output? From the picture it doesn't look like a hardware problem, so we need to see the software to find an error.

Hi Pylon,

thanks for offering help. I found the root cause (although not understanding it). I was using SHT2x lib (github) and it was working for SDP6xx sensors after some modifications. After switching from UNO-R3 to Mega the same code stopped working (I was aware of the different pins for i2c...) After replacing following lines with the delay(100) it worked:

delay(100); //new; replacing "digitalRead(18)", which is not working on MEGA /*wait for measurement to complete. timeout= millis()+300; while (!digitalRead(18)) { if (millis()>timeout) { return 0; } } //end wait digitalread */ I guess the code was checking the ACK-bit but I´m not sure..

Thanks a lot, Barny


for all who want to talk to sensirion sdp sensors (quite similar to sht sensors).

Feel free to base on my file (and let me know if you like).


SDP6xx.h (2.88 KB)

SDP6xx.cpp (4.28 KB)

simple_PA_read_float_sort_01_working.ino (2 KB)

In this code the original programmer made some "don't"s

     /*wait for measurement to complete. */
     timeout= millis()+300;
     while (!digitalRead(18)) {
          if (millis()>timeout) {
               return 0;
     } //end wait digitalread

digitalRead(18) is the same as digitalRead(A4) which reads the status of SDA on an UNO (but not on a Mega2560). You should always use the A4 version because then the reader sees (without knowing the Arduino library internas) what the programmer wanted to do. Then he did the timing wrong. that code should read:

     /*wait for measurement to complete. */
     uint32_t saved_millis= millis();
     while (!digitalRead(SDA)) { // SDA is defined in pins_arduino.h
          if (millis() - saved_millis > 300) {
               return 0;
     } //end wait digitalread

That way it works correctly even if the millis() value overflows and it works on all supported Arduino models.