14 Jun 2014

IO Expander PCF8574 with Raspberry Pi

With the Raspberry Pi, you can easily run out of IO pins. A good way to solve that problem is to use an IO expander to give your Pi a couple more IO. On Tayda Electronics website, you have two different chips, the 8-bits PCF8574 and the 16-bits MCP23017 from Microchip. These two devices can communicate with the PI using the I2C protocol, it only uses two pins : SDA (data) and SCL (clock). The MCP23017 is a bit more complicated than the other one because you have access to a lot of registers on the device. The PCF8574 does not use register address : you simply read or write from it.

First, you will need to activate the I2C port on your Raspberry Pi. There are several tutorials showing how to do this and I covered a couple links in my article about Adafruit's I2C LED matrix. You also have to find the address of your device. If it's a PCF8574A the address will be different.


The three least significant bits of the 7-bits address are A2, A1, and A0. These three bits are set on the pins of the chip that have the same name. That means that you could have a maximum of 8 PCF8574 connected to your Pi without having address conflict. This means you can have up to 64 more IO. If you use 8 PCF8574 and 8 PCF8574A you can double this number :)

On my breadboard, I wired 8 orange leds to the PCF8574 using 220 Ohms resistors. I plugged to chip on the 5V line to have a little bit more current on the LEDs than on the 3.3V line. A2, A1, and A0 are shorted to ground. The address of the device is then 0x38 (0111000). At first I did not understand it was this address because of the picture above, where we see a 0 next to A0. But A0 is really the least significant bit. An I2C address is always 7-bit.

On the rev2 Raspberry Pi, the I2C port is #1 and on rev1 it is #0. This little detail is important to know when we initialize the I2C device. Once everything is wired up and I2C is enabled, we can issue a detect command :


In the command i2cdetect -y 1 we specify that we are using port #1 for the rev2 Pi. There is also a really useful command we can use :
i2cset -y 1 0xADDRESS 0xDATA

For example, if I want to turn on all the LEDs directly on the command line, I can issue this command :
i2cset -y 1 0x38 0xFF

It's easy as that!!!

Python example

This demo would not be complete without some LED chasing and a demo Python program. In the sample below, LED1 through LED8 are the bit position of each LED in hexadecimal. I used the cycle function from the itertools library to create an infinite array that cycle from LED1 to LED8, and back to LED1, etc... Without pausing between each iteration, the Raspberry Pi would do that sequence really fast, so I'm adding a 100ms delay so we can see what is happening.

from smbus import SMBus
from itertools import cycle
from time import sleep

LED1 = 0x01
LED2 = 0x02
LED3 = 0x04
LED4 = 0x08
LED5 = 0x10
LED6 = 0x20
LED7 = 0x40
LED8 = 0x80

PATTERN = (LED1, LED2, LED3, LED4,
           LED5, LED6, LED7, LED8,
           LED7, LED6, LED5, LED4,
           LED3, LED2)

bus = SMBus(1) # Port 1 used on REV2 

for LED in cycle(PATTERN):
    bus.write_byte(0x38, LED)
    sleep(0.1)

Demo video

1 Jun 2014

Blasting wire tester project

I have been working on a new project for the last weeks. One of my friend is an electrical technician in a gold mine and regularly test blasting wire used to detonate dynamite. Sometimes, the wire is damaged, either open or short-circuited and he have to find where the problem is. He asked me if it was possible to create a simple portable device that continuously make a electrical contact for 1 second and release it for 1 second.

The challenge is quite easy: configure a 555 timer in the astable mode and wire the ouput to a relay coil and power all that with a 9V battery. However, I wanted to have something that looks nice and is robust so it needed a little effort on my part as my project are mostly programming and electronics.

The circuit

This time again I used Diptrace to design the schematic and PCB of my project. Before doing the PCB I tested the functionality of my circuit on a breadboard. The 555 timer is good to work from 5V to 16V and can source 200mA with its output pin, which is more than enough to drive my small relay. 


The circuit is pretty straightforward, I have a switch to connect the 9V battery to the circuit and I use a green "Power Good" LED and a red LED that flashes as the contact relay closes and open. The timing of the 555 is done with the 10uF capacitor (C2) and the 1k and 100k resistors (R4, R3). Below are pictures of the Printed Circuit Board.



The hardware


All the components used in the circuit have been bought on Tayda Electronics website, my favorite part supplier. I bought an aluminum case on Tayda to put the circuit in it and I wanted to have the top of the case look great, so I made a drawing on Inkscape and asked a local store to print a sticker out of it. Below is the original artwork:


I adjusted the DPI and size of the frame in Inkscape so I have a printed drawing the right size (2.4"x4.5"). The two following pictures are the sticker and the aluminum case with the sticker on it.




Once the sticker in place, I used a step drill bit (see picture below) to make holes the right size to put my LED holders and switch. The LED are held in place with a bezel panel mounting clip.





Video demonstration

If you want to hear the relay click and the red LED flash, just take a look at this YouTube video



That's it! I really like the outcome of this little project, this make me wants to do a lot more of good looking prototype put into cases :)

See you soon,
Fred