Stephen Smith's Blog

Musings on Machine Learning…

Breadboarding with my Raspberry Pi

with 4 comments


Introduction

One of the cool things about the Raspberry Pi is that it has a set of general purpose input/output (GPIO) pins. Many Raspberry Pi starter kits (including my Canakit) come with a breadboard and few simple electronic components you can play with. In this article I’ll talk about connecting up some LED lights and controlling them from both Python and Scratch. Below is a bit of a hazy picture of my Raspberry Pi hooked up to the breadboard and a Scratch program running.

Here is a closer look at the breadboard with a few LEDs and resistors connected.

You don’t need to do this for many standard tasks, after all the Pi has four USB ports, Wifi, Bluetooth, HDMI, sound/composite video and ethernet ports. But the GPIO port is great for electronic enthusiasts, hobbyists and educators to get their hands dirty playing with electronic components.

Hooking up an LED

Each GPIO pin can be individually controlled and will provide 3.3V when activated. It is then specified to keep the current under 16mA or you can damage the circuits. My kit came with a number of 220 Ohm resistors and by Ohm’s law these would case the current to be 3.3V/220Ω = 15mA, so just right. You need to have a resistor in series with the LED since the LED’s resistance is quite low (typically around 13 Ohms and variable). I connected 3 LEDs and for each LED you connect a wire from a GPIO pin (in this case I used 17, 27 and 22) to the positive lead of the LED then you connect the negative side to a resistor and the other side of the resistor to the -3.3V line on the breadboard. Really quite simple.

Python

It’s quite simple to control the GPIO pins via a Python package. You just need to import RPi.GPIO and you can get going. This package came pre-installed so all I had to do was write some lines of code and away it went. Basically I just need to set the mode for since the package supports a few different boards and chipsets, then configure the pins I’m using for output. Then I just need to turn the LEDs on and off. You need to add some sleep statements or the whole thing executes faster than you can see.

Scratch

Scratch is a very simple and visual programming language/environment developed by the MIT Education Department. It is used to teach programming to students as young as in kindergarten. It is really amazing the animations and games that kids can produce with this system. It comes pre-installed on the Raspberry Pi and you can also control the GPIO pins with it, just like you can in Python. You have to run the GPIO server from the edit menu and then you use the broadcast statement to control the GPIO functions. Here is the Scratch version of the simple Python program displayed above.

More on the GPIO

The GPIO has 26 pins, two are +3.3V, two are +5V, 5 are ground and then that leaves 17 as general GPIO pins.

In the same way we configured the pins for output to control LEDs you can configure them for input and then for instance read the setting of a switch.

However this isn’t all there is to GPIO, besides the functions we’ve talked about so far, which are rather limited, a number of the pins have “alternate” functions that you can select programatically. For instance pins 3 and 5 can support the I2C standard that allows two microchips to talk to eachother. There are pins that can support two serial ports which are handy for connecting to radios or printers. There are pins that can support PWM and PPM which are handy for controlling electical motors.

Summary

The Raspberry Pi 3 is a very versatile device. It runs a most Linux software and has a very flexible architecture allowing it to interface to a great many devices. It has four USB ports, Wifi, Internet and Bluetooth. Plus there is the general purpose GPIO bus that allows a great deal of flexibility to interface the Pi to almost anything. That is why you see Raspberry Pi’s built as the brains of drones, robots, home security systems, information kiosks and so much more.

 

Advertisements

Written by smist08

November 15, 2017 at 9:14 pm

Posted in raspberry pi

Tagged with , ,

4 Responses

Subscribe to comments with RSS.

  1. […] I would port my simple flashing light program that I showed in Python and Scratch in my previous blog posting on breadboarding over to Fortran. So Python and Scratch come with GPIO libraries pre-installed and was really easy. […]

  2. […] this article we’ll re-implement in C our flashing LED program that I introduced here. We’ve now run the same program in Python, Scratch, Fortran and C. The Raspberry Pi is a great […]

  3. […] introductory material has gotten fairly long, so we won’t get to an assembler version of our flashing LED program. But perhaps in a future […]

  4. […] example, so I wanted to produce an Assembler  language version of the little program I did in Python, Scratch, Fortran and C to flash three LEDs attached to the Raspberry Pi’s GPIO port on a breadboard. So […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: