Behavior, Content, Money – 3 Things you should never give away for free!!!

BCmoney MobileTV

Raspberry PI 2 – Python controlled circuits Button & LED experiments

Posted by bcmoney on August 31, 2016 in IoT, Python with No Comments

No Gravatar
이진 카운터가 구성된 큰 빵판

RaspberryPI digital clock/counter (Photo credit: Wikipedia)

So the last set of experiments definitely piqued my interest in the Raspberry PI platform and its capabilities to measure, control and otherwise interact with the physical world. In reality though, all I’ve done is play with some cheap hobbyist parts that could be picked up at any electronics shop. Still circling around the true capabilities of the PI, I realize I now need to connect the hardware capabilities to the software for creating a proper controller or event-driven system. Afterall, the hardware is where the “Things” aspect of the “Internet of Things” comes in to play and the software is what potentially reaches out to the “Internet” to connect multiple distributed devices or call out to cloud services for message relay between systems, analytics or additional intelligence.

As with the last article I sifted through the web resources out there to try to find the most relevan ones. The following videos show you how to extend the basic Breadboard/Circuit knowledge with the use of logic gates to do “code-on-chip” types of behaviors where your board/circuit can add a bit more intelligence to its basic input/output capabilities, how to safely extend the basic components with real-world sensors, and how to connect directly to the Raspberry PI as a power source via GPIO ribbon cable to control physical electronics experiments with code:

As I advised in the last post, do yourself a favor and watch those through to the end! It’s less than an hour to sit through and you’ll learn some excellent further insight on all the main electronic components, how they’re used, and how to connect them to the PI safely. You should also checkout the following useful links:


Connecting the Breadboard to the Raspberry PI

In order to connect the breadboard to your Raspberry PI, you’ll need to place the “T-board” (this is the term I’ll used to summarize/abbreviate the formal components named “40-Pin T-Shaped (Assembled) Breakout Board and Ribbon Cable” from the CanaKit) on the Breadboard as per the following:

CanaKit Breakout Board attachment to Breadboard

For safety (helps prevent static shocks, electricity jumping from GPIO pins to board, etc, you’ll then need to plug in the ribbon cable and run it from the PI into the top of the “T-board” now atop the Breadboard:


Experiment#1 – Reading button input from the Breadboard on the PI

So sticking with the easy and the obvious just to get my head around this last piece of the puzzle, I got the input button connected to the LED feeding clicks into the PI and it is receiving these outputing a message on the command-line. Here’s the simple Python code to read input from the GPIO in a loop and report on it as it comes in:

import RPi.GPIO as GPIO		
import time

#setup the input PINs

#initialise a previous input variable to 0 (requires assuming button not pressed at onset of script, implying light should be off... a true light switch should enforce this)
prev_input = 0
while True:
  #take a reading
  input = GPIO.input(17)
  #if the last reading was low and this one high, print
  if ((not prev_input) and input):
    print("Switch flipped state!")
  #update previous input
  prev_input = input
  #slight pause to "debounce"


I can think of some interesting applications of this already, since essentially you could think of this example as a code-connected IoT button, something akin to Amazon’s “DASH order button”, or if you like a “TV Remote Control”, or “Video Game Controller” button for triggering IoT related events. This reminds of some interesting “Staples Easy Button” hacks which made the tech new headlines last year but really just gives you a larger version of this exact same type of “easy button”.


Experiment#2 – Sending output to the Breadboard from the PI

Of course the next experiment was going the other way, sending an event to trigger an action from Python code running on the Raspberry PI over to electronic components on the Breadboard via the “T”. Here’s the code sample for making the LEDs blink 15 times, stay lit up solid colors for 5 seconds, then turn back off:

#import necessary libs
import RPi.GPIO as GPIO
import time

#constant PIN number we'll be working with
PIN = 4

#initialize the GPIO

#define a function to turn the light on then off
def blinkOnce(pin):
#use blinkOnce on PIN4 in a loop, 15 times, then stop and begin a "shutdown sequence" of solid light
for i in range(0,15)

#turn light on

#sleep for 5 seconds
print "Starting shutdown cycle: %s" % time.ctime()
print "Ending shutdown cycle: %s" % time.ctime()

#turn light off

#cleanup and close this script's GPIO connection


With this basic example I can definitely imagine expanding out to create something like a timed alarm system that turns on in your home, lights that turn off on a delay after a certain amount of time, a garage door that automatically closes when you walk in, or say, a motion sensor that triggers a call into the PI which then automatically activates a home entertainment center.


GPIO SDKs/Libraries

These can also be recreated using GPIO libraries from any number of other programming languages. Here are the main GPIO libraries I know of for all the platforms that come istalled on the PI:

For any other specific programming languages or some basic code examples, check out the official code samples/guides.



Finally, the true potential of the PI seems within reach, and some of the crazy ideas I’ve been dreaming up for experiments will be on my personal hobby project roadmap as soon as I can fit them in! While I’ve definitely got a ton of experiments I’d like to try in the future, I have found this stuff to be a bit time-consuming and I anticipate I’ll be having less and less time due to increasing demands at work and home. Hopefully, I’ll find a bit of time to play around with this stuff even more, but I’m happy with the basics I’ve been able to pick up thus far. In the very least, with my company’s new Innovation initiatives starting to look into a self-service retail checkout experience based on Arduino and/or RaspberryPi, maybe I’ll have a chance to combine hobby and work a bit!?