Click here to Skip to main content
13,706,637 members
Click here to Skip to main content
Add your own
alternative version

Stats

3.5K views
44 downloads
1 bookmarked
Posted 7 Aug 2018
Licenced CPOL

8-bit LED Binary Counter

, 7 Aug 2018
Rate this:
Please Sign up or sign in to vote.
Raspberry PI implementation of binary counter originally developed for Arduino board.

Introduction

This article offers a simple implementation of the binary counter powered by Raspberry PI3 and 8 LEDs. It is very similar to what was done before for Arduino board in the previous article Connecting an Arduino to a Breadboard to Light Up LEDs. In this project, we will be using Python to program the board.

For this project, we will need a Raspberry PI kit (I'm using one of these CanaKits), a breadboard, 8 LEDs, 8 220 Ohm resistors (can be different, e.g., 330, etc.) and 9 male-to-female jumper wires. If you decide not to remote to Raspberry, then you will also need a monitor with HDMI cable, mouse and keyboard.

Setting Up Work Environment

I'm using a default installation of Raspberry PI 3 which comes with Raspbian OS and Python pre-installed. Make sure you have the latest GPIO module installed as the code uses it to write to GPIO pins.

$ sudo apt-get update
$ sudo apt-get install python-rpi.gpio python3-rpi.gpio

I enabled Wi-Fi on my Raspberry and also configured Secure Shell (SSH), so I can remote to it from my Windows machine using Putty. This way, we won't need a monitor, mouse, keyboard and Ethernet cable to communicate with the device. The only cable that we need is a power cord.

After installing Samba on Raspberry, we can access working files from Windows machine and edit them in our favorite text editors. Alternatively, we still can use nano editor in terminal window which comes with the distribution.

Connecting Wires

We will need 8 wires to connect GPIO pins to corresponding LEDs via their individual resistors (plus 1 wire for the ground). Here, I decided to use pin 33 for bit 0, pin 37 for bit 1, etc (see the code). The layout below shows which pins are available.

Using the Code

In the code, we first set 8 selected pins into output mode. Then in a loop, we extract current value of each bit of the counter by applying corresponding bit mask and set individual GPIO pin mapped to that bit.

# counter.py
import RPi.GPIO as GPIO
import time

print "=== Binary counter ==="

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD) # access pins by their numbers	

# Configure 8 pins to output mode
GPIO.setup(33, GPIO.OUT) # pin 33 for bit 0
GPIO.setup(37, GPIO.OUT) # pin 37 for bit 1
GPIO.setup(7,  GPIO.OUT)
GPIO.setup(11, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)
GPIO.setup(19, GPIO.OUT)
GPIO.setup(21, GPIO.OUT) # pin 21 for bit 7

cnt = 0
print "Press Ctrl-C to exit"

try:
	while True:
		GPIO.output(33, cnt & 0x01) # set bit 0
		GPIO.output(37, cnt & 0x02) 
		GPIO.output(7,  cnt & 0x04)
		GPIO.output(11, cnt & 0x08)
		GPIO.output(13, cnt & 0x10)
		GPIO.output(15, cnt & 0x20)
		GPIO.output(19, cnt & 0x40)
		GPIO.output(21, cnt & 0x80) # set bit 7
		time.sleep(0.1)             # wait 100 msec
		cnt += 1 %  256				# increment counter up to 255, then reset to 0

except KeyboardInterrupt:
	GPIO.cleanup()

If you saved the code in counter.py file, just run this command to start the counter:

python counter.py

And that's it. Happy coding!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Leonid Fofanov
Web Developer
United States United States
In my last 20+ years I was building mostly web applications using C#/ASP.NET, Sitefinity, SharePoint, J2EE, Classic ASP and ColdFusion. Earlier I spent several years coding for embedded systems.

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180920.1 | Last Updated 7 Aug 2018
Article Copyright 2018 by Leonid Fofanov
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid