Page 1 of 1
I2C on Raspberry Pi
Posted: Thu Oct 02, 2014 5:00 pm
by bilko2
I am having problems with I2C.
To control my heating system I need approx 24 inputs and 24 outputs so I have made a PCB with 3 off MCP23017 lots of relays, optical isolators etc.
The MCP23017 has 16 IO pins and an IC2 interface.
My language of choice is node.js, this has an I2C module which works sometimes but gives "segmentation faults". A bit of googling shows I am not the only one with this problem.
The MCP23017 module in node works better but also gives segmentation faults (it uses the I2C module)
I have tried using the dev file interface but I cannot get ioctl to work with node.js
I have tried calling the IC2-TOOLS programs i2cget and i2cset, these work better but every 2000 calls or so the system crashes.
I am aware of the clock stretching bug in the RasPi. The MCP23017 does not do clock stretching.
I have made another system that uses Raspberry Pi/Node.js and a serial interface for control. This runs for weeks/months (so far) without problem.
My questions are:
Has anyone got I2C to work reliably in node.js? My system has to work for months making an I2C transaction every second or so.
Has anyone got I2C to work RELIABLY in anything else (eg python, c)?
I could redesign the pcb to use addressable latches ..... but I would rather not.
Re: I2C on Raspberry Pi
Posted: Thu Oct 02, 2014 5:19 pm
by FTrevorGowen
bilko2 wrote:
...
Has anyone got I2C to work RELIABLY in anything else (eg python, c)?
I could redesign the pcb to use addressable latches ..... but I would rather not.
I've certainly had no problems with 'C'-based I2C - either via 'C' system calls or via the
wiringPi library. AIUI, (some) Python code is also
wiringPi-based, ie. "Python wrapped". My first "GPIO project" was MCP23017 based:
http://www.cpmspectrepi.webspace.virgin ... OfPio.html and I've used other I2C devices subsequently. I have no experience with
node.js, so can't comment on that - it may be worth you looking through the "Interfacing" section of the forum (
http://www.raspberrypi.org/forums/viewforum.php?f=44).
Trev.
Re: I2C on Raspberry Pi
Posted: Thu Oct 02, 2014 5:49 pm
by DougieLawson
When I looked at the NodeRED stuff for i2c it appears to be based on Gordon's wiringPi library.
So take a step back, get your MCP23017s (which are easy to program) running with Gordon's wiringPi natively if it fails, tell Gordon. Once you're sure the hardware and underlying GPIO library works, move back to Node.js and run the same sequence of experiments. When it fails write a bug report for the Node.js maintainers.
Re: I2C on Raspberry Pi
Posted: Thu Oct 02, 2014 6:40 pm
by bilko2
I can talk to my MCP23017s with the wiringPi and IC2-TOOLS but once every few thousand times it does not work and causes the application to crash (this is with IC2-TOOLS, I have not done a soak test with wiring-pi).
The IC2 module in node.js causes a segmentation fault quite quickly.
Re: I2C on Raspberry Pi
Posted: Thu Oct 02, 2014 6:45 pm
by DougieLawson
Re: I2C on Raspberry Pi
Posted: Thu Oct 02, 2014 6:56 pm
by bilko2
No, I have not seen that .....
I do:
var spawn = require('child_process').spawn,
cp=spawn('i2cget', ['-y', '1', iicAddr, 0x12, 'w']);
Re: I2C on Raspberry Pi
Posted: Thu Oct 02, 2014 8:03 pm
by hampi
Best thing to do is probably to make a small C test program to write values to the chip and read them back - if this is possible with the chip. That is how I have tested my PiPIC (the code pipictest.c is in my repository).
Re: I2C on Raspberry Pi
Posted: Thu Oct 02, 2014 8:53 pm
by DougieLawson
There's a sample (using Gordon's wiringPi library) at:
http://wiringpi.com/extensions/i2c-mcp23008-mcp23017/
Re: I2C on Raspberry Pi
Posted: Thu Oct 02, 2014 9:37 pm
by FTrevorGowen
bilko2 wrote:I can talk to my MCP23017s with the wiringPi and IC2-TOOLS but once every few thousand times it does not work and causes the application to crash (this is with IC2-TOOLS, I have not done a soak test with wiring-pi).
FWIW, I've been able to software multiplex an ancient** 8-digit, seven-segment display via a
wiringPi driven MCP23017:
http://www.cpmspectrepi.webspace.virgin ... g._display
Re-visiting my notes in the above page it looks as if the code ran ~800 pairs of I2C 8-bit writes at 5ms intervals w/o problems. (I don't recall if I ran it with a main loop count > 800 - that was long enough for me to take the 'photos.)
Trev.
** Salvaged from my first calculator, a Sinclair Cambridge, built from a kit.
Re: I2C on Raspberry Pi
Posted: Fri Oct 03, 2014 6:41 am
by hansotten
Are you sure it is not hardware related? 'Once in every thousands' often in my projects was due to power spikes, interference from switching relays like you have, high impedance long wiring.
I would advise to have a look at your wiring, pullup resistors and power decoupling of the IC's.
Re: I2C on Raspberry Pi
Posted: Fri Oct 03, 2014 8:02 am
by bilko2
At the moment relays are not inserted. There are about 20cm of I2C track and lots of 0.1u capacitors scattered around.
Re: I2C on Raspberry Pi
Posted: Fri Oct 03, 2014 8:20 am
by joan
As you seem to be getting errors at regular intervals it sounds more likely that a finite resource has been consumed, perhaps memory or file handles.
To go back to your original question about I2C reliability,
I, like probably 10s of 1000s of others, use an I2C Inertial measurement Unit (IMU) on the Pi. I might typically take 150 I2C readings per second. That would be left running for hours at a time. Perhaps 4 million error free I2C readings in a session.
Re: I2C on Raspberry Pi
Posted: Fri Oct 03, 2014 10:42 am
by bilko2
I, like probably 10s of 1000s of others, use an I2C Inertial measurement Unit (IMU) on the Pi. I might typically take 150 I2C readings per second. That would be left running for hours at a time. Perhaps 4 million error free I2C readings in a session.
What language is this in?
(I accept that it's probably my error ..... these things usually are)
Re: I2C on Raspberry Pi
Posted: Fri Oct 03, 2014 10:49 am
by joan
bilko2 wrote:I, like probably 10s of 1000s of others, use an I2C Inertial measurement Unit (IMU) on the Pi. I might typically take 150 I2C readings per second. That would be left running for hours at a time. Perhaps 4 million error free I2C readings in a session.
What language is this in?
(I accept that it's probably my error ..... these things usually are)
In my case normally C. Python should work just as well. Anything using /dev/i2c-x will work.
I doubt you've done anything wrong. It's likely to be a bug within the node.js environment (which I know nothing about).
Re: I2C on Raspberry Pi
Posted: Sat Oct 04, 2014 1:15 am
by BAStumm
I/O Pi from AB Electronics UK uses two mcp23017 chips (default 0x20 and 0x21) which is supported by wiringPi. I am using this board in my project and it works fine. My code is written in C. I've not had a seg fault ever and have been running for at least a month and at least 5 or more hours per day. I can't share my real project code but here is a snippet I modified from the "example" included in wiringPi. I connected a momentary contact button (reset button of a desktop pc case modified with a resistor inline) to pin 16 of IC1 of the I/O Pi and to ground.
Code: Select all
/*
*
* Using the IO Pi board for its dual mcp23017
*
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
***********************************************************************
*/
#include <stdio.h>
#include <wiringPi.h>
#include <mcp23017.h>
int main (void)
{
int i;
wiringPiSetup () ;
mcp23017Setup (100, 0x20) ;
printf ("Raspberry Pi - MCP23017 Test\n");
for (i = 0 ; i < 10 ; ++i)
pinMode (100 + i, OUTPUT) ;
pinMode (100 + 15, INPUT) ;
pullUpDnControl (100 + 15, PUD_UP) ;
for (;;)
{
// push button connected to pin 16 of IC1 and ground of Pi.
if(digitalRead(115) == 0) {
printf("Button Pushed\n");
} else {
printf("Button NOT Pushed\n");
}
delay (1000) ;
}
return 0 ;
}