Raspberry Pi



Raspberry Pi

0 1


A01slides


On Github epic709 / A01slides

Raspberry Pi

WiFi

Makespace.my 2

password = "makesomethingtoday."

Hi There

I'm Adrian

Social entrepreneur and maker enthusiast

Based in KL

Previously enterprise software specialist

Introduction

Raspberry Pi 2 Model B

Image from adafruit.com

Pi? Raspberry?

  • low-cost single-board computer
  • developed by the Raspberry Pi Foundation, UK
  • mainly runs Linux operating systems
  • Pi => Python

Why Raspberry Pi?

  • originally for teaching computer science in schools
  • functions just like a regular PC:browse internet, plays videos and games, word processing
  • easy way to control electrical components such as sensors using software
  • affordably priced at USD35 (≈ RM130)

What's in the Pi 2?

  • quad-core 900MHz ARMv7 processor
  • dual-core VideoCore IV GPU
  • 1GB RAM
  • HDMI output (up to 1920x1200)
  • Built-in Ethernet port
  • 4 USB 2.0 ports

Some history

  • Model A (April 2012)
  • Model B (Oct 2012)
  • Compute Model (April 2014)
  • Model B+ (July 2014)
  • Model A+ (Nov 2014)
  • Version 2 Model B (Feb 2015)

What's not in the box?

  • Display monitor (duh!)
  • Keyboard & mouse (obviously...)
  • Power supply (via Micro USB port)
  • Storage (microSD card)

Connecting to the Pi

Standalone

Raspberry Pi + Monitor + Keyboard + Mouse

Headless

Raspberry Pi + Network + Your Laptop/PC

Networking with the Pi

  • Built-in Ethernet for wired connections
  • Wireless supported with a compatible WiFi dongle
  • We will connect to the Pi through a network using SSH

SSH

  • stands for Secure Shell
  • allows remote login to a networked computer
  • uses encryption to secure data communications
  • involves an SSH server (the Raspberry Pi)and an SSH client (your laptop)

SSH Clients

  • Linux and Mac OS X have a built-in command:ssh
  • Windows users can installPuTTY

   tip: PuTTY GUI client is also available for Linux

Configure laptop

  • Set your Ethernet adapter:
      IP Address: 10.1.1.1
      Subnet mask: 255.255.255.0
      Default gateway: 10.1.1.1
  • Windows7 users: - open Network & Sharing Centre -> change adapter settings - right-click Ethernet adapter -> Properties - double-click TCP/IPv4

Connecting via SSH

  • Each Pi has been assigned a fixed IP address
  • For Linux and Mac OS X, type:ssh pi@10.1.1.10
  • For Windows, run PuTTYHostname = 10.1.1.10, Port = 22Click 'Open'

Basic CLI & Python

Command Line Interface (CLI)

  • pwd    - show current directory

Where am I now?

ls    - list files in current directory

What is inside here?

cd xxxxxx    - change directory

Take me to xxxxxx

More CLI

  • mkdir yyyyyy    - make directory

Create a directory (folder) named yyyyyy

nano myfile.py    - edit file

Open myfile.py in text editor

sudo command    - super user do!

Execute command as super user

example commands are python and nano

Python

  • is a programming language
  • you create a file (called a Python script) that contains a set of instructions
  • these instructions tell the computer what to do, like read input from the keyboard or print some text to the screen
  • you then pass the script to the Python interpreter which will execute those instructions

Hello Python

Lets write a simple Python script to read input from the keyboard and then print it to the screen

Hello Python

1. Create a new directory

mkdir ~/python-intro

2. Change to that new directory

cd ~/python-intro

3. Create a Python script

nano hello.py

Simple Python Program

4. Type in the following

name = raw_input("Hi, what is your name? ")
print("Hello %s, have fun hacking today!" % name)
          

Tip: name stores your keyboard input and %s substitutes it into the line of text inside print

Simple Python Program

5. Save the file by pressing Ctrl+X

6. When asked to confirm, press Y, then hit ENTER

7. Run the Python script by typing:

python hello.py

Modifying a Python Program

To edit your Python script, run the following again

nano hello.py

Try adding another question and response

GPIO and Circuits

What is GPIO

  • General-purpose input/output
  • generic pins on an integrated circuit
  • each pin can be configured as input or output
  • input/output values can be high (1) or low (0)
  • exposed as a 20x2 row of pins

GPIO on the Pi

The GPIO pins are exposed on the 20x2 pin header of the Pi

Image from adafruit.com / cropped from original

GPIO Pinout

Note how some of the pins have a specific function

Voltage Supply Rails

The Pi supplies 2 voltage levels: 5V and 3.3V

What voltage level to use depends on the component

Voltage Supply Rails

  • 2 x 5-volt pins (red)
  • 2 x 3.3-volt pins (orange)
  • 8 x ground pins (grey)

Warning

The GPIO pins on the Pi are NOT 5V tolerant

Never connect a 5V pin directly to any other pin

as this can permanently damage the Pi

Remember

Always ensure the Pi is turned off before making any changes to your circuit

Turning the Pi Off

1. Run this command

sudo halt

2. Wait for the lights to stop flashing

or

Disconnect the USB power cable / switch off the power socket

GPIO Pin Breakout

Image from adafruit.com / modified from original
Image created with Fritzing

Download

all the code we will be using from

www.github.com/epic709/thinkerersA01

We will transfer the code to the Pi using a network share

Network share

  • log on to your Pi
  • from your laptop, open:
\\10.1.1.10\pishare
copy all the files across move the files to thinkerers directory:
mv ~/share/* ~/thinkerers

Controlling an LED with GPIO

Image created with Fritzing

Controlling an LED with GPIO

Get the script

~/thinkerers/05-led-gpio.py

And run it

sudo python 05-led-gpio.py

sudo is required when using GPIO

LED Patterns

Image created with Fritzing

LED Patterns

Get the script

~/thinkerers/05-led-patterns.py

And run it

sudo python 05-led-patterns.py

Reading Input

Image created with Fritzing

Reading a Switch

Get the script

~/thinkerers/06-switch-input.py

And run it

sudo python 06-switch-input.py

Press the switch and watch the output on the screen (the LEDs won't do anything, see next slide)

To quit the program, press Ctrl+C

Reading a Switch

To Control LED Pattern

Get the script

~/thinkerers/06-switch-led.py

And run it

sudo python 06-switch-led.py

Press the switch and watch the LED patterns change

Sensory Madness

(inputs)

Reading Light

Image created with Fritzing

Reading Light

Get the script

~/thinkerers/07-reading-light.py

And run it

sudo python 07-reading-light.py

Move your hand over the LDR and watch the reading change

To quit the program, press Ctrl+X

Reading Temperature

Caution

Before you turn on the Pi

pinch the temperature sensor with your fingers

If it gets HOT, immediately PULL IT OUT

Image created with Fritzing

Some modules need to be enabled

Run this

sudo modprobe w1-gpio
sudo modprobe w1-therm		  
		  

Note: this isn't permanent

Reading Temperature

To check if module detected the sensor

cd /sys/bus/w1/devices

A file with the name "28xxxxxxxx" should exist

Reading Temperature

Get the script

~/thinkerers/08-reading-temp.py

And run it

sudo python 08-reading-temp.py

Touch the temperature sensor and watch the reading change

To quit the program, press Ctrl+C

? Magic Ruler ?

Image created with Fritzing

Sonar Sensor

Get the script

~/thinkerers/08-sonar-sensor.py

Hold the sensor steady and point it at a nearby flat surface

Then execute the script

sudo python 08-sonar-sensor.py

Motor-ing

(outputs)

Image created with Fritzing

Controlling a DC Motor

Script to use:

~/thinkerers/09-dc-motor.py

And run it

sudo python 09-dc-motor.py

Can you reverse the motor direction using only code?

Caution

Take care removing the L293D

Slowly wedge it out by gripping the sides

Try not to bend the sharp pins

Controlling a Servo Motor

  • The Pi isn't actually good for servo control
  • Only one hardware PWM pin - GPIO18 (pin 12)
  • Linux OS interrupts the pulse generated
  • Both software and hardware solutions available

tip: check out ServoBlaster

Image created with Fritzing

Controlling a Servo Motor

Script to use:

~/thinkerers/09-servo-motor.py

Execute the script

sudo python 09-servo-motor.py

Enter angle between 0 and 180 degrees

Automation

Input + Output = AUTOMATION

Try to combine what you just learnt

Can you control the DC motor based on light?

Example: bright = move forwards

    dim = move backwards

Controlling a Motor

Based on Light

Open the *untested* script

~/thinkerers/10-light-dc-motor.py

Fix it! Then

sudo python 10-light-dc-motor.py

Slowly cover / uncover the LDR and see if you can reverse the motor's direction

Image created with Fritzing

Remote Monitoring and Control

Run a Simple File Server

Create a directory

mkdir ~/website

Change to that directory

cd ~/website

Create a test file, enter some text, and save it

nano test.txt

Run this

sudo python -m SimpleHTTPServer 80

Run a Simple File Server

Enter your Pi's IP address into your browser's address bar:

http://10.1.1.XXX/

Can you see the test file you created?

Basic Web Server

Get the script

~/thinkerers/11-basic-server.py

And run it

sudo python 11-basic-server.py

In your browser, go to

http://10.1.1.XXX/any-text-you-like

Web Server + Sensor

Load temperature sensor modules

sudo modprobe w1-gpio
sudo modprobe w1-therm		  
		  

Check if module detected the sensor

cd /sys/bus/w1/devices

A file with the name "28xxxxxxxx" should exist

Image created with Fritzing

Get the script

~/thinkerers/11-sensor-server.py

And run it

sudo python 11-sensor-server.py

Access your Pi's IP address in your browser

After the Workshop

Changing The Pi's IP Address

Edit the configuration file

sudo nano /etc/network/interfaces

Modify these lines to match your local network

address 10.1.1.XXX
gateway 10.1.1.1
          

For example:

address 192.168.0.XXX
gateway 192.168.0.1
          

Changing The Wireless Network

Edit the configuration file

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

Modify the ssid and psk values to match your local network

For example:

ssid="MyWirelessSSID"
psk="MySecretPassword"
          

Recommended resources

Too many to choose!

The End

Thanks for coming!

mind doing a short survey?

This work by Adrian Lai is licensed under aCreative Commons Attribution-ShareAlike 4.0 International License

Python code samples provided are free of known copyright restrictions

The Raspberry Pi and the Raspberry Pi Logoare trademarks of the Raspberry Pi Foundation

All other registered trademarks are property of their respective owners