Sunspot Home

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>
#include <pcf8574.h>
#include <stdio.h>
int main()
{
int i;
pcf8574Setup(100,0x27); //100 is the chosen name of this chip at 27
// so these 8 pins on this chip have names 100,101- - - 107
for(i=0;i<8;i++)pinMode(100+i,OUTPUT);
while(1)
{
i=0;
for(i=0;i<=8;i++)
{
printf("CurrentLED=%d\n",100+i);
digitalWrite((100+i),HIGH);delay(100);
digitalWrite((100+i),0);
delay(100);
}
}
}

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 -

/*
* PCF8574.cpp
*
* Created on: 2013-06-30
* Author: jlg
*/

#include <iostream>
#include "wiringPi/wiringPi.h"
#include "wiringPi/pcf8574.h"

using namespace std;

#define BASE_I2C 128

int main (void)
{
int i;

cout << "NXP PCF8574"<< endl;

wiringPiSetup(); // initialise wiringPi
pcf8574Setup(BASE_I2C, 0x20); // initialise PCF8574

pinMode(BASE_I2C + 0, INPUT); // define P0 as input
for (i = 1 ; i < 7 ; ++i) // define remaining GPIOs as output
pinMode(BASE_I2C + i, OUTPUT);

while (1)
{
digitalWrite(BASE_I2C + 7, 1); // set P7 to "1" (LED off)
delay(50); // 50µs warten
if (digitalRead(BASE_I2C + 0) == 0) // if P0 "0"
delay(150); // wait 150µs
digitalWrite(BASE_I2C + 7, 0); // set P7 to "0" (LED on)
delay(50); // wait 50µs
if (digitalRead(BASE_I2C + 0) == 0) // if P0 "0"
delay(150); // wait 150µs
}
return 0;

}



Testing the PCF8591 analog read I2C chip

PCF8591.c

#include <wiringPi.h>
#include <pcf8591.h>

main ()
{
wiringPiSetup () ;
pcf8591Setup (200, 0x4C) ;

for (;;)
printf ("%4d %4d %4d %4d\n", analogRead (200), analogRead (201), analogRead (202), analogRead (203)) ;
}

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)}'


It just prints the temperature then stops

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
WARNING! This program can confuse your I2C bus, cause data loss and worse!
I will probe file /dev/i2c-0.
I will probe address range 0x03-0x77.
Continue? [Y/n]
.....0 .1 .2 .3 .4 .5 .6 .7 .8 .9 .a .b .c .d .e .f
00: .........-- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: 20 21 22 -- -- -- -- 27 -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- 39 -- 3b 3c 3d -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- 4d -- 4f
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

20 - PCF8574P - Read Maplin thermometers - port A (top middle)
21 - PCF8574P - Read Maplin thermometers - port A (top left)
22 - PCF8574P - read door states etc

27 - PCF8574P - small test box if connected

39 - PCF8574AP - A to D multiplex driver

3b - PCF8574AP - 8 yellow LEDs - water solenoids
3c - PCF8574AP - 8 red LEDs - water solenoids
3d - PCF8574AP - 8 green LEDs - water solenoids

4f - LM75 thermometer
4d - PCF8591 on small ghouse board

-and see this old note

 

SUNSPOT HOME

more to come . . .