RPi-08: LED Hello World

In this tutorial, we’re going to be use Processing running in Raspberry Pi to control an simple LED. The idea being that you could use this tutorial as a starting point in order to get a basic understanding of physical computing through Raspberry Pi, Processing and electronic circuits. This tutorial will explain the code behind the Processing program line-by-line. After this tutorial, you should have a grasp on how basic programs operate and be able to modify our code.

First you’ll need to install Processing.

  1. Installing Processing on Raspberry Pi:
    curl https://processing.org/download/install-arm.sh | sudo sh
    (you’ll need your RPi to be connected to wifi to download Processing from the internet)
  2. Wire up an LED and a 330 ohm resistor to GPIO pin 17, as shown below. If you need more guidance, follow this guide.

    led-gpio17

  3. In your RPi, run Processing (in the main Programming menu) and create a new sketch with the following code:
    import processing.io.*; // use the GPIO library
    
    // store the desired state of the LED in a variable
    boolean ledOn = false; 
    
    void setup() {
      // set pin 17 as an output:
      GPIO.pinMode(17, GPIO.OUTPUT);  
    }
    
    void draw() {
      if (ledOn == true) { // If the desired state is on, then:
    
        // turn the LED on:
        GPIO.digitalWrite(17, GPIO.HIGH);
    
        // and set the background red:
        background(255, 0, 0);
      }
    
      else { // otherwise:
    
        // turn the LED off:
        GPIO.digitalWrite(17, GPIO.LOW);
    
        // and set the background black:
        background(0, 0, 0);
      }
    }
    
    void mouseClicked() {
      // When the mouse is clicked, store the opposite of 
      // ledOn into ledOn, which toggles ledOn:
      ledOn = !ledOn;
    }
  4. Save and Run your Processing sketch. When you click in the window, the background will turn red and the LED will turn on. When you click again, they will both toggle back to their original state.
  5. Now Try wiring up more LEDs and see if you can control them independently by clicking on different parts of your sketch window.

Content for this tutorial from Raspberry Pi website:

https://www.raspberrypi.org/learning/introduction-to-processing/

RPi-07: Remote Desktop with Raspberry Pi

Often all you need to control your Raspberry Pi is an SSH connection, however sometimes it is useful to be able to remotely control your RPi using the mouse and seeing just what you would see on the screen of your RPi.
VNC (Virtual Network Connection) is a standard for doing just this. To use it, you have to install some software on your Pi. There are a number of VNC server applications, and the one we are going to use is called “tightvnc”.
We can install the VNC server software using the SSH connection that we established earlier.
sudo apt-get install tightvncserver

Then we run VNC server:
vncserver :1 -geometry 1024x768 -depth 24

on mac: open screen sharing and type:
vnc://192.168.10.70:5901
(replace the above IP address with your own RPi’s)

If you’d like to run VNC Server automatically when your RPi starts up, look at the following tutorial https://learn.adafruit.com/adafruit-raspberry-pi-lesson-7-remote-control-with-vnc/running-vncserver-at-startup

RPi-06: Shutting Down and Restarting

Overview

When connecting electronics it’s good practice to shutdown and power off your Raspberry Pi.

Shutdown Raspberry Pi

To shutdown your Raspberry Pi from the command line, type:

sudo shutdown -h now or you can also use sudo halt

Restarting your Raspberry Pi

To restart your Raspberry Pi from the command line, type:

sudo shutdown -r now or sudo reboot.

Now the fun begins, let’s hook up an LED and make it blink!

RPi-05: Making Sound with Raspberry Pi

Creating a tone

Raspbian comes with the speaker-test program that allows us to test our speakers by creating tones.

How about you make a tone on your Raspberry Pi with the following command:

speaker-test -t sine -f 600 > /dev/null

Press ctrl+c (that’s the control key and the c key at the same time) to make the damn noise stop!

Not hearing anything? Apart from the obvious check of ensuring the headphones are plugged in all the way, there are a couple of things you can check:

  1. You can tell your Pi whether it should use the headphone or the HDMI port for audio. We can do that by using

sudo raspi-config

again. Choose Advanced Settings and this time choose A6 Audio. Then select option 1 – ‘Force 3.5mm (‘headphone’) jack, and exit raspi-config as usual.

  1. In certain situations, the kernel module required to support audio may not be enabled. We can do that with:

sudo modprobe snd_bcm2835
Now try the speaker test command again.

Text to Speech

Our Raspberry Pi is more than just a tone generator. We can do things like text-to-speech and playing mp3 files. In fact this is what differentiates it from other platforms (such as the Arduino).

To give your Raspberry Pi a voice we must first install some more software called eSpeak.

Install espeak by using the following command:
sudo apt-get install espeak

We’re now ready to give our Raspberry Pi the gift of speech! This is done by running:

espeak “Hello World!” > /dev/null

Playing a .wav file

Before we play some audio, we first need some audio to play with. Use the command line downloader wget to download a sample from the Little Bird Electronics website.

wget https://ia800207.us.archive.org/29/items/MLKDream/MLKDream_64kb.mp3

The Raspberry Pi comes with a piece of software called aplaythat lets you play .wav files.

To play a .wav file run the following command.

aplay ~/sample.wav

We can also play handy calibration sounds; for example:

aplay /usr/share/sounds/alsa/Front_Center.wav

Use your directory traversal skills to have a look around the /usr/share/sounds/alsa directory for some other calibrations sounds you can play.

Ok, you’ve made enough noise! Let’s learn how to hook up some electronics. But first we need to learn how to safely switch off our Raspberry Pi!

RPi-04: Connecting to RPi via SSH

Now that we’ve got our Raspberry Pi connected to the Wi-Fi we can connect to it over the network by using SSH. SSH (Secure Shell) lets you log into a remote machine (such as your Raspberry Pi) and execute commands, but it also supports a whole lot more such as tunnelling, port forwarding and the secure transfer of files (SFTP) and copying files (SCP).

Working out your Raspberry Pi’s IP address

Since we’ve got a console cable, it’s easy for us to find out our Raspberry Pi’s ip address by using the ifconfig (interface config) command. This command lists all our Pi’s network ports (both wired and wireless).

  • en0 is our ethernet port
  • wlan0 is our Wi-Fi module

Run the command ifconfig now and see your output.
rpi_ifconfig
This device has been given the IP: 192.168.3.19 by the wifi router.
If you do not see an inet address, your raspberry pi hasn’t connected to wifi successfully.

To only get our wlan’s IP address we use:

ifconfig wlan0 | grep -i inet

What’s that weird | character you’re seeing and what is that grep command?

The | symbol is known as the pipe character (and it’s found above your return key). On Linux we use the pipe symbol to pipe the output of one command into the input of another. E.g program1 | program2 | program3. So in our example we’re taking the output of the ifconfig command and sending it to grep. The question then beckons, what’s this grep command?

Grep is a command-line utility for searching plain-text data sets for lines matching a regular expression. In layman’s terms it looks for a snippet of text (we call them strings) and outputs any match to the screen. The -i flag or argument to grep tells it to ignore case. Lastly, the inet section is the snippet of text we want to match, if you look at the output of ifconfig you’ll notice that the ip addresses are on the line with the snippet inet.

SSH on Mac or Linux

SSH-ing on Mac or Linux is easy. To SSH onto your Raspberry Pi use the command:

ssh pi@the-ip-address-of-you-pi

E.g. ssh pi@10.0.1.1

Whoa! What’s this? ‘Connection refused’? That’s correct, SSH access is disabled by default, and that’s a good thing, too. If that wasn’t the case, then, as soon as a new RPi appeared on a network, it could be accessed easily with the default login and password. (Speaking of which, let’s change that later on.)

Enabling SSH access

Type:

sudo raspi-config

and you’ll see Setup Options. Select ‘8. Advanced Options’. Then select ‘A4 SSH Enable/Disable’ remote command line access to your Pi using SSH’. Finally, select ‘Enable’.

Press ‘ESC’ a few times until you’re back out in the command line. Now try to SSH again:

ssh pi@the-ip-address-of-you-pi

You may receive a message along the lines of:

The authenticity of host '10.0.1.9 (10.0.1.9)' can't be established. RSA key fingerprint is 64:4a:97:2d:4c:ff:3a:f3:8d:02:c0:e0:38:66:a1:0d. Are you sure you want to continue connecting (yes/no)?

Go ahead and answer yes by pressing the ‘Y’ key.

You’ll get prompted for a password, just use the same password raspberry as you did when logging in via the console cable.

SSH on Windows

SSH isn’t built into Windows, so you’ll need to use putty to connect.

  1. Select SSH, Enter your IP address and leave the port number at 22.
  2. If you get a security alert, click “Yes”.
  3. Enter pi as the username, and raspberry as the password.

Time for a sanity check! Let’s sanity check that you’re on the right Raspberry Pi by playing some audio out of the headset port

RPi-03: Connecting RPi to Wifi

Overview

For these tutorials we will not use the Emily Carr Wifi network as it is problematic, but I recommend using your own hotspot or your own home network:

  • SSID: wifi network name
  • Password: wifi password

To connect to this network from your Raspberry Pi, we’ll need to edit the /etc/network/interfaces file.

We’re going to use nano to do this. Nano is a text editor for Unix-like computing systems or operating environments using a command line interface.

To edit the file type:
sudo nano /etc/network/interfaces

Modify the file so it looks the same as below:

auto lo
 
iface lo inet loopback
iface eth0 inet dhcp
 
allow-hotplug wlan0
auto wlan0
 
 
iface wlan0 inet dhcp
        wpa-ssid "workshop"
        wpa-psk "internet"

Now save the file by using the key combination ctl+O (press the control button and the o button at the same time) Press ‘return’ to confirm the file name. Finally, to exit nano press ctl+X.

We’ll need to restart the network interface in order for it to connect to the network. We can do that with:
sudo /etc/init.d/networking stop

and then
sudo /etc/init.d/networking start

Now that we’re connected to Wifi let’s connect our computer to the RPI

RPi-02: Exploring your RPi

The command prompt

Now that you’ve logged in you’ll see a command prompt with:

pi@raspberrypi ~ $

What does it mean?

  1. The pi section is your username followed by @.
  2. The raspberrypi (the default hostname), the ~ is your current working directory.
  3. The ~ symbol is short hand for your home directory. When you log into your Raspberry Pi, you first land in your home directory.
  4. The tailing $ is your shell prompt, anything you type to the right of it gets executed as commands.

Poking around the home directory

When you first login, you’re in the home directory. The home directory is represented as ~ on the command prompt. The ~ character can be used as a shortcut to jump into your home directory.

To see what’s inside your home directory use the ls command.

Other Directories

Your home directory has the following path /home/pi. There are other folders/directories above your home directory.

Don’t believe me? We can ask Linux to tell us what our current directory is by running:

pwd

you should see the following output:

/home/pi

pwd stands for print working directory.

Changing directory

To change directory use the cd command. What did you think cd stands for?

Moving up a directory

To move up a directory from pi’s home directory to the /home directory use the following command:

cd ..

Now when you print the current working directory, you should see.

/home

To go back to your home directory you would use the command:

cd pi however the following commands could also do the trick:

  • cd /home/pi (specifying the full path)
  • cd ~ (using the ~ shorthand)
  • cd (even typing just cd takes you back to your home directory, the maker of cd sure hates to type extra characters)
  • cd - (the - argument takes us to the directory we were immediately previously in, so this command would also work)

Other directories

Of course there’s more to your Pi than the /home directory. Let’s have an explore.

The folder structure on your Raspberry Pi looks something like this (with your home directory highlighted).’

folder-structure

Directory Description
/ The upper most of your hard disk (or SD Card)
/bin Programs and commands that all users can run
/boot All the files needed for booting your Raspberry Pi
/dev Files that represent devices on your Raspberry Pi
/etc Configuration files
/etc/init.d Scripts to start, stop and otherwise command services
/etc/X11 X11 Configuration Files
/home All the user home directories (except for root’s)
/home/pi The Pi user’s home directory
/lib This is where the Kernel modules / drivers live.
/media There is where hard disks, SD Cards and other removable media are mounted
/proc Virtual directory that provides details of running processes
/sbin Programs primarily used for systems maintenance
/sys Another special folder that represents hardware devices
/tmp A space for temporary files
/usr Programs and data usable by every user
/usr/bin Most programs you’ll run live in here
/usr/lib Libraries to support programs and programming languages
/usr/local By convention software specific to a machine goes here.
/usr/sbin More system software
/usr/share Supporting files that aren’t specific to chip architecture
/usr/src Source code!
/var System logs
/var/backups Typically compressed copies of system logs
/var/cache apt-get and other programs keep their data here
/var/log/ All the system and service logs
/var/mail This is where your mail goes (if you’ve set it up)
/var/spool Data that is waiting to be processed or dealt with lives here (e.g. mail or print jobs)

Ok, enough exploring let’s see what other commands we can do on the Pi!

RPi-01: Connecting RPi via the Console Cable

Overview

Despite having four full sized USB ports and one micro USB port (for power), you can’t connect to your Raspberry Pi from your computer via USB (without additional hardware).

You can however connect to your Raspberry Pi via a “USB to TTL Serial Cable”, also sometimes called a Console Cable. This cable allows you to access the command line of your Raspberry Pi, by plugging the USB end into your computer and the other end into specific serial GPIOs on the Raspberry Pi.

What you’ll need

For this exercise you’ll need a console cable:

Caveat: Make sure your computer delivers at least 500 mA (milliamps) at 5 V (Volts). This will assure you that your RPi will receive the power it needs otherwise there’s a chance you’ll fry your Pi!

Software Installation (Mac)

Mac OS X includes a Terminal (inside /Applications/Utilities/), so the only other software you need to install is the drivers for the cable.

You can download the Mac drivers here.

Software Installation (Windows)

Windows does not include a Terminal application that you can use to connect over serial.

For this workshop we’ll use Putty. You can get the “putty.exe” app from the puttywebsite.

Download the one called “PuTTY” under For Windows on Intel x86 (direct link).

Putty downloads as an uncompressed binary.

Now that you’ve got Putty, you’ll also need to install the cable drivers here.

Software Installation (Linux)

Assuming you’re running a v2.6 kernel or later you shouldn’t need to install drivers for the cable. If you find that your distribution doesn’t have the drivers installed you can download the drivers from the FTDI website.

You may also need to install screen. The screen command (some Linux distributions such as Ubuntu 12.10 don’t include the screen software). To see if you’ve got screen installed type screen and if you get an error install screen by running: sudo apt-get install screen.

Connecting the cable

The console cable handily provides the name of the line on the side of the terminal header.

Connect the console cable to the following pins:

  • DC → RPi Pin 02 (DC Power 5V)
  • GND → RPi Pin 06 (Ground)
  • RXD → RPi Pin 08 (Data Transmit)
  • TXD → RPi Pin 10 (Data Receive)

Connecting via the Shell

Bringing up the Shell On Mac / Linux

TL;DR:

If you’re running Mac OS X all you need to do is open a Terminal window and run screen in the command line using the command:

screen -L /dev/tty.usbserial 115200

screen is the command. /dev/cu.usbserial is the path to your serial device and 115200 is the baud rate (the speed at which we send data across the serial interface).

If you’re on Linux you’ll need to escalate your privileges by putting sudo in front of the command.

E.g:

sudo screen /dev/cu.usbserial 115200

Bringing up the Shell on Windows

If you’re running Windows, you’ll need to find out what com port the console cable is using.

  1. Search for Device Manager
  2. Expand the Ports (COM & LPT) section

You now need to open putty

  1. Select Serial as the connection type
  2. Enter the name of the COM port you’ve found
  3. Set the speed to 115200
  4. Click Open and you should see the terminal

Logging In

Once you’ve brought up the shell, you’ll be faced with a login prompt. You can log into your Raspberry Pi with the following credentials:

  • username: pi
  • password: raspberry

Ok we’re in! Let’s have a look around in the next tutorial.