Raspberry Pi Kit Manual1
Raspberry Pi Kit Manual1
com
Overview
In the first lesson of this series, we showed you how to prepare an SD card containing
an operating system for your Raspberry Pi. In this lesson, we will show you how to setup
your Raspberry Pi the first time you boot it up.
We do this using a tool called Raspi-Config that runs automatically the first time you
boot your Raspberry Pi. This starts before the windowing system and so you have to use
the cursor keys and Return key to navigate the menu system.
It is a bit like adjusting the BIOS settings on a PC, once you have things right, you
probably won't need it again. We will start with the options that are most important and
then look at some of the other options that you may wish to configure.
This may seem a bit strange, but by default the Raspberry Pi only uses as much of the
SD card as the operating system requires. This means that even though you might have
used a large SD card, the operating system won't use it.
www.b2cqshop.com
To fix this so that all the space on the SD card can be used, use up / down cursor keys
to select the 'expand_rootfs' menu option and hit return.
Once you do that, there will be some screen flashing as a script is run and then you will
see the following confirmation.
Being designed to work with TVs, you may find that your Pi is only using the middle
portion of the screen and there is a big unused area all round the screen.
This is not true of all TVs and monitors, but if its happening for you then selecting the
option to Disable Overscan may fix this for you.
Use the left and right cursor keys to make your selection and then hit Return.
If after disabling overscan, you cannot see the left edge of the screen then see the
section 'Running Raspi_Config After Booting'.
www.b2cqshop.com
Changing Timezone
Skipping past a few options for a moment, the next thing that you almost certainly
need to do is (unless you live in the GMT timezone) is to change the timezone.
From the options, first select the Geographic Area, then the Timezone within that area.
www.b2cqshop.com
By default, when the Raspberry Pi boots, you just get a command line. No windows
just a terminal where you can login and type commands.
The final configuration that you definitely want to make, unless you don't like
windowing environments is to change the boot behaviour so that it automatically starts the
windowing system and logs you in.
www.b2cqshop.com
Select the sensible option and hit Return.
Other Options
The options described above are those which could pretty much be considered
essential when running your Raspberry Pi for the first time. There are some other options
listed that are worth describing briefly.
change_pass – allows you to change the system password for the user 'pi' the
default user on the system. By default, this password is 'raspberry' so those
preoccupied with security may wish to change the password.
change_locale – For non-English speakers, you can select which locales should
be available on the system and which should be the default for the operating system.
memory_split – allows you to ajust how much of the shared system memory is
available for graphics and how much for the main processor. If you plan to run
graphics hungry games, or video playback, then you may decide to alter these
settings.
ssh – in a later tutorial we will look at remote controlling your Pi from another
computer using ssh. This option allows you to enable ssh so that you can do that.
www.b2cqshop.com
update – this option tries to find a newer version of Raspi_Config and download it.
It is possible that new options will be added to the system in the future, so you may
wsh to do this.
You can run raspi-config any time you like, if you find that there are some settings
that you need to make.
Click on the desktop icon 'LXTerminal' to open a terminal session.
1. sudo raspi-config
www.b2cqshop.com
Reboot your Pi – click the icon bottom right – and this time you should boot straight up
into the windowing environment.
www.b2cqshop.com
Overview
In this lesson you will learn how to remote control your Raspberry Pi over your local
network using Secure Shell (SSH).
A common reason for remote controlling your Pi from another computer is that you
may be using your Pi solely to control some electronics and therefore not need a keyboard,
mouse and monitor, other than for setting it up.
It also can just save on desktop clutter, and the problem of having multiple keyboards
and mice all over the place.
Enabling SSH
Secure Shell (SSH) is a feature of Linux that allows you to effectively open a terminal session
on your Raspberry Pi from the command line of your host computer.
To use SSH, you need to first enable your Pi for using it. The easiest way to do this is to use
Raspi Config, which you first saw back in “ Introduction 1 “
If you didn't setup your Pi for SSH when you first booted, no problem you can do it now.
Open LX Terminal on your Pi and enter the following command to start Raspi Config:
sudo raspi-config
www.b2cqshop.com
A script will run and then you will see the following as confirmation:
www.b2cqshop.com
Now switch over to using the computer from which you wish to control the Pi.
If you are using a Mac or Linux PC then open a Terminal. On the Mac, you can find this in the
Utilities folder of your Applications folder.
Enter the following command into the Terminal window.
Note that you will need to replace the IP address above with that of your Pi. You can find this
by running the command “sudo ifconfig” from the Terminal.
www.b2cqshop.com
The option “-l pi' specifies that we want to log into the Pi as the user “pi”. The first time you
run the command, you will get a security warning about not being able to verify the identity of the
machine, say that you want to continue and enter your password (“raspberry” by default) when
prompted.
You will notice that the command prompt will change to indicate that you are now connected
to your Pi. Try using the “ls” command to show the contents of the current folder on the Pi.
If you use windows, then you will need to download a free program called
“putty” from here:http://www.putty.org/.
Having downloaded and installed Putty (its a single file called putty.exe), run the
program.
Enter the IP address that you found earlier and click “Open”. This will give you a warning (the
first time) and then prompt you for the user (“pi”) and password (“raspberry”).
www.b2cqshop.com
Try exploring your files system by using 'ls' to list the files in the current directory and
'cd' followed by a directory name to change the current directory.
You can edit files using 'nano' followed by the file name and also install software using
the 'apt-get' command, as described in some of the earlier tutorials in this series.
When finished with your ssh session, close the client application/window or simply
type in exit into the shell window.
www.b2cqshop.com
Troubleshooting
If you encounter a connection reset by peer error when trying to connect to your Pi,
there could be a problem with the SSH keys. You can 'reset' the keys with the following
commands.
5v Power
The 5v power pins, two of them,
provide your project with any current
your power adaptor can muster minus
that which is used by the Pi itself.
5v power is great for regulating
down to 3.3v, which you can then use to
power an ATMega that's logic-level
compatible with your Pi.
If you need more than 5v, driving
motors for example, you'll have to find
your power elsewhere!
3.3v Power
At a measly 50mA max current,
the 3.3v pin on the Raspberry Pi isn't
terribly useful, but it's enough to light
an LED or two.
www.b2cqshop.com
GPIO Setup
Overview
One of the great things about the Raspberry Pi is that it has a GPIO connector to which
you can attach external hardware.
The GPIO connector actually has a number of different types of connection on them.
There are:
True GPIO (General Purpose Input Output) pins that you can use to turn LEDs on
and off etc.
I2C interface pins that allow you to connect hardware modules with just two
control pins
SPI interface with SPI devices, a similar concept to I2C but a different standard
Serial Rx and Tx pins for communication with serial peripherals
In addition, some of the pins can be used for PWM (pulse Width Modulation) for power
control and another type of pulse generation for controlling servo motors called PPM
(Pulse Position Modulation).
In this tutorial, you are not actually build anything, but you will learn how to configure your
Raspberry Pi and install useful libraries ready to start attaching some external electronics
to it.
This tutorial is written for Raspbian & Raspbian-derived installations (like Occidentalis)
only
www.b2cqshop.com
The diagram below show the pins on the GPIO connector for a Raspberry Pi
Version 1 (which is what existed when this tutorial was released) Version 2 has pin 27
replacing pin 21 but it otherwise the same
As well as supplying power (GND, 3.3V and 5V) all the GPIO pins can be used as
either digital inputs or outputs. The pins labelled SCL and SDA can be used for I2C. The
pins labelled MOSI, MISO and SCKL can be used to connect to high speed SPI devices.
All the pins have 3.3V logic levels and are not 5V-safe so the output levels are 0-3.3V and
the inputs should not be higher than 3.3V. If you want to connect a 5V output to a Pi
input, use a level shifter
A popular way to actually make the connections to the Raspberry Pi is to use a Pi
Cobbler.
www.b2cqshop.com
BCM GPIO
To connect the T- Cobbler to your Pi Board, Please made sure it is correct, check
the following picture .
Make extra extra double-check sure that the PIN 1 indicator is in the corner of the Pi. If
you have a gray cable its probably a red stripe, for black cables, a white stripe. That pin
must not be next to the TV connector. Turn around or twist the cable until it is right
This uses a ribbon cable to connect the GPIO connector to solderless breadboard,
where you can add your own components.
www.b2cqshop.com
Raspberry Pi Code
To make life easy for those wishing to experiment with attaching electronics to their Pi,
Adafruit have produced an extensive and extremely useful collection of code. This
includes simple Python libraries for a large number of modules, including displays,
sensors and PWM controllers etc.
To fetch this code, you need to use some software called 'git'. This comes pre-installed
on Occidentalis, but on Raspbian you must install it by entering the following commands
into LX Terminal.
You will find the icon for LX Terminal on your desktop.
Before we go any further, issue the following command in LXTerminal. This will
ensure your package can be found and that you get the latest version. It does not matter
which directory you are in.
Command :
Before we go any further, issue the following command in LXTerminal. This will ensure
your package can be found and that you get the latest version. It does not matter which
directory you are in.
www.b2cqshop.com
Copy Code
The update may take a while, especially if this is the first time you have run it on your
Pi. Eventually it should give you another command prompt '$' and it will be ready for you to
type the next command which is:
Once git is installed (if its not already there) you can "check out" the Adafruit Pi Python
repository onto your Pi using the following commands
cd Adafruit-Raspberry-Pi-Python-Code
ls
www.b2cqshop.com
If there is any problem during any of the steps above, you will see an error message. The
most common reasons why something should fail to install are:
a problem with your Internet connections
a mis-typed command. Remember everything in Linux is case sensitive. It is best
to open this page on your Raspberry Pi so you can just copy and paste the
commands.
You will find all sorts of goodies in here, many of which we will use in later tutorials.
Configuring GPIO
The GPIO pins can be used as both digital outputs and digital inputs. As digital
outputs, you can write programs that turn a particular pin HIGH or LOW. Setting it HIGH
sets it to 3.3V setting it LOW sets it to 0V. To drive an LED from one of these pins, you
need a 560Ω resistor in series with the LED as the GPIO pins can only manage a small
amount of power.
If you use a pin as a digital input, then you can connect switches and simple sensors
to a pin and then be able to check whether it is open or closed (that is, activated or not)
Some Adafruit projects that use just GPIO.
To program the GPIO ports in Python, we need to install a very useful Python 2 library
called Rpi.GPIO. This module gives us a simple to use Python library that will let us
control the GPIO pins.
The installation process for this is the same whether you are using Raspbian or
Occidentalis. In actual fact, some versions of Raspbian include this library, but these
instructions will also have the effect of updating to the latest version, which is worth doing.
To install RPi.GPIO, you first need to install the Python Development toolkit that
RPi.GPIO requires.
To do this enter the following command into LXTerminal:
Thats all there is to it. You are ready to try some of the projects I mentioned at the top
of this section.
Configuring I2C
I2C is a very commonly used standard designed to allow one chip to talk to another.
So, since the Raspberry Pi can talk I2C we can connect it to a variety of I2C capable chips
and modules.
If you are using Occidentalis, then your Pi is ready to go with I2C as far as enabling the
hardware goes. However, if you are using Raspbian, you will need to open LXTerminal
and enter the following command:
1. i2c-bcm2708
2. i2c-dev
www.b2cqshop.com
After editing the file, you will need to reboot for the changes to take effect.
If you have problems with I2C on Raspbian, then it is well worth updating to the latest
version. These instructions were tested with the release dated 2012-10-28.
The I2C bus allows multiple devices to be connected to your Raspberry Pi, each with
a unique address, that can often be set by changing jumper settings on the module. It is
very useful to be able to see which devices are connected to your Pi as a way of making
sure everything is working.
To do this, it is worth running the following commands in the Terminal to install the
i2c-tools utility.
If you do not have this file then there is nothing to do, however, if you do have this file,
you need to edit it and comment out the lines below:
1. blacklist spi-bcm2708
2. blacklist i2c-bcm2708
.. then edit the file so that it appears as below, and then save and exit the file using
CTRL-x and Y.
Once this is all done, you can type the following command to see all the connected
devices (if you are running a 512MB Raspberry Pi Model B)
Copy Code
1. sudo i2cdetect -y 1
www.b2cqshop.com
This shows that two I2C addresses are in use – 0x40 and 0x70.
Note that if you are using one of the very first Raspberry Pis (a 256MB Raspberry Pi
Model B) then you will need to change the command to:
Copy Code
1. sudo i2cdetect -y 0
The Raspberry Pi designers swapped over I2C ports between board releases. Just
remember: 512M Pi's use i2c port 1, 256M ones use i2c port 0!
www.b2cqshop.com
Overview
This recipe will allow you to turn an LED into an output device for your
Raspberry Pi and will guide you through writing a program to make it flash.
Wire LED :
1. Take one end of the resistor to the blackboard and connect it with the
cathode of the LED (nearest flat edge and the shorter lead), and connect
them to the GND .
2. Connect the anode ( longer lead ) of the LED to the P7 of the T-Cobbler with
a jumper cable .
Python Script
(Please note that the program must be run as root by putting “sudo” in front of the
Python command.)
www.b2cqshop.com
2 . Type in the code below (Pro Tip: Any lines beginning with a # symbol are
comments so don’t need to be included for the program to work - they will, however, help
you to understand the code)
3. Type in the code below - type “Ctrl + X” and “Y” to exit nano and save the
file.
4. Run the program
sudo python FlashLED.py
You can edit the file after the fact using the simple text editor nano, that is run
nano FlashLED.py to make simple edits
Next up, we'll make the file into an application (executable), type
chmod +x FlashLED.py
Overview
In the Button , the Pin “1” and “4” is conduction , and the Pin “2” and “3” is
conduction .
Wiring :
For the Button , please check this following wiring . And notice that to use the 3.3V .
www.b2cqshop.com
Python Script
3. type “Ctrl + X” and “Y” to exit nano and save the file.
chmod +x raspi-audio-button.py
When you push the button, the Led will be turn on , Otherwise , it is off .
www.b2cqshop.com
Overview
Hardware
The DS18B20 "1-wire" sensors can be connected in parallel - unlike nearly any other sensor sold! All sensors should
share the same pins, but you only need one 4.7K resistor for all of them
The resistor is used as a 'pullup' for the data-line, and is required to keep the data transfer stable and happy
Be careful to get the DS18B20 the right way around. The curved edge should be to the left as shown in the figure below. If you
put it the wrong way around, it will get hot and then break.
In this Lesson , we will use the DS18B20 module , that you can just plug it to the
blackboard with jumpers .
The module’s Schematic
Please note that the pins of the DS18B20 module (as we find that some of the
module cable is not correct ).
3 GND
2 VCC (3.3v)
1 P7 of the T-cobbler ( GPIO 4)
www.b2cqshop.com
Although the DS18B20 just looks like a regular transistor, there is actually quite a
lot going on inside.
The chip includes the special 1-wire serial interface as well as control logic and
the temperature sensor itself.
Its output pin sends digital messages and Raspbian/Occidentalis includes an
interface to read those messages. You can experiment with the device from the
command line or over SSH (see Lesson 6), before we run the full program.
Type the commands you see below into a terminal window. When you are in the
'devices' directory, the directory starting '28-' may have a different name, so cd to the
name of whatever directory is there .
Software
The Python program deals with any failed messages and reports the temperature
in degrees C and F every second.
import os
import glob
import time
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
return temp_c, temp_f
while True:
print(read_temp())
time.sleep(1)
www.b2cqshop.com
The program starts by issuing the 'modprobe' commands that are needed
to start the interface running.
The next three lines, find the file from which the messages can be read.
A problem has been reported with occasional hangs when reading the
temperature file when using Raspbian. If you find you have the same problem,
try replacing the function read_temp_raw with the code below. You will also
need to add a line at the top of the file 'import subprocess'.
def read_temp_raw():
catdata = subprocess.Popen(['cat',device_file], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out,err = catdata.communicate()
out_decode = out.decode('utf-8')
lines = out_decode.split('\n')
return lines
The main loop of the program simply loops, reading the temperature and printing
it, before sleeping for a second.
To upload the program onto your Raspberry Pi, you can use SSH to connect to
the Pi, start an editor window using the line:
nano thermometer.py
and then paste the code above, before saving the file with CTRL-x and Y.
Configure Test
The program must be run as super user, so type the following command into the
terminal to start it:
sudo python thermometer.py
Overview
In this tutorial, you will learn how to use an Infrared remote with a Raspberry Pi
configured as a media center.
The IR receiver is attached to the GPIO connector on the Raspberry Pi.
Before tackling this project, you need to follow this tutorial to set up your
Raspberry Pi as a media center.
Hardware
Pin Connect :
GND gnd
www.b2cqshop.com
VCC +5v
D cobbler P1 (GPIO 18)
The IR sensor has just three pins, that will connect with three pins on the GPIO
connector. To do the connecting, we can use jumper cable and blackboard . These
make a good reliable connection as the IR sensor has unusually thick leads for an IC.
Make the connections as shown below. Note that you do not have to use the same
colored jumper wires. But selecting adjacent wires that are still in a 'ribbon' will help
keep things neat.
Note that the IR sensor chip needs to be operated at 3.3V not 5V when used with
the Raspberry Pi.
LIRC
The interface between the hardware and the Raspberry Pi media centre is
managed by a piece of software called LIRC (Linux Infrared Remote Control).
This is pre-installed on most recent Raspberry Pi distributions and is included
in the Rasbmc distribution, so there is nothing to install, however, there is
some setting up to do.
To make sure that the IR hardware is correct, we can connect to the Raspberry Pi
running Rasbmc using SSH, which is automatically enabled on this distribution.
If you have not connected to a Raspberry Pi using SSH before, please see this tutorial. “ Introduction 2. Using SSH “
You can find the IP address of the Raspberry Pi using the XBMC System Info
page.
To be able to test the IR receiver without XBMC, you need to make sure
that the IR remote feature is turned off, or you will not be able to use LIRC from
the SSH. So run the Rasbmc Settings program and make sure that the
option Enable GPIO TSOP IR Receiver is disabled.
www.b2cqshop.com
Now hold the remote in front of the receiver and you should see a series of 'pulse' /
'space' messages appear each time you press a button.
Congratualtions! The IR receiver is working.
Now that we know that the hardware is okay, we need to give LIRC a config
file to tell it about the keys on the remote that we are using.
www.b2cqshop.com
From the SSH session, issue the command:
nano lircd.conf
... and then paste the following text into it, before saving the file by clicking
CTRL-x then Y.
# Please make this file available to others
# by sending it to <[email protected]>
#
# this config file was automatically generated
# using lirc-0.9.0-pre1(default) on Thu Mar 14 14:21:25 2013
#
# contributed by
#
# brand: /home/pi/lircd.conf
# model no. of remote control:
# devices being controlled by this remote:
#
begin remote
name /home/pi/lircd.conf
bits 16
flags SPACE_ENC|CONST_LENGTH
eps 30
aeps 100
begin codes
KEY_VOLUMEDOWN 0x00FF
KEY_PLAYPAUSE 0x807F
KEY_VOLUMEUP 0x40BF
KEY_SETUP 0x20DF
KEY_UP 0xA05F
KEY_STOP 0x609F
www.b2cqshop.com
KEY_LEFT 0x10EF
KEY_ENTER 0x906F
KEY_RIGHT 0x50AF
KEY_KP0 0x30CF
KEY_DOWN 0xB04F
KEY_BACK 0x708F
KEY_KP1 0x08F7
KEY_KP2 0x8877
KEY_KP3 0x48B7
KEY_KP4 0x28D7
KEY_KP5 0xA857
KEY_KP6 0x6897
KEY_KP7 0x18E7
KEY_KP8 0x9867
KEY_KP9 0x58A7
end codes
end remote
This file should be saved in the home directory for the user pi.
Now, return to the Rasbmc Settings program and enable the option Enable GPIO
TSOP IR Receiver. At the same time, change the GPIO Remote Profile as shown
below:
Restart XMBC and when it has rebooted, you should see a small popup message in the bottom
right corner like the one below.
www.b2cqshop.com
You should now find that your IR remote control will work and that you no longer need the
keyboard and mouse to control XMBC.
I generated the config file for this remote using a utility that is part of LIRC called 'irrecord'.
If you have a different remote, then you can generate a config file for it using this tool.
The process is as follows:
Turn the remote off on XMBC using Rasbmc as we did before using 'mode2'.
Type the command 'irrecord –list-namespace'. This will tell you the allowed key names that you can use when prompted.
Follow the instructions to the letter. It all seems a bit odd, but the program has to work out the timings and encodings used
by the remote.
www.b2cqshop.com
www.b2cqshop.com
Overview
Adding a LCD to any project immediately kicks it up a notch. This tutorial explains how
to connect a inexpensive HDD44780 compatible LCD to the raspberry pi using 6 GPIOs.
While there are other ways to connect using I2C or the UART this is the most direct
method that get right down to the bare metal.
This technique:
The example python code sends date, time and the IP address to the display. If you
are running a Pi in headless mode being able to determine the IP address at a glance is
really handy.
www.b2cqshop.com
The LCD
Whenever you come across a LCD that looks like it has 16 connectors it is most likely
using a HD44780 controller. These devices provide the same pinouts making them
relatively easy to work with. The LCD uses a parallel interface meaning that we will need
many pins from our raspberry pi to control it. In this tutorial we will use 4 data pins (4-bit
mode) and two control pins.
www.b2cqshop.com
The data pins are straight forward. They are sending data to the display (toggled
high/low). We will only be using write mode and not reading any data.
The register select pin has two uses. When pulled low it can send commands to the
LCD (like position to move to, or clear the screen). This is referred to as writing to the
instruction or command register. When toggled the other way (1) the register select pin
goes into a data mode and will be used to send data to the screen.
The read/write pin will be pulled low (write only) as we only want to write to the LCD
based on this setup.
LCD Pinout
1. Ground
5. Read/Write (R/W). R/W=0: Write, R/W=1: Read (we won't use this pin)
11. Bit 4
12. Bit 5
13. Bit 6
14. Bit 7
Before wiring, check that your LCD has an LED backlight, not an EL backlight. LED
backlights use 10-40mA of power, EL backlights use 200+ma! EL backlights are often
cheap to get but are not usable, make sure you don't use one or you will overload the Pi.
Some cheap LCDs that have LED backlights do not include a resistor on the LCD module
for the backlight, if you're not sure, connect a 1Kohm resistor between pin 15 and 5V
instead of connecting directly.
www.b2cqshop.com
Wiring Diagram
First, connect the cobber power pins to the breadboard power rail. +5.0V from the
cobbler goes to the red striped rail (red wire) and GND from the cobbler goes to the blue
striped rail (black wire)
Then connect up the potentiometer, the left pin connects to ground (black wire) and the
right pin connects to +5V (red wire)
Schematic
www.b2cqshop.com
Before you start, make sure you have a strip of 0.1" male header and a 10K
potentiometer. All Adafruit Character LCDs come with these parts so you should be good
to go.
Most LCDs have a strip of 16 pins on the top, if the header is a little longer, just break
it off until its the right length
www.b2cqshop.com
Next you'll need to solder the header to the LCD. !!! You must do this, it is not OK to just try
Start by connecting the 5V and GND wires from the cobbler to the breadboard. Then
connect pins #1, #2 and #15, #16 to the breadboard power rails as shown. The backlight
should come on. If it doesn't, check the wiring!
www.b2cqshop.com
Next, wire up the contrast potentiometer as shown above, with the middle pin
connecting to LCD pin #3 and the other two pins going to 5V and ground.
Twist the potentiometer until you see the first line of the LCD fill with boxes. If you
don't see the boxes, check your wiring!
Finish the wiring for the RS, RW, EN, D4, D5, D6, and D7 pins as shown in the
diagram up top
www.b2cqshop.com
That's it! Now you're ready to run the python script to draw text on the screen!
Necessary Packages
This guide is based on Debian's "Wheezy" release for Raspberry Pi. It was made
available in Mid July 2012. The following items must be installed in order to utilize the
Raspberry Pi's GPIO pins. If you are running Adafruit's Occidentalis you can just skip this page.
Upgrade distribute (required for RPi.GPIO 0.3.1a) - [No image for this one]
www.b2cqshop.com
$ sudo apt-get install python-setuptools
$ sudo easy_install -U distribute
$ sudo apt-get install python-pip
Python Script
The Python code for Adafruit's Character LCDs on the Pi is available on Github at
https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code
www.b2cqshop.com
There are two files we will be working with:
2. Adafruit_CharLCD_IPclock_example.py - code for IP address and date/time calls methods from Adafruit_CharLCD.py
The first file Adafruit_CharLCD.py is a mashup from two different sources of LCD
code. Github user lrvick put together a nice python class. His work is the baseline that is slowly
being changed as we are bringing in more elements from the Arduino LiquidCrystal library.
The easiest way to get the code onto your Pi is to hook up an Ethernet cable, and
clone it directly using 'git', which is installed by default on most distros. Simply run the
following commands from an appropriate location (ex. "/home/pi"):
Testing
You can test the wiring from the previous step by simply running the
Adafruit_CharLCD.py python code, as it has a little code it in that will simply display a
test message when wired correctly
If you're using a Version 2 Raspberry Pi, pin #21 has been replaced with pin #27 so
edit Adafruit_CharLCD.py and change:
def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 21, 22], GPIO = None):
to
def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 27, 22], GPIO = None):
chmod +x Adafruit_CharLCD.py
sudo python ./Adafruit_CharLCD.py
www.b2cqshop.com
IP Clock Example
#!/usr/bin/python
lcd = Adafruit_CharLCD()
cmd = "ip addr show eth0 | grep inet | awk '{print $2}' | cut -d/ -f1"
lcd.begin(16,1)
def run_cmd(cmd):
output = p.communicate()[0]
return output
while 1:
www.b2cqshop.com
lcd.clear()
ipaddr = run_cmd(cmd)
lcd.message(datetime.now().strftime('%b %d %H:%M:%S\n'))
sleep(2)
Init Script
It's all fine and dandy to have a script like Adafruit_CharLCD_IPclock_example.py which
we can manually run, but wouldn't it be nice to have the time and ip address pop up on the display
when the raspberry pi boots up? This is done using a init script which runs the
Adafruit_CharLCD_IPclock_example.py code on boot and kills it during system shut down.
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
#! /bin/sh
# /etc/init.d/lcd
export HOME
case "$1" in
start)
/home/pi/Adafruit-Raspberry-Pi-Python-Code/Adafruit_CharLCD/Adafruit_CharLCD_IPclock_example.py
2>&1 &
;;
stop)
kill -9 $LCD_PID
;;
*)
exit 1
;;
esac
exit 0
Make the lcd init script known to the system by using the update-rc.d command.
www.b2cqshop.com
Time Zone
Last, but not least. My pi came configured with UT (Universal Time). I prefer to see
time based on my time zone which is Mountain. Here is how to configure time on the pi for
any location. This is a one time configuration setting that will survive between reboots.
The command launches a curses based program which allows arrow keys to be used
to select the region specific time zone.
www.b2cqshop.com