Lennard Electronics Ltd
Using Xojo to talk to the I2C bus on a Raspberry Pi | |
What you need: |
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 ![]() 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 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 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 Thread1.Kill ⦁ In the Run event for thread1, add the following code:While True
TextField1.text = str(i2cByte) |