Published on

Buzzer System

Authors

In the article, you will create a system that uses the ultrasonic sensor and a buzzer to make a sound if it gets too close to an object. You will also learn the basics of a sensor feedback loop.

The feedback loop allows the computer to be “aware” of its surroundings by sensing its environment and reacting accordingly. An example, if a simple feedback loop is a thermostat. If the thermostat detects the house is getting too cold it will turn on the heater until the house gets to a set temperature.

Important Ports

Materials

Item NameAmount
Arduino Uno1
Ultrasonic Rangefinder1
BreadBoard1
Wires6
Buzzer1
  • Arduino Uno
Important Ports
  • Ultrasonic Rangefinder
Important Ports
  • Breadboard
Important Ports
  • Wires
Important Ports
  • Buzzer
Important Ports

Setup

Important Ports
Important Ports

Code

blinkandbutton.ino

// Define the pin numbers
#define trigPin 8
#define echoPin 9
#define buzzer 10

// intiating the variables needed
long duration;
float distanceInch;
int timer;

// the setup function runs once when the code is run
void setup() {
//setting the trigPin and echoPin for ultrasonic rangefinder and buzzer pin
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzer, OUTPUT);
}

//the loop runs continually once the code is run
//The loop continually obtains data from the utrasonic sensor and adjusts
//  the pulse strenght of the buzzer based on the distnce
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
// measuringthe time taken to hear the pulse back from range finder

distanceInch = duration * 0.0133 /2;
// calculating distance using the speed of sound in air

digitalWrite(buzzer, HIGH);
delay(50);
digitalWrite(buzzer, LOW);

timer = distanceInch * 10;
// change pulse speed based on distance

delay(timer);

}