1

I recently bought on ebay a sensor for a drone project im working on MPU9250 BMP280 10DOF the thing is i always get a "bad handle" error, and honestly i cant figure it out.

2017-11-18 22:10:03 i2cReadByte: bad handle (-74) Valor de 0x3B: -25

note: the "Valor de 0x3B" its just for info purpose

in this example im trying to retrieve the X axis of the accelerometer

i dont know if its a bad conecction or an error on the code, but to be sure can anyone check it just in case, my code its as follows

#include <cstdlib>
#include "../pigpio.h"
#include <unistd.h>
#include <iostream>

/*   Leer datos del sensor usando I2C
 * 
*/
int main(void) {

typedef int *Acc;      // Defino un handle (Apuntador) // not sure about this one
gpioInitialise();      // inicializa la libreria
if (gpioInitialise() <=0)
   {  
      std::cout << "gpio not initialise";
      return 0;
   }
   else
   {
      std::cout << "gpio working fine" << std::endl;
   } 


  //i2cOpen(0,0x3B,0); habro el i2c y leeo la informacion en la direccion 0x3b
  //i2cOpen(Bus seleccionado, Direccion sensor, Flag debe ser 0)
 //Acc i2cOpen(0,0x3B,0);  

for (;;)
{
  std::cout << "Valor de 0x3B: "<< i2cReadByte (i2cOpen(0,0x3B,0)) << std::endl; // Leeo informacion del bus, menos codigo
}

gpioTerminate();
return 0;
}

I wired the follow pins on the sensor to the I2c conecctions on the pi

Sensor --- RP3

3,3v to pin 1

Gnd to pin 9

SCL to Pin 5

SDA to pin 3

2

On most Pis you need to use I2C bus 1. I2C bus 0 is pretty much reserved for system use with HATs etc.

The first parameter to i2cOpen is the bus.

int i2cOpen(unsigned i2cBus, unsigned i2cAddr, unsigned i2cFlags)
This returns a handle for the device at the address on the I2C bus.

  i2cBus: >=0
 i2cAddr: 0-0x7F
i2cFlags: 0

I suggest you change i2cOpen(0,0x3B,0) to i2cOpen(1,0x3B,0).

  • you where right, i was using the wrong bus, now im using bus 1 and recieving data, also moved the i2cOpen() out of the Loop, that was the main problem, thanks. – lightshadown Nov 20 '17 at 0:07
1

ok for some odd reason you cant use i2cOpen INSIDE a loop, that was the main issue, also the bus was wrong (i was using bus 0) i switched to bus 1 just like joan sugested, also im using i2cReadByteData instead of i2cReadByte , the first one retrieves data from an especific address, and the second one its for one type of data only

#include <cstdlib>
#include "../pigpio.h"
#include <unistd.h>
#include <iostream>


/*   Leer datos del sensor usando I2C
*  Addres of MPU-9250 :0x68
*/

int main(void) {

int sensor; //handler for the sensor

gpioInitialise();      // inicializa la libreria

if (gpioInitialise() <=0)  // check if library its working
   {  
      std::cout << "gpio not initialise";
      return 0;
   }
   else
   {
      std::cout << "gpio working fine" << std::endl;
   } 

//i2cOpen(1,0x68,0); enable i2c comms, bus is 1, the addres of the sensor is 0x68
//i2cOpen(Bus seleccionado, Direccion sensor, Flag debe ser 0) 

sensor = i2cOpen(1,0x68,0); // handler its a variable, in this case sensor, NEVER INSIDE A LOOP!!!!!
for (;;)
{
   // Reading Gyroscope data
   std::cout << "Gyro 'Y': " << i2cReadByteData(sensor,0x46) << "   ";
   std::cout << "Gyro 'X': " << i2cReadByteData(sensor,0x44) << "   ";
   std::cout << "Gyro 'Z': " << i2cReadByteData(sensor,0x48) << "   ";
   std::cout << "Temperatura: " << i2cReadByteData(sensor,0x42) << std::endl;
}

gpioTerminate();
return 0;
}

now im receiving data from the sensor, its a little odd because it spikes all over the place, probably its noise but i will try filter it.

heres what i get from the sensor

gpio working fine
Gyro 'Y': 217   Gyro 'X': 208   Gyro 'Z': 124   Temperatura: 16
Gyro 'Y': 239   Gyro 'X': 13   Gyro 'Z': 128   Temperatura: 144
Gyro 'Y': 247   Gyro 'X': 255   Gyro 'Z': 130   Temperatura: 176
Gyro 'Y': 4   Gyro 'X': 2   Gyro 'Z': 114   Temperatura: 160
Gyro 'Y': 229   Gyro 'X': 6   Gyro 'Z': 130   Temperatura: 112
Gyro 'Y': 11   Gyro 'X': 251   Gyro 'Z': 141   Temperatura: 16
Gyro 'Y': 13   Gyro 'X': 13   Gyro 'Z': 135   Temperatura: 144
Gyro 'Y': 250   Gyro 'X': 237   Gyro 'Z': 148   Temperatura: 144
Gyro 'Y': 249   Gyro 'X': 3   Gyro 'Z': 137   Temperatura: 160
Gyro 'Y': 11   Gyro 'X': 250   Gyro 'Z': 138   Temperatura: 160
Gyro 'Y': 18   Gyro 'X': 232   Gyro 'Z': 145   Temperatura: 176
Gyro 'Y': 245   Gyro 'X': 202   Gyro 'Z': 135   Temperatura: 176
Gyro 'Y': 15   Gyro 'X': 220   Gyro 'Z': 87   Temperatura: 144
Gyro 'Y': 8   Gyro 'X': 228   Gyro 'Z': 125   Temperatura: 224
Gyro 'Y': 250   Gyro 'X': 236   Gyro 'Z': 135   Temperatura: 176
Gyro 'Y': 3   Gyro 'X': 242   Gyro 'Z': 127   Temperatura: 144
Gyro 'Y': 237   Gyro 'X': 238   Gyro 'Z': 129   Temperatura: 176
Gyro 'Y': 17   Gyro 'X': 221   Gyro 'Z': 122   Temperatura: 176
Gyro 'Y': 15   Gyro 'X': 14   Gyro 'Z': 141   Temperatura: 224
Gyro 'Y': 22   Gyro 'X': 29   Gyro 'Z': 148   Temperatura: 176
Gyro 'Y': 13   Gyro 'X': 5   Gyro 'Z': 130   Temperatura: 160
Gyro 'Y': 228   Gyro 'X': 185   Gyro 'Z': 92   Temperatura: 176
Gyro 'Y': 0   Gyro 'X': 242   Gyro 'Z': 144   Temperatura: 144
Gyro 'Y': 12   Gyro 'X': 252   Gyro 'Z': 116   Temperatura: 176
Gyro 'Y': 7   Gyro 'X': 251   Gyro 'Z': 118   Temperatura: 144
Gyro 'Y': 27   Gyro 'X': 219   Gyro 'Z': 118   Temperatura: 176
Gyro 'Y': 17   Gyro 'X': 13   Gyro 'Z': 115   Temperatura: 144
Gyro 'Y': 11   Gyro 'X': 244   Gyro 'Z': 125   Temperatura: 208
Gyro 'Y': 52   Gyro 'X': 231   Gyro 'Z': 120   Temperatura: 160
Gyro 'Y': 38   Gyro 'X': 216   Gyro 'Z': 131   Temperatura: 64
Gyro 'Y': 50   Gyro 'X': 41   Gyro 'Z': 146   Temperatura: 160
Gyro 'Y': 17   Gyro 'X': 235   Gyro 'Z': 145   Temperatura: 144
Gyro 'Y': 25   Gyro 'X': 218   Gyro 'Z': 140   Temperatura: 144
Gyro 'Y': 28   Gyro 'X': 208   Gyro 'Z': 134   Temperatura: 176
Gyro 'Y': 16   Gyro 'X': 232   Gyro 'Z': 146   Temperatura: 176
Gyro 'Y': 22   Gyro 'X': 211   Gyro 'Z': 150   Temperatura: 176
Gyro 'Y': 249   Gyro 'X': 229   Gyro 'Z': 161   Temperatura: 176
Gyro 'Y': 27   Gyro 'X': 233   Gyro 'Z': 167   Temperatura: 144
Gyro 'Y': 36   Gyro 'X': 234   Gyro 'Z': 146   Temperatura: 160
Gyro 'Y': 4   Gyro 'X': 250   Gyro 'Z': 125   Temperatura: 112
Gyro 'Y': 14   Gyro 'X': 237   Gyro 'Z': 153   Temperatura: 160
Gyro 'Y': 59   Gyro 'X': 234   Gyro 'Z': 172   Temperatura: 160
Gyro 'Y': 21   Gyro 'X': 221   Gyro 'Z': 133   Temperatura: 240
  • 1
    You could use i2cOpen in the loop if you have a corresponding i2cClose in the loop. However I believe it is more usual to open/close I2C just once in a program. – joan Nov 20 '17 at 8:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.