Donnerstag, 1. Mai 2014

Ultrasonic Sensor: first try

After few weeks waiting they finally came to me direct from China: my new Arduino Nano and a couple of ultrasonic sensors.


  • 1 x Ultrasonic Module HC-SR04 Distance Measuring Transducer Sensor - $2.56 / piece
  • 1 x Funduino Nano 3.0 controller - $7.89 / piece.
As soon as I have time I decided so do simple experiment.

First of all I connected VCC and GND pins to common rails and then to corresponding pins of ultrasonic sensor. After that I connected Trigger to Pin 3 and Echo to Pin 2 of Arduino board. As in many existing examples I decided to output signal through Piezo Buzzer. I simply connected Pin 4 of Arduino to positive leg of buzzer and the other leg to common ground.

After that I wrote simple sketch.

 #define TRIG_PIN 3  
 #define ECHO_PIN 2  
 #define BUZZ_PIN 4  
 void setup() {  
  pinMode(TRIG_PIN, OUTPUT);  
  pinMode(ECHO_PIN, INPUT);  
 void loop() {  
  digitalWrite(TRIG_PIN, HIGH);  
  digitalWrite(TRIG_PIN, LOW);  
  int distance = pulseIn(ECHO_PIN, HIGH) / 2 / 29.1;  
  if (distance < 100)  
   tone(BUZZ_PIN, map(distance, 0, 100, 1000, 15), 100);  

As the result I've got working example, which can detect an object closer 1 meter and react using sound. My dog found it quite interesting to play with it.

Keine Kommentare:

Kommentar veröffentlichen