How to connect ultrasonic sensor to Arduino Uno
Publish Time: 2020-06-05 Origin: Site
Step 1: Hardware connection
Hardware connection: -Arduino piezoelectric ultrasonic transducer Ultrasonic Sensor 5v-》 Vccgnd-》 Gnd
Pin No. 8-Trig Pin
Pin 7-"Echo Pin
Arduino buzzer
9th stitch -> +ve stitch
Gnd-》-ve needle
Step 2: Procedures and results
Ultrasonic sensor and Arduino interface.
int buzzer = 9;
int triggerPin = 7;//Trigger pin 7
int echoPin = 8;//Echo pin 8
void setup()
Serial.begin (9600); We will start serial communication, so we can see the distance on the serial monitor Serial.println ("Tech Ponder‘s ultrasonic distance measurement sensor Tutorial").
pinMode (triggerPin, OUTPUT); //define the pin
pinMode (echoPin, INPUT);
pinMode (buzzer, output);
digitalWrite (buzzer, LOW);}
void loop()
{int duration, distance;//Add duration and distance
digitalWrite (triggerPin, HIGH);//trigger wave (for example to make the LED blink)
delay(10);
digitalWrite (triggerPin, LOW);
duration = pulseIn (echoPin, HIGH);//Special functions for listening and waiting for waves
distance = (duration/2)/29.1;//Convert the number to cm (If you want inches, you must change 29.1 with the appropriate number
delay (1000);
Serial.print (distance); print numbers
Serial.print ("cm"); and unit
Serial.println (""); only print to a new line
if (distance "35"
digitalWrite (buzzer, HIGH);
Serial.println ("Buzzer On");
digitalWrite (buzzer, LOW);
The result is displayed on the serial monitor.
Arduino uses an ultrasonic distance sensor to calculate the distance every second. When the distance is less than 35 cm, arduino will detect the threshold and the buzzer will turn on, which can be used as an obstacle detector. When the obstacle is closer than 35 cm, it will sound an alarm.