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:

You can also add an LED and resistor from each port to ground.
This way, you can use the same circuit for either writing to or reading from.
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
Vdd Pin 1 (3v3)
GND Pin 9 (GND)
SDA Pin 3 (GPIO 2)
SCL 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. It also assumes you are using the Lenanrd Electronics I2C Board, or have added LED's to the above circuit.
⦁ 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 window layout like below, by adding 17 labels and a textfield.
Label9 to label16 will show the logic level of the ports on the PCF8574.
The Textfield will show the byte value.



⦁ Add a Timer to Window1, set it's period to 10 milliseconds.
⦁ Add a Thread to Window1
⦁ Add an Open event to Window1
⦁ Add a Close event to Window1
⦁ Add a property of type integer to Window1. Call it "handle"
⦁ Add a property of type byte to Window1. Call it "i2cByte"
⦁ Add the following code to the Open event:

handle = gpio.I2CSetup(&h22) //start talking to the PCF8574 at address 0x22
call GPIO.I2CWrite(handle, &hFF) //set all ports high, so they can be used as inputs
thread1.run

Add the following code to the Close event:

Thread1.Kill

⦁ In the Run event for thread1, add the following code:

While True
  i2cByte = GPIO.I2CRead(handle)
  App.DoEvents(50)
//Small delay helps with CPU usage
Wend

In the Action event for timer1, add the following code:

TextField1.text = str(i2cByte)
if str(i2cByte and &h01) = "0" then
  label9.text = "0"
else
  Label9.text = "1"
end if
if str(i2cByte and &h02) = "0" then
  label10.text = "0"
else
  Label10.text = "1"
end if
if str(i2cByte and &h04) = "0" then
  label11.text = "0"
else
  Label11.text = "1"
end if
if str(i2cByte and &h08) = "0" then
  label12.text = "0"
else
  Label12.text = "1"
end if
if str(i2cByte and &h10) = "0" then
  label13.text = "0"
else
  Label13.text = "1"
end if
if str(i2cByte and &h20) = "0" then
  label14.text = "0"
else
  Label14.text = "1"
end if
if str(i2cByte and &h40) = "0" then
  label15.text = "0"
else
  Label15.text = "1"
end if
if str(i2cByte and &h80) = "0" then
  label16.text = "0"
else
  Label16.text = "1"
end if

Compile for Linux, Arm32. Copy the build folder over to your Raspberry Pi, and run it. As you press the buttons, you should see the status of each port change, as well as the byte value.