First experiments with the Raspberry Pi and its GPIO outputs

Shadow TopStill 1a
Shadow Bottom

I’ve completed my first experiment in building something physical with my new Raspberry Pi.

It’s not overly useful, but I’ve built a binary counter using LEDs.  The following video explains more…

I originally started writing the code in Python. Mostly because many of the examples for controlling the Raspberry Pi are in Python, but having very little experience in Python made this frustrating – I want to control the Raspberry Pi, not learn yet another programming language. In the end I switched to C. It’s been about 15 years or more since I last wrote any C and remembering how primitive it is compared with modern high-level languages slowed me down a bit.  Why did I choose C? Well it seemed to have good support in terms of third party libraries for driving the GPIO outputs and having downloaded a python wrapper for what was basically a C library, it seemed logical.  The library I’ve chosen is the wiringPi library available under the GNU license here: This attracted me because it has support for the PWM output on the Pi and it seems pretty quick (particularly when writing and compiling native C rather than using Python).

I couldn’t for the life of me work out how to convert a number into a binary bit pattern – it must be easier than the code I have written – perhaps using bit shifting or something. Anyway, it seems to work. What I do is work backwards from 128 to 1; if the value is higher than the bit I am looking for, then the bit must be 1 so I set the bit High, then subtract the value of the bit from the value and move on. Seems complicated, but it isn’t if you follow the process:

Example: Value 74.

Bit Number Bit Value Result New Value Bit Pattern
8 128 0 74 0
7 64 1 10 10
6 32 0 10 010
5 16 0 10 0010
4 8 1 2 10010
3 4 0 2 010010
2 2 1 0 1010010
1 1 0 0 01010010

Here is a simple circuit diagram for the electronics side of the project.

So, whats next. Not sure really, I think I will continue playing and perhaps do something involving the GPIO pins as inputs. I bought some small tactile switches from Maplin today…

About the Author

chrisChris works as an IT Security Specialist for a large FTSE 100 company. He is a geek at heart and loves nothing more than trying to automate his home. The problem is, his wife simply doesn't get it and can't understand why they can't have 'normal' lights like everyone else! Chris is dedicated therefore to implementing automation in a family friendly way.View all posts by chris →

Leave a Reply