Raspberry Pi Lab


Three Blink LED – Part Two

by on Jan.05, 2014, under Electronics, Projects

3LED-transparentA couple of days ago I created a circuit for a three led blinking circuit.  The circuit description and the Python code can be found in my previous post.

In that example, I drove the LEDs directly off the GPIO pin with a 560 ohm resister.  That worked well because the GPIO pin can provide 3.3 v at up to 16mA.  However, I reader suggested that driving the LEDs directly off the 5v power rail and switching them on and off with a transistor on the GPIO pin.

The readers was absolutely right.  The transistor is basically a digital switch.  It has three pins called the collector, emitter, and base.  The collector and emitter pins complete the circuit.  The base pin is the switch.  By connecting the base pin to the RPi’s GPIO and setting it to High (1), the circuit is complete and the LED lights up.  Setting the GPIO to low (0) opens the circuit and turns the LED off.

This allows the LED to get its power from some other source than the GPIO pin itself.  The RPI has a 5v pin that is capable of 300 mA.  This additional voltage and current would allow us to use more LEDs per GPIO pin or control other devices that require more than the GPIO is capable of providing..

In my new circuit I took the 5v pin of the RPi to a 560 ohm resistor.  The resistor connected to the positive leg of the LED which then connected its negative leg to the collector pin of an N2222 transistor.  The emitter leg of the transistor then connected to ground.  The base pin of the transistor connected to a GPIO pin.  I set op three of these LED circutis.  Red on GPIO 25.  Green on GPIO 23.  Blue on GPIO 18.

three led isolatedIt really didn’t matter which GPIO pins I used.

I chose these particular pins because those are the same pins I used in my previous Three LED Blink circuit.  Therefore the Python code did not have to change at all for this to work.  The GMail Notify example I posted will also work with this re-designed circuit.

As a side note… The 560 ohm resister is larger than the minimum resister needed for this circuit.  According to Ohm’s Law (I = V/R), the minimum resister value would be 150 ohms.  That is R = (5v – 2v) / 20mA = 150 ohms.  5v being the voltage supplied by the power rail and 2v being the requirement of my particular LED.  The difference, 3v, needts to be resisted away so as not to blow up the LED.  By using the 560 ohm resistor instead of the 150 ohm minimum, my LED will shine sligntly less bright, but I’m ok with that.




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

GMail Notify

by on Jan.03, 2014, under Code, Projects

gmail-plus-raspberryThe idea of today’s project is to make an Gmail Notify program for the RPi.  I started with the idea for this program from the AdaFruit tutorial site, but I have made some significant changes to their code to make it do more of what I thought would be useful.

I used the same circuit I built for the Three LED Blink program I built in my previous post.  That circuit used a blue LED.  This program doesn’t use the blue LED, but since it is on the circuit, I went ahead and set up the pin on the GPIO and insured that it was, in fact, turned off.

Before building this project. I added these three lines to my python setup.

sudo pip install feedparser
sudo easy_install -U distribute
sudo pip install RPi.GPIO

The ‘feedparser’ line is specific to the needs for this program.  However, I believe the other two lines should be added to my RPi GPIO setup script.  I have NOT added them to the script – yet.  So I executed them before starting this project.

The idea of this program is simple.  When the program starts assume that I already know I have messages that are unread and just go ahead and set the alert status to “all clear.”  The fact is, I have – quite letterally – thousands of unread messages.  So if I were to set the “alert condition” If I have any unread messages would be pretty counter productive.

The “all clear” condition sets the green LED to a solid on state.  The “alert status” condition sets the red LED to a blinking state.  This lighting scheme is based on alert vs all clear.  If a stop light makes more sense to you, then you might want to reverse the colors.  Make it what works for you.

With that said, here is the code…

#!/usr/bin/env python

# Script:   r00t   root@bjbranson.com
# v1.0      2 January 2014

# GMail Atom RSS Feed: http://email.about.com/od/gmailtips/qt/et081005.htm
# Feedparser Docs: http://pythonhosted.org/feedparser/

# GMail and Google Apps use the same URL for the RSS Feed:
# Use your full email address as the username to distinguish
# that you are using Google Apps.

# I have thousands of unread messages so I want the program
# to start with the assumption of "no new messages" then alert
# me when additional new mail arrives.

import RPi.GPIO as GPIO
import feedparser
import time
DEBUG = 0                 # turn debug mode on or off
USER = "youruserid"       # if gmail just userid before the @; if apps use full email address
PASS = "yourpassword"
BASE_OFFSET = 0           # How many more than inital count will trigger alert
MAIL_CHECK_FREQ = 10      # check mail every X seconds
def get_new_messages ():
   feed = feedparser.parse(imapurl)
   return newmails   

RED_LED = 25
BLUE_LED = 18                   # Blue LED not used, but is wired

GPIO.output(RED_LED, False)     # Used as alert
GPIO.output(BLUE_LED, False)    # Not used but make sure it is turned off 
GPIO.output(GREEN_LED, False)   # Used to indicate "no attention needed"

imapurl = "https://"+USER+":"+PASS+"@mail.google.com/gmail/feed/atom" 
   print imapurl

initial_count = get_new_messages()
offset = BASE_OFFSET + initial_count

while True:

   newmails = get_new_messages()
   if DEBUG:
      print "You have", newmails, "new emails!"
   if newmails > offset:
      GPIO.output(GREEN_LED, False)  # Set alert condition.
      GPIO.output(RED_LED, True)     # Red LED on and Green LED off.
      blinks = MAIL_CHECK_FREQ
      while (blinks > 0):
         blinks = blinks - 1
         GPIO.output(RED_LED, True)  # Program is intended to check messages every
         time.sleep(0.5)             # MAIL_CHECK_FREQ seconds. Therefore, the blink
         GPIO.output(RED_LED, False) # rate can not take more than one second per cycle.
         time.sleep(0.5)             # So be on 1/2 second and off 1/2 second.
         if DEBUG:
            print ("Blinks Remaining is %s." % (BLINKS))
      GPIO.output(GREEN_LED, True)   # NO alert
      GPIO.output(RED_LED, False)    # Green on; Red off
      time.sleep(MAIL_CHECK_FREQ)    # Since we are NOT blinking we can just sleep


Leave a Comment :, , , more...


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!