// For the Arduino forum: 
//   https://forum.arduino.cc/t/tsl2561-luminosity-sensor-not-giving-correct-value/946515
//

int scl = SCL;            // changed from 52 to 'SCL'
int sda = SDA;            // changed from 53 to 'SDA'
unsigned int arr[4];
double ch0,ch1;

#define SDA_HIGH  pinMode(sda,INPUT)
#define SDA_LOW   pinMode(sda,OUTPUT)
#define SCL_HIGH  digitalWrite(scl,HIGH)
#define SCL_LOW   digitalWrite(scl,LOW)


void delay_i2c()
{
  delayMicroseconds(1);
}

void starti2c()
{
  SDA_HIGH;
  delay_i2c();
  SCL_HIGH;
  delay_i2c();
  SDA_LOW;
  delay_i2c();
  SCL_LOW;
}

void stopi2c(void)
{
  SCL_LOW;
  delay_i2c();
  SCL_HIGH;
  delay_i2c();
  SDA_LOW;
  delay_i2c();
  SDA_HIGH;             
  delay_i2c();
}

void clocki2c()
{
  SCL_HIGH;
  delay_i2c();
  SCL_LOW;
  delay_i2c();
}

void i2c_tx(byte data) 
{
  for (byte bits = 0; bits < 8 ; bits++)   /////////////1-7 bit
  {                                          
    if(0x80 & data)
    {
      SDA_HIGH;
    }
    else
    {
       SDA_LOW;
    }
    data = data << 1;
    clocki2c();
  }
  SDA_LOW;
  clocki2c();
 
  SCL_HIGH;
  SDA_HIGH;
  SCL_LOW;
  delay_i2c();  
}

unsigned char i2c_rx()
{
  byte d=0;
  SDA_HIGH;
  for (int bits = 0; bits < 8 ; bits++)   /////////////1-7 bit
  {              
    if(digitalRead(sda) == HIGH)
    {
      d=d+1;
    }
    else
    {
      d=d+0;
    }
    SCL_HIGH;
    //delay_i2c();    
    
    d = d << 1;
    SCL_LOW;
    //delay_i2c();
  }
  SDA_LOW;
  clocki2c();
 
  SCL_HIGH;
  delay_i2c();
  SCL_LOW;
  SDA_HIGH;
  return d;     
}

void wakeupi2c()
{
  ////////////////////////////////////////power on
  starti2c();
  i2c_tx(0x39);        //0x39
  i2c_tx(0x80);       //0x80
  i2c_tx(0x03);      //0x03
  stopi2c();
////////////////////////////////////////set gain
  starti2c();
  i2c_tx(0x39);
  i2c_tx(0x81);     //0x81
  i2c_tx(0x02);     //0x02
  stopi2c();
}

void fetching_output()         
{
  for(int i = 0; i < 4; i++)
  {
    ///////////////////////////////////////////////////////////sending ch0,ch1 registers value
    starti2c();
    i2c_tx(0x68 << 1);        // changed 57 -> 0x68 << 1
    i2c_tx(140+i); ////////////////////////// 08C,08D,08E,08F
    //stopi2c();
 
    ////////////////////////////////////////////////////////////  reading
    starti2c();
    i2c_tx(0x68 << 1);        // changed 57 -> 0x68 << 1
    arr[i]=i2c_rx();
    stopi2c();
    delay(200);
  }
}

void disp_data()
{
  ch0=(256*(arr[1]& 0xFF)+(arr[0]& 0xFF));
  ch1=(256*(arr[3]& 0xFF)+(arr[2]& 0xFF));
  Serial.print("ch0 = ");
  Serial.println(ch0);
  Serial.print("ch1 = ");
  Serial.println(ch1);
  Serial.print("ch1 - ch0 = ");
  Serial.println(ch0-ch1);
}

void setup() 
{
  pinMode(scl,OUTPUT);
  Serial.begin(9600);
  // wakeupi2c();           // changed, commented out
}

void loop() 
{
  //Serial.println(read_TSL2561C0());
  fetching_output();
  disp_data();
  delay(500);
}
GND5VSDASCLSQWRTCDS1307+
D0D1D2D3D4D5D6D7GNDLOGIC