Raspberry Pi Lab

Overclocking the RPi

by on Apr.13, 2014, under Installation

The RPi runs at a default speed of 700 mhz.  I don’t know about you, but that is a bit slow for my tastes.  While I understand that overclocking does generate additional heat and does reduce the life of the device, I am willing to take that risk to get a lttle more speed out of my RPi.  After all at a price of $35 USD, I can afford to replace one occasionally.

The overclocking can be done from the ‘sudo raspi-config’ utility.  However, the real settings are in the file “/boot/config.txt”.

Just for those who aren’t aware… the RPi does NOT have a BIOS.  Therefore the base paramaters that would normally be loaded from a BIOS are moved to the filesystem of the SD card on “/dev/mmcblk0p1”.  This file system is mounted in the OS as “/boot”.

First, make a copy of your “/boot/config.txt” file with “sudo cp /boot/config.txt config.txt.original”.  It’s always good practive to back-up a file before monkeying around with it.

I tried pushing my RPi to the 1 ghz max clocck speed, but it did not work.  The system immediately crashed.  So I resored my config.txt file, re-booted and tried again.  This time I configured it for 950mhz.  This setting has been working for about 48 hours without issue.

Here is the relevant portion of my /boot/config.txt file…

#uncomment to overclock the arm. 700 MHz is the default.
arm_freq=950

# for more options see http://elinux.org/RPi_config.txt
core_freq=250
sdram_freq=450
over_voltage=6

 

 

 

 

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

RPi Keeps Crashing

by on Apr.12, 2014, under Troubleshooting

I have been having issues with the RPi staying online.

My setup is a RPi Model B with a WiFi antanea attached and powered by a 1A wall wort.  There is no keyboard, mouse, or video attached.  All access to the RPi is done over SSH.

The RPi will stay online for aprox. one to three hours then disappear from the network.  Powering off for a hard re-boot would bring it back.  However, one to three hours and we’d have to repeat.  Not acceptable.  Eventually, the constant crashing did corrupt the image on the SD card.

After speaking with some geek friends and consulting Google, I believe the issue is the WiFi adpater.  I believe the WiFi adapter is drawing just enough off the USB to cause power related stability issues.

For now, I have disconnected the WiFi and attached the RPi directly to the network via RJ-45.  After flashing an SD card with a new Raspbian image, the RPi is up and has beed stable for the best part of 24 hours.

When I get ready to do some more testing, I’m going to attach the WiFi via an external, powered USB hub.  This should allow the WiFi adapter to draw sufficient power without having to depend on the RPi for its power source.

More to come…

 

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

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...

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...

Current Draw In My Three LED Circuit

by on Jan.04, 2014, under Electronics

ohms-lawI an previous post I described a Three LED circuit and Python code to drive the LEDs.  A reader posted, via Facebook, the following question…

“You decided to drive them directly through a resistor instead of going through a transistor. What is the current draw on the GPIO pins?”

Well, I am certainly not an electrical expert, but I think I can attempt to talk about the math behind this circuit.

As you recall,  I drove the LED but connecting a GPIO pin to a 560 Ohm resister.  The resistor was connected to the positive leg of the LED.  Lastly, the negative leg of the LED was connected to ground.

I used a 10mm Red LED purchased from Ada Fruit.  The datasheet for this LED can be found at on the Ada Fruit website.  In short, the forward voltage of the LED is a typical 2.0 v with a maximum of 2.4 v.  It draws 20mA.

Ohm’s Law reads the current (I) = Voltage (v) / Reistance (R).   I=V/R

Well, we already know that the LED wants to run at 2.0 V and draw 20mA.  We just need to know what size resistor to place on the LED so as not to blow up the LED.  A simple algebraic re-arrangement of Ohm’s law tells us that R=V/I

The GPIO pins on the RPi all output 3.3v.  Since the GPIO is giving us 3.3 V when we only need 2.0 V, we will have to remove (resist) 1.3V.  Now we can calculate the minimum resistor size.

R = (3.3 V = 2.0 V) / 0.02A = 65 ohms.

That means the minimum size resistor for this circuit using this LED is 65 Ohms.  Using a larger resistor just means the LED won’t shine as brightly as it would with the minimum sized resistor.

So why did I decide to use the 560 ohm resistor?  Simple… That is what I had available in my toolbox.

I think I can now answer the reader’s actual question of how much current was actually drawn from the GPIO pin.

Because I used a 560 ohm resistor and I know the voltage provided by the GPIO and the voltage used by the LED, I can calculate the current draw via Ohm’s Law.

I = V/R

The LED needs 2v of the 3.3 v provided on the GPIO and we know the resister is 560 ohms.

I = (3.3 V – 2.0 V) / 560 ohms = 0.0023 A = 0.23mA

The spec sheet says we can draw up to 16mA from a GPIO (as long as we don’t do that much for more than one pin at a time).  So our 0.23mA is well within spec..

 

 

 

 

 

 

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)
   newmails=int(feed["feed"]["fullcount"])
   return newmails   

GPIO.setmode(GPIO.BCM)
GREEN_LED = 24
RED_LED = 25
BLUE_LED = 18                   # Blue LED not used, but is wired
GPIO.setup(GREEN_LED, GPIO.OUT)
GPIO.setup(RED_LED, GPIO.OUT)
GPIO.setup(BLUE_LED, GPIO.OUT)

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" 
if DEBUG:
   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))
   else:
      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...

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...

Create A Baseline Image

by on Dec.30, 2013, under Installation

sd-cardNow that you have installed the stock image, configured the video, set-up the WiFi, and updated the system, it it time to do a baseline image of your new, working system.

By making your own image, you will be able to re-flash your SD card back to this current state at some point in the future.  Like, for example, if your file systems gets corrupted.

To do this you need to…

  • Properly shutdown your Rpi.  Run ‘sudo shutdown now -h‘.  The ‘h’ means halt.
  • Remove the SD card from the RPi and mount it on your Linux PC.
  • Determine the device id of the SD card.  ‘sudo fdisk -l‘.   NOT the mounted partition.  For example.  My device is /dev/mmcbkl0 but mounted partitions are /dev/mmcblk0p1 and /dev/mmcblk0p2.  You want ONLY the device.
  • Now we’ll make an image of the card… sudo dd bs=1M if=/dev/mmcblk0 of=/home/myhomedir/imagename.img
  • sudo sync
  • Remove the card, replace it in the RPi and boot up.

NOTE:  This will take quite some time to complete.  The dd command displays absolutely no indication that it is working and may appear frozen.  Be patient.

NOTE II:  The stock Occidentalis image is only 2.6Gb.  But your image, if you followed all the steps I have outlined in my previous articles, will be the full size of your SD card.  In my case, that is 8Gb.

 

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!