Sensor setup problem

I have a problem with my Brickpi sensor.
Recently, when I use any sensors, BrickPisetup returns 0 (It’s OK), but BrickPiSetupSensors returns -1. So It does not work.
when I try many times, BrickPiSetupSensors sometimes returns 0. And sensors work well. So I don’t think sensor’s breakdown.

What version of the BrickPi are you using?

I have a BrickPi+.
/

Can you share the code you’re running? With a BrickPi+, if you don’t have a pause in between the initiation and the sensor setup, you can cause and error. This could indeed be a dead sensor though.

Here is my code. I used github’s code as it is.

#include <stdio.h> 
#include <math.h> 
#include <time.h> 

#include "tick.h" 
#include "BrickPi.h" 

#include <linux/i2c-dev.h>   
#include <fcntl.h> 

// Compile Using: 
// sudo gcc -o program "EV3 - IR Sensor proximity test.c" -lrt -lm 
// Run the compiled program using: 
// sudo ./program 

//#define DEBUG 
int result,val; 
//#undef DEBUG 
#define IR_PORT         PORT_1                      // Infrared Sensor on Port_1 

int main() { 
  ClearTick(); 

  result = BrickPiSetup(); 
  printf("BrickPiSetup: %d\n", result); 
  if(result)									// Check to see if everything is alive. 
    return 0;									// If not, kill the program. 
  printf("BrickPiSetup Done!\n");				// If so, proceed. 
  BrickPi.Address[0] = 1; 
  BrickPi.Address[1] = 2; 

  BrickPi.SensorType[IR_PORT] = TYPE_SENSOR_EV3_INFRARED_M0;	// Setting SEnsor to mode 0. 
  result = BrickPiSetupSensors();								// Call this to setup the sensors.   
   
  printf("BrickPiSetupSensors: %d\n", result); 					// Print the result of sensor setup. 
  if(!result){ 
    usleep(10000); 
    while(1){ 
      result = BrickPiUpdateValues();			// Update values from the BrickPi. 

      if(!result){ 
      	 val = BrickPi.Sensor[IR_PORT];			// Put values into a variable. 
	     if(val <= 100) 
           printf("Results: %d \n", val );		// Print the value returned.  This is the relative IR sensor, in this example.
      } 
	  //usleep(10000); 
    } 
  } 
  return 0; 
}

Hmm . . . that leaves a question. Can you try modifying the code to have a few sleeps in it between calls? Something like this:

BrickPi.Address[0] = 1; 
BrickPi.Address[1] = 2; 

usleep(10000);

BrickPi.SensorType[IR_PORT] = TYPE_SENSOR_EV3_INFRARED_M0;	// Setting SEnsor to mode 0. 
result = BrickPiSetupSensors();								// Call this to setup the sensors.

I think the calls may be made too rapidly to the BrickPi+, especially if you’re using a newer version of the Raspberry PI.