Teaching Notes (extremely technical) detailing my Raspberry Pi program. This is for the second set of courses- LEDs, programming, and putting the two together.
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Course set two full notes
1. Raspberry Pi: a course in several parts
Part II: Lights and Sound
2-1: Lights
Objectives:
Safety review
Build a circuit
Enhance that circuit
Set up the Pi kits, as per normal. Do connect the ribbon cable to the Pi, but leave the
breadboard disconnected. No software is needed for this part, so log in to the Pi or
not, but do not start the X Windows System.
Review electrical safety. Don't get zapped, don't fry the Pi, go home in the same
condition you came.
Start by building the same LED circuit as in session 1-2 – we will use this as a basis for
expansion. The basic connections are as follows:
3.3V to +
+ to 160-ohm resistor
Resistor to LED (long lead)
LED (short lead) to -
- to GND
Note that it's easier to leave the LED leads straight, just angle them slightly to that they
can be plugged into separate rows. Resistors, however, need to be bent, and are
best bent fairly close to the body so they don't take up much horizontal space. Also
note that resistors will conduct current in either direction, but LEDs, being diodes, will
only work in one direction. Attach the ribbon cable to the breadboard and watch it
glow.
Disconnect the ribbon cable from the breadboard. Now set up a second LED circuit
in similar fashion, but with a second resistor. The + and – bars can be reused.
+ to 160-ohm resistor
Resistor to 2nd resistor
2nd resistor to LED (long lead)
LED (short lead) to -
Plug the ribbon cable into the breadboard and watch both LEDs glow. Note that the
second one is not as bright as the first. Unplug the ribbon cable from the breadboard
again.
2. Now let's add a button. The buttons included with the kit have rather short, sharp
fangs, and they can be very difficult to get in and out of the breadboard. They seem
to be easiest to remove when installed on the bottom rows, so that it is much easier to
get a fingernail under the component.
Position the button so its connections are in 28E, 30E, 28H, and 30H.
When the button is pressed, it completes the circuit. So, we want the button placed
in the circuit so it interrupts the flow unless it is pressed. Change the connections for
one of the LEDs as follows:
+ to 28A
30A to resistor
Now plug in the breadboard. Notice that the LED no longer glows. Push the button,
and it should instantly come on!
Tricky bits: the white LED
If you have time, this can be fun to explore. The 4-pole white LED is actually a
combination RGB (red/green/blue) LED. The colors blend, so it visually can take on
most any hue when wired appropriately.
The poles have the following roles:
Shortest (all the way on one end) = green input
Longest (second, next to the shortest)= to GND – this is opposite of the single-
color LEDs
Second longest (third, other side of the longest) = blue input
Second shortest (all the way on the other end) = red input
Treat the colors as three separate LEDs. Wire three sets of + to 160-ohm resistor, and
wire each resistor to one of the red/green/blue inputs. Wire the long pole to -.
Connect the ribbon cable to the breadboard and it should glow white. Now
disconnect power, pull one of the red/green/blue input wires out, and reconnect
power. You should see a new color!
When you pack it up, disconnect the breadboard first, log in (pi, library), issue the
“sudo shutdown -h now” command, and when it's down, remove power from the Pi.
Make sure to straighten any bent leads for easier storage and inventory all the
components before calling it a day – each kit will need all of its LEDs and resistors for
future sessions.
3. 2-2: Sound
Objectives:
Create a circuit whose state can be detected
Use Scratch to play a sound when the button is pressed
This session leverages ScratchGPIO and basic principles from
http://simplesi.net/scratchgpio/scratchgpio-1st-project. If you have not installed this
on the kits already, please do so.
Counting pins on the breakout board becomes important here. Start from the top left
(3V3, row 1) and count ACROSS then DOWN. 3V3 is pin 1, 5V on the other side is pin
2, SDA in row 2 is pin 3, the second 5V on the other side is pin 4, SCL is pin 5, GND in
row 3 is pin 6, and so on. There are 26 pins on the model B breakout.
Connect headphones to the Pi. Log on to the Pi, and start the X Windows System via
the startx command. Launch ScratchGPIO6. Start by making sure Scratch can play
a sound. Drag the “play sound” element to the script area, and click it. You should
hear the sound (meow by default).
Now let's create a circuit to control the sound. All we really need is a button.
Wire pin 7 (which is confusingly labeled P4) to 28A
Put a switch in 28E and 30E
Wire 30A to -
Wire – to GND (13A)
In order to tell what the button is doing, we need to check it periodically. Put the
following script together:
Start with a “forever” from the Control category
Inside the forever, place a “say … for 2 secs” from the Looks category
4. Inside the say, place a “slider sensor value” from the Sensing category
Change the “slider” in the sensor value to “pin7”
Click the forever to start it. It should say 1 when the button is not pressed, 0 when the
button is pressed. Now that we know how to detect what the button is doing, let's
connect the pieces together.
Start with a “forever if” from the Control category
Inside the condition of the “if … then” place a “< _ = _ >” operator from the
Operators category
On one side of the equals operator, put the number zero.
On the other side of the equals operator, put the “pin7 sensor value” from the
Sensing category
Inside the body of the “forever if” place a “play sound” from the Sound
category
Customize the sound if you like. If you want a visual indicator as well, place a “say …
for 2 secs” from the Looks category in the body of the “forever if” block.
Congratulations, you have a working button that does something useful!
Tear down as normal. Make sure all the components stay with their kits.
2-3: Lights AND sound
Objectives:
Build a bunch of LED circuits
Use a software package to synchronize lights & sound
This session makes use of lightshowPi (http://www.lightshowpi.com/)for its
synchronized awesomeness. Make sure it is installed ahead of time, preferably in a
consistent, simple location, such as ~/lightshowpi as specified on their site, and
consider changing the default preshow delay from 30 to something smaller, like 10 or
3. Also, if you have other music that you wish to use (without violating copyright, of
course), put it in an appropriate location on the device. The sample directory
(~/lightshowpi/sample/music)is as good a place as any.
Set up the Pi kits as normal. Attach headphones. Do log in, but do not start the X
Windows System.
For this project, we are going to set 8 strings of Christmas lights … well, 8 LEDs, at
least … to music. There is plenty of room on the breadboard for this, although space
utilization will be important. Wire eight simple LED circuits, but instead of taking power
from +, we will take power from the various pins on the GPIO breakout.
Pin to resistor
Resistor to LED
LED to -
5. - to GND
Note that there is no need for the 3V3 or + in this project. It will be easier to correlate
the sounds with what the lights are doing if the LEDs are wired in a regular fashion, but
technically the order does not matter at all. Also note that the 4-pole RGB LED can
be substituted in for three of the channels. The GPIO pins to use, in order, are as
follows:
1. P17 (6A)
2. P18 (6J)
3. P27 (7A)
4. P22 (8A)
5. P23 (8J)
6. P24 (9J)
7. P25 (11J)
8. P4 (4A)
Now that the wiring is complete, double-check it to make sure that the connections
are correct. Since the breadboard probably has components close together, make
sure there are no bits of metal touching each other, since they could cause short
circuits.
Use the software to run a test. From the command prompt, assuming you have
installed the software per the instructions, do the following:
cd ~/lightshowpi
sudo py/hardware_controller.py –state=flash
Notice the tilde before the forward slash in the cd (change directory) command.
Also notice that the “state” flag has two dashes in front of it. These commands will
initiate a hardware test, causing each LED to flash in turn. If any LEDs do not light up,
or if they are not illuminating in the desired order, check your connections.
Ready for the real thing? Do the following:
sudo py/synchronized_lights.py –file=sample/music/<your choice of music>.mp3
Turn off the lights, put on your headphones, and enjoy the collective glowing fruits of
your labor!
6. It is worth talking about what else you could do with this. What if instead of LEDs, you
used relays that could turn real strings of lights on and off? The lightshowPi package
incidentally also allows SMS text message control of the light show, such as collecting
votes for the next song to be played from a playlist.
When you are satisfied with the rewards of this project, tear it down and pack it up.
Straighten all those bent leads for easier storage, and make sure to inventory
everything to make sure the kits stay whole.