Makespace.my 2
password = "makesomethingtoday."
I'm Adrian
Social entrepreneur and maker enthusiast
Based in KL
Previously enterprise software specialist
Raspberry Pi 2 Model B
Raspberry Pi + Monitor + Keyboard + Mouse
Raspberry Pi + Network + Your Laptop/PC
tip: PuTTY GUI client is also available for Linux
IP Address: 10.1.1.1 Subnet mask: 255.255.255.0 Default gateway: 10.1.1.1
Where am I now?
ls - list files in current directoryWhat is inside here?
cd xxxxxx - change directoryTake me to xxxxxx
Create a directory (folder) named yyyyyy
nano myfile.py - edit fileOpen myfile.py in text editor
sudo command - super user do!Execute command as super user
example commands are python and nano
Lets write a simple Python script to read input from the keyboard and then print it to the screen
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
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
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
To edit your Python script, run the following again
nano hello.py
Try adding another question and response
The GPIO pins are exposed on the 20x2 pin header of the Pi
Note how some of the pins have a specific function
The Pi supplies 2 voltage levels: 5V and 3.3V
What voltage level to use depends on the component
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
Always ensure the Pi is turned off before making any changes to your circuit
1. Run this command
sudo halt
2. Wait for the lights to stop flashing
Disconnect the USB power cable / switch off the power socket
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
\\10.1.1.10\pisharecopy all the files across move the files to thinkerers directory:
mv ~/share/* ~/thinkerers
Get the script
~/thinkerers/05-led-gpio.py
And run it
sudo python 05-led-gpio.py
sudo is required when using GPIO
Get the script
~/thinkerers/05-led-patterns.py
And run it
sudo python 05-led-patterns.py
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
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
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
Before you turn on the Pi
pinch the temperature sensor with your fingers
If it gets HOT, immediately PULL IT OUT
Run this
sudo modprobe w1-gpio sudo modprobe w1-therm
Note: this isn't permanent
To check if module detected the sensor
cd /sys/bus/w1/devices
A file with the name "28xxxxxxxx" should exist
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
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
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?
Take care removing the L293D
Slowly wedge it out by gripping the sides
Try not to bend the sharp pins
tip: check out ServoBlaster
Script to use:
~/thinkerers/09-servo-motor.py
Execute the script
sudo python 09-servo-motor.py
Enter angle between 0 and 180 degrees
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
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
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
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?
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
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
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
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
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"
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