About:
In this tutorial, we are basically going to integrate an ultrasonic sensor (HC-SR04) with LED and Piezo Buzzer using Surilli Basic M0. Whenever any object is brought closer to an ultrasonic sensor, the LED starts blinking, and as a result of this, the Piezo buzzer will begin 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 Basic M0.
- HC-SR04 Ultrasonic Sensor.
- Piezo buzzer.
- LED x 1.
- 10k ohm resistor.
- Breadboard.
- Connecting wires.
- Arduino IDE software.
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 Basic M0 respectively, and the trig and echo pins to any digital I/O pin on Surilli Basic M0.
- The HC-SR04 sensor attach to the breadboard.
- The sensor VCC connect to the Surilli Basic M0 USB.
- The sensor GND connect to the Surilli Basic M0 GND.
- The sensor trig connect to the Surilli Basic M0 digital I/O 9 PIN.
- The sensor echo connect to the Surilli Basic M0 digital I/O 10 PIN.
Buzzer and LED:
- The buzzer attach to the breadboard.
- The buzzer long leg (+) connect to the Surilli Basic M0 digital PIN 11.
- The buzzer short leg (-) connect to the Surilli Basic M0 GND.
- The LED attach to the breadboard.
- The resistor connect to the LED long leg (+).
- The resistor other leg (from LED's long leg) connect to the Surilli Basic M0 digital PIN 13.
- The LED short leg (-) connect to the Surilli Basic M0 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);
SerialUSB.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
SerialUSB.print("Distance: ");
SerialUSB.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 application.
If you make something fun and interesting, do share it with our community.
That’s all for now. If you have any queries, visit 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.