- Published on
Buzzer System
- Authors
- Name
- Daiwik Pal
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.
Materials
Item Name | Amount |
---|---|
Arduino Uno | 1 |
Ultrasonic Rangefinder | 1 |
BreadBoard | 1 |
Wires | 6 |
Buzzer | 1 |
- Arduino Uno
- Ultrasonic Rangefinder
- Breadboard
- Wires
- Buzzer
Setup
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);
}