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: https://projects.drogon.net/raspberry-pi/wiringpi/. 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).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <wiringPi.h> int main( void ) { printf ("A Binary Counter with WiringPin") ; // Initialise the wiringPi library and die if it fails. if( wiringPiSetup() == -1 ) { exit( 1 ); } // Loop for GPIO pins 0 to 7 for( int pin = 0 ; pin < 8 ; ++pin ) { // Set the pin as an OUTPUT pinMode( pin, OUTPUT ); // Set the pin LOW (off) digitalWrite( pin, LOW ); } // Count 0 to 255 for( int count=0; count=0; p-- ) { // I calculate the binary value by checking if it is higher than // the bit value, if it is, set the bit and then subtract the bit // value. Seems to work... if( x >= exp2(p) ) { digitalWrite( p, HIGH ); x = x - exp2(p); } else { // It isn't higher than this bit value so set the pin LOW digitalWrite( p, LOW ); } } // Wait a second delay( 1000 ); } return 0; } |
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…