Build a Raspberry Pi music box ft. Dr Sally Le Page

Build a Raspberry Pi music box ft. Dr Sally Le Page


– Today, you’re going to wire
buttons to your Raspberry Pi and use Python to make a
(arcade tone) sound machine. (upbeat music) To make this project, you will need: a Raspberry Pi, a breadboard, some buttons, and some male to male, and
male to female jumper cables. A Raspberry Pi has 26 GPIO pins. GPIOs stands for General
Purpose Input Output. These pins allow you to send
and receive on/off signals to and from electronic components like LEDs, motors, and buttons. Each pin has a number, and
there are additional pins that provide 3.3 volts, 5
volts, and ground connections. A pin diagram or labeler like
this one helps you to know which pin is which. Buttons give you control over a circuit, and let you send signals
to a computer like the keys on your keyboard. Before wiring your circuit,
switch off your Raspberry Pi. Place one of your buttons onto
your breadboard like this. Make sure you push the legs
as far down into the holes as you can. Next, connect a ground
pin on the Raspberry Pi to the negative rail on the breadboard using a male to female jumper wire. Then, connect the negative
rail to the button with a male to male jumper wire. Now connect the other leg of your button to GPIO pin 17 using another
male to female jumper wire. You’ve know created an electrical circuit. It has power and a pathway to
conduct the electric current. In your circuit, the current
flows from the Raspberry Pi’s GPIO pin through the
circuit to the ground pin. Your button acts as a switch
that can break the circult. When the switch is open,
no current can flow. When the switch is closed, the
current flows from negative to positive through the completed circuit. Which way does the current
flow in your circuit? Early scientists believed that
current flowed from positive to negative. They called this conventional current. We now know that current actually flows in the opposite direction
from negative to positive. But we still use the
term conventional current which can be confusing when
you’re new to electronics. What we can agree on is that
electronics is all about controlling electric current
to make it do something useful. That’s the hardware complete. Now for the software. We’re going to use a Python
programming environment or IDE. Create a new project by clicking New. Setup your button by typing from gpiozero import button. Then create a variable, btn=button (17). Next, define a function that
will print the word Hello. Def hello(): print(“hello”) and finally, create a trigger
that calls the function. Btn.when_pressed=hello and save it as Music Box. When you run your program, the
message Hello should appear each time the button is pressed. Now let’s add some sound. Create a directory called Music
Box in your home directory and save the sound files
you want to use in here. They need to be wav file
types for them to work. I’m going to use some
existing sound effects from Python Games on the Raspberry Pi. To play a sound file with Python, import the Pygame module
and initialize it. Import pygame pygame.init(). Next create a sound object and provide it with a path to your file. Beat1=pygame.mixer.sound (‘ the path to your file .wav’). Finally, inside the Hello function, underneath your print statement, type beat1.play() to play the sound. Now when you press your button,
you’ll hear the sound play. (arcade short tones) Go ahead and add more sounds
and buttons in the same way. You might like to change the
text in your print statements to something more useful like
when the button is pressed. I’ve also named my functions
after the color of the buttons to make it easier to
find them in my program. Def red(): print(“red button pressed”) beat1.play(). Now that you know how to connect
a button to do something, what will you make next? (upbeat music) Share your
ideas in the comments below. I didn’t realize until last year that when people were
talking about Anchorman, it wasn’t like a fishing trawlerman. (laughter) ‘Cause we don’t call them TV anchors.

16 thoughts on “Build a Raspberry Pi music box ft. Dr Sally Le Page

  1. I know this is off-topic, but what theme are you running on that Raspbian? Or is it Raspbian at all?
    On topic: I realize more and more that I should learn python….! You make it look so easy!

  2. I thought the anchorman was the person at each end of the rope, behind everyone else in a tug of war competition.

  3. Gee in wish i wanted to Build a Music Box Like These so i Could Hock up my iPod iPhone and iPad in so i could Play Some Music.

Leave a Reply

Your email address will not be published. Required fields are marked *