Devices & Components
Arduino Uno Rev3
Buzzer
Jumper wires (generic)
Ultrasonic Sensor - HC-SR04 (Generic)
Breadboard (generic)
Hardware & Tools
Tape, Foam
Circular Stuffed Animal/Ball
Basketball Hoop
Software & Tools
Arduino IDE
Project description
Code
Code
c_cpp
Basketball Buzzer with Sensor
1const int trig_pin = 12; 2const int echo_pin = 11; 3const int buzzer_pin = 6; 4int distance_cm; 5long duration; 6 7void setup() 8{ 9//Setting up the sensor 10pinMode(trig_pin, OUTPUT); 11pinMode(echo_pin, INPUT); 12pinMode(buzzer_pin, OUTPUT); 13} 14void loop() 15{ 16//Sensor detecting movement 17 digitalWrite(trig_pin, LOW); 18 delayMicroseconds(2); 19 digitalWrite(trig_pin, HIGH); 20 delayMicroseconds(10); 21 digitalWrite(trig_pin, LOW); 22 duration = pulseIn(echo_pin, HIGH); 23 distance_cm = (duration*0.034)/2.0; 24 25//If the movement is far away, do not turn the buzzer on 26 if (distance_cm >= 10 || distance_cm <= 0) 27 { 28 digitalWrite(buzzer_pin, LOW); 29 } 30//Otherwise, turn the buzzer on 31 else 32 { 33 digitalWrite(buzzer_pin, HIGH); 34 } 35}
Code
c_cpp
Basketball Buzzer with Sensor
1const int trig_pin = 12; 2const int echo_pin = 11; 3const int buzzer_pin = 6; 4int distance_cm; 5long duration; 6 7void setup() 8{ 9//Setting up the sensor 10pinMode(trig_pin, OUTPUT); 11pinMode(echo_pin, INPUT); 12pinMode(buzzer_pin, OUTPUT); 13} 14void loop() 15{ 16//Sensor detecting movement 17 digitalWrite(trig_pin, LOW); 18 delayMicroseconds(2); 19 digitalWrite(trig_pin, HIGH); 20 delayMicroseconds(10); 21 digitalWrite(trig_pin, LOW); 22 duration = pulseIn(echo_pin, HIGH); 23 distance_cm = (duration*0.034)/2.0; 24 25//If the movement is far away, do not turn the buzzer on 26 if (distance_cm >= 10 || distance_cm <= 0) 27 { 28 digitalWrite(buzzer_pin, LOW); 29 } 30//Otherwise, turn the buzzer on 31 else 32 { 33 digitalWrite(buzzer_pin, HIGH); 34 } 35}
Downloadable files
Schematics
Remember to use an Active Buzzer
Schematics

Comments
Only logged in users can leave comments