// Define pins const int piezoPin = 2; const int buttonPin = 3; void setup() { // Initialize the piezo pin as an output pinMode(piezoPin, OUTPUT); // Initialize the button pin as an input pinMode(buttonPin, INPUT); } void loop() { // Read the button state (HIGH when pressed) int buttonState = digitalRead(buttonPin); // Check if the button is pressed if (buttonState == HIGH) { // Make the piezo produce sound tone(piezoPin, 1000); // 1000 Hz frequency } else { // Stop the piezo sound noTone(piezoPin); } }