Getting started with some brief GPIO stuff with the Raspberry Pi 2

Today I wanted to get into using the GPIO on my raspberry pi 2, so I found a nice little intro to it which used the Raspberry Pi GPIO Breakout Kit from CanaKit that I had.  Link to the tutorial.

For now, all I would be doing is using the Raspberry Pi and its GPIO connected to a breadboard to make an LED blink repeatedly.  I did this following the tutorial linked and the code used is from the tutorial as well.

I started by checking for updates and upgrades using "sudo apt-get update" and then "sudo apt-get upgrade".  Then I ran "sudo apt-get install rpi.gpio" to install whatever necessary files to use the GPIO.  Next, I went into the config to enable the I2C (Inter-integrated-circuit) and the SPI (serial to parallel interface), then rebooted the Pi.

Here's where I ran into some issues:  I went to go and run the command "lsmod | grep i2c", except the keyboard wouldn't type the pipe.  So after some searching, I found out that this was an issue with the locale of the Pi and keyboard settings associated with that.  After even more searching, I found out that I could change the content of /etc/default/keyboard to fix this issue.  So I ran "sudo vi /etc/default/keyboard" and in the file I changed the letters "gb" to "us", rebooted the pi with "sudo reboot" and then the issue was fixed.

However, in fixing that issue, another one came up, where some terminal generated messages would be in Spanish.  I had no idea why and couldn't find any reason on the internet for it.  Regardless, I pressed on with what I was intending to do.

Then I set up the breadboard connected to the GPIO with some wires, an LED, and a resistor as shown in the following picture.

Next I had to make the program, so I ran the command "sudo nano led_blink.py" to create a python file and open it to edit.  Then I entered the following code into the file

#import the GPIO and time package
import RPi.GPIO as GPIO
import time #used to be able to specify a time in seconds
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.OUT)
# loop through the code 50 times, on/off for 1 second
for i in range(50) :
     GPIO.output(7, True)     # send high signal on pin 7
     time.sleep(1)                   # wait for 1 second
     GPIO.output(7, False)    # send low signal on pin 7
     time.sleep(1)                   # wait for 1 second
GPIO.cleanup()

Then I saved the file, and ran the program using "sudo python led_blink.py" which worked in making the LED blink.


via GIPHY

This was a nice little thing to do despite most of the work being troubleshooting keyboard and region issues.  Next I'm likely going to do some research on how the python library for the Pi/GPIO works so that I can do something that's potentially more in depth than this.

Comments