PICkit I2C Demo Board, MCP23008 Expander to LEDS, Temp sensor. No results?

Ladies and Gents,

Project final goal : Display Temp data on Leds through expander

I am trying desperately to light up the leds on Pikit I2C demo board, which are connected to MCP23008 output.. but I can't get any results. I also trying to get temperature from Temp Sensor (MCP9800) but couldn't get any results. Here is the following code : which i extracted from the forums. I don't see any error in the lines, why am I not getting the leds turned on or temp back ?

Using Arduino Uno
Got A4 connected to SDA
A5 connected to SCL

Chips addresses are included on the board (0x40) for expander and 0x92 for temp sensor

the PICkit board has 2 pullup resistors 1.5k

MCP23008 Expander to leds

//#include <Wire.h>

#include <Wire.h>

//Arduino analog input 5 - I2C SCL
//Arduino analog input 4 - I2C SDA


void setup() {
   Wire.begin();                      // initialise the wire library and hardware
    Wire.beginTransmission(0x40);      // start talking to the device
   Wire.write(0x00);                   // select the IODIR register
   Wire.write(0x00);                   // sets all pins as outputs on MCP23008
   Wire.endTransmission();            // stop talking to the devicevice
    delay(500);   
   

    
}
//byte x = 0;

void loop() { //blink leds
  Wire.beginTransmission(0x40);      // start talking to the device
  Wire.write(0x09);                   // select the GPIO register
  Wire.write(0xff);                   // set register value-all high
  Wire.endTransmission();            // stop talking to the device

   //delay(500);                        // wait for 1/2 a second

   Wire.begin();                      // initialise the wire library and hardware

   Wire.beginTransmission(0x40);      // start talking to the device
   Wire.write(0x09);                   // select the GPIO register
   Wire.write(0x00);                   // set register value-all low
   Wire.endTransmission();            // stop talking to the device
    
   // x++ ;
    
   delay(500);                        // wait for 1/2 a second

}

Temp sensor

#include "Wire.h"
//wire library

#define address 0x92
//address of the temperature sensor

#define delayC 1000
//delay count in ms

#define baudrate 9600
//baudrate for communication

void setup()
{
Wire.begin();
Serial.begin(baudrate);
}

void loop()
{
Serial.print("temperature in Celsius: ");
//let's signal we're about to do something

int temperature;
//temperature in a byte

Wire.beginTransmission(address);
//start the transmission

Wire.write(0x00); //ambient temp register

Wire.requestFrom(address, 1); //read
if (Wire.available()) { //is data available
temperature = Wire.read(); //assign it to variable

Serial.println(temperature); 
} else {
Serial.println("---");
}

Wire.endTransmission();
//end the transmission

delay(delayC);
}

Source :
Temp sensor

MCP23008

Ladies and Gents,

Project final goal : Display Temp data on Leds through expander

I am trying desperately to light up the leds on Pikit I2C demo board, which are connected to MCP23008 output.. but I can't get any results. I also trying to get temperature from Temp Sensor (MCP9800) but couldn't get any results. Here is the following code : which i extracted from the forums. I don't see any error in the lines, why am I not getting the leds turned on or temp back ?

Using Arduino Uno
Got A4 connected to SDA
A5 connected to SCL

Chips addresses are included on the board (0x40) for expander and 0x92 for temp sensor

the PICkit board has 2 pullup resistors 1.5k

MCP23008 Expander to leds

//#include <Wire.h>

#include <Wire.h>

//Arduino analog input 5 - I2C SCL
//Arduino analog input 4 - I2C SDA


void setup() {
  Wire.begin();                      // initialise the wire library and hardware
   Wire.beginTransmission(0x40);      // start talking to the device
  Wire.write(0x00);                   // select the IODIR register
  Wire.write(0x00);                   // sets all pins as outputs on MCP23008
  Wire.endTransmission();            // stop talking to the devicevice
   delay(500);   
  

   
}
//byte x = 0;

void loop() { //blink leds
 Wire.beginTransmission(0x40);      // start talking to the device
 Wire.write(0x09);                   // select the GPIO register
 Wire.write(0xff);                   // set register value-all high
 Wire.endTransmission();            // stop talking to the device

  //delay(500);                        // wait for 1/2 a second

  Wire.begin();                      // initialise the wire library and hardware

  Wire.beginTransmission(0x40);      // start talking to the device
  Wire.write(0x09);                   // select the GPIO register
  Wire.write(0x00);                   // set register value-all low
  Wire.endTransmission();            // stop talking to the device
   
  // x++ ;
   
  delay(500);                        // wait for 1/2 a second

}

Temp sensor

#include "Wire.h"
//wire library

#define address 0x92
//address of the temperature sensor

#define delayC 1000
//delay count in ms

#define baudrate 9600
//baudrate for communication

void setup()
{
Wire.begin();
Serial.begin(baudrate);
}

void loop()
{
Serial.print("temperature in Celsius: ");
//let's signal we're about to do something

int temperature;
//temperature in a byte

Wire.beginTransmission(address);
//start the transmission

Wire.write(0x00); //ambient temp register

Wire.requestFrom(address, 1); //read
if (Wire.available()) { //is data available
temperature = Wire.read(); //assign it to variable

Serial.println(temperature); 
} else {
Serial.println("---");
}

Wire.endTransmission();
//end the transmission

delay(delayC);
}

Source :
Temp sensor

MCP23008

Have you run an I2C scanner to verify the addresses and that the peripherals are talking. That is the first thing that I do when I have trouble with I2C.

Wire begin() does not belong in loop().

Wire.endTransmission();            // stop talking to the device
    //delay(500);                        // wait for 1/2 a second
    Wire.begin();                      // initialise the wire library and hardware
    Wire.beginTransmission(0x40);

"but couldn't get any results"

What do you mean?

Have you broken your project into small sections and got them working on their own before adding another part?

How have you tested your program? What results did you get?

We need more "My project doesn't work, here is the code."

Weedpharma

Do not cross-post. Threads merged.

groundfungus:
Have you run an I2C scanner to verify the addresses and that the peripherals are talking. That is the first thing that I do when I have trouble with I2C.

Wire begin() does not belong in loop().

Wire.endTransmission();            // stop talking to the device

//delay(500);                        // wait for 1/2 a second
    Wire.begin();                      // initialise the wire library and hardware
    Wire.beginTransmission(0x40);

What do you exactly mean I2C scanner? I removed the wire.begin() and kept it in the setup.. nothing yet

The ""PICKIT serual I2c demo board has 5 chips and below each

This is the I2C scanner written by Nick Gammon.
Copy this, paste into your IDE, upload and run it.
The sketch will find all of the I2C devices on the bus and tell you the addresses of the devices it finds if there are devices that are able to communicate.

#include <Wire.h>

void setup() {
  Serial.begin (115200);

  // Leonardo: wait for serial port to connect
  while (!Serial) 
    {
    }

  Serial.println ();
  Serial.println ("I2C scanner. Scanning ...");
  byte count = 0;
  
  Wire.begin();
  for (byte i = 1; i < 120; i++)
  {
    Wire.beginTransmission (i);
    if (Wire.endTransmission () == 0)
      {
      Serial.print ("Found address: ");
      Serial.print (i, DEC);
      Serial.print (" (0x");
      Serial.print (i, HEX);
      Serial.println (")");
      count++;
      delay (1);  // maybe unneeded?
      } // end of good response
  } // end of for loop
  Serial.println ("Done.");
  Serial.print ("Found ");
  Serial.print (count, DEC);
  Serial.println (" device(s).");
}  // end of setup

void loop() {}

groundfungus:
This is the I2C scanner written by Nick Gammon.
Copy this, paste into your IDE, upload and run it.
The sketch will find all of the I2C devices on the bus and tell you the addresses of the devices it finds if there are devices that are able to communicate.

#include <Wire.h>

void setup() {
Serial.begin (115200);

// Leonardo: wait for serial port to connect
while (!Serial)
{
}

Serial.println ();
Serial.println ("I2C scanner. Scanning ...");
byte count = 0;

Wire.begin();
for (byte i = 1; i < 120; i++)
{
Wire.beginTransmission (i);
if (Wire.endTransmission () == 0)
{
Serial.print ("Found address: ");
Serial.print (i, DEC);
Serial.print (" (0x");
Serial.print (i, HEX);
Serial.println (")");
count++;
delay (1); // maybe unneeded?
} // end of good response
} // end of for loop
Serial.println ("Done.");
Serial.print ("Found ");
Serial.print (count, DEC);
Serial.println (" device(s).");
} // end of setup

void loop() {}

Used the code. Gave me following result

I2C scanner. Scanning ...
Found address: 32 (0x20)
Found address: 72 (0x48)
Found address: 73 (0x49)
Found address: 77 (0x4D)
Found address: 80 (0x50)
Found address: 81 (0x51)
Found address: 82 (0x52)
Found address: 83 (0x53)
Found address: 84 (0x54)
Found address: 85 (0x55)
Found address: 86 (0x56)
Found address: 87 (0x57)
Done.
Found 12 device(s).

12 devices!!! .. I got only 5 out there . and these addresses are non of which are assigned to the chips I got on demo board

By the way , I modified the MCP9800 code and got a result. but Issue Is that, even if i remove the SDA , SCL.. I get constant input, and even if they are connected. Same result. Which doesn't change when I heat up the sensor

-1 when temp is assigned as integer,
and 24 in my case or binary or double

#include "Wire.h"
//wire library

#define address 0x92
//address of the temperature sensor

#define delayC 1000
//delay count in ms

#define baudrate 9600
//baudrate for communication

 int led=13;


void setup()
{
  //creat a wire object
  Wire.begin();
   //start serial , monitor
  Serial.begin(baudrate);
  pinMode(led, OUTPUT); //led on
}

void loop()
{
 
//Serial.print("temperature in Celsius: ");
//let's signal we're about to do something
 //unsigned long temp=0;

//temperature in a byte
  //send a request
  
  Wire.beginTransmission(146); //address); //1001 0010, 146
//start the transmission
  Wire.write(0x00); //sent a bit asking for ambient temp register
  Wire.endTransmission(); // complete transmission
 
 Wire.beginTransmission(146); //address); //1001 0010, 146
//start the transmission
  Wire.write(0x00); //sent a bit asking for ambient temp register
  Wire.endTransmission(); // complete transmission
//read temp from device
  Wire.requestFrom(address, 1);  
//Wire.onReceive(0x00);


  while(Wire.available()); //wait for info/response
//  pinMode(13, OUTPUT); //led on
 digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  float temp = Wire.read(); //get temp and assign it to variable
  //scaling from 2's complement and ADC resolution 0f standard 9bits to scale of 0.5 degrees celcius increment. Refresh rate is 30ms typical given by datasheet
  // 2^9 = 512. 0 to 255 , -255 to -511
  // data recieved in hexadecimal . convert to decimal first
  // MSB temp register = 00101001b = 2^5 + 2^3 + 2^0 = 41
  //LSB temp regesiter =100000000b = 2^-1 = 0.5 
  //Temperature will be = 41.5 degrees celcius
 
 
  if (temp > 255)
  {
     temp=(temp-511)*(2*10^-4);
     Serial.println(temp);
  }
  else if (temp < 256)
  {
    temp=(temp*(2*10^-4));

    Serial.println(temp);
  }
 // Serial.println("TEMP");
 
// else {
//Serial.println("---");
//}
//send temp to screen
//Serial.println(temp); // The data is stuck on 255 only doesn't update or 24 after conversion above
//Wire.endTransmission();
//end the transmission

  delay(2000);
}

And this is what I see from the oscilloscope on SCL

SDA

The addresses from the data sheet need to be shifted one to the right (divide by 2).

data sht add scanner add component

0xA0-0xAF 0x50-0x57 24LC02B // uses 8 addresses (any one will do)
160-174 80-87

0X92 (146) 0X49 (73) MCP9801

0X9A (154) 0X4D (77) MCP3221

0X90 (144) 0X48 (72) TC1321

0X40 (64) 0X20 (32) MCP23008

Use the addresses reported by the scanner and they should work.

Yes It's Working.