krystal92586
Posts: 9
Joined: Mon Jan 07, 2013 5:39 am

Frustrated with project

Tue Feb 12, 2013 6:35 am

I'm trying to help my daughter with this program to play an mp3 sound when a button is pressed. We're run into nothing but trouble and I cannot help her with this one. First, here are some lessons we've learned:
1) Don't buy cheap breadboard wires from ebay. Half of them don't connect from one end to the other. That cost many hours before we realized it.
2) Don't buy cheap buttons from ebay. They don't connect the right direction for fitting in the breadboard. That cost us many more hours before we realized it.

Now, we still cannot get the thing to work. Right now, it just plays the sound over and over until it freezes up and disconnects from the SSH session. I changed all of the "False" to "True" in the program and now it plays over and over with a pause in between and doesn't freeze up. I even simplified it so much that I have only a few wires from the Pi to the breadboard, no more ribbon cable. I took the buttons out and used a wire as a replacement. Even with nothing whatsoever plugged in, it still doesn't work. We typed the program exactly as it is in the instructions here: http://learn.adafruit.com/downloads/pdf ... rry-pi.pdf We chmod'd it and we run it and it doesn't work. Anybody able to get this one to work or have any suggestions what we may have done wrong? She blogged about her first try with it when we had the wires plugged into the pin numbers instead of the GPIO numbers. We've fixed that now, but it didn't help. http://raspberrypikid.wordpress.com/201 ... on-sounds/

Thanks, Mike and Krystal

User avatar
joan
Posts: 14652
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Frustrated with project

Tue Feb 12, 2013 10:34 am

Are the button presses being detected properly? i.e. can you see a state change between pressing and releasing a button?

User avatar
bgreat
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm

Re: Frustrated with project

Tue Feb 12, 2013 1:18 pm

To verify your buttons are working, try this as a first program. Once it is working, then change the print lines to play the sounds.

Code: Select all

#!/usr/bin/env python

from time import sleep
import os
import RPi.GPIO as GPIO

# Three buttons are connected to the BCM GPIO as follows
button1 = 23
button2 = 24
button3 = 25

# Use the GPIO BCM numbering mode
GPIO.setmode(GPIO.BCM)

# Set the button connections as inputs
GPIO.setup(button1, GPIO.IN)
GPIO.setup(button2, GPIO.IN)
GPIO.setup(button3, GPIO.IN)

# loop forever reading the button inputs, when a button is pressed, print a message
while True:
    if ( GPIO.input(button1) == False ):
        print "Button 1 pressed"
    if ( GPIO.input(button2) == False ):
        print "Button 2 pressed"
    if ( GPIO.input(button3)== False ):
        print "Button 3 pressed"
    # wait 1 second before going back to top of loop
    sleep(1);
I changed the numbers to named variables to make the program more understandable and maintainable. If you move the button connection, you only need to change the code in one spot.

Also, please be aware of that the switch orientation may be incorrect for the switches you are using. You may need to rotate them 90 degrees so that you are actually using them as a switch... The pins on one side are tied together with the switch then connecting them when pressed to the two pins on the opposite side.

Enjoy!
Bill

User avatar
bgreat
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm

Re: Frustrated with project

Tue Feb 12, 2013 1:47 pm

And here is a simple drawing of the connections that are being used for the test. This may be easier to follow than the photo if you are unfamiliar with schematics and wiring.
buttons.png
Three buttons
buttons.png (49.72 KiB) Viewed 1993 times
When the buttons are unpressed, the resistors pull the corresponding GPIO pin to 3.3v (True) level. When the button is pressed, the GPIO pin is connected to GND (False) level.

Hope this helps. I see the Raspberry Pi as a great learning tool. Frustration for new users to interfacing is not what is needed.

Enjoy!
Bill

krystal92586
Posts: 9
Joined: Mon Jan 07, 2013 5:39 am

Re: Frustrated with project

Wed Feb 13, 2013 7:44 am

(This is dad typing now.) Thanks, that sample code will help. Pressing the buttons doesn't seem to do anything; it starts playing sounds right away and nothing stops it except ctrl-c sometimes or waiting until it freezes and just unplugging it. The picture is also easy to follow. I believe that this is exactly how we had it hooked up. We learned that the switches we bought needed to be rotated, but then they don't fit in the breadboard, so we just started using a jumper wire as a make-shift switch. I understand schematics and circuits, but I just couldn't figure this out when the circuits are combined with a device that we're new with and a programming language we're not sure about.
I now understand why the loops are set to "False." Basically, the switch and the GPIO input are connected in parallel, so when the low-resistance switch is pressed, most of the current flows through it to ground instead of to the GPIO pin. Is that accurate? If so, I understand why it would play music when all of the wires are disconnected, because the GPIO pins would all read False since there's no current running to them.
I hate to even ask this, but is it possible that we damaged the RPi when we had the wires connected wrong? We connected to pins 23, 24, and 25 instead of GPIO23, GPIO24, and GPIO25. I guess that the simple code will help us determine that.
What a great community to help out a couple of rookies like this.

toxibunny
Posts: 1382
Joined: Thu Aug 18, 2011 9:21 pm

Re: Frustrated with project

Wed Feb 13, 2013 9:13 am

Post your code, dude!
note: I may or may not know what I'm talking about...

User avatar
joan
Posts: 14652
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Frustrated with project

Wed Feb 13, 2013 9:28 am

krystal92586 wrote:...
Pressing the buttons doesn't seem to do anything
...
That's the first thing to sort out.

texy
Forum Moderator
Forum Moderator
Posts: 5161
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Frustrated with project

Wed Feb 13, 2013 12:01 pm

Do you have access to a multimeter? Set that to volts and measure the switch with respect to GND pressing the button on and off - check there is a difference of approximately 0 volts, and then 3,3 volts. Once you are happy that the voltages are correct, do the same measurements at the GPIO port pins - once you are happy which ones you have wired up (and made the program follow the correct GPIO lines also).
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

User avatar
piglet
Posts: 912
Joined: Sat Aug 27, 2011 1:16 pm

Re: Frustrated with project

Wed Feb 13, 2013 12:31 pm

My initial mistake with the cheap buttons I got from ebay was putting them in the board rotated 90 degrees.... with the pair of connected legs over the gap, so was always on.

User avatar
bgreat
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm

Re: Frustrated with project

Wed Feb 13, 2013 12:58 pm

toxibunny wrote:Post your code, dude!
Chill! ;) The code is in the link from the first post. The code I posted for testing is the same code plus comments and the sound calls replaced with print statements.

Of course, posting the actual code would allow confirmation of the indenting. I assume this is correct, since the code was looping.

Enjoy!
Bill

User avatar
bgreat
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm

Re: Frustrated with project

Wed Feb 13, 2013 1:16 pm

krystal92586 wrote:I now understand why the loops are set to "False." Basically, the switch and the GPIO input are connected in parallel, so when the low-resistance switch is pressed, most of the current flows through it to ground instead of to the GPIO pin. Is that accurate? If so, I understand why it would play music when all of the wires are disconnected, because the GPIO pins would all read False since there's no current running to them.
Basically correct, but the inputs are voltage sensitive and draw very little current. With just the resistor to 3.3V the voltage presented at the current draw of the input results in almost no voltage drop across the resistor. This presents very nearly 3.3V to the input -- a high, logic 1 or true level. When the switch is depressed, the very low resistance of the switch as compared to the resistor results in a 0V on the input -- a low, logic 0 or false level.

If the resistors are not connected, the default high impedance of the inputs would be "floating". In this case the value read from the inputs would be dependent on any stray or residual voltage present at the pins. The value returned would be for practical purposes unpredictable. To avoid this, the resistor is used to present a known default voltage level. Note that the GPIO pins do support a mode where you can enable a built-in pull up or pull down resistor to eliminate the need for an external resistor. :) But that is a future lesson...
krystal92586 wrote:I hate to even ask this, but is it possible that we damaged the RPi when we had the wires connected wrong? We connected to pins 23, 24, and 25 instead of GPIO23, GPIO24, and GPIO25.
What a great community to help out a couple of rookies like this.
It is unlikely you damaged the GPIO inputs by using pins 23, 24, 25 instead of GPIO 23, GPIO 24, GPIO 25. Looking at the pin designations shows these as GPIO 11, GPIO 8, and GND. By default the GPIO pins are set as inputs, so connecting them to GND through the switch is no issue. And connecting GND to GND... ;)

Enjoy!
Bill

krystal92586
Posts: 9
Joined: Mon Jan 07, 2013 5:39 am

Re: Frustrated with project

Fri Feb 15, 2013 3:46 am

toxibunny wrote:Post your code, dude!
Sorry, I should have said that the code is in the pdf referenced in the original post.

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: Frustrated with project

Fri Feb 15, 2013 11:59 am

You could possibly go even lower level testing the hardware setup without any software (to mix things up) first:

connect a jumper from RPi 3.3V to the breadboard -> a small resistor -> led -> jumper to GND on RPi

The led should light up. If not, check the led direction (turn 180 degs) and of course verify that correct GPIO pins used. Possibly try another led.

Then add a button between the led and the jumper to GND.

If the led lights straight away, the button is the wrong way - turn it 90 degrees and check that the led lights when pressing the button. If the led does not light at all, try another button or try replacing the button with a jumper to make sure everything else works.

Always secure the components are properly in the breadboard and a multimeter is a good investment!
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

Return to “Beginners”