A collection of examples in Python, Ruby, and Node, introducing the use of the Raspberry Pi GPIO interface. Slides and more information at: http://bit.ly/1wTL57E.
Make an LED blink 5 times
sudo python python/01-blink.py
sudo ruby ruby/01-blink.rb
node node/01a-blink.js
node node/01b-blink-async.js
- High five if: the LED blinked 5 times
- Can you...
- Make it blink more times?
- Make it blink faster/slower?
Read input from a button/switch
sudo python python/02a-switch.py
sudo python python/02b-switch-async.py
sudo ruby ruby/02-switch.rb
gpio mode 1 down
node node/02-switch.js
- High five if: you see "Button pressed" in the terminal whenever you press the button
- Can you...
- Output a count of how many times the button has been pressed?
- Only display the text for every other button press?
Blink an LED based on input from a button/switch
sudo python python/03-switch-led.py
sudo ruby ruby/03-switch-led.rb
gpio mode 1 down
node node/03-switch-led.js
- High five if: the LED blinks whenever you press the button
- Can you...
- Keep the LED on until the button is pressed again?
Play an MP3 file based on input from a button/switch
mkdir /home/pi/audio
wget http://goo.gl/MOXGX3 -O /home/pi/audio/example.mp3 --no-check-certificate
- Run one of the following code samples (hit Ctrl+C when done to stop running it)
sudo python python/04-switch-audio.py /home/pi/audio/example.mp3
sudo ruby ruby/04-switch-audio.rb /home/pi/audio/example.mp3
gpio mode 1 down
node node/04-switch-audio.js /home/pi/audio/example.mp3
- High five if: the MP3 plays whenever you press the button
- Can you...
- Make it play your favorite song?
- Setup a drum kit using multiple buttons?
Stream button input to a web page (Full tutorial: https://learn.adafruit.com/node-embedded-development/why-node-dot-js)
eth0: ...
inet 192.168.1.21/24 ...
- Run the following (hit Ctrl+C when done to stop running it)
gpio export 18 in
node node/05-switch-stream.js
- Go to http://[your_ip_address]:8080/ to view button logging
- High five if: the web page is appended with a "1" whenever the button is pressed down, and a "0" when it's released
- Can you...
- Edit the Node example "02 - Switch" to keep track of the number of button presses and publish the count to a web page? Hint: instead of streaming input with
button.pipe(res), write the current count as a string to the HTTP response and callres.end(). - Download/install ngrok and run
./ngrok 8080to make your page available on a public internet address.
- Edit the Node example "02 - Switch" to keep track of the number of button presses and publish the count to a web page? Hint: instead of streaming input with
Use the Google Calendar API to pull event data and light an LED if a room is reserved on the calendar (i.e., event is currently in progress)
- Build the circuit

- Go to https://console.developers.google.com/project to create a new project

- Enable the Calendar API

- Create an API access key

- Create a Google Calendar with events and find its ID under "Calendar Settings". Important: make sure that the calendar has public access.

- Update the
CALENDAR_IDandAPI_KEYvalues
CALENDAR_ID = "__YOUR_CALENDAR_ID__"
API_KEY = "__YOUR_API_KEY__"
- Run the following (hit Ctrl+C when done to stop running it)
cd /home/pi/gpio-examples/ruby
mkdir tmp
sudo ruby 06-room-reservations.rb
- High five if: the LED is lit when there's an event in progress and otherwise is turned off. Note that there may be up to a 30-second delay from the time an event is updated on the calendar to the time the LED switches on or off. This prevents us from bombarding the server with too many API requests.
- Can you...
- Only change the LED when the event is held in a particular location?
- Add a green LED that lights up when there's no event in progress?
Email me if you have any questions.
Valerie Concepcion
valerie.concepcion@gmail.com




