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. 

Montag, 7. Dezember 2015

Introduction to Raspberry pi - gpio & led

Intro

Things you need:
.) raspberry pi
.) one led (no matter which color you got)
.) resistor (about 330 Ohm)
.) breadboard
.) gpio jumper wire
.) Basic Knowledge of how to use the unix-shell-commands

This post is dedicated to show you the basics of working with the gpio-pins from your raspberry pi. You don't have to have any knowledge about this topic, i will explain every single step.
Wether which raspberry model you've got, you will se more or less gpio pins. To give you an overview of the numeration of the pins, look at this site (depending on which model you got you have to adapt the search-term)

This table shows the pin number and the related gpio-number/function. For Example pin 12 is the 18th gpio pin, pin 6 is a 0V-pin (ground).

1. Setting up the Circuit

Before we can have any fun with lighting up leds, we have to prepare the circuit. We're now going to use pin 12 ( = GPIO 18) and pin 6 (ground) from our raspberry. Connect a jumper wire on both pins and plug it into your breadboard. Breadboards have a wire-system as shown on this picture. The upper and lower horizontal lines are connected, and the inner vertical green lines. Next on the same line as your GPIO-18-wire you put a resistor in (for not destroying your led with too high voltage). After the resistor we put in the led. LEDs have a cathode and an anode, the anode will be on the side of the resistor, the cathode will go to the ground-wire (led-anode-cathode). If finished we should have a circuit similar to this:

2. controlling the led

When step 1 is finished we can begin to light our led by writing a simple program. For this we use the python programming language, which can handle gpio functions. You can use your favourite text-editor or the python ide, simply write the following lines in a new file called led.py (the '.py' stands for executable python program). Don't worry if you don't understand what this means, I'll explain soon.

command lines for led.py file:

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18,GPIO.OUT)
GPIO.output(18,GPIO.HIGH)
time.sleep(5)
GPIO.output(18,GPIO.LOW)

Save and close your file, get into your shell, make sure you are in the same directory as your led.py file is, and type python led.py. This will execute your python-file and light up your led for five seconds.
But what did just happen?
First of all we imported the GPIO-Interface and the time. We need to do this to be able to work with the pins and telling the program to "sleep" for some seconds. Next we set up the nunbering mode to GPIO.BCM = the GPIO numbers, e.g. GPIO18 (=pin12, but we do now call it 18). Then we tell the program to put no warnings out.
Now it's getting interesting: We put the GPIO18 as output and set it to HIGH, which means the circuit gets electricity. To see what happens we let it sleep 5 seconds till we put the output to LOW and the program ends.

You can do this with any pin and led you want. Also you aren't limited by the amount of leds or pins you use to control them. Stay tuned for further raspberry posts!