Raspberry Pi Lab

Tag: python

NOT Logic

by on Jan.07, 2014, under Code, Digital Logic, Electronics

not-symbol-transparentSo when building electronic circuits, there may be times when you want the circuit to behave opposite of the input it received.  For this we need a logical NOT gate.

A NOT gate is a simple concept.  Whatever the input, the output is the opposite.

To demonstrate this concept, I’ve created a circuit in which the LED is always on unless it receives a HIGH signal from the GPIO.  Here is the circuit I used…

not-gateThe LED gets power directly from the 5v rail.  R1 is the limiter that prevents the LED from being blown out by too much voltage.  The LED stays lit because the transistor at Q1 does not have an input signal.  Therefore Q1 is “open.”  When the GPIO is changed to HIGH, the transistor closes and allows the flow of electrocity to by-pass the LED straight to ground.

The R2 resistor could be a variety of sizes.  In fact, you could, probably get by without it at all.  The 2N222A transistor I used is rated for a typical 40v and 600 mA.

The Python code on the RPi is equally simple.  All we need to do is set the #23 pin to HIGH and the LED goes off.  If you want it to blink, add a pause and then set the #23 pin to low and pause again.  Repeat.

Here’s the code…

#!/usr/bin/env python

# The circuit that goes along with this program is
# wired in such a way that the LED is always on unless
# a HIGH signal is received from the Rpi.
 
import RPi.GPIO as GPIO
 
DEBUG = 1
 
GPIO.setmode(GPIO.BCM)
RED_LED = 23
GPIO.setup(RED_LED, GPIO.OUT)
 
if DEBUG:
   print "LED turned OFF"
while True:
   GPIO.output(RED_LED, True)

 

1 Comment :, , , more...

A Three LED Blink

by on Jan.03, 2014, under Code

My first RPi circuit.

My first RPi circuit.

My first actual circuit is a three LED blinking system.  It is a pretty simple circuit.  I have a ground pin coming off the GPIO to the blue rail on the bread board.  Then a red LED on pin 25, a green LED on pin 24, and a blue LED on pin 18.  Each LED has a 560 ohm resister.

The Python code then cycles through each LED making them blink in sequence.

It isn’t a complicated circuit or a complicated program.  However, it is an excellent starting point and a great “Hello World” program.

In my next post, I’ll use the same circuit to indicate when I have new messages waiting in my GMail account.  Should be fun.

Here is the code I used…

#!/usr/bin/env python
 
import RPi.GPIO as GPIO, time
 
DEBUG = 0
PAUSE = 1
 
GPIO.setmode(GPIO.BCM)
RED_LED = 25
GREEN_LED = 24
BLUE_LED = 18
GPIO.setup(RED_LED, GPIO.OUT)
GPIO.setup(GREEN_LED, GPIO.OUT)
GPIO.setup(BLUE_LED, GPIO.OUT)

while True:
        if DEBUG:
                print "LED ON."

        GPIO.output(RED_LED, True)
        GPIO.output(GREEN_LED, False)
        GPIO.output(BLUE_LED, False)

        time.sleep(PAUSE)

        if DEBUG:
    	print"LED OFF"

        GPIO.output(RED_LED, False)
        GPIO.output(GREEN_LED, True)
        GPIO.output(BLUE_LED, False)

        time.sleep(PAUSE)

        GPIO.output(RED_LED, False)
        GPIO.output(GREEN_LED, False)
        GPIO.output(BLUE_LED, True)

        time.sleep(PAUSE)

Leave a Comment :, , , , , , more...

RPi GPIO Setup Script

by on Jan.02, 2014, under Code, Installation

I figure I’ll be setting up and re-setting up RPi units for the forseeable future, so I converted my last post on installing the Python libraries for the GPIO into a Bash script.

Copy and paste the code to a text file in your home directory and make sure it is executable with ‘chmod +x filename’.  Then cd into your home directory and ‘./filename’ to run the script.

Here is the code…

#!/bin/bash

#####################################
# Setup the Raspberry Pi GPIO       #
# Script:  R00t  root@bjbranson.com #
# Version: 1 January 2014  V1.0     #
#                                   #
# Change Log: V1.0  Initial Script  #
#                                   #
#####################################

#########################
# CONFIGURATION SECTION #
#########################
mydir=$HOME/RPiDev

######################################
# Make sure the repository source is #
# up-to-date.                        #
######################################
echo '[I] Updating repository sources. Be Patient!'
sudo apt-get -qq update

#######################################
# Make sure the git utility is both   #
# installed and at the latest version #
#######################################
echo "[I] Verifying latest version of 'git' is installed."
sudo apt-get -y -q install git

#######################################
# Download the AdaFruit Rapberry Pi   #
# Python development libraries then   #
# list the directory to show that the #
# files were successfully downloaded  #
#######################################
if [ -d "$mydir" ]; then
   echo '[W] Specified download directory already exists.'
   echo '[I] Download of AdaFruit Python libraries skipped.'
else
   echo '[I] Downloading Python libraries from AdaFruit.'
   mkdir $mydir
   cd $mydir
   git clone http://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git
   cd $mydir/Adafruit-Raspberry-Pi-Python-Code
   ls
fi

#########################################
# Download and install Python libraries #
# for controlling the RPi's GPIO        #
#########################################
echo '[I] Installing Python Dev libraries for the RPi GPIO'
sudo apt-get -y -q install python-dev
sudo apt-get -y -q install python-rpi.gpio
sudo apt-get -y -q install python-pip


#################
## END OF FILE ##
#################

 

Leave a Comment :, , , , , , , , more...

Getting Python Scripts and Libraries

by on Dec.30, 2013, under Code

python-logo-transparentI’ve been doing the basic setup stuff, but I want to build stuff.  So…

First we need to make sure the program git is installed and is the latest version.  If you installed the Occidentalis stock image, git is already installed.  If you used the Raspbian stock image, git may not be pre-installed.  In either case we can, very easily make certain git is installed and is the latest version

sudo apt-get update
sudo apt-get install git

If it isn’t installed, it will be.  If it was installed but not the latest version, it will be replaced. If it was installed and already the latest version, nothing will happen.

Next, we need to obtain the boat load of Python scripts and libraries made available by AdaFruit.

git clone http://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git
cd Adafruit-Raspberry-Pi-Python-Code
ls

This will download a collection of scripts and libraries  that will be useful in attaching electronics to our RPi.

Then we need the Python library for actually controlling the GPIO on the RPi.  These commands will install the packages or update the existing packages (if needed).

sudo apt-get install python-dev
sudo apt-get install python-rpi.gpio

 

 

 

 

Leave a Comment :, , , , , more...

Archives

All entries, chronologically...

Our Partners

Please take a minute to visit our partners

Looking for something?

Search the Site

If you can't find what you are looking for, please leave a comment somewhere, subscribe to our feed and hopefully your question will be answered shortly, so please visit again!