Freitag, 25. Dezember 2015

Raspberry pi - led 7-segmentdisplay

led 7-segmentdisplay


I really recommend you to go through my first post about controlling leds with the raspberry before starting with this topic, if you haven't already read it.

Things you need:
.) raspberry pi
.) 7-14 leds (not necessary to have all of the same color, but it looks better)
.) resistors (about 330 ohm)
.) breadboard
.) gpio jumper wire
.) breadboard jumper wire
.) Basic Knowledge of using the unix-shell

The aim of this post is to show you how to build a one digit 7-segment-display with single leds. You can change the breadboard/raspberry-layout of the circuit or the program itself so it fits your preferences. The following posts will continue with 7-segment displays in form of ticker-displays, counter-displays etc. This will just be the basement of the next posts.

Note: I'm using a raspberry pi model b+ so i have 40 gpio-pins. If you have an other model, you just have to change the pins your using.

1. Thinking about what we're doing now


Before we get started we need to make sure we are aware of everything we need to do in this session. Basically we want to have 7 different segments of leds we can control, so we need at least 8 gpio pins to work with (7 controls & 1 ground), but its more comfortable if we use 2 ground pins.
For each segment we need at least one led, but to get clarity it's good to use 2 leds per segment.
It's also useful to document which gpio pins you're working with, so you don't have to check the gpio-table for the pin-number.


2. Setting up the circuit


Now things get little bit difficult, but don't worry. I chose the last 7 gpio pins for this, you can choose the pins you want. So we need to attach to every (pair of) led(s) a jumper wire. If you take two leds per semgent, you need to connect them in parallel, but to keep it clear for the moment I made a diagram with one led per segment.
As you can see, i collect the ground signal from the single leds to 2 lines, so I only have to use 2 ground pins (34, 30). If you want to use two leds per segment, every segment will look similar to this:

 Here two leds are connected parallel to one gpio signal. The ground leads to one line where multiple grounds are connected and then brought back to one ground pin all together. The reason why the leds are crooked is just because otherwise you couldn't realise how they are connected. In reality they can be straight, no matter.

Behind the circuit as shown above (7 segment) is a system. Every segment has a GPIO-pin-number and a new internal controlling number. If you chose the same sequence as i did, theese two diagrams will explain what I mean.


  
 The red number is the new internal number, which we need later when we program our controlling system. The numbering is not the same as the standard but it works fine for me.

3. Programming


When we have finished the previous task, we can switch to the programming work. As already mentioned we need something to control 7 led-segments. Each segment got our internal number and the gpio-pin-number. Keep this in mind, but first I have to explain some programming stuff.

List

A list is a datatype which can store multiple instances of datas. For example an integer-list can store undefined amount of integers. Same principel with other types as strings or doubles.

Methods

Methods are program parts which you can call to do something over and over again. For example you need to get done everytime the same calculations with two numbers? Put the calculation into a method and commit the two numbers to it. So you can call this method with two numbers and get the result back.

Loops

Loops are program parts which redo their actions as often as we tell them to. There are various ways to define loops, but I will only explain that one we're using in our program.

If we understand these things, we can begin coding. You can copy the following code into your file.

import RPi.GPIO as GPIO
import time


al = [21,20,16,12,7,8,25]


GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)


for i in range(len(al)):
    GPIO.setup(al[i],GPIO.OUT)
  


def alloff():
    for i in range(len(al)):
        GPIO.output(al[i],GPIO.LOW)


def printleft(nu):
        if nu == 0:
                GPIO.output(al[1],GPIO.HIGH)
                GPIO.output(al[0],GPIO.HIGH)
                GPIO.output(al[5],GPIO.HIGH)
                GPIO.output(al[6],GPIO.HIGH) 
                GPIO.output(al[4],GPIO.HIGH) 
                GPIO.output(al[2],GPIO.HIGH) 

        elif nu == 1:
                GPIO.output(al[2],GPIO.HIGH)
                GPIO.output(al[5],GPIO.HIGH)
        elif nu == 2:
                GPIO.output(al[1],GPIO.HIGH)
                GPIO.output(al[2],GPIO.HIGH)
                GPIO.output(al[3],GPIO.HIGH)
                GPIO.output(al[4],GPIO.HIGH)
                GPIO.output(al[6],GPIO.HIGH)
        elif nu == 3:
                GPIO.output(al[1],GPIO.HIGH)
                GPIO.output(al[2],GPIO.HIGH)
                GPIO.output(al[3],GPIO.HIGH)
                GPIO.output(al[5],GPIO.HIGH)
                GPIO.output(al[6],GPIO.HIGH) 
        elif nu == 4:
                GPIO.output(al[0],GPIO.HIGH)
                GPIO.output(al[2],GPIO.HIGH)
                GPIO.output(al[3],GPIO.HIGH)
                GPIO.output(al[5],GPIO.HIGH)
        elif nu == 5:
                GPIO.output(al[1],GPIO.HIGH)
                GPIO.output(al[0],GPIO.HIGH)
                GPIO.output(al[3],GPIO.HIGH)
                GPIO.output(al[5],GPIO.HIGH)
                GPIO.output(al[6],GPIO.HIGH)
        elif nu == 6:
                GPIO.output(al[1],GPIO.HIGH)
                GPIO.output(al[0],GPIO.HIGH)
                GPIO.output(al[3],GPIO.HIGH)
                GPIO.output(al[5],GPIO.HIGH)
                GPIO.output(al[6],GPIO.HIGH) 
                GPIO.output(al[4],GPIO.HIGH) 
        elif nu == 7: 
                GPIO.output(al[1],GPIO.HIGH)
                GPIO.output(al[2],GPIO.HIGH)
                GPIO.output(al[5],GPIO.HIGH)
        elif nu == 8:
                GPIO.output(al[1],GPIO.HIGH)
                GPIO.output(al[0],GPIO.HIGH)
                GPIO.output(al[3],GPIO.HIGH)
                GPIO.output(al[5],GPIO.HIGH)
                GPIO.output(al[6],GPIO.HIGH) 
                GPIO.output(al[4],GPIO.HIGH) 
                GPIO.output(al[2],GPIO.HIGH) 
        elif nu == 9:
                GPIO.output(al[1],GPIO.HIGH)
                GPIO.output(al[2],GPIO.HIGH)
                GPIO.output(al[3],GPIO.HIGH)
                GPIO.output(al[5],GPIO.HIGH)
                GPIO.output(al[6],GPIO.HIGH) 
                GPIO.output(al[0],GPIO.HIGH)


for i in range(10):
    printleft(i)
    time.sleep(1)
    alloff()



This program is our base. We can reuse this stock everytime now when we want to create the 7 led things.
In the first lines we imported the GPIO-Interface and the time.
al = [21,20,16,12,7,8,25]

Then we created a list called "al" which will save integers. The numbers it contains are exactly the numbers of the GPIO-Numbering from our GPIO-Pins we are using.
Next we set up the Numbering-Mode and we turn off the warnings.
for i in range(len(al)):
    GPIO.setup(al[i],GPIO.OUT)

 Here we are making use of an so called "for-loop". It repeats the GPIO.setup instruction as often as the Length of our "al"-List is. We got 7 items in the list, so the loop will repeat 7 times.
Inside of the GPIO.setup is we also use our "al"-List. With square brackets we can call the item in the list on the position we say in the brackets. For example the first time, i begins with 0, so "al[i]" means at the first time "al[0]". This expression calls the first item of "al" which is 21.
In the further steps we set up every item from "al" to the GPIO.OUT-mode.
def alloff():
    for i in range(len(al)):
        GPIO.output(al[i],GPIO.LOW)

 Here we create a method with "def" (= definition) which is called "alloff". We use it to turn off all our leds. Inside of the method we find a for loop again, which sets all "al"-list-items to GPIO.LOW, which means they are turned off.

def printleft(nu):
        if nu == 0:
                GPIO.output(al[1],GPIO.HIGH)
                GPIO.output(al[0],GPIO.HIGH)
                GPIO.output(al[5],GPIO.HIGH)
                GPIO.output(al[6],GPIO.HIGH) 
                GPIO.output(al[4],GPIO.HIGH) 
                GPIO.output(al[2],GPIO.HIGH) 

        elif nu == 1:
                GPIO.output(al[2],GPIO.HIGH)
                GPIO.output(al[5],GPIO.HIGH)

Then we create our printleft method, which gets one parameter, a number. Inside of this we check if the number is wether 0,1 etc.

for i in range(10):
    printleft(i)
    time.sleep(1)
    alloff()

Finally we have an for-loop in the range of 10. so it will redo everything 10 times. Then we call printleft with the number of "i". Which will be 0,1,2,3,4, etc. After that we let the program "sleep" for one second, and then we turn alloff, so we can start the loop again.

Note: This is not the most effective way to get to a 7-segment-led, but this should only be seen as a guidance. 

Keine Kommentare :

Kommentar veröffentlichen