Wholetthesmokeout
Posts: 6
Joined: Tue Apr 09, 2013 12:51 am

I2C worked but not working anymore.

Tue Apr 09, 2013 12:59 am

I bought the MPL115A2 from Adafruit, and I hooked it up to the pi, ran i2cdetect, and found found the slave address of 0x60 just fine. OK. Fast forward to today and nothing is working. Here is the backstory:

Initially wanted to just blink some LEDs to get basic GPIO working. Messed up the pin connection and instead of 3.3V -> LED -> Resistor -> GND I did 5V -> LED -> Resistor -> GPIO pin. Yikes. Well I realized my error switched things around and everything seems to work ok as far as blinking LEDs go. (Edit: The GPIO pins are also at 3.3V but when I attach an LED to them and not use the pins, the voltage floats at 2.7V) Also, the GPIO's (channel 3 and 5) can read and write signals just fine. Now on to the I2C. As I said, plugged it in, worked. But now after literally doing nothing different, it is not working. I double triple checked connections and everything looks fine. I rechecked GPIO functionality on pins 3 and 5 and they work just fine. I seriously doubt I fried the sensor because I did not connect anything differently! I must note I did mess around with the GPIO.PUD_UP and DOWN functions, but I always use GPIO.cleanup() after.

My only tools are a rubbish volt meter and an equally rubbish solder iron - no scope or anything else.

Please help! Any suggestions are appreciated (and yes I turned it off and back on again a bazillion times)

Tfantonsen
Posts: 5
Joined: Sat Jan 12, 2013 9:16 pm

Re: I2C worked but not working anymore.

Tue Apr 09, 2013 8:06 pm

Does i2cdetect find the device after things broke?

Anyway, I had similar problem with I2C and the same equipment as you.

Then I figured that the RasPi actually is a poor mans 'scope/logic analyzer. As my solution is interrupt based RPI will be limited in frequency to around 10KHz and as I'm making a system call within my interrupt handler I guess 1KHz or less is the practical limit. So try something like:
1. Lower the I2C bus to something manageable:
a > sudo modprobe -r i2c_bcm2708 (remove i2c)
b > sudo modprobe i2c_bcm2708 baudrate=1 (add i2c, now running at a mangeable 1Hz)
2. Add connection from SDA and SCL to unused GPIO pins on the RPI, I used pin 7 and 10
3. Write a small piece of code interrupting when state of SCL changes, logging a timestamp and the value of the GPIO pin connected to SDA. The main loop just print the two logged values comma seperated ready to dump to gnuplot or spreadsheet. GPIO.cleanout suggest you are using Python, but I wrote my program in c (compile with 'gcc -lwiringPi oscilloscope.c -o oscilloscope' and run using 'sudo ./ascilloscope'), using Gordon WiringPi library:

Code: Select all

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/time.h>
#include <wiringPi.h>

int count = 0;
struct timeval last, now;
int times[1000000];
int values[1000000];

void myInterrupt (void){
	gettimeofday(&now, NULL);
	times[count] = /*(int)(now.tv_sec*1000000ULL+*/now.tv_usec;
	values[count++] = digitalRead(10); ;
}

int main(int argc, char* argv[]){
	gettimeofday(&last, NULL);
	if (wiringPiSetup () < 0){
		fprintf (stderr, "Unable to setup wiringPi: %s\n", strerror (errno)) ;
		return 1 ;
	}
	if (wiringPiISR (7, INT_EDGE_BOTH, &myInterrupt) < 0){
		fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno)) ;
		return 1 ;
	}
	int i = 0;
	while(1)
		if(i<count)
			fprintf (stdout, "%d,%d\n", times[i],values[i++]) ;
	return 0;
}
I would try running without any device connect to i2c just to verify the RPi is using i2c properly, then I would try connecting a device, to verify that i2c still works between RPi and the device.
RasPi Model B
Using the RasPi as a 'poor mans 'scope/logic analyzer: http://www.raspberrypi.org/phpBB3/viewtopic.php?p=327908#p327908

Wholetthesmokeout
Posts: 6
Joined: Tue Apr 09, 2013 12:51 am

Re: I2C worked but not working anymore.

Tue Apr 09, 2013 9:40 pm

No the device isn't detected using i2cdetect -y 1 and i2cdetect -y 0 and using sudo. I will give your method a shot, it sounds like a neat debug experiment. I will also try probing the I2C lines (should idle high) and report back.

Wholetthesmokeout
Posts: 6
Joined: Tue Apr 09, 2013 12:51 am

Re: I2C worked but not working anymore.

Tue Apr 09, 2013 10:58 pm

Ok. So I have absolutely no idea what happened. It worked. I was doing what you told me to do and I just ran a simple LED blinker program using the GPIO interrupts and the pins for the I2C and that worked. I just wanted to familiarize myself with using the treaded callback function. And then I ran the smbus I2C function (python):

testvar = read_byte(0x60)

Now, when I ran this before, I always got some I/O Errno5 error. For whatever reason, after changing the speed to 1 Hz and then back to 32KHz, that error disappeared. I then plugged in the module and ran i2cdetect and voila, it worked. I guess screwing around with the I2C got it working like slapping an old DVD player when it freezes or so I would like to think. I have no idea...if you can shed some light on what exactly happened I will be grateful. Or maybe I should just live in ignorance and be happy, like if someone deposits $1mil in your bank - just don't question it and accept it.

Tfantonsen
Posts: 5
Joined: Sat Jan 12, 2013 9:16 pm

Re: I2C worked but not working anymore.

Wed Apr 10, 2013 7:07 am

Glad it is working :)

The part of my code (actually it's from a wiringPi example) "strerror (errno)" converts the error number to a human readable error message. IIRC errno 5 is 'Input/output error' and in my case it was usually the device ending up in some unresponsive state, resetting it always returned it to a working state.
RasPi Model B
Using the RasPi as a 'poor mans 'scope/logic analyzer: http://www.raspberrypi.org/phpBB3/viewtopic.php?p=327908#p327908

Wholetthesmokeout
Posts: 6
Joined: Tue Apr 09, 2013 12:51 am

Re: I2C worked but not working anymore.

Wed Apr 10, 2013 5:48 pm

So I guess the 'turn it off and back on' methodology worked here :P. Also, I realized that there needs to be some sort of wait function (time.sleep() in python) after you write I2C because the processor speed is way faster than the I2C speed.

Anyways, thanks for your help!

Return to “Interfacing (DSI, CSI, I2C, etc.)”