In this tutorial, we are basically going to integrate the Ultrasonic sensor (HC-SR04) with LED and Piezo buzzer using Surilli GSM. Whenever any object is brought closer to an ultrasonic sensor, the LED starts blinking, and as a result of this, the piezo buzzer will start beeping.
What Is a Piezo Buzzer?A buzzer or beeper is an audio signalling device, which may be mechanical, electromechanical, or piezoelectric (piezo for short). A piezo buzzer is an electronic device commonly used to produce sound. Light weight, simple construction and low price make it usable in various applications like car/truck reversing indicator, computers, call bells, etc.
Ultrasonic Sensor (HC-SR04)The HC-SR04 ultrasonic module has 4 pins: Ground, VCC, trig and echo. The ground and the VCC pins of the module needs to be connected to the ground and the 5 volts pins on the Surilli board respectively and the trig and echo pins to any digital I/O pin on the Surilli board. Trigger (Transmitter) emits an ultrasound at 40,000 Hz which travels through the air and if there is an object or obstacle on its path it will bounce back to the echo (receiver). Considering the travel time and the speed of the sound you can calculate the distance.
Hardware Required:- Surilli GSM.
- HC-SR04 ultrasonic sensor.
- Piezo buzzer.
- LED.
- 10k ohm resistor.
- Breadboard and connecting wires.
HC-SR04 Sensor:
- The HC-SR04 ultrasonic module has 4 pins, Ground, VCC, trig and echo. The ground and the VCC pins of the module needs to be connected to the ground and the 5 volts pins on the Surilli GSM respectively, and the trig and echo pins to any digital I/O pin on Surilli GSM.
- The HC-SR04 sensor attached to the breadboard.
- The sensor VCC connect to the Surilli GSM USB.
- The sensor GND connect to the Surilli GSM GND.
- The sensor trig connect to the Surilli GSM digital I/O 9 PIN.
- The sensor echo connect to the Surilli GSM digital I/O 10 PIN.
Buzzer and LED
- The buzzer attached to the Breadboard.
- The buzzer long leg (+) connect to the Surilli GSM digital PIN 11.
- The buzzer short leg (-) connect to the Surilli GSM GND.
- The LED attached to the breadboard.
- The resistor connect to the LED long leg (+).
- The resistor other leg (from LED's long leg) connect to the Surilli GSM digital PIN 13.
- The LED short leg (-) connect to the Surilli GSM GND.
Make sure you have selected the right port, board and processor for the Surilli as shown in the picture below and it is programmable (compile and upload “Blink” from File>Examples>Digital>Blink onto your Surilli to check if everything is working fine).
STEP 2: The CircuitryThe circuitry is very simple. It's mostly the programming. Follow the figure below to set up your hardware.
Now you have completed setting up your hardware and Arduino IDE. Copy and paste the Arduino sketch given below into your Arduino IDE and hit upload.
After it is uploaded, the circuit will start working.
Code:// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int buzzer = 11;
const int ledPin = 13;
// defines variables
long duration;
int distance;
int safetyDistance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzer, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
safetyDistance = distance;
if (safetyDistance <= 5){
digitalWrite(buzzer, HIGH);
digitalWrite(ledPin, HIGH);
}
else{
digitalWrite(buzzer, LOW);
digitalWrite(ledPin, LOW);
}
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}
Play with the program to see how it reacts to different values and logic. This will develop your understanding about ultrasonic sensors so you can use them in your practical applications.
If you make something funny and interesting, do share it with our community.
That’s all for now. If you have any queries, visit our website surilli.io or contact our support. Stay connected with the Surilli family for more amazing stuff. :-)
Comments
Please log in or sign up to comment.