Driving the Raspberry Pi GPIO.
Objective
Control external devices using the GPIO pins directly and via the I2C bus
Method
Use the WiringPi library from here
Install wiringPi
I downloaded from here
I decompressed on the Mac and used Cyberduck sftp to load the files into folder /home/graham/wiringpi
then
root@raspberrypi:/home/graham/wiringpi# ./build
blink.c test
#include <wiringPi.h> int main (void) { wiringPiSetup () ; pinMode (6, OUTPUT) ; for (;;) { digitalWrite (6, HIGH) ; delay (500) ; digitalWrite (6, LOW) ; delay (500) ; } return 0 ; } |
To compile, cd to the folder containing blink.c and do
root@raspberrypi:/home/graham/C_code/wiringpi# gcc -Wall -o blink blink.c -lwiringPi
run with
root@raspberrypi:/home/graham/C_code/wiringpi# ./blink
NB wiringPi pin 6 is header pin 22
Drive I2C port expander PCF8575
PCF8575_blink.c
#include <wiringPi.h> |
root@raspberrypi:/home/graham/C_code/wiringpi# gcc -Wall -o PCF8575_blink PCF8575_blink.c -lwiringPi
root@raspberrypi:/home/graham/C_code/wiringpi# ./PCF8575_blink
I found this - not yet tested -
/* #include <iostream> using namespace std; #define BASE_I2C 128 int main (void) cout << "NXP PCF8574"<< endl; wiringPiSetup(); // initialise wiringPi pinMode(BASE_I2C + 0, INPUT); // define P0 as input while (1) } |
Testing the PCF8591 analog read I2C chip
PCF8591.c
#include <wiringPi.h> main () for (;;) |
Compile it -
To test the PCF8591 demo boards from China see this
gcc -Wall -o PCF8591 PCF8591.c -lwiringPi
Read an LM75 thermometer
I found this here (many thanks) - o
I have yet to learn how it works - 0x4F is the address of the LM75
#!/bin/bash i2cget -y 0 0x4F 0x00 w | awk '{printf("%.1f\n", (a=( (("0x"substr($1,5,2)substr($1,3,1))*0.0625)+0.1) )>128?a-256:a)}' |
root@raspberrypi:/home/graham/C_code/wiringpi# ./LM75_2.sh
20.2
So I will be able to call that from Blassic
I2C speed
The default speed is 100000 and my long I2C line gave errors.
The speed can be changed by putting -
dtparam=i2c0_baudrate=20000
at the end of /boot/config.txt
The i2c bus that i2cdetect uses is bus 0 for my original Pi B
(there is also a bus 1in the dmesg printout - perhaps for a different Pi?))
Download the C code files above and compiled binaries (ready to run) files here
Notes for my own reference :-
root@raspberrypi:~# i2cdetect 0 20 - PCF8574P - Read Maplin thermometers - port A (top middle) 27 - PCF8574P - small test box if connected 39 - PCF8574AP - A to D multiplex driver 3b - PCF8574AP - 8 yellow LEDs - water solenoids 4f - LM75 thermometer |
-and see this old note
more to come . . .