Using I2C

Hi, all!

I'm trying to read from a Sensor which speaks I²C, but I have many problem...

The Sensor is attached to the parallel port and I have 4 pins (SDA+SCL IN and SDA+SCL OUT) to speak with him.

I tryed to write a little script in PERL, but I can't read anything...

Is somewhere a good example (PERL or C, running on Linux) to send and get data from I²C devices attached to the parallel port? Can someone help me?

Thanks a lot! Luca Bertoncello (

Reply to
Luca Bertoncello
Loading thread data ...


I am working on a hobby robot and I am using a veleman I2C board. I have a series classes that interface with the board in Linux. It uses the Linux parallel port I2C driver.

Take a look at the code at

formatting link

Reply to

Oh, I forgot, if don't want to use the Linux drivers, you can search the web for velleman 8000 code example code.

Reply to

mlw ha scritto:

I downloaded your code... Could you say me where can I find the code to send and receive data to/from I2C?

Thanks Luca Bertoncello (

Reply to
Luca Bertoncello


The I2C specification can be found at:

formatting link
BTW, which device uses seperate SDA/SCL IN and OUT ?

The I2C stuff that I use has 4 pins: +5V, SDA, SCL and GND and uses repeated starts to switch from writing to reading from the device.

Don't forget the pull-up resistors on SDA and SCL (one set per bus, not per device). I also use current limiting resistors (just in case :-)).


Reply to
Simon Clubley

OK, some history, there is some code out there that talks directly to the parallel port via ins and outs, but that required that my application run as root, and I didn't want to do that. I probably still have that code on some machine somewhere, but finding it would be a PITA.

I found that the Linux kernel had drivers for the Velleman I2C board supported through an I2C infrastructure. Now, the transition from the 2.4 kernel to the 2.6 kernel broke a few things and there may be "ifdefs" in my code. (I don't remember)

Anyway getting to it: I don't like the Velleman 8000 as it is very slow, so I wrote the code so that it should be very easy to replace it with something much better, so if it seems a bit obfuscated, just think of it as modular OOP code.

The Velleman 8000 is supported in k8000.cpp. The K8000 class is a subclass of "IOBoard" as defined in mdevice.h. "IOBoard" is mostly just a virtual class.

Also in mdevice.h is the definition of "MDevice" which is a generic system device. An implementation of MDevice is MFDevice which uses a file handle for device access. In mdevice.cpp, we have the functions, Select, SelectPort, Read, Write, ReadByte, and WriteByte, these are the functions through which a device is communicated.

Now, (This is the important file) look at linuxdev.cpp and linuxdev.h, we have a class "I2CBus." This class is used by the previously mentioned classes to talked directly to the Linux kernel driver. linuxdev.h has all the definitions for the I2C system, and linuxdev.cpp has what is needed beyond what is in MFDevice to talk to the I2C bus, specifically I2CBus overloads SelectPort and open.

If you look at linuxdev.cpp, the function "SelectPort" issues an ioctl call to the I2C bus to select the device. "I2COpen" opens the device as if it were a file.

So, once you create the class or code for your object, you will open the I2C bus device using the I2CBus class, and issue a "SelectPort(...) to select your device, and then WriteByte or ReadByte to communicate with it.

Easy enough?

Not quite, there are two scripts, and, these scripts load and configure the printer port correctly. was found on the net with no attribution and it assumes that there is a velleman driver installed (it came with the 2.4 kernel.) I wrote and is very simple, it loads the i2c-dev and the i2c-parallel port driver. In 2.6 they did away with specific vendors and just use configuration of the pins. The velleman is "type=2."

Hope that helps

Reply to

PolyTech Forum website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.