Beyond software solutions

We don't stop at building software. We offer a range of services to help projects in the domain of software development to thrive forward. We put our focus on qualitative and innovative development skills! #Software #Business #Solutions #Architects

Binary Christmas Calendar

Binary Christmas Calendar6 min read

Binary Advent Calendar

Disclosure: Please do not judge me on the pertinence of working on this project. The background of this prototype development is that I have a girlfriend who really loves Christmas. And I am a boyfriend, who really loves binary

Ok so let’s start on that journey by showing you the end result. Of course, it is not the most beautiful Christmas calendar. But it is a working binary calendar, teaching my girlfriend how to read binary day numbers.

Right, so here the actual working calendar, on Tuesday 6th December 2018:

As you can see, I used a Raspberry Pi 3 with the GPIO pins coupled to a Breadboard using a T-Cobbler Plus with an IDE-cable.

The actual calendar feature is to transcribe the current date day number into its binary representation and display this on the LEDs accordingly. Additionally, during the Advent period of this month of this December, my girlfriend has to unroll post-its on which she can see the binary representation of the day as well.

Building the circuit

As a first, we will need to create an electronic circuit for which we will be using the following parts:

  • Power cables (Bridges and circuit creation)
    • 8 for the positive legs of our LEDs
    • 8 for the negative legs of our LEDs
    • 10 for our needed power bridges
    • 1 for our shutdown button
  • LEDs (8 red LEDs and 8 yellow LEDs)
  • A resistance of 10 moh
  • A shutdown button (optional)

Now let’s define on which GPI pins we will be connecting our LEDs. For this, simply plug one end of the power cable in the corresponding GPIO pin – In case you don’t know how to tell which GPIO pin you are connecting too, check the following picture and read the caption beneath it.

T-Cobbler Plus for Raspberry Pi
As you can see #16, #20, #21 and on the other side #5, #6, #13, #19 and #26. Those are GPIO pin numbers. Power cables must be coupled to the rows in which those numbers are plugged.

Our Power cables and resistance will be connected directly to those GPIO pin numbers with the following order and positions:

  • Power cable 1: GPIO pin #17
  • Power cable 2: GPIO pin #27
  • Power cable 3: GPIO pin #22
  • Power cable 4: GPIO pin #13
  • Power cable 5: GPIO pin #19
  • Power cable 6: GPIO pin #26
  • Power cable 7: GPIO pin #20
  • Power cable 8: GPIO pin #21
  • Power cable 9: GPIO pin #18
  • Resistance of 10 Ohms: GND

Additionally, we will couple a resistance of 10 Ohms to ground (GND). In a later step, this is also where we will couple negative legs of our LEDs.

Now simply bridge the power with those cables, into a lower part of your breadboard – as an example you can start at row #30 of your breadboard. I recommend to keep 3 pins between each cable connection (in the vertical). After doing this, you will have full rows on your breadboard which are coupled directly to GPIO pins of your Raspberry Pi. Do this for all 9 cables, and keep the last one a bit lower as this is where we will plug our shutdown button.

This step will help us later in keeping our cables organized, given that we are working with 16 LEDs, this can get complicated very easily.

Now take your LEDs, and pin them into the other side of your breadboard, where the current power cables are not being bridged yet. This will let us have a feeling of a screen of LEDs. Fine, so now that you plugged all 16 LEDs – with their positive leg aligned with the breadboard’s other side power bridges – we can couple the resistance to negative legs and also start forming our power bridges to illuminate the LEDs.

So lets first couple the resistance to avoid any dramatic power damages. Take the breadboard pin in which you have added your resistance, and insert a cable to bridge it with each LEDs’ negative leg. You will need to build row-bridges, as shown in this picture:

MB-102 830 Point Solderless Breadboard+Power Module ...
The blue cable is used to bridge power from row 19 on the left side to the row 51 on the right side. This blue cable is an example of a power bridge as used above.

Now, we will build our power bridges to finalize the breadboard prototype. These power bridges are quite simple, they will simply forward the power from the one side of the breadboard, into the other side, where our LED’s positive legs are pinned.

Of course, after building the hardware part, we will also need to develop some software. I chose to develop a quick Python script which I added to the startup of my Raspberry Pi, as I will show you in the next section.

Interfacing with your Raspberry Pi

The Raspberry Pi 3, with the raspbian operating system installed, comes with Python pre-installed. You can find a full copy of the source code related to this article by following this link.

Our source code will be very simple. It will be using the RPi.GPIO library to perform tasks on our Raspberry Pi’s GPIO pins.

In fact, all our script needs to actually execute are following steps :

  • Read the day number of the current date
  • Transcribe the day number in its binary representation
  • Interact with the corresponding GPIO pins

In Python, these instructions would look a little bit like the following script, which you can save in a new file binary-calendar.py:

import RPi.GPIO as gpio
import time
import datetime

# we are using RPi.GPIO in BCM mode
gpio.setmode(gpio.BCM)

# setup our GPIO pins
leds_gpio = [17, 27, 22, 13, 19, 26, 20, 21]
for p in legs_gpio:
    gpio.setup(p, gpio.OUT)

current_date = datetime.datetime.fromtimestamp(time.time())
day = bin(str(current_date)[8:10])[2:]

# binary representation is read from right to left
at = 7
for b in day.rjust(8, '0'):
    if b == '1':
        gpio.output(led, 1)
    at--

print("See you soon..")
gpio.cleanup()

As I said, it is kept very simple and to the point. This script also initializes and cleans your Raspberry’s GPIO. The execution of this script can be done with a simple command as follows:

$ python binary-calendar.py

Executing this script, you will see that each light that is turned on, corresponds to a binary 1. As an example, say today is the 7th of December, this will be represented in binary as: 00000111. Now the program would turn on the first three lights when starting to count from the right.

That’s it already! You can now reproduce my awesome binary Christmas calendar! You can find a full copy of the source code related to this article by following this link.

Thank you for reading and I hope to see you again soon!

Cheers,
Grégory Saive

eVias

Founder of eVias Services, Greg provides quality development supported by a team of technology experts and developers. eVias Services stands for high quality and innovative development skills -- Feel free to browse through Greg's projects and don’t hesitate to contact him for any questions: https://github.com/evias

Leave a Reply

%d bloggers like this: