Get tutorials Raspberry Pi Super Kit V2.0 for RaspberryPi

Raspberry Pi Pin Number Introduction

Currently, there are three methods of pin numbering for Raspberry Pi.

  1. Numbering based on the physical location of pins
  2. Numbering appointed by C language GPIO library wiringPi
  3. Numbering appointed by BCM2835 SOC

If we want to operate Raspberry Pi GPIOs in C language based on the wiringPi library, choose numbering appointed by wiringPi. You can see from the above diagram that GPIO0 in wiringPi corresponds to pin 11 numbered by physical location, and GPIO30, to pin 27.

The picture below demonstrates the physical numbers of the three pins 11, 12, and 13 in detail:

 

If you are a C user, please install wiringPi.


WiringPi introduction:

WiringPi is a GPIO library function applied to the Raspberry Pi platform. It complies with GUN Lv3. The functions in wiringPi are similar to those in the wiring system of Arduino. They enable the users familiar with Arduino to use wiringPi more easily.


How to install

Step 1: Get the source code of wiringPi

   git clone git://git.drogon.net/wiringPi


Step 2: Install wiringPi

   cd wiringPi

   git pull origin

   ./build


After you press Enter, with the script build, the source code of wiringPi will be compiled automatically and installed to the appropriate directory of Raspberry Pi OS.


Step 3: Test whether wiringPi is installed successfully or not

WiringPi includes lots of GPIO commands which enable you to control all kinds of interfaces on Raspberry Pi. You can test whether the wiringPi library is installed successfully or not by the following instructions.

gpio –v

 

If the message above appears, the wiringPi is installed successfully.

gpio readall

 

If you are a Python user, you can program GPIOs with API provided by RPi.GPIO.


RPi.GPIO Introduction:

RPi.GPIO is a module to control Raspberry Pi GPIO channels. This package provides a class to control the GPIO on a Raspberry Pi. For examples and documents, visit http://sourceforge.net/p/raspberry-gpio-python/wiki/Home/

Raspbian OS image of Raspberry Pi installs RPi.GPIO by default, so you can use it directly.


Next, use C and Python language to program. You will learn how to use wiringPi and RPi.GPIO module to control Raspberry Pi GPIOs. Take blinking LED for example. First, connect circuit according to the following diagram:

 

Note: The resistor here is 220Ω. If the resistance is too high, the LED will be too dim or even won’t light up. .


For C language users (based on the wiringPi library):

Step 1: Create a C file named led.c

      touch led.c


Step 2: Use nano or other code edit tools to open led.c, write down the following code and save it.

#include <wiringPi.h>

#include <stdio.h>

 

#define  LedPin    0

 

int main(void)

{

if(wiringPiSetup() == -1){//when the wiringPi initialization fails, print message to the screen.

        printf("setup wiringPi failed !");

        return 1;

    }

   

    pinMode(LedPin, OUTPUT);

 

    while(1){

        digitalWrite(LedPin, LOW);   //led on

        printf("led on...\n");

        delay(500);

        digitalWrite(LedPin, HIGH);  //led off

        printf("...led off\n");

        delay(500);

    }

    return 0;

}

 

Step 3: Compile

      gcc led.c –o led –lwiringPi


Step 4: Run

   sudo ./led


You should see the LED blinking. Press Ctrl+C to terminate the program.


For Python users (based on the RPi.GPIO python module):

Step 1: Create a Python file named led.py

      touch led.py


Step 2: Use nano or other code edit tools to open led.py, write down the following code and save it.

#!/usr/bin/env python

import RPi.GPIO as GPIO

import time

 

LedPin = 11    # pin11

 

GPIO.setmode(GPIO.BOARD)       # Numbers GPIOs by physical location

GPIO.setup(LedPin, GPIO.OUT)   # Set LedPin's mode as output

GPIO.output(LedPin, GPIO.HIGH) # Set LedPin as high(+3.3V) to turn off the led

 

try:

    while True:

        print '...led on'

        GPIO.output(LedPin, GPIO.LOW)  # led on

        time.sleep(0.5)

        print 'led off...'

        GPIO.output(LedPin, GPIO.HIGH) # led off

        time.sleep(0.5)

except KeyboardInterrupt:  # When 'Ctrl+C' is pressed, the following code

                             will be  executed.

    GPIO.output(LedPin, GPIO.HIGH)     # led off

    GPIO.cleanup()                     # Release resource

 

Step 3: Run

    sudo python led.py


You should see the LED blinking. Press Ctrl+C to terminate the program.


Copyright © 2012 - 2016 SunFounder. All Rights Reserved.


Previous chapter: Notice

Next chapter: Get Source Code

SunFounder
Feb 02 2016 at 04:01 am



© Developed by CommerceLab