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:
I2C Development Board
Raspberry Pi GPIO Header
Pin 1 (3v3)
Pin 9 (GND)
Pin 3 (GPIO 2)
Pin 5 (GPIO 3)
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
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
- Type in:
- gpio –v
If Wiring Pi is not installed, go to this website: http://wiringpi.com/download-and-install/ and follow the instructions.
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"
if handle <> -1 then
timer1.Enabled = true
- In the timer action event add the following code:
call GPIO.I2CWrite(handle, i2cByte)
i2cByte = i2cByte + 1
- In the Open Event, add the following code:
You should have a Program that looks something like this: