Raspberry Pi Part 2: Powered by Python

Today marks the 1 year anniversary of the Raspberry Pi. That’s right, the teeny device has been leading the creative movement of tiny computers for a year now. To celebrate Raspberry Pi’s first year of inspiring tech fiends and computer wizards to push the boundaries of computing creativity in new directions, I bring to you part 2 in my Raspberry Pi journey.

 

Brief

Last month I began my journey into exploring the possibilities of the Raspberry Pi, demonstrating the basic process of bringing it to life. This month I am back with another delve; attempting to combine my Raspberry Pi with Python to explore its GPIO capabilities. With a recent surge in pushing technology into education, opportunities to educate and explore the possibilities of programming are becoming more accessible and widespread (a notable project being Code.org) so I thought for part 2 of my Raspberry Pi journey I would delve into a bit of programming. The coding language I chose was Python, which is a language I don’t use on a day-to-day basis. This fitted in with the Pi vision, I was re-learning and re-acquainting myself with a programming language and combining it with my Pi for edifying purposes! Also, it was a step towards my fulfillment of the original aim of the Raspberry Pi trilogy; being able to use this tiny piece of computing magic to manipulate household objects and devices. To do this you really need to take advantage of the Raspberry Pi’s programming abilities and its GPIO capabilities. Feeling like a new age Thomas Edison, I thought I would shine a light on these capabilities by using Python to control nothing other than an LED light- a popular device of choice and a good place to begin before moving on to the more ambitious inclusion to feature next month.

 

Setting up the environment

Luckily, I got my version of Python at a time when the Raspberry Pi had already become a widely used and supportive world-wide community. So included in Python was all it’s libraries as well as a version of Raspbian; part of the Debian Wheezy operating system. The most important Python library for this project is the GPIO library. This library is vital as it allows the input and output pins on the Raspberry Pi’s GPIO header to be easily configured, read and written.

 

When writing Python, there are two specific pre-installed editors that can be used at your disposal; these being IDLE and IDLE3. The primary difference between these two versions is that IDLE and IDLE3 support Python 3.x; whereas IDLE supports all other versions of Python. So, if you are attempting this project yourself using pre-dated versions of Python then you should use IDLE. If your not using a pre-dated version of  IDLE then you should use IDLE3. In my case I used IDLE3. IDLE3 allows you to take advantage of how Python has been tidied up; apart from this difference the two systems in most senses are very much the same.

 

A piece of required equipment for this project is a Breadboard. Breadboards are readily available online or at local electronic stores (such as Maplin). They are often bundled up as part of electronic kits. For this kind of project solderless breadboards are usually the best to use. In my case this was definite as I’m going to be switching through projects frequently. Ideally before taking on a project like this you should already be aware of how the mechanics behind a breadboard operate as they are an important part of this project.

 

Further to the breadboard; also required are the correct GPIO jumper leads, female-male, resistor based on your LED, and of course the LED light. If the resistor is too low, your LED light will simply burn out, if too high, the LED will appear to be very dim, so it’s important you review your choice of resister using Ohm’s Law, for simplicity something between 270 and 330 Ohms will be fine.

 

Wiring up the Raspberry Pi

Wiring up the Raspberry Pi really isn’t difficult, but it’s vital that you follow a GPIO layout map to ensure you’re using the correct labels and pins. If you have  lots of connections to make, then a useful example is the Pi Cobbler from Adafruit. However, in the case of this project, there were only a couple of connections. So the Pi Cobbler would be superfluous. The GPIO layout map that I used is pictured below;

 

Here you’ll see that the physical pins are numbered and  labelled with the broad comm names. These are what the chip uses to activate and deactivate each pin. So within my Python code, I referenced pins by their labels. Something to note about the electronics involved is that LED are Light Emitting Diodes. The Diode part is important for this project, they only pass electricity one way so they need to need to be put in the right way round. So you need to know which is the right way and this can be done through taking a look at the pegs; the positive is the long peg and the shorter one the negative. The LED needs to be inserted into the breadboard in different rows; the negative growing into ground, which is pin 6 on the Raspberry Pi.

 

Code and execution

The Python script is very simple and simply defines pins and will activate and deactivate them based on timer delay. The code I used is as follows;

 

 

To run the code, you will need to open the file on “superuser” setting. Python is not set up as “superuser” therefore you will need to re-configure this. So, to do this you will need to open up a terminal and change the directory to the file and run the Python script as “superuser” using the sudo python; name_of_program.py. Once this is executed correctly, the LED will flash twice.

 

Result

In my case, this first step into the basics of powering external devices with the Raspberry Pi was a success. This leads me in the right direction towards my next move; implementing a button control system to accept input. If this is a success then it will be hooked up to the robotic arm to complete the finale of my experimental trilogy using the Raspberry Pi. So, stay tuned for the results of my next step on my Raspberry Pi journey. For those of you out there who gave this a go, please do share your results. Or better still, if you have any exciting Raspberry Pi projects please do share!

 

This is the basics of powering an external device with the Raspberry Pi. Going forward, to complete the series, I’ll be taking a look into using buttons to accept input and obviously, hooking this up to the robotic arm.

SHARE: