Skip to content

Step by Step: Raspberry Pi

Greg Toth edited this page Mar 20, 2017 · 21 revisions

Step by Step: Raspberry Pi

There are many different ways to set up and use a Raspberry Pi and it can be confusing because there are so many options. The instructions below describe the typical things we do when preparing and using a Raspberry Pi for sensor applications.

Luckily these steps only have to be done once when first setting up a Raspberry Pi.


1. Download Raspbian Operating System

This step downloads the operating system software for your Raspberry Pi. The operating system is the core software that makes your Raspberry Pi work. There are several flavors and versions of operating systems available for the Pi and the flavor we use is called "Raspbian Wheezy."

  1. Web browse to https://www.raspberrypi.org/downloads/raspbian/
  2. Look for the Raspbian Wheezy section and click the Download ZIP button. It should starting downloading a large ZIP file (about 1GB) to your hard drive. The filename should look similar to this: 2015-05-05-raspbian-wheezy.zip
  3. (optional) Verify SHA-1 signature on the downloaded file matches the SHA-1 signature shown on the download page.
  4. Unzip the downloaded ZIP file which will generate an IMG file. The filename should look similar to this: 2015-05-05-raspbian-wheezy.img

2. Program MicroSD Card

This step transfers the operating system software you just downloaded onto a MicroSD card that you can plug into a Raspberry Pi so it will boot up when you power it up. The steps to prepare a MicroSD card differ between Mac and Windows.

Using Mac computer:

BE CAREFUL HERE! If you use the wrong disk number you could wipe out your entire Mac computer's hard drive. It's best to have someone who is knowledgeable in Mac system administration help with this.

  1. Get a blank MicroSD card that is 4GB or 8GB in size.
  2. Insert the MicroSD card into your Mac using a USB adapter or a MicroSD adapter that converts the MicroSD to a full-size SD card.
  3. Open the Terminal program.
  4. Type the following command:
    • diskutil list
  5. You should see a list of disks in the form /dev/diskN where N is a number starting at 0 and increasing by 1.
  6. Look for the disk that shows "DOS_FAT_32 NO NAME" as part of its description. That is the MicroSD card in its blank state. Any disks that includes "Apple" anywhere it its information is one of the Mac's hard disks and you don't want to use that.
  7. Our example here will assume the MicroSD card is /dev/disk3. This is only for this example...your disk number may be different.
  8. Use the 'cd' command to change into the directory where you unzipped IMG file resides.
  9. These are the commands to prepare a MicroSD card located on /dev/disk3: (yours will likely be a different number):
    • diskutil list
    • diskutil unmountdisk /dev/disk3
    • sudo dd if=2015-05-05-raspbian-wheezy.img of=/dev/rdisk3 bs=1m
      • You'll be prompted for a password; enter your Mac root password.
      • It will take a while for the MicroSD card to be loaded. If your MicroSD adapter has a light you may see it flashing while it's being loaded. When the loading is done your terminal prompt will return to $.
    • diskutil unmountdisk /dev/disk3
  10. At this point you can remove the MicroSD card from your Mac computer.

Using Windows computer:

(to be added)


3. Boot MicroSD Card on Raspberry Pi

This steps puts the MicroSD card you just prepared into the Raspberry Pi's memory card slot and powers up the Pi.

  1. Insert the MicroSD card into the Raspberry Pi's memory card slot, which is on the bottom of the board. Make sure it is fully engaged in the slot.
  2. Connect your Raspberry Pi to an Ethernet network using a network cable.
  3. (optional) Connect an HDMI monitor and USB keyboard to the Raspberry Pi's HDMI and USB ports. This is only needed if you want to set up the Pi using an HDMI monitor and regular USB keyboard. If you don't do this, you'll be logging into the Pi over the Ethernet network using 'ssh' program on Mac or 'PuTTY' program on Windows.
  4. Connect your Raspberry Pi power supply and turn it on. You should some lights flash on the Raspberry Pi.
  5. Give the Pi about a minute to fully boot up, at which point the lights on the Pi should stop flashing.

4. Log Into the Raspberry Pi

This step logs into the Pi so you can issue commands on the Pi itself.

There are three different ways to log into a Raspberry Pi and the choice depends on how you've connected it. The three methods are:

  1. ssh over Ethernet or Wifi network
  2. HDMI monitor and USB keyboard
  3. Serial port

For the description below we'll use the first method: ssh over Ethernet or Wifi network.

Using Mac computer:

  1. Connect your Mac computer to the same LAN network where your Pi is connected.
  2. Open the Terminal program.
  3. Determine the IP address of the Pi. This step is a little tricky and there are different ways to do it. Your network system administrator may be able to help you the first time. Here are a few example ways:
  4. Here we'll assume that the Pi is at IP address 192.168.0.131
  5. The default username and password for a fresh Raspberry Pi is:
    • Username: pi
    • Password: raspberry
  6. Use the following command in Terminal to log into a Pi at address 192.168.0.131:
    • ssh pi@192.168.0.131
    • Answer yes if you get a message asking if you want to continue connecting
    • When prompted for a password, enter raspberry
  7. If you successfully log into the Pi you should see several messages that look something like this:
    • Linux raspberrypi 3.18.11-v7+ #781 SMP PREEMPT Tue Apr 21 18:07:59 BST 2015 armv7l
    • The programs included with the Debian GNU/Linux system are free software;
    • the exact distribution terms for each program are described in the
    • individual files in /usr/share/doc/*/copyright.
    • Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
    • permitted by applicable law.
    • NOTICE: the software on this Raspberry Pi has not been fully configured. Please run 'sudo raspi-config'
    • pi@raspberrypi ~ $
  8. If you see the messages above then you've successfully logged into the Pi over the network. The text pi@raspberrypi ~ $ is called the Command Prompt and it means the operating system is waiting for you to type in a command.
  9. Display the hostname and IP address of your Pi using thes commands and make a note of it. It might change over time depending on your LAN networking environment, but it should generally be stable for the rest of today.
    • hostname
    • hostname -I

Using Windows computer:

(to be added)


5. Configure Raspberry Pi Base Settings

This step sets up some initial configuration details and only needs to be done the first time you're setting up a fresh Pi.

When you enter commands, you should see a command prompt pi@raspberrypi ~ $ on the left side of your terminal window. That means the Pi is waiting for you to enter a command followed by return.

  1. sudo raspi-config
    • Select Expand Filesystem and press Enter
    • At the prompt press Enter
  2. Use arrow keys to select Internationalisation Options and press Enter
    • Select Change Locale and then press Enter
    • You'll see a large pick list that is navigated using the up/down arrow keys
    • Arrow down and eventually you should see a line with [*] showing: en_GB.UTF-8 UTF-8
    • If you're in the US, use your down arrow key to put the cursor on en_US.UTF-8 UTF-8
    • Press the Space Bar to make [*] appear next to en_US.UTF-8 UTF-8. Each time you press the Space Bar it will toggle the '*' on or off and you want it to be on.
    • Press Tab key to move the selector to <Ok> and then press Enter
    • If you're in the US, use arrow keys to highlight en_US.UTF-8 to select that as the Default locale for the system environment and then press Enter
    • You should see some messages about generating locales, and then you'll be back to the menu.
    • Select Internationalisation Options and then press Enter
    • Select Change Timezone and then press Enter
    • Scroll through the menus to select your timezone, e.g. US > Eastern
    • After the timezone is set you'll be returned to the main menu
    • Press Tab key until <Finish> is highlighted, then press Enter to exit the configuration menu
    • Select <Yes> at the "Would you like to reboot now?" message
    • The Pi should start rebooting and you'll be disconnected from your ssh connection
  3. Give the Pi 30-45 seconds to reboot. During the reboot you should see a flashing light on the Pi indicating disk activity. When the reboot is done it should stop flashing.

6. Update Raspbian Operating System

The Raspbian operating system is periodically updated for bug fixes, security patches, and program enhancements. This step will apply the latest available updates to your Pi. You can perform this step periodically in the future as well, if you want to keep your Pi operating system software updated.

  1. Log into your Pi.
    • Use the same ssh pi@192.168.0.131 command from Terminal and raspberry default password.
  2. Update operating system packages by issuing these commands on the Pi:
    • sudo apt-get update
    • sudo apt-get upgrade
      • Answer Y to the "Do you want to continue [Y/n]?" prompt
    • You'll see a variety of messages as the operating system is updated, and it may take a little while since this is the first time you're updating the Pi.
    • When finished you'll be back at the pi@raspberrypi ~ $ command prompt.
  3. Now reboot the Pi so it comes up fresh with the updates you just loaded:
    • sudo reboot

7. (optional) Update Raspberry Pi Firmware

This optional step updates the firmware in your Raspberry Pi. The firmware is low-level machine code on the Pi that controls the Pi hardware. This step is not mandatory and your Pi should work fine without doing it, but if you want the latest updates then perform this step.

  1. Log into the Pi.
  2. Run the following command on the Pi to update the firmware:
    • sudo rpi-update
  3. Run the following command to reboot the Pi and activate the new firmware:
    • sudo reboot

8. (optional) Enable Raspberry Pi I2C Bus

This step enables the I2C bus on the Pi, which is used to connect to external sensors. I2C is a type of data communications interface that the Pi uses to talk to attached sensors. This is an optional step that will be automatically performed when you install the GrovePi+ software, so if you will be installing GrovePi+ you can skip this step.

  1. Log into the Pi.
  2. sudo apt-get install i2c-tools python-smbus
  3. sudo raspi-config
  4. Select Advanced Options and then press Enter
  5. Select I2C and press Enter
  6. Press Tab key to select <Yes> and then press enter
  7. At the prompt press Enter
  8. At the prompt for "Would you like the I2C kernel module to be loaded by default?" prompt select and then press Enter
  9. At the prompt press Enter
  10. Press Tab to select <Finish> and then press Enter
  11. At the "Would you like to reboot now?" prompt select <No> and then press Enter
  12. Edit the /etc/modules file and add the following line at the end if it's not already there (edit the file using sudo nano /etc/modules or sudo vi /etc/modules):
    • i2c-dev
    • After adding this line, save the file and exit the editor
  13. Reboot the Pi:
    • sudo reboot
  14. After reboot is finished, log back into the Pi.
  15. Verify the i2c interface is present:
    • sudo i2cdetect -l (that is lower-case L, not the digit 1)
    • You should see something like this:
      • i2c-1 i2c 3f804000.i2c I2C adapter

9. Set Up Python Tools

  1. Log into your Pi.
  2. Check your Python version:
    • python --version
      • It should report: Python 2.7.3
  3. At the command prompt:
    • sudo apt-get install python-pip
      • Answer Y at the "Do you want to continue [Y/n]?" prompt

10. Load the IoT Education Source Code

  1. Log into your Pi.
  2. Display your current directory using the pwd command. It should say /home/pi and then is the "home directory" for the 'pi' user account on your Pi. This is where you'll be placing programs that you'll be working with.
  3. Clone the IoT Education github site using this command:
    • git clone https://github.com/IoTDevLabs/iot-educ.git
    • You should see a message saying Cloning into 'iot-educ'
  4. List files in your home directory by typing the ls command. You should see two directories:
    • iot-educ - these are the IoT & Education project source code files
    • python_games - these are some programs that are included in the base Raspbian operating system
  5. Change into the iot-educ directory using these command:
    • cd iot-educ
    • ls
    • cd rpi
    • ls
  6. The ls command lists the files in the current directory, so you can see what's there.
  7. Verify you're in the /home/pi/iot-educ/rpi directory by typing this command:
    • pwd
      • Should display /home/pi/iot-educ/rpi
    • If not, change into the right directory by using this command:
      • cd /home/pi/iot-educ/rpi
  8. Install the supporting Python packages by typing this command (be sure to include the period at the beginning):
    • ./install-python-packages.sh

11. Run Your First IoT Education Program

The IoT Education programs are a series of software programs that interface with various sensors and actuators. The programs can easily be modified, combined and remixed in different ways for your own sensor designs. Many of the programs also interface with Pi Land which is a real-time data bulletin board where live sensor data can be posted and viewed through a web browser.

  1. Log into your Pi.
  2. Change to the program source code directory using this command:
    • cd /home/pi/iot-educ/rpi
  3. Run the program that reads and displays your Pi's CPU temperature:
    • python read-cpu-temp.py
    • You should see the temperature displayed in both C and F
  4. Run the program that continuously reads the Pi's CPU temperature and writes it to Pi Land:
    • python cpu-temp-piland.py
    • The default room number where your temperature is written is coded in the program as Room 404. Other people may be using that same room, so you may want to pick a different room number that no-one is using. You can easily change the room number by editing the program and changing the 'room' variable to a different room number, e.g. 414, 424, 474, etc.
    • When the program is running, you should see a line of text something like this each time another measurement is taken:
      • http://piland.socialdevices.io/404/write/1?name=Pi+CPU+Temp&value=36.318+C
  5. You can view your live data by web browsing to Pi Land room 404 (or the room you changed to) which is at the following URL:
  6. To stop the running program, press Control-C on the keyboard and it should terminate the program and return you to the command prompt.
  7. Remember that other people might be using the same room number, so you may see other data in the room. You can easily change the room number and data slot number by editing the cpu-temp-piland.py program source code and changing the room and/or slot variables (around 1/3 down from the top of the file) to different numbers.
  8. If you blow on the Raspberry Pi you may be able to make it a little hotter or colder and see the temperature change. This example program uses the temperature sensor that's built into the Raspberry Pi's CPU chip. It's not very useful for real-world temperature sensing but it does offer an easy way to get your first program running and connected to the Pi Land data exchange dashboard.

12. Add GrovePi+ Board and Software

This step installs the GrovePi+ board which is what allows you to connect a variety of sensors and actuators to the Pi. Instructions are adapted from the GrovePi+ setup instructions.

The current GrovePi installation programs assume you're installing under /home/pi/Desktop directory and the instructions below follow that assumption.

  1. Log into your Pi.

  2. pwd

    • Current directory should be /home/pi
    • If not, change to your home directory with this command: cd /home/pi
  3. git clone https://github.com/DexterInd/GrovePi

  4. cd GrovePi/Script

  5. sudo chmod +x install.sh

  6. sudo ./install.sh

  7. When you see "Press ENTER to begin... or ctrl+c to abort" press Enter to proceed.

  8. Enter Y at the "Do you want to continue [Y/n]?" prompt.

  9. A series of messages will be displayed as the installation proceeds.

  10. After the installation finishes the Pi will automatically reboot.

  11. Log into your Pi.

  12. Shut the Pi down gracefully by issuing this command:

    • sudo halt
  13. Wait for the Pi's lights to stop blinking, which means it has stopped writing to the disk.

  14. Unplug the Pi power supply so the Pi is completely turned off.

  15. Install the GrovePi+ board by connecting it to the Raspberry Pi's GPIO header. Make sure the pins are aligned properly.

  16. Power on the Pi and wait until the lights stop blinking during boot-up.

  17. Log into your Pi.

  18. Issue the following command to verify the GrovePi+ is detected:

    • sudo i2cdetect -y 1 (that is the digit 1)
  19. You should see the following output. The 04 in row 00 column 4 indicates the GrovePi+ is connected and responding. You may also see numbers in other rows and columns.

          0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
     00:          -- 04 -- -- -- -- -- -- -- -- -- -- -- 
     10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
     20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
     30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
     40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
     50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
     60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
     70: -- -- -- -- -- -- -- --
    
  20. You can test the GrovePi+ using a Grove LED module:

    • Connect Grove LED (e.g. red or green LED) to port D4 on the GrovePi+ using a Grove cable.
    • cd /home/pi/GrovePi/Software/Python
    • sudo python grove_led_blink.py
    • If the LED blinks on and off your GrovePi+ install installed and working.
    • Press Control-C on the keyboard to stop the blink program.
  21. Now install the GrovePi+ library so it can be used from anywhere on the Pi:

    • cd /home/pi/GrovePi/Software/Python
    • sudo python setup.py install

13. (optional) Update the GrovePi+ Firmware

This is an optional step that will update your GrovePi+ firmware to the latest available version.

  1. Make sure the GrovePi+ board has been previously installed and tested.
  2. Disconnect all sensor and actuator components from the GrovePi+ board (leave the board attached to the Pi).
  3. Power up the Pi with the GrovePi+ board in place.
  4. Log into your Pi.
  5. cd /home/pi/Desktop/GrovePi
  6. sudo git fetch origin
  7. sudo git reset --hard
  8. sudo git merge origin/master
  9. cd /home/pi/Desktop/GrovePi/Firmware
  10. sudo chmod +x firmware_update.sh
  11. sudo ./firmware_update.sh
    • Press Y at the "Do you want to update the firmware? [y,n]" prompt
    • Press Enter at the "Press any key to start firmware update" prompt
  12. The firmware update will proceed you'll see status messages as it progresses.
  13. If you see avrdude: safemode: Fuses OK followed by avrdude done. Thank you. at the very end then your firmware has been successfully updated.

14. Interface Grove Button to Pi Land

This steps connects a push button to your Pi and interfaces your button presses with a Pi Land data room.

  1. Connect a Grove button to port D3 on the GrovePi+ using a Grove cable.
  2. Log into your Pi.
  3. cd /home/pi/iot-educ/rpi
  4. The program defaults are room 404 and slot 11. If you want to change these, edit the file grove-button-piland.py and change them to something different.
  5. sudo python grove-button-piland.py
  6. View the live data room by web browsing to Pi Land Room 404. If you changed your program to a different room then go to that room instead.
  7. Watch Slot 11 in the data room. When you press the button it should change to "on" and when you release the button it should change to "off".
  8. Press Control-C on the keyboard to stop the program.

15. Interface Grove DHT11 Temperature & Humidity Sensor to Pi Land

  1. Connect Grove DHT11 temperature & humidity sensor to port D8 on the GrovePi+ using a Grove cable.
  2. Log into your Pi.
  3. cd /home/pi/iot-educ/rpi
  4. The program defaults are room 404 and slots 24 and 25. If you want to change these, edit the file grove-dht11-piland.py and change them to something different.
  5. sudo python grove-dht11-piland.py
  6. View the live data room by web browsing to Pi Land Room 404. If you changed your program to a different room then go to that room instead.
  7. Watch Slots 24 and 25 in the data room. Temperature should be in slot 24 and humidity in slot 25.
  8. Blow on the DHT11 sensor and you should see the temperature and/or humidity rise and fall.
  9. Press Control-C on the keyboard to stop the program.

16. Interface Grove DHT22 Temperature & Humidity Sensor to Pi Land

  1. Connect Grove DHT22 temperature & humidity sensor to port D7 on the GrovePi+ using a Grove cable.
  2. Log into your Pi.
  3. cd /home/pi/iot-educ/rpi
  4. The program defaults are room 404 and slots 22 and 23. If you want to change these, edit the file grove-dht22-piland.py and change them to something different.
  5. sudo python grove-dht22-piland.py
  6. View the live data room by web browsing to Pi Land Room 404. If you changed your program to a different room then go to that room instead.
  7. Watch Slots 22 and 23 in the data room. Temperature should be in slot 22 and humidity in slot 23.
  8. Blow on the DHT22 sensor and you should see the temperature and/or humidity rise and fall.
  9. Press Control-C on the keyboard to stop the program.

To be added:

  • Additional sensors and actuators
Clone this wiki locally