We use cookies on this site to remember: your settings, your history, whether you are logged in and to give you a better experience.
More info about cookies
Accept Cookies
Opt Out
You have an excellent choice of browser. Why not add our quick launch app so you can easily manage your shop in the future.Get our free app from the Google Chrome Store.
Login       Register

Raspberry Pi Counting in Binary

As a simple Christmas project at GroovyCart we thought we would have a play with a Raspberry Pi computer, we got various small projects working and we thought it would be worth posting some examples and samples of our code for anyone else that it might benefit.

This article is intended to help people that are developing their Raspberry Pi, however it may also inspire some of our shop owners to turn their hand to a bit of programming.

So the first project we will be looking at will be setting up 3 LEDs on a breadboard and connecting them to a Raspberry Pi that will programatically get them to turn on and off counting in binary. These are the GroovyCart Christmas lights.

Here are the numbers for anyone interested:
001   010   011   100   101   110   111
(0=light off, 1= light on)

Here is a video of the finished project here:

We used a breadboard to connect 3 LEDs to the Raspberry Pi via the GPIO ports, we will skip over that part because there are so many guides to doing that on the internet.

Raspberry Pi connected to a Breadboard
Raspberry Pi connected to a Breadboard.

We found the easiest way to interface with the GPIO ports was using Python, once again there are loads of guides for setting up python on a Raspberry Pi and we don't want to go around repeating what others are already saying.

The red, yellow and white cables are the power cables for the LED of that colour
The red, yellow and white cables are the power cables for the LED of that colour.

So a really quick overview of what we set up. The Raspberry Pi has 26 pins called GPIO pins that can be used to either send a signal out of the Pi or detect a voltage coming into the Pi. From the Pi we connected pin 6 to the Breadboard and used this as the ground for our LEDs. We then used pin 11, 13 and 15 (sidenote: the Pi refers to these as GPIO 17, 21 and 22 respectively) to send a voltage out to power the LEDs.

We also had to put a resistor before each LED and for us the hardest part of the project was finding out what resistors to use.

Now we get to the fun part.

In Python we used PRi.GPIO 0.4.1 to start activating the GPIO pins.

Once this is installed you can pull up a Python terminal window and enter:

import time import RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) GPIO.setup(11,GPIO.OUT) GPIO.setup(13,GPIO.OUT) GPIO.setup(15,GPIO.OUT) L1 = 11 L2 = 13 L3 = 15 delay = 0.5 def changelights(led1,led2,led3): GPIO.output(L1, led1) GPIO.output(L2, led2) GPIO.output(L3, led3) print "LEDs = ",((led1)+(led2*2)+(led3*4)) time.sleep (delay) return while True : changelights(False, False, False) changelights(True, False, False) changelights(False, True, False) changelights(True, True, False) changelights(False, False, True) changelights(True, False, True) changelights(False, True, True) changelights(True, True, True) time.sleep (delay)

This will output onto the screen:

LEDs = 0 LEDs = 1 LEDs = 2 LEDs = 3 LEDs = 4 LEDs = 5 LEDs = 6 LEDs = 7 LEDs = 0 LEDs = 1 ....

So lets break it down:

import time import RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) GPIO.setup(11,GPIO.OUT) GPIO.setup(13,GPIO.OUT) GPIO.setup(15,GPIO.OUT)

This just sets up the delay and also the GPIO software

L1 = 11 L2 = 13 L3 = 15 delay = 0.5

This initialises the variables, the first 3 lines select the pins we are going to use and the last one is the speed the lights change at.

def changelights(led1,led2,led3): GPIO.output(L1, led1) GPIO.output(L2, led2) GPIO.output(L3, led3) print "LEDs = ",((led1)+(led2*2)+(led3*4)) time.sleep (delay) return

This is just a function used to change the lights and then display a count in the terminal. Functions in Python are done by indentation.

while True : changelights(False, False, False) changelights(True, False, False) changelights(False, True, False) changelights(True, True, False) changelights(False, False, True) changelights(True, False, True) changelights(False, True, True) changelights(True, True, True) time.sleep (delay)

This last bit of code just loops around and also selects what lights should be on.






Be groovy and share us around:
You might be interested in:
You can also find us on: