Vous êtes sur la page 1sur 10

Getting started with GPIO on Raspberry Pi

Before we go further, its assumed that you either have a SSH connection with Raspberry Pi or better still, a full blown setup (Raspi + Display + Keyboard). If not, please read my earlier post on setting up a direct SSH connection to the Raspberry Pi. The below post is for intermediate users having a fair understanding of Electronics (and aware of the GPIO pins their logic levels). Well not explain the GPIO pins, their functions or their precautions here. A simple Google search should suffice. Well directly jump into getting our LED being controlled by this mini-monster

There are many ways through which you can interact with the GPIO pins. You can use Assembly, C, Java, Python, Ruby etc, provided their appropriate libraries and interpreters or runtimes are installed. In this note below, well use direct pin write using file access system and then use Python. Note: Please be aware of the precautions to take while working on GPIOS. Accidental short circuiting of the 5V logic pins with other pins can permanently damage the Raspberry Pi. The author takes no responsibility for any damage or loss to property or person incurred directly or indirectly.

Wiring it up:
Connect Pin 25 (GND) to the negative terminal of LED Connect Pin 23 (GPIO11) to the positive terminal of the LED through a small resistor (220-330 ) as shown below:

Now, open the shell terminal and execute the following commands. In this method, well directly create a file-type access to the GPIO pin and mod it accordingly:

Now, we can write to the export file in the /sys/class/gpio subdirectory, the system will create a file with appropriate GPIO structure. In this case, well directly write to GPIO 11 to control a LED connected to it. Create a GPIO file access

Configure the pin direction:

Write value 1 to turn on the LED:

Write 0 to switch it off:

After done testing, please free up the pin by running the unexport command

In short, run these commands to turn on/off a LED on GPIO 11:

Using Pythons RPi library


Lets now connect the Anode (+ve terminal) of the LED to Pin 11 (GPIO17) of the board. After that, we need to run the below commands to have our LED lighted

Ive included the mistakes done Lets close this post by writing a simple python script thatll blink our LED for a given number of times

There is an error in the above script. Please identify it.

The above script will blink our LED N number of times where N is entered by the User

Using the Wiring Pi Library


This is another cool library that allows us to use

Vous aimerez peut-être aussi