Lennard Electronics

Kiwi made, innovative electronics

Using Xojo to talk to the I2C bus on a Raspberry Pi

What you need:
⦁    A Raspberry Pi 2 or 3
⦁    A PCF8574 I2C to 8 bit Port IC
⦁    For Breadboarding, use a PCF8574P, or PCF8574AP.
⦁    4 Jumper leads – male to female

The following example uses a Lennard Electronics I2C development board, which has a number of I2C chips on it, including a PCF8574T.  But you can easily build up a circuit on a breadboard by following this circuit:

With the address pins, A0, A1, and A2 connected to ground, the PCF8574's address with be 0x20.  If you are using a PCF8574A, it's address will be 0x38.  Before powering up your Raspberry Pi, connect the I2C development board up to the Raspberry Pi’s GPIO header as follows:
I2C Development Board
  Raspberry Pi GPIO Header
  Pin 1 (3v3)
  Pin 9 (GND)
  Pin 3 (GPIO 2)
 Pin 5 (GPIO 3)
DO NOT connect Vdd to pins 2 or 4 on the Raspberry Pi header.  Although the chips on the I2C development board will work with 5V, you could end up damaging the SCL and SDA ports on the Raspberry Pi as it is designed to work with 3v3 logic levels.

Power up the Raspberry Pi.  The 8 LEDs on the I2C development board will light up.  This is the default state of the PCF8574’s ports on power up.

  • Enable I2C in the Raspberry Pi’s configuration panel
  • Install I2C tools:
  • Sudo apt-get install i2c-tools
  • Check the devices on the I2C development board are seen on the I2C bus
  • Sudo i2cdetect –y 1
You should see four ID’s show up.

The PCF8574T, on the Lennard Electronics I2C development board, and the PCF8574P, will have an address in the range 0x20 to 0x27, depending on the address switch settings for that chip.
If using a PCF8574AP, or AT, then it’s address will be in the range 0x38 to 0x3F.

Before we start any programming in Xojo, run some command line tests to check that the I2C bus is working:
The following assumes the address is 0x22 – replace with whatever your PCF8574’s address is.

  • Turn off all ports on the PCF8574:
  • i2cset –y 1 0x22 0x00
Check things are working by running the same command again but trying other hexadecimal numbers in place of 0x00. If the above is successful, then we know that I2C is working on the Raspberry Pi. We now need to make sure that “WiringPi” is installed on the Raspberry Pi. This is the library that your Xojo code will talk to in order to access the I2C bus.
  • Type in:
  • gpio –v
If you get information back, then Wiring Pi is installed, and you can skip the next step and start programming in Xojo.
If Wiring Pi is not installed, go to this website:  http://wiringpi.com/download-and-install/ and follow the instructions.

Xojo programming:
Start a new Desktop project in Xojo. 
Also, open one of the Raspberry Pi projects in the Examples folder, and copy the GPIO module over to your new project.

  • Create a new global property called “handle” of type integer.
  • Create a new global property called “i2cByte” of type byte.
  • Add a label to Window1.
  • Add a timer to Window1.
  • Add an open event to Window1.
  • Add a pushbutton to Window1.  I find that making the Button Style "Textured", and height about 40, looks best on the Raspberry Pi.  In it’s action event add the following code:

//replace xx with the address of your PCF8574
handle = GPIO.I2CSetup(&hxx)
if handle = -1 then
   MsgBox "Error setting up I2C"
end if
if handle <> -1 then
   timer1.Enabled = true
end if

  • In the timer action event add the following code:
Label1.Text = "Byte value:  " + str(i2cByte)
call GPIO.I2CWrite(handle, i2cByte)
i2cByte = i2cByte + 1

  • In the Open Event, add the following code:
Timer1.Enabled = false

You should have  a Program that looks something like this:

Compile for Linux, Arm32.  Copy the build folder over to your Raspberry Pi, and run it.  When you hit the Pushbutton, the ports on the PCF8574 should start counting in binary.