Raspberry Pi

nonsmoking

THANK YOU FOR NOT SMOKING

(Your Raspberry Pi, or yourself, or your family, or pets, or house…)

The material on this page poses a genuine risk to software, hardware, and possibly even you. The use of ‘super user’, sudo, root, or admin poses a real threat to the health of your system, and the existence of the software and information contained within it. One specific risk is the use of the ‘dd’ command, and any disk partitioning work using sudo from the command line. Get a disk ID wrong, and you may wipe your main HDD/SSD. Be careful, be aware of the risks, and triple check your work, before committing to it with a super-user command. It’s good practice to….. ASSUME I AM WRONG, AND CHECK THE MATERIAL ON THESE PAGES AGAINST OTHER INTERNET SOURCES WITH SIMILAR INFORMATION. Make backups and make them often. And test software and hardware configurations in virtual machines, or another 2nd or 3rd-line machine, and not your main computing environment. Don’t risk your “mission critical” gear without understanding what you are doing, and the consequences if it goes wrong. Hardware may be damaged through incorrect wiring such as channeling 5V down pins not designed specifically to accept such a load, and doing so may result in you:  SMOKING YOUR RASPBERRY PI OR ARDUINO! Messing with live electrical devices and equipment poses a risk to your health, and that of your family, pets, and your children’s imaginary friends. BE CAREFUL, AND IF YOU DON’T UNDERSTAND WHAT YOU ARE ABOUT TO DO, DON’T DO IT UNTIL YOU DO. Be safety conscious always, and take precautions around electrical equipment and other dangerous gear like gas-powered or electrical soldering irons! Leaving one of those unattended is asking for your house to burn down.

With that having been said………Enjoy and good luck!

Make a bootable RasPi SD via a Mac (Not NOOBs).

(The quickest and easiest method that I have come across to set up your bootable O/S SD card is via the Command Line Interface)  

Step 1 – Download your Raspberry O/S Flavour (usually in .ZIP format), and navigate to it from within the terminal. Raspberry Pi Foundation Downloads page: http://www.raspberrypi.org/downloads Tip: In Mac, you can short-cut your way to the appropriate directory by dragging the file from the desktop (or wherever within OSX) onto the terminal window. This will print the full directory address for the file’s location at the command prompt.

Step 2 – (Optional) You can check the SHA-1 Checksum with the following command within the Mac’s terminal (if that’s the way you roll):

shasum 2013-12-20-wheezy-raspbian.zip
Checking the SHA sum within the terminal.
Checking the SHA sum within the terminal.

Step 3 – Unzip the file with the following command:

unzip 2013-12-20-wheezy-raspbian.zip
Unzip the file.
Unzip the file.

Step 4 – Download the SD Card Formatter from the SD Association’s website here.  Prepare the SD Card by inserting it & opening the formatter (type the admin password), and select the following settings:

SD Formatter
SD Formatter
  • Choose the correct SD Card from the drop down list labelled “1)”,
  • Within “Option” choose Yes to allow Logical Address Adjustment to increase interoperability between devices,
  • For 2), select “Overwrite Format” to ensure a greater chance of booting success: takes longer, but is worth it!
  • Specify a name for the card’s volume (keep it simple, nothing too creative!), and
  • Format!
The SD Association Format Tool
The SD Association Format Tool

Step 5 – The next step involves determining the SD Card’s device ID from within Mac.   This is an important step because the use of sudo within the terminal to wipe a drive or install a fresh O/S (Raspbian), can result in a complete loss of data, partitions, the O/S and all your files if the incorrect device ID is used. partition/SSD/HDD. Two ways to determine the correct device ID for the SD Card are:

  • Click the Apple Icon (top left corner of Mac desktop) and select “About This Mac”
  • Then click “More Info”
  • Then avoid the temptation to open the “Storage” tab, and instead click the System Report button
  • Navigate to the Storage heading on the left side beneath Hardware
  • Next, highlight your SD Card’s volume by checking the corresponding size in GB & Volume name (“RASPY” in my case: see image)
  • Now, as you can see in my images, once you have highlighted your SD’s volume in system report, OSX provides additional info within a pane at the base of the window.  Now look for the SD Volume’s “BSD Name” and that is the Device ID within Mac (mine is highlighted in green)
  • It will normally be something like “Disk1” (for the drive itself) or “Disk1S1” (for the dive & partition) or “/Dev/Disk1” for the full address.
1) About This Mac
1) About This Mac

.

2) System Report
2) System Report

.

3) Storage and Volume info / BSD Name (note mine in green)
3) Storage and Volume info / BSD Name (note mine in green)

The second method for determining the SD Card’s Device ID is via the Command Line Interface or Terminal within OSX and is as follows:

  • If installed, eject the SD Card via the Finder by clicking the “eject” icon.
  • Open the terminal and use one of the following commands to print a list of your currently connected storage media:
df -h

or

diskutil list
  • Take note of the devices listed as a result of either df -h or diskutil list, and re-insert your SD Card.
  • Run either df -h or diskutil list again, and note down the address of the newly listed device (which was absent from the first list).
  • This should be your Raspberry Pi SD Card; just compare the capacity and volume name (mine is RASPY) are the same as what you were expecting. It should be something like /dev/disk1s1
df -h without the SD Card
df -h without the SD Card
df -h with the SD Card
df -h with the SD Card

OR

diskutil list without SD Card
diskutil list without SD Card
diskutil list with the SD Card
diskutil list with the SD Card
  • As you can see, here are two similar ways to skin the cat from within the terminal (there are more, but I don’t want to bore you).
  • Personally, I prefer diskutil list as it is a clearer format and a simpler layout for this type of work.

Step 6 – Next unmount the disk from within the terminal with the following command (substituting the red for your device’s BSD address).

sudo diskutil unmount /dev/disk1s1

Step 7 – Navigate to the folder containing your unzipped Raspbian (or other) image using cd. Use ls to verify the file is contained in that directory and to confirm the correct file name format (if you deem it necessary, otherwise move onward to Step 8).

cd ~/Home/Desktop
ls
Note the zipped & unzipped Rasbian image in between the folders on Porn.
Note the zipped & unzipped Rasbian images in between the folders of porn.

Step 8 – Use the dd command to bake the image to the prepared (then unmounted) SD Card.  Care should be taken to ensure your disk’s device ID is accurate, or your may re-image the Mac’s HDD/SSD to run Raspbian (not that it would work, just in case you were wondering).  Again, substitute the red to match your SD Card’s Device ID, and the unzipped image file name (depends on what you are installing). N.B. Carefully note the “r” within the drive’s Device ID toward the end of the command, this is intentional and NOT a typo.

sudo dd bs=1m if=2013-12-20-wheezy-raspbian.img of=/dev/rdisk1
SD Card image burning with dd
SD Card image burning with dd

Step 9 – Once OSX reports the job complete. Eject the disk with: diskutil eject, place the SD Card in the RPi and power it on.  In no time, (computer GODs willing) you should be greeted with the Raspbian configuration tool (or other corresponding distro welcome screens).

sudo diskutil eject /dev/disk1

After Installation, booting, and initial configuration of the Raspberry Pi…..

……….Prepare python for use on the Raspberry Pi.

Once into Raspbian, open the terminal if you are using X, otherwise, if you are in the command line, punch out the following:

sudo apt-get update && sudo apt-get upgrade
sudo apt-get install python-dev
sudo apt-get install python-pip
sudo easy_install -U distribute
sudo pip install RPi.GPIO
sudo reboot -h

……….Configure the Raspberry Pi’s I2C channels.

Once into Raspbian, open the terminal if you are using X, otherwise, if you are in the command line, punch out the following:

sudo raspi-config

And within the configuration program enable support for “SPI” within Raspbian.

Then exit the config tool, and reboot the Pi. Once it is powered back up, at the terminal or within the command line environment:

sudo nano /etc/modules

Then add the following to the end of the file

i2c-bcm2708 
i2c-dev

Save (with CTRL-O), exit (with CTRL-X) and reboot the pi.

In addition to correctly configuring the I2C Raspbian Support, it is also advised to install the I2C-tool utility by:

sudo apt-get install i2c-tools
sudo apt-get install python-smbus

On the latest Raspbian version (07JAN14 at the time of writing), the  /etc/modprobe.d/raspi-blacklist.conf file should also be changed to ensure that components of I2C bus support are not blacklisted.  If it does exist, then type the following into the CLI and make the below-mentioned changes:

sudo nano /etc/modprobe.d/raspi-blacklist.conf

If this file doesn’t exist, then you need not do anything.  If is does exist then edit these lines:

blacklist spi-bcm2708
blacklist i2c-bcm2708

By adding a comment flag ,”#”, to the beginning of the line to look like this:

#blacklist spi-bcm2708 
#blacklist i2c-bcm2708

Again, save (with CTRL-O), and exit (with CTRL-X).

Although perhaps unnecessary, at this stage I would also recommend to update and upgrade the system via:

sudo apt-get update && sudo apt-get upgrade

And once that is finished:

sudo reboot -h

Raspbmc on the Raspberry Pi within Linux (Debian).

(reproduced from here with may thanks to the team behind Raspbmc)  

Raspbmc Logo

Step 1 – Use the follow terminal commands to download and install the Raspbmc using python scripts:

wget http://svn.stmlabs.com/svn/raspbmc/release/installers/python/install.py
chmod +x install.py

Step 2 – Next, run the installation script with root privileges:

sudo python install.py

Terminal_001

 Done! Eject your SD card, stick it into your Pi and boot it up. After a while, some downloading, and a few reboots; your Pi is now furnished with the latest and very greatest edition of Sam Nazarko’s brilliant Raspbmc!

Connecting your Raspbmc Pi to the XBMC remote.

Step 1 – Use the Apple Store/Google Play to download and install the official XBMC remote apps for your phone/tablet.  The apps are available here for Apple iOS and here for Android devices.

Step 2 – Boot into your RaspBMC installation on the Raspberry Pi and connect to your network via either ethernet or WiFi.

Step 3 – Once your Raspbmc Pi is connected to your network, navigate to the SYSTEM menu & click on SETTINGS then SERVICES. Navigate to WEBSERVER and ensure that “Allow control of XBMX via HTTP” is checked. Enter values in the PORT, USERNAME, & PASSWORD areas as you see fit (ie: PORT 80, USERNAME matt, & PASSWORD whatever). You may be required to reboot the Pi to ensure changes are implemented, but the Raspbmc OS will let you know if this is needed.

Step 4 – Once the Pi has rebooted (if needed), the navigate through the SYSTEM, SYSTEM INFO menus, & then click on NETWORK. Copy down the IP address of your Pi directly into the XBMC Remote software as you ‘create’ or add an ‘XBMC Host’. Enter the other details into the XBMC Remote also (such as: Name, HTTP API Port {usually 80}, username and password etc).

XBMC Settings

Step 5 – Click OK on the XBMC Remote software once the details have been added, and then try to connect to your Pi from the connect button on the main menu page.

Step 6 – Hopefully the above process was a success, and you can now navigate your Raspbmc Pi from your iOS or Android device. If not, then troubleshoot your settings, especially checking to ensure that BOTH the Raspbmc Pi AND iOS/Android device ARE ON THE SAME NETWORK, whether it be wireless or wired. Failing that, double check your settings in both the Rasbmc Pi and XBMC Remote to ensure that they both match. If this doesn’t help the situation, call Dr Google and attempt to fault correct the installation of the Pi and the configuration of the Raspbmc settings.

Connecting to Raspbmc with SSH.

Step 1 – Usually (or at least in my case anyway), Raspbmc is installed with the SSH server enabled by default, however, you can confirm this by navigating to the PROGRAMS menu heading, and clicking on the RASPBMC SETTINGS app icon. This menu area should be familiar to you if you needed to connect your Pi to your wireless network. Regardless, once in you need click on the SYSTEM CONFIGURATION heading, & scroll almost to the bottom of the page to see the SERVICE MANAGEMENT heading, of which SSH SERVER is the second item from the top. Ensure that the SSH Server is option is selected.

Step 2 – Open your Mac or Linux terminal and attempt to connect to the Pi via SSH using the following command: ssh <username>@<ip address>. Under normal circumstances (ie: the current default settings), the username for your Raspbmc Pi is “pi” and the password is “raspberry”, as is the case with the Raspbian Wheezy distros (well, the ones I have worked with in the past). If successful, you may get a couple of further configuration pages to navigate, with the end result being a terminal window of your Raspbmc Pi’s bash shell login, à la the below image:

ssh shell

Installing Raspbian on the Raspberry Pi within OSX 10.9

(Get  NOOBS from the Raspberry Pi Foundation here OR just get Raspbian from here)  

Raspbian

Step 1 – Download NOOBs (or Raspbian individually) from the link(s) above. Step 2 – Download the SD Card Formatter from the SD Association’s website here. As per the SD Association’s website, this is the recommended tool to use for formatting SD cards, and I would agree having had several issues with SD cards not booting, not mounting or otherwise not behaving when I have prepared them using other methods (no matter what OS I am using: Mac/Linux/Windows). As the SD Association puts it…..

The SD Formatter was created specifically for memory cards using the SD/SDHC/SDXC standards. It is strongly recommended to use the SD Formatter instead of formatting utilities provided with operating systems that format various types of storage media. Using generic formatting utilities may result in less than optimal performance for your memory cards.

Step 3 – Download the Mac OS, Rasbperri Pi SD Card tool from this random website with no information about its author, or his motives for creating it……

Step 4 – Obtain a well-known brand SD Card with a capacity of >4gig for the Raspberry Pi’s operating system to be installed upon.

I have used several SD cards for this purpose, and I highly recommend going with a major brand due to some compatibility issues I have had from either, smaller brand cards, or knock-off brand eBay “specials”.  These tend to be a lot less reliable, often failing to be:

  • recognised by the OS,
  • mounted correctly,
  • formatted properly,
  • OS loaded, or even
  • booted from.

Moral of the story,

  1. Get a good brand-name card,
  2. Larger than 4gig (16gig is my sweet spot: this allows for plenty of apt-get installations once up & running),
  3. Go for a medium-range card by balancing good performance, with maximum compatibility. Don’t go for the bleeding-edge in read/write speeds, or you risk getting a card that is unsupported by the Raspberry Pi, and
  4. Format the card with the official SD Association formatting tool as linked above.

Instructions direct from the NOOBs (Vers 1_3_2)…. NOOBS INSTALLATION INSTRUCTIONS 1. Insert an SD card that is 4GB or greater in size into your computer. 2. Format the SD card using the platform-specific instructions below: a. Windows i. Download the SD Association’s Formatting Tool from https://www.sdcard.org/downloads/formatter_4/eula_windows/ ii. Install and run the Formatting Tool on your machine iii. Set “FORMAT SIZE ADJUSTMENT” option to “ON” in the “Options” menu iv. Check that the SD card you inserted matches the one selected by the Tool v. Click the “Format” button b. Mac i. Download the SD Association’s Formatting Tool from https://www.sdcard.org/downloads/formatter_4/eula_mac/ ii. Install and run the Formatting Tool on your machine iii. Select “Overwrite Format” iv. Check that the SD card you inserted matches the one selected by the Tool v. Click the “Format” button c. Linux i. We recommend using gparted (or the command line version parted) ii. Format the entire disk as FAT 3. Extract the files contained in this NOOBS zip file. 4. Copy the extracted files onto the SD card that you just formatted so that this file is at the root directory of the SD card. Please note that in some cases it may extract the files into a folder, if this is the case then please copy across the files from inside the folder rather than the folder itself. 5. Insert the SD card into your Pi and connect the power supply. Your Pi will now boot into NOOBS and should display a list of operating systems that you can choose to install. If your display remains blank, you should select the correct output mode for your display by pressing one of the following number keys on your keyboard: 1. HDMI mode – this is the default display mode. 2. HDMI safe mode – select this mode if you are using the HDMI connector and cannot see anything on screen when the Pi has booted. 3. Composite PAL mode – select either this mode or composite NTSC mode if you are using the composite RCA video connector. 4. Composite NTSC mode If you are still having difficulties after following these instructions, then please visit the Raspberry Pi Forum ( http://www.raspberrypi.org/phpBB3/ ) for support.

Installing Raspbian onto a SD Card within Linux (Mint 17.1).

Insert your SD Card into your laptop or computer’s SD Card port.  Open the terminal (CTRL-ALT-T) and locate your SD Card (noting the assigned nomenclature), with something like:

sudo fdisk -l
Using fdisk -l to locate the SD Card.
Using fdisk -l to locate the SD Card.

OR

df -h
Using df -h to locate the SD Card.
Using df -h to locate the SD Card.

As you can see above, my SD Card is mounted as /dev/sdb, with /dev/sda as the computer’s main HDD.  Obviously it’s important to correctly identify the SD Card’s assigned address.  Failure to use the correct drive identification can result in you formatting your computer’s main hard drive/disk.

Once you have your SD Card’s correct address, format the device with: (where “x” is your SD Card’s assigned address: ie /dev/sdb)

sudo fdisk /dev/sdx

to be continued:

.

Onion Pi!        Raspberry Pi + TOR = Onion Pi!

Blog entry coming soon…..In the meantime, see Adafruit’s rundown on their Onion Pi, TOR project! http://learn.adafruit.com/onion-pi/overview

Thanks to the Adafruit Onion Pi Project!
Thanks to the Adafruit Onion Pi Project!

Manage Ethernet & WiFi from the Raspbian CLI.

Find out important network information from within the CLI by entering “ifconfig”.  The results of this command should contain information pertaining to your wired Ethernet connection (eth0), your local loopback (lo), and your Wifi network (wlan0).

ifconfig

Check your network connection (either Ethernet or WiFi) by “pinging” a local IP address such as your router’s DHCP server.  (to stop the infinite cycle of pinging & reporting, just hit CTRL-Z)

ping 192.168.10.1

Another useful for troubleshooting USB devices within GNU/Linux is the “lsusb” command which generates a report on the currently attached USB devices on the system (including device IDs, manufacturer’s details, and chipset details in some instances).

lsusb

Use iwlist to scan for WiFi networks (assuming your wireless card is compatible with the Raspberry Pi & installed correctly in Raspbian).

sudo iwlist wlan0 scan

“lsmod” provides the user with information regarding all currently loaded modules.

lsmod

Finally, to find out which driver your WiFi device (wlan0) is currently using, enter the following:

readlink /sys/class/net/wlan0/device/driver
pi@Pibow2: ~_002 me@me-MacBookPro: ~_003

Use the RasPi Camera from the Raspbian CLI.

Use the following to take pictures and video from the Raspberry Pi Camera attachment via the Command Line Interface.

raspistill -o nameofimage.jpg

Use the above for still shots, and below for video!

raspivid -o nameofvideo.h264

The above command will record 5 seconds of h264 video, while the below command will record 10 seconds.

raspivid -o nameofvideo.h264 -t 10000

And for the video from the Raspberry Pi Foundation on how to install the camera module, here it is!

Measure the internal temperature of the RasPi from the CLI.

Use the following to have the RasPi report the current internal operating temperature:

/opt/vc/bin/vcgencmd measure_temp

And the output should look something like this….

RasPi Temp Monitoring
RasPi Temp Monitoring

Use the Freetronics 16×2 Arduino Display Shield with Raspberry Pi.

Raspberry Pi with 16x2 Display
Raspberry Pi with 16×2 Display showing: Month, Day, Time & eth0 IP.

In order to make my Freetronics 16×2 Arduino Display Shield work with the Raspberry Pi, I used the following references:

Freetronics Product Info – 16×2 LCD / Keypad Shield:

http://www.freetronics.com/collections/all-products/products/lcd-keypad-shield#.Ut3_eN9-8Qh

Adafruit Raspberry Pi Display Tutorial:

http://learn.adafruit.com/drive-a-16×2-lcd-directly-with-a-raspberry-pi/wiring

Schematics for Freetronics Arduino Display Shield:

http://cdn.shopify.com/s/files/1/0045/8932/files/16x2LCDShieldv2.pdf

The Layout.

Below is the Pin layout for a 16×2 HD44780 compatible display.  The Adafruit tutorial uses this layout for its wiring guide, the problem with the Freetronics display is, although it uses a HD44780 display, it is mounted on a secondary IC board to match the Arduino Shield configuration, measurements, and PIN layout.  The Freetronics Display Shield also incorporates 5 hardware switches, and a reset button (these are user programmable as various projects necessitate), which are not factored in (or used) for my project.  The difference in PIN layouts between the Shield configuration and the regular display set-up required that I ‘translate’ the various display PINs to reflect their corresponding shield PINs in order to wire them in correctly with the PINs on my Adafruit  ‘T-Cobbler’ GPIO breakout, (lower right corner of above photo).

Pin layout of 16-Pin, HD44780-compatible displays.

(Thanks to Adafruit, here)

  1. Ground
  2. VCC – 5v not 3.3v
  3. Contrast adjustment (VO) from potentiometer
  4. Register Select (RS). RS=0: Command, RS=1: Data
  5. Read/Write (R/W). R/W=0: Write, R/W=1: Read (Not used)
  6. Clock (Enable). Falling edge triggered
  7. Bit 0 (Not in 4-bit configurations)
  8. Bit 1 (Not in 4-bit configurations)
  9. Bit 2 (Not in 4-bit configurations)
  10. Bit 3 (Not in 4-bit configurations)
  11. Bit 4
  12. Bit 5
  13. Bit 6
  14. Bit 7
  15. Backlight LED Anode (+)
  16. Backlight LED Cathode (-)

The below image (made by me with Fritzing for this blog entry) shows the wiring configuration for connecting a Freetronics LCD Shield for Arduino to the Raspberry Pi GPIO pins via the Adafruit T-Cobbler breakout board and GPIO cable.

LCD Adruino Shield to Raspberry Pi via GPIO.
LCD Adruino Shield to Raspberry Pi via GPIO.

Points to note:

  • The middle wire (orange) from the Variable Resistor (the Pot glued to the board in lower-left corner) is soldered directly to PIN 3 of the LCD, not the shield.
  • Be really, really careful and triple check your wiring (AND MY WIRING DIAGRAMS!) before powering up the Pi; I assume you kinda don’t want to send 5V directly down the 3V3 (or any other) GPIO pin. Otherwise, you might see the soul of your precious little RasPi going to heaven in a puff of acrid smoke.
  • The diagram above shows a regular Adafruit Cobbler GPIO breakout board and not the “T-Cobbler” like in my photo (Adafruit hadn’t included the T-Cobbler in their Fritzing component image libraries). Regardless, everything is positioned & labelled the same for the Cobbler as it is for the T-Cobbler, so the Fritzing image remains true to my photograph.
  • Using a Freetronics LCD Shield as a regular 16×2 display for the Pi is probably overkill and a waste of resources (when a plain old 16 Pin LCD would have been less hassle).  But I didn’t have a spare 16×2 display lying around to wire up; and I did have the display shield available.  Also, as I become more competent with this sort of thing, I hope to make full use of the five programmable buttons mounted on the shield, which are absent from a regular LCD display.
  • Those with an eagle-eye may have noticed that my LCD shield is spanning two breadboards (a full & a half-sized bb). This is due to the fact that the width of an Arduino Shield’s Header Pins is wider than that of a regular breadboard, and therefore wont fit on a single board.  Whereas, with two breadboards aligned carefully & glued in position next to each other, you have copious room to tinker and experiment!
  • There is a slight variation in GPIO Pin labelling between the RasPi Model B Version 1 & Version 2.  Pin #21 on the RasPi Model B V1 has been re-labelled as Pin#27 on the Version 2, RasPi Model B.  This affects the LCD to GPIO wiring, and also the python script used to drive the display.

Parts list used for my creation.

  • Tonight, Matt’s Raspberry Pi (Model B, Rev2) is dressed in the beautiful PiBow from Pimoroni. More info here: Pimoroni.
  • The breakout board (a Pi T-Cobbler Breakout Kit), as previously mentioned, is courtesy of Adafruit. See more here: T-Cobbler.
  • A run of the mill Raspberry Pi GPIO cable (from Core Electronics).
  • The Pi Dish prototyping platform from Adafruit. Check it out here: Pi Dish
  • The display (as mentioned) is a Freetronics LCD Arduino Shield and the Pot is from the Freetronics Arduino Experimenter’s kit. Info for both products can be found here: Freetronics Australia.
  • The wires and 1/2 length breadboard are generic & available from Jaycar Electronics.

Finally, I have not reproduced the Adafruit tutorial on how to setup and configure the LCD display for the Raspberry Pi here.  I recommend anyone interested in doing such a thing, go directly to the Adafruit Learning System tutorial page (here) and follow their simple to understand, thorough, and well compiled guide straight from the horse’s mouth (so to speak). 

Install & run the Freetronics RTC Module on the RasPi.

 

Freetronics RTC - Underside view.
Freetronics RTC – Underside View (Picture courtesy of Freetronics).

 

Freetronics RTC – Top View (Picture courtesy of Freetronics).

In order to setup, install and run my Freetronics Real-Time-Clock module on the Raspberry Pi, I used information from:

  • Adafruit’s RTC Installation Guide for the Raspberry Pi (for their own RTC Module): Available here.
  • Freetronics’ product quick-start page for the Freetronics RTC Module: Available here.

A quick word on RTC chips.

The Adafruit RTC module uses a DS1307 chip at its heart, whereas the Freetronics model uses the higher spec’d and costlier DS3232 IC.  From the little reading that I have done regarding these chips, I have noted that the DS1307 uses an external crystal for timekeeping, Vs the Freetronics’ DS3232 which has the crystal incorporated into the chip itself.  The internal placement of the crystal in the DS3232 has payoffs relating to long-term accuracy (from what I have read: NOT MY OWN REAL-LIFE TESTING!), with the DS3232 less affected by ‘time-creep’ or loosing a certain amount of time per week/month/year etc etc.  The DS3232 is also temperature-compensated via an internal “thermometer” of sorts (probably more accurately referred to as a temp-sensor or something like that).  Having the DS3232 temperature aware, and therefore able to compensate for changes in temp, further increases its accuracy over time (pun-intended).  In addition to these features, the  DS3232 has 2 programmable alarms  and 236 Bytes of addressable non-volatile memory (for data logging and related purposes).  The DS3232 is apparently also able to report its internal temperature to the operating software when configured correctly, however, at this stage, I don’t have the foggiest on how to achieve this.

Freetronics RTC Module – Mounted.

Setup.

First of all, I began by opening the Freetronics RTC, and soldering the PINs to the board in the usual fashion.  There’s nothing complicated to it, it’s obvious where they are supposed to end up.  Next, I removed the module’s button-type battery sleeve, allowing the battery’s terminals to contact their corresponding points on the RTC’s battery cradle/holder/mounting bracket.  Following this, I powered down the RasPi, plugged the module into a spare spot on the breadboard, and hooked everything up.  In order to establish the wiring configuration, I took inspiration from the two references listed above, making allowances for the fact that I was using the Freetronics RTC, and not the Adafruit module (Adafruit’s guide is based on their product; funnily enough).  Although the Adafruit and Freetronics RTCs use different chips at their heart (Adafruit:  DS1307 & Freetronics: DS3232), I found that the Adafruit Guide was a great resource from which to base the installation of my RTC on.  In comparison, the Freetronics RTC quick-start guide doesn’t specifically mention the module’s use with the RasPi ………rather, it is geared to the module’s most obvious choice of companion: any of Freetronics’ range of Arduino-compatible boards (or an actual Arduino).

The RTC positioned on the breadboard.

Wiring.

The Freetronics RTC’s pin configuration is as follows:

  • VCC:     5V or 3.3V power supply pin compatible with both the Arduino’s & RasPi’s power output.
  • GND:     A ground connector for the Arduino or RasPi’s earth pin.
  • SDA:     The I2C bus data pin: connect to either the RasPi’s SDA0 GPIO, or the Arduino’s A4 pin.
  • SCL:     The I2C bus clock pin: connect to either the RasPi’s SCL0 GPIO, or the Arduino’s A5 pin.
  • 32K:     32kHz square-wave output pin (not used in my setup).
  • BAT:     A connection for the ‘+’ of an external backup battery if needed/present (not used in my setup).
  • SQI:     A controllable square-wave pin (I have no idea what this is for……In any case, its not used in my setup).
  • RST:     Currently unused, may form part of a reset switch if needed/configured (Not used in my setup).
Wiring the Freetronics RTC to RasPi.
Wiring the Freetronics RTC to RasPi.

Prepare python for use on the Raspberry Pi

(as previous discussed…..)

Once into Raspbian, open the terminal if you are using X, otherwise, if you are in the command line, punch out the following:

sudo apt-get install python-dev
sudo apt-get install python-pip
sudo easy_install -U distribute
sudo pip install RPi.GPIO
sudo apt-get update && sudo apt-get upgrade
sudo reboot -h

Configure the Raspberry Pi’s I2C channels

(as previous discussed….)

Once into Raspbian, open the terminal if you are using X, otherwise, if you are in the command line, punch out the following:

sudo raspi-config

And within the configuration program enable support for “SPI” within Raspbian.

Then exit the config tool, and reboot the Pi. Once it is powered back up, at the terminal or within the command line environment:

sudo nano /etc/modules

Then add the following to the end of the file

i2c-bcm2708 
i2c-dev

Save (with CTRL-O), exit (with CTRL-X) and reboot the pi.

In addition to correctly configuring the I2C Raspbian Support, it is also advised to install the I2C-tool utility by:

sudo apt-get install i2c-tools
sudo apt-get install python-smbus

On the latest Raspbian version (07JAN14 at the time of writing), the  /etc/modprobe.d/raspi-blacklist.conf file should also be changed to ensure that components of I2C bus support are not blacklisted.  If it does exist, then type the following into the CLI and make the below-mentioned changes:

sudo nano /etc/modprobe.d/raspi-blacklist.conf

If this file doesn’t exist, then you need not do anything.  If is does exist then edit these lines:

blacklist spi-bcm2708
blacklist i2c-bcm2708

By adding a comment flag ,”#”, to the beginning of the line to look like this:

#blacklist spi-bcm2708 
#blacklist i2c-bcm2708

Again, save (with CTRL-O), and exit (with CTRL-X).

Although perhaps unnecessary, at this stage I would also recommend to update and upgrade the system via:

sudo apt-get update && sudo apt-get upgrade

And once that is finished:

sudo reboot -h

When you are back up and running, enter one of these commands to check the configuration of I2C on your Pi:

For Raspberry Pi  Model B, Version 1 (the 256mb RAM models):

sudo i2cdetect -y 0

For Raspberry Pi  Model B, Version 2 (the 512mb RAM models):

sudo i2cdetect -y 1
sudo i2cdetect -y 0 & sudo i2cdetect -y 1
sudo i2cdetect -y 0 & sudo i2cdetect -y 1 on a RasPi Model B, Version 2 (via ssh)

Note the “68” within the table, that is the hex address for the RTC Module which verifies that it is correctly configured within Raspbian.

Setting the date & time.

Begin by loading the RTC module with:

sudo modprobe rtc-sd3232

Then enter the root shell with:

sudo bash

And run the following commands depending on which Revision of Model B RasPi you have:

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device (For Version 1)

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device (For Version 2)

Then exit the root bash:

exit

And check to see if you can communicate directly with the RTC module using:

sudo hwclock -r
sudo hwclock -r
sudo hwclock -r

Now, check that the date and time is correct using the ‘date’ command as per:

date

If the time is not correct, the easiest way for the RasPi to update the time and date is via synchronising with internet time servers.  Just connect your Pi to the internet via a WiFi or Ethernet network, and it should be sorted out automatically.  If, during any of the above commands, you are getting error messages or ‘Invalid Arguments’. just reboot the RasPi and once it’s back up and running again, re-try the commands again and you should have luck (that worked for me anyway).

Anyway the next step involves setting the RTC module to load up every time the RasPi boots by adding it to the list contained within /etc/modules.  Run:

sudo nano /etc/modules

And add rtc-ds3232 to the end of the file, save (CTRL-O to save, and CTRL-X to exit).

Next, you will need to add the ds3232 device creation at boot to by editing /etc/rc.local with:

sudo nano /etc/rc.local

And adding the following lines to the file before “exit 0” line at the end:

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device (For Version 1)

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device (For Version 2)
sudo hwclock -s (For both versions)

Once again, save (CTRL-O), and exit (CTRL-X ) the file and reboot the Pi.  If everything goes to plan, the Raspberry Pi should have remembered the correct time, regardless of whether or not it is connected to the internet (and thus having access to internet time servers).

Set a python script to run at boot on the RasPi.

Write the script and save it to somewhere you can remember.

Make the script executable with:

sudo chmod a+x somescript.py

Ensure that the first line of the script tells the computer to use python to interpret the syntax.  Something like “#!/usr/bin/python”

Copy the script into /etc/init.d with:

sudo cp somescript.py /etc/init.d

Force the Raspbian Terminal to cease screen “blanking” / ie. disable the screen saver.

Boot into the Rasbian Terminal / bash prompt. Navigate to /etc/kbd/ and edit the config file changing the lines:

BLANK_TIME=30
POWERDOWN_TIME=30

to

BLANK_TIME=0
POWERDOWN_TIME=0

So, from ./

sudo nano ./etc/kbd/config
Edit /ect/kbd/config as described above
Edit ./ect/kbd/config as described above

Scroll down the config document to:

BLANK_TIME=30

and

POWERDOWN_TIME=30

and change the values to zero (0).

./etc/kbd/config
Note the two lines requiring attention, one indicated by the orange curser, the other highlighted with blue.

Once these lines have been changed, CTRL-O and hit “Enter” to commit the changes to the file without changing the filename from config.

Once saved, CTRL-X to exit back to the bash prompt and

sudo reboot

To enable the changes to take effect.

.

.

.

.

.

Creative Commons License

Creative Commons Attribution-ShareAlike 3.0 Unported License.

2 thoughts on “Raspberry Pi

  1. Pingback: Lo-Fi Coding
  2. Dear Matt,
    Thank you so much for your complete instruction about creating a bootable RasPi SD, I followed them on Mac OS 10.11 and 2015-11-21-raspbian-jessie and worked perfectly
    Would you plz post some information about sharing the Mac internet through the Ethernet to the raspian and what is the alternative app for Xming in Mac in order to have the feature of removing desktop to Raspian( I installed wine bottler and tried to use Xming by this app but i it didn’t work properly)

    Thank you in advance
    Niousha

Leave a comment