I pulled Gordon's code and used it to do some "hello world" LED stuff. After that, I spent some time figuring out how to read/write the pins in C from Gordon's examples and some others floating around the web.
Using a couple of bipolar junction transistors I had around, an infrared LED, and the buzzer from a smoke alarm, I've turned the pi into a (very vocal) chirping light detector. It's got the following components:
- BJT amplifier with an IR phototransistor in the base attached to a read-mode GPIO pin
- Shell script using Gordon's cmd-line pin reader that looks for a change in the pin state between 100ms sleeps...
- ...which invokes a pulse train generator that I wrote in C that turns a pin off and on with programmable frequency and duration...
- ...which pushes another BJT circuit that makes the buzzer chirp away
The photo detector circuit is pretty sensitive so I put the whole contraption (pi+case+breadboard circuitry) in a cardboard box with a hole cut for the photo transistor to "see" out of. With that configuration, I can point it at a window and it will chirp any time someone walks in front of it.
I've also made a couple of longer pulse train variants that I can invoke from the web server (via CGI) remotely so I can make it chatter at the wife and kids when I'm out of town for work (I think the kids are probably more amused than my wife is...).