Example Project: Button And Buzzer problem

I’m working through the example projects on my Grove Pi+ and have managed to get the LED to blink. However when I try the button and buzzer project, nothing happens.
Can anyone help please?

1 Like

Not yet. . . (:wink:), as we have no idea what you did, or how you did it, or even what kind of Raspberry Pi you have or the operating system version you are using.

Please provide more details so that we can understand your problem better.

Thanks!

1 Like

Thanks for the reply! i am using a Raspberry pi 4 model B and am running legacy Raspbian. I tried again yesterday and after running the python code twice i managed to get the buzzer going how ever i couldn’t turn it off! There was also nothing being printed in the terminal. Here is the code that i am putting into the terminal:

cd Dexter/GrovePi/Projects/Button_And_Buzzer

sudo python Button_And_Buzzer.py

And this is the python code:

import time
from grovepi import *
import math

buzzer_pin = 2 #Port for buzzer
button = 4 #Port for Button

pinMode(buzzer_pin,“OUTPUT”) # Assign mode for buzzer as output
pinMode(button,“INPUT”) # Assign mode for Button as input
while True:
try:
button_status= digitalRead(button) #Read the Button status
if button_status: #If the Button is in HIGH position, run the program
digitalWrite(buzzer_pin,1)
# print “\tBuzzing”
else: #If Button is in Off position, print “Off” on the screen
digitalWrite(buzzer_pin,0)
# print “Off”
except KeyboardInterrupt: # Stop the buzzer before stopping
digitalWrite(buzzer_pin,0)
break
except (IOError,TypeError) as e:
print(“Error”)

Thanks, SamBB

1 Like

You are hand typing this into a python terminal session?

For something like this, you need to create a “new file”, (document), that has a name ending in “.py” - and then run it in something like Thonny.

For example you’d create a “new file” named “my_script.py” in your home directory and then type into the document the commands you are typing into the terminal session.

I’m sure you can find a zillion YouTube videos on how to use Thonny to create a new file and type stuff into it.

Try that and let me know what happens.