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!

Keine Kommentare :

Kommentar veröffentlichen