Pages: [1] 2 3   Go Down
Author Topic: Due I2C not working  (Read 11600 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Guys,

I am having trouble interfacing a 5v SRF02 sonar sensor with the Arduino Due. I have tried a few different level shifters, resistor combinations, and both I2C ports on the Due. Using an oscilloscope I can see the signal is being sent. There is just no response from the sensor.

Main program
Code:
//External Libraries
#include <Wire.h>

//Internal Headers
#include "IC2Sonar.h"

//Generate Sonar Objects
IC2Sonar sonar0(113);
IC2Sonar sonar1(114);
IC2Sonar sonar2(115);
IC2Sonar sonar3(116);

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  delay(2000);
}

void loop()
{

  //SONAR DEBUG - must be running updateMap to fill SonarArray
   Serial.print(sonar0.updateSonar());
   Serial.print(" ");
   Serial.print(sonar1.updateSonar());
   Serial.print(" ");
   Serial.print(sonar2.updateSonar());
   Serial.print(" ");
   Serial.print(sonar3.updateSonar());
   Serial.println();
 
   delay(500);
}

Class for each sonar sensor
Code:
#ifndef __IC2SONAR_H__
#define __IC2SONAR_H__

#include "Arduino.h"
#include <Wire.h>

class IC2Sonar {

  //Define class variables (private)
  int sensorVal;
  int sensorAddress;

public:
  //Constructor
  IC2Sonar(int address)
  {
    sensorVal = 200;
    sensorAddress = address; 
  };

  int updateSonar()
  {
    Wire.beginTransmission(sensorAddress);
    Wire.write(byte(0x00));                               
    Wire.write(byte(0x51));           
    Wire.endTransmission();

    delay(100);
   
    Wire.beginTransmission(sensorAddress);
    Wire.write(byte(0x02));                                           
    Wire.endTransmission();
   
    Wire.requestFrom(sensorAddress, 2);
 
    if(2 <= Wire.available())
   {   
      int reading = Wire.read();
      reading = reading << 8;    // shift high byte to be high 8 bits
      reading |= Wire.read();
      sensorVal = reading;
   }

    return sensorVal;
  }

private:
};

#endif // __IC2Sonar_H__

This program works fine on a Mega and returns 200 for each sensor not plugged in. The Due, however, always returns 0 for each sensor.
Your help is appreciated
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Update:

I have gotten the sensor to work without level shifting it (as sda/scl are open collector). However it is very temperamental and will sometimes just not work at all. I also cannot get four devices to work, three will but not four. Again, this code and these devices work fine together on a Mega.

Does anyone have any experience with similar I2C problems?
Logged

London, UK
Offline Offline
Newbie
*
Karma: 1
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, I've encountered I2C problems as well. And I am not the only one,

It seems the I2C implementation on Due is still not working 100%, possibly because of issues with the implementation of the wire library.
Logged

White River Junction, Vermont USA
Offline Offline
Full Member
***
Karma: 5
Posts: 106
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Need more details on the range finder (I've never used this one).  If it's a 5V uC on that board, then the I2C i/o may not play well at 3volt levels.  'Open-collector' has nothing to do with this specification.  Open-collector is a requirement of the I2C bus.  The voltage requirements are determined by the devices that you connect to the bus, and they must agree.

I expect that all bets are off if you don't use a level translator.  I believe that the Mega is a 5v system, and that's probably why the SRF02 works with that version.

I don't see any commands in your code that shouldn't work with the present, but limited, capabilities of the wire library.

Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 619
Posts: 33966
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I have gotten the sensor to work without level shifting it (as sda/scl are open collector).
Yes they are but what are they being pulled up to?
If it is more than 3V3 at the sensor end then they need some level translation. If there is no pull ups then you need some, but only to 3V3 on the Due end.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I was under the impression the Due had internal 3.3v pull up resistors on the I2C lines. Is this correct? Nevertheless it is working as is, but just not 100% reliable
Logged

White River Junction, Vermont USA
Offline Offline
Full Member
***
Karma: 5
Posts: 106
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I was under the impression the Due had internal 3.3v pull up resistors on the I2C lines. Is this correct?
Yes, this is correct.  However, the DUE is a 3.3V system, and the SRF is most-likely a 5V gadget.  If that is the case, then you need a level translator, as the logic thresholds probably do not agree.
Quote
Nevertheless it is working as is, but just not 100% reliable
That is the point.

-Chris
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well this is where it gets confusing. It works without the logic converter. But not with the logic converter. I have tried two types of logic converters, both bi-directional. The signal comes through but the Arduino will not cooperate
Logged

Offline Offline
Full Member
***
Karma: 7
Posts: 144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What speed are you running the I2C at? I imagine that with different pullups and level translators there could be a lot of skew on the edges.

ETA: this link http://www.robot-electronics.co.uk/forum/viewtopic.php?f=2&t=250 suggest that the SFR02 uses a PIC micro with VIH = 4V, so a pullup to 3.3V seem to be insufficient. In that case though, I can't see how it works at all, unless there is a lot of tolerance on that 4V level.

I'm keen to find out if level translation on I2C is practical, or whether I need to spec peripherals to be 3.3V only. Unfortunately I haven't the kit to play with at the moment.
« Last Edit: February 06, 2013, 08:39:25 pm by bobcousins » Logged

Please don't PM me asking for help. Ask questions in the forum.

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm facing a similar problem: I have a Mega and a Due side-by-side, and am testing a Chronodot and a Wii nunchuck dongle, each running at 3.3V.

Using I2Cscanner (http://playground.arduino.cc/Main/I2cScanner) I can easily get the address of either device (just testing them independently) using the Mega and pins 20/21. The Due fails to see these devices with the same program via the same pins 20/21. No additional pull-ups were used (my understanding is that the appropriate resistors are already present on both boards). Since the peripherals are running at 3.3V Vcc shouldn't be an issue (if anything, I'd expect the Mega to have issues with that, NOT the Due!)

Considering I just spent a fair chunk of change on the Due, I'm a little surprised this is not working... I'm particularly worried that it's some sort of hardware issue (hopefully if it's a software thing that'll be fixed sooner than later). And I've been careful not to overload these inputs. I'm eager to see what you learn...

Note: the software was compiled with the 1.5.1r2 compiler, as well as the 1.5.2-beta (which I rebuilt a day ago). No difference, same symptoms.

Logged

London, UK
Offline Offline
Newbie
*
Karma: 1
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Using I2Cscanner (http://playground.arduino.cc/Main/I2cScanner) I can easily get the address of either device (just testing them independently) using the Mega and pins 20/21. The Due fails to see these devices with the same program via the same pins 20/21. No additional pull-ups were used (my understanding is that the appropriate resistors are already present on both boards). Since the peripherals are running at 3.3V Vcc shouldn't be an issue (if anything, I'd expect the Mega to have issues with that, NOT the Due!)

I had the same issue with a digital pot from Microchip. The pot is -according to the datasheet- 3.3V compatible but I2CScanner can't find it, it work fine with an Uno (running the pot at 3.3V).

However, I AM able to write to and control the pot from the Due , so in the end it was not a problem. The moral of this story is that it might still work even if  I2Cscanner can't find the device.
Logged

White River Junction, Vermont USA
Offline Offline
Full Member
***
Karma: 5
Posts: 106
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I2C Scanner will not work as expected with the DUE.

endTransmission is flawed with 1.5.1r2

See:
   
(I2C) Wire.endTransmission always returns non-zero.
: http://arduino.cc/forum/index.php/topic,144700.0.html

-Chris
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I2C Scanner will not work as expected with the DUE.
endTransmission is flawed with 1.5.1r2
See:(I2C) Wire.endTransmission always returns non-zero. : http://arduino.cc/forum/index.php/topic,144700.0.html
-Chris
Whoa... thanks for the info. This is definitely a non-trivial issue then. I might've waited on the Due had I known that something as fundamental as I2C isn't actually working yet.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I first noticed that the Due has two 1K pull up resistors on SCA and SDA pins when I read its schematic.  I have not seen an I2C circuit that uses such low value pull-ups and initially I thought that it was a misprint, but late verified that the schematic matches the PCB for the resistor value.  I don't know why the controller needs such low value pull-ups. These two 1K pull-ups may be the cause why the I2C works better without a 3.3V to 5V logic translator.  A translator chip like the TXB0108 has an internal 4K current limit resistor in series with each I/O pin.  It won't pull down an input pin low enough to make it as logic "0" if the input pin has a 1K pull-up, it does not even work if the pull-up is 10K. It requires a 50K pull-up as it's recommended by adafruit  http://www.adafruit.com/products/395  so it can pull the input pin down to (3.3V x 4)/54=0.135V.  If you use a MOSFET circuit as a logic level translator, the 5V output may not be able to pull down the 3.3V input lower enough to make Vgs large enough to fully turn on the MOSFET.   https://www.adafruit.com/products/757

If you are brave enough you can remove those two pull-up resistors, the I2C circuit should start to work normally. You can always add pull ups externally. The resistor network that contains the 2 pull up resistors has 4 resistors in its package, fortunately other two resistors are not used.

Without a proper 3.3V to 5V voltage translator, a 5V output pin will still be able to control a 3.3V input via a current limit resistor, but it will stress the microcontroller.  It may work for a while, but eventually the controller will die if the resistor value is not very large.  The lower resistance the shorter controller's life. With a 1M resistor it may last forever, with a 10K resistor it may last years, but with an 1 ohm resistor it may only last a few seconds.  That's one of reasons why some electronic products don't last forever.  So a proper voltage translator is important to prevent a microcontroller from degrading.
   
Just FYI,  the UNO does not have on-board pull-up resistors on I2C pins and the MEGA has two 10K pull ups, not 1K.


Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just so it's clear, in my case I'm interfacing 3.3v I2C peripherals - no translation required. And the TXB0108 is specifically NOT recommended for I2C.

Per Chris, I think the main problem is the broken library. I'm beginning to wonder how you can ship a board without any library to test it against to validate the design (if I2C is incorrectly implemented here in terms of the circuitry - there could be valid reasons for the unusually low resistance - then it should have shown up in pre-production testing... but how was it tested without a working Wire library?)

Edit: I just opened the board file and the schematic as well. The pull-ups for SCL0-3/SDA0-3 (RN5) are marked 1K5 on the schematic (1.5Kohm?), yet the actual part on the board is marked 102 (1Kohm?) and the unused pins do measure at almost exactly 1Kohm. So, either they meant to have a 10K in there, or a 1.5K, or a 1Kohm resistance. Strange...

Edit2: And yes, the Mega does indeed have a 103 (which should be a 10Kohm part).
« Last Edit: February 08, 2013, 06:53:03 am by MartyMacGyver » Logged

Pages: [1] 2 3   Go Up
Jump to: