Getting started with Raspberry Pi

From ICO wiki
Revision as of 14:24, 24 September 2015 by Arzirk (talk | contribs) (added a simple python script for blinking a led)
Jump to navigationJump to search

Raspberry Pi is a credit-card sized single-board computer which can be used for variety of purposes. The official operating system for Raspberry Pi is Raspbian. Rasbian and Ubuntu both are derived from Debian, providing seamless access to thousands of software package ready to go via Advanced Packaging Tool. Compared to other similar single-board computers Raspberry Pi has vibrant community and plenty of code examples that you can run out of the box. Before you continue make sure you're comfortable with basic command-line usage, if that's not the case take a look at CodeAcademy track for learning command line.

Feel free to improve this wiki page by clicking edit above.


Preparing SD card

Before you continue make sure you have backed up any important data on the SD-card

Download and uncompress Raspbian:

wget https://downloads.raspberrypi.org/raspbian/images/raspbian-2015-05-07/2015-05-05-raspbian-wheezy.zip
unzip 2015-05-05-raspbian-wheezy.zip

For Linux there is no need to fetch extra tools to write the image to a SD card, simply use dd:

 sudo dd bs=4M if=2015-05-05-raspbian-wheezy.img of=/dev/mmcblk0

Make sure you flush buffers, otherwise you'll get partial write:

 sync

If you're running on Windows, you can use Win32 Disk Imager to write the image to the SD card.

Powering up

Use USB-UART bridge to power up Raspberry Pi and gain access to the command-line.










Accessing command-line

For Linux there are variety of programs for connecting to serial port:

screen /dev/ttyUSB0 115200
picocom /dev/ttyUSB0 -b 115200
minicom -b 115200 -o -D /dev/ttyUSB0

If you get the error message [screen is terminating], you must put your user in the "dialout" group. To do this, enter the following command (only with your own computer):

sudo gpasswd -a $USER dialout

Then you will see a message that your user has been added to the group dialout. Now log out from Ubuntu and log in once again.


For Windows you need to install driver for your USB-UART bridge first. For Windows 8 use this workaround. Then you can use built-in HyperTerminal, PuTTY or KiTTY for connecting to the virtual serial port.

The default user is pi with the password raspberry. Make sure you change the default password. For more information about user management see Raspbian documentation.

If you're facing issues with USB-UART bridge, simply proceed to network section below.

Setting up network

Raspbian as Debian attempts to request IP address using DHCP on the Ethernet port. This assumes a functioning DHCP server on the network, which is the case most of the time. If you want to connect your laptop directly to the Raspberry Pi you can set up manual IP addresses. The networking configuration is stored in /etc/network/interfaces, remember to issue service networking restart after you've tweaked the configuration.

Use nmap to discover Raspberry Pi on your network:

 nmap 192.168.0.1-255 -p22

Don't worry if you mess up the network configuration, you can always recover with the USB-UART bridge method described above.

Remote shell

For more convenient access to command-line SSH is recommended. You can start by installing OpenSSH server on Raspberry Pi:

apt-get install openssh-server

Assuming Raspberry Pi sits on an DHCP enabled network you can use following on your Linux laptop to connect to remote shell:

ssh pi

Otherwise determine the IP address of your Raspberry Pi, for example with ip addr list:

ssh 192.168.x.x


Make your life easier by setting up public key authentication. Windows users might want to take a look at PuTTYgen instructions.

Note that while you are connected via USB-UART bridge you can simply copy the contents of your public key and paste it to Raspberry Pi as follows:

mkdir -p /root/.ssh
cat - >> /root/.ssh/authorized_keys
# Paste now
# Press Ctrl-D

Beyond installation

Once you are comfortable with Raspberry Pi command line and SSH you can do a lot of cool stuff:

Blinking a LED

A led is connected to each RaspberryPi on to GPIO 18.

Blinking from the terminal

To blink it from terminal you first have to become a root user.

sudo su

Then you have to export gpio number 18

echo 18 > /sys/class/gpio/export

After that is done you have to set the direction of the gpio pin, in this case you have to set it to out direction.

echo out > /sys/class/gpio/gpio18/direction

And now you can blink your led on and off.

# Turn the led on
echo 1 > /sys/class/gpio/gpio18/value

# Turn the led off
echo 0 > /sys/class/gpio/gpio18/value
# blink the led forever with 1 second interval
while true; do echo 1 > /sys/class/gpio/gpio18/value; sleep 1; echo 0 > /sys/class/gpio/gpio18/value; done

Blinking with a Python script

Save this script as a blink.py in the raspberry pi home directory. You can use program called nano to do that

nano blink.py

In nano Ctrl+O writes the file to the disk and Ctrl+X exits nano.

The blink.py script

# import Raspberry Pi gpio support into python
import RPi.GPIO as GPIO
# import a sleep function from time module
from time import sleep

led = 18  # gpio number where the led is connected

# Tell the GPIO module to use gpio numbering used by cpu
GPIO.setmode(GPIO.BCM)
# Set gpio nr 18 to output mode
GPIO.setup(led, GPIO.OUT)

# Blink some leds
while True:
    GPIO.output(led, False)
    sleep(1)  # Sleep for 1 second
    GPIO.output(led, True)
    sleep(1)