write to i2c slave working only one time, after write hangs??

Hi ,

i interface arduino uno with i2c based io expander PCF8574. when i try to write values to port it is setting only once, my code not working once it writes to pscf8574.

for checking i put blinking after write, it is not working.

Could anyone tell me how to resolve this?

my sketch

#include <pcf8574.h>
#include <Wire.h>

PCF8574 PCF_39(0x20);  // add leds to lines      (used as output)

void setup()
{
Wire.begin();
pinMode(13,OUTPUT);
}

void loop()
{
  for(int i = 1;i<=15;i++)
  {
  value = i;
  PCF_39.write8(value);
  digitalWrite(13,HIGH);
  delay(100);
  digitalWrite(13,LOW);
  delay(100);
  }
}

Thanks

Alagappan

Could anyone tell me how to resolve this?

Start with posting the code that produced the hex file that is actually on the Arduino. That code isn't it, since it won't even compile.

PCF_39.write8(value);Write 8 of what? Because you do this 15 times.

Is this the library that you are using playground 8574 library? Have you tried running an I2C scanner to confirm address and communication with the 8574? Are the external pullups installed on SDA and SCL? The pullups enabled by Wire library may not be strong enough.

// I2C scanner by Nick Gammon.  Thanks Nick.

#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() {}

my compiling code:

#include <pcf8574.h>
#include <Wire.h>

PCF8574 PCF_39(0x20);  // add leds to lines      (used as output)
int value;
void setup()
{
Wire.begin();
pinMode(13,OUTPUT);
}

void loop()
{
  for(int i = 1;i<=3;i++)
  {
  value = i;
  PCF_39.write8(value);
  digitalWrite(13,HIGH);
  delay(100);
  digitalWrite(13,LOW);
  delay(100);
  }
}

My libraryfile

#include "PCF8574.h"

#include <Wire.h>

PCF8574::PCF8574(int address)
{
  _address = address;
  Wire.begin();
}

uint8_t PCF8574::read8()
{
  Wire.beginTransmission(_address);
  Wire.requestFrom(_address, 1);
#if (ARDUINO <  100)
   _data = Wire.receive();
#else
   _data = Wire.read();
#endif
  _error = Wire.endTransmission();
  return _data;
}

uint8_t PCF8574::value()
{
  return _data;
}

void PCF8574::write8(uint8_t value)
{
  Wire.beginTransmission(_address);
  _data = value;
  Wire.write(_data);
  _error = Wire.endTransmission();
}

uint8_t PCF8574::read(uint8_t pin)
{
  PCF8574::read8();
  return (_data & (1<<pin)) > 0;
}

void PCF8574::write(uint8_t pin, uint8_t value)
{
  PCF8574::read8();
  if (value == LOW)
  {
    _data &= ~(1<<pin);
  }
  else
  {
    _data |= (1<<pin);
  }
  PCF8574::write8(_data);
}

void PCF8574::toggle(uint8_t pin)
{
  PCF8574::read8();
  _data ^=  (1 << pin);
  PCF8574::write8(_data);
}

void PCF8574::shiftRight(uint8_t n)
{
  if (n == 0 || n > 7 ) return;
  PCF8574::read8();
  _data >>= n;
  PCF8574::write8(_data);
}

void PCF8574::shiftLeft(uint8_t n)
{
  if (n == 0 || n > 7) return;
  PCF8574::read8();
  _data <<= n;
  PCF8574::write8(_data);
}

int PCF8574::lastError()
{
  int e = _error;
  _error = 0;
  return e;
}

Yes, when i replace Wire.endTransmisson() with Wire.endTransmission(false) it is working,

I am checking in proteus simulation only. i doubt why it is not working when Wire.endTransmission().

if i pass false as argument is that repeated start? how to perform repeated start in i2c arduino

alagappan:
if i pass false as argument is that repeated start? how to perform repeated start in i2c arduino

Looks like it defaults to true, but if false is a repeated start: Arduino - WireEndTransmission