Getting Started With GPIO on Raspberry Pi

11
Getting started with GPIO on Raspberry Pi Before we go further, it’s 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). We’ll not explain the GPIO pins, their functions or their precautions here. A simple Google search should suffice. We’ll 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, we’ll 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:

Transcript of Getting Started With GPIO on Raspberry Pi

Page 1: Getting Started With GPIO on Raspberry Pi

Getting started with GPIO on Raspberry Pi

Before we go further, it’s 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). We’ll not explain the GPIO pins, their functions or their precautions here. A simple Google search should suffice. We’ll 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, we’ll 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:

Page 2: Getting Started With GPIO on Raspberry Pi

Now, open the shell terminal and execute the following commands. In this method, we’ll 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, we’ll 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:

Page 3: Getting Started With GPIO on Raspberry Pi

Write 0 to switch it off:

Page 4: Getting Started With GPIO on Raspberry Pi

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 Python’s 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

Page 5: Getting Started With GPIO on Raspberry Pi

I’ve included the mistakes done

Let’s close this post by writing a simple python script that’ll blink our LED for a given number of times

Page 6: Getting Started With GPIO on Raspberry Pi

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

Page 7: Getting Started With GPIO on Raspberry Pi

Using the Wiring Pi Library

This is another cool library that allows us to use

Page 8: Getting Started With GPIO on Raspberry Pi
Page 9: Getting Started With GPIO on Raspberry Pi
Page 10: Getting Started With GPIO on Raspberry Pi