Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

I2C interface

I2C interface with arduino

  • reason

    Votes: 0 0.0%
  • solution

    Votes: 0 0.0%

  • Total voters
    0
Status
Not open for further replies.

mikeOJ

Member
Hi all

Im trying to get the hang of the I2C port on the arduino UNO
i followed this tutorial:


however when i try to implement the code my serial monitor remains blank.


Oddly enough though if i change this line

Code:
 while(Wire.available() == 0){};
//get the temp
int c = Wire.read();


to while(Wire.available() == 1){};

i get readings to my serial monitor however it is only ever -1C and never changes

the full code is shown below.
any help would be much appreciated


Code:
//include i2c library
#include <Wire.h>

int temp_address = 72;

void setup()
{
  Serial.begin(9600);
 
  Wire.begin();  // initialize listening on I2C bus
}



void loop()
{
  //send a request
  //start talking
Wire.beginTransmission(temp_address);
// ask for register zero
Wire.write(0);
// complete transmission
Wire.endTransmission();
// request 1 byte
Wire.requestFrom(temp_address,1);
// wait for response
while(Wire.available() == 0){};
//get the temp
int c = Wire.read();
//convert celcius to farenheit
int f = round(c*9.0/5.0+32.0);
//print the results
Serial.print(c);
Serial.print("C,");
Serial.print(f);
Serial.println("F");
// delay, then repeat
 
  delay(500);
 
 
}
}
 
This is the code direct from www.tronixstuff.com

C:
#include "Wire.h"
#define cn75address 0x48 // with pins 5~7 set to GND, the device address is 0x48

void setup()
{
   Wire.begin();// wake up I2C bus
   Serial.begin(9600);
}

void getCN75data(byte*a,byte*b)
   {
   // move the register pointer back to the first register
   Wire.beginTransmission(cn75address);// "Hey, CN75 @ 0x48! Message for you"
   Wire.write(0);// "move your register pointer back to 00h"
   Wire.endTransmission();// "Thanks, goodbye..."
   // now get the data from the CN75
   Wire.requestFrom(cn75address,2);// "Hey, CN75 @ 0x48 - please send me the contents of your first two registers"
   *a=Wire.read();// first received byte stored here
   *b=Wire.read();// second received byte stored here
}

void showCN75data()
{
  byteaa,bb;
  floattemperature=0;
  getCN75data(&aa,&bb);
  if(aa>127)// check for below zero degrees
     {
     temperature=((aa-128)*-1);
     if(bb==128)// check for 0.5 fraction
     {
     temperature-=0.5;
     }
  }
  else// it must be above zero degrees
  {
   temperature=aa;
   if(bb==128)// check for 0.5 fraction
   {
     temperature+=0.5;
     }
  }
   Serial.print("Temperature = ");
   Serial.print(temperature,1);
   Serial.println(" degrees C");
   delay(1000);
}

void loop()
{
   showCN75data();
}
 
Thanks for replying.


I see that this is for the cn75, im using the TC74 so its not a direct port, but im going to have a tinker with it and see if i can get it to work.

what i did realise is that the code you posted doesn't have the potentially infinite wait loop in line

while(Wire.available() == 0){};

what i do find is that if i comment it out or change the '==0' to '==1'. my serial window shows an output, but one where the readings do not change, any idea why?

The tutorial, said that this line will alert you if there is data to receive, and how many bytes. But i guess its not necessary, as one can just read from the line?

i uploaded a screenshot of what ive been getting.

thanks
 

Attachments

  • Screen Shot 2014-07-25 at 00.16.55.png
    Screen Shot 2014-07-25 at 00.16.55.png
    39 KB · Views: 444
while(Wire.available() == 0){};
I wouldn't use that line...


When you ask for a byte, it very rarley fails... What pullup resistors have you got on the I2C bus.

If you dont pull both SDA and SCK to 5v it won't work.... I use 4k7 resistors...
 
theres a 10k on each, i tried it with 4k7, for to eliminate that as a possibility but got the same serial printout
 
datasheet said:
The TC74 is internally programmed to have a default
SMBus/I2C address value of 1001 101b. Seven other
addresses are available by custom order (contact
Microchip Technology Inc

Where did you get 0x48 ( decimal 72 ) from??
 
MY chip says "A0" on the package.
This is an extract from page 13
 

Attachments

  • Screen Shot 2014-07-25 at 14.54.32.png
    Screen Shot 2014-07-25 at 14.54.32.png
    147.3 KB · Views: 395
The output remains the same regardless of which address is placed in the field. Ive tried both addresses, even something completely random 'B1111000'

i still get the same -1C output as shown before.

Incidentally i also tried the i2c scanner on Arduino playground and it doesn't record any device on the line

code from: https://playground.arduino.cc/Main/I2cScanner


Code:
// --------------------------------------
// i2c_scanner
//
// Version 1
//    This program (or code that looks like it)
//    can be found in many places.
//    For example on the Arduino.cc forum.
//    The original author is not know.
// Version 2, Juni 2012, Using Arduino 1.0.1
//     Adapted to be as simple as possible by Arduino.cc user Krodal
// Version 3, Feb 26  2013
//    V3 by louarnold
// Version 4, March 3, 2013, Using Arduino 1.0.3
//    by Arduino.cc user Krodal.
//    Changes by louarnold removed.
//    Scanning addresses changed from 0...127 to 1...119,
//    according to the i2c scanner by Nick Gammon
//    http://www.gammon.com.au/forum/?id=10896
// Version 5, March 28, 2013
//    As version 4, but address scans now to 127.
//    A sensor seems to use address 120.
//
//
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
//

#include <Wire.h>


void setup()
{
  Wire.begin();

  Serial.begin(9600);
  Serial.println("\nI2C Scanner");
}


void loop()
{
  byte error, address;
  int nDevices;

  Serial.println("Scanning...");

  nDevices = 0;
  for(address = 1; address < 127; address++ )
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");

      nDevices++;
    }
    else if (error==4)
    {
      Serial.print("Unknow error at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.println(address,HEX);
    }   
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");

  delay(5000);           // wait 5 seconds for next scan
}




i think this is telling me that my chip may be faulty..
 

Attachments

  • Screen Shot 2014-07-25 at 19.01.31.png
    Screen Shot 2014-07-25 at 19.01.31.png
    45.7 KB · Views: 427
  • Screen Shot 2014-07-25 at 18.58.26.png
    Screen Shot 2014-07-25 at 18.58.26.png
    41.3 KB · Views: 374
  • Screen Shot 2014-07-25 at 19.00.01.png
    Screen Shot 2014-07-25 at 19.00.01.png
    37.9 KB · Views: 378
original, i think my arduino is fine i believe its the temp sensor that is the issue. I have a gyro that tie I2C seems to work fine with
 
you said:
Incidentally i also tried the i2c scanner on Arduino playground and it doesn't record any device on the line
Sorry I assumed nothing was working, and you thought the Arduino was at fault..
 
Do make sure that you are connecting to the "Analog pins " of the Arduino with the I2C data & clock pin. If you have an oscilloscope/logic analyzer /bus pirate ( The best thing ... ever.) , try monitoring the signal from the data/ the clock pin.

the important thing that i want to add is that, the address scheme for I2C in arduino is a bit bizarre. You have to look into how the I2C addressing done in arduino. There is some sort of evil thing going on there, and beware of that.
 
Sorry I assumed nothing was working, and you thought the Arduino was at fault..

Sorry thats my fault for not being clear, i mean when i ran the I2C scanner it didnt locate any slaves on the bus at any address from zero to 128. i.e the arduino assumes the bus is empty
 
Do make sure that you are connecting to the "Analog pins " of the Arduino with the I2C data & clock pin. If you have an oscilloscope/logic analyzer /bus pirate ( The best thing ... ever.) , try monitoring the signal from the data/ the clock pin.

the important thing that i want to add is that, the address scheme for I2C in arduino is a bit bizarre. You have to look into how the I2C addressing done in arduino. There is some sort of evil thing going on there, and beware of that.

In the interest of completeness, and because i really hate when i look online and the thread has died with seemingly no explanation or resolution.

As embarrassed as i hate to say it. After reading your post Magnetron, i went back to basics and checked my wiring (which would usually be my my first stop, but my 'relative inexperiance' using I2C practically lead me to question the code).

ANYWAY.. it turns out that this time when I wired my arduino for I2C i was using pins 4 & 5 instead of pins A4 and A5 as id been doing previously. A quick switch resolved the issue.

Thanks all.
 

Attachments

  • Screen Shot 2014-07-25 at 22.51.54.png
    Screen Shot 2014-07-25 at 22.51.54.png
    39.9 KB · Views: 377
Congos for your 1st I tooo See thingy. Yeah, I hate to say.. " I told you so" any way the occasional visit of success is enough for us engineers to strive forward; and a quick thing, if you want to REALLY know what's going on, try not to do it with arduino.
 
I'm glad you found your issue.... We are sometimes blinded by our first thoughts.... When you are convinced it is one thing, you don't look at the obvious...

Pin4, Pin5... A4, A5 Good one though... I hope others read this and learn...
 
Congos for your 1st I tooo See thingy. Yeah, I hate to say.. " I told you so" any way the occasional visit of success is enough for us engineers to strive forward; and a quick thing, if you want to REALLY know what's going on, try not to do it with arduino.

Oh i 100% agree with you here, to be honest I'm a lot more familiar with the PIC18 family of devices and to a slighter lesser extent the Arm Cortex. However ive always had an arduino sitting here and never used it (much like my I2C ports on other devices) but i thought this would be a good icebreaker, conceptually i 100% understand it but the practicalities of 'releasing the line' for Ack ext, i was unsure of.

I feel much more comfortable with it now. However i want to 'see inside' the arduino library functions, such as Wire.endTransmission, and Wire.requestFrom for a more fundamental understanding.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top