banner



Can I Use Arduino To Trigger An Scmos Camera

In this tutorial we will learn how the HC-SR04 ultrasonic sensor works and how to use it with Arduino. This is the most popular sensor for measuring distance and making obstruction fugitive robots with Arduino.

I have already used the HC-SR04 ultrasonic sensor for building several cool Arduino projects and yous tin can bank check them out here:

  • Arduino Radar (Sonar)
  • Range Measurer and Digital Spirit Level
  • Security and Warning Organisation Project
  • Arduino Emmet Hexapod Robot

You can lookout the following video or read the written tutorial beneath. It has boosted information and examples for the sensor, including specifications, pinouts, wiring diagrams, codes and more.

HC-SR04 Hardware Overview

The HC-SR04 is an affordable and easy to apply distance measuring sensor which has a range from 2cm to 400cm (about an inch to 13 feet).

The sensor is composed of two ultrasonic transducers. One is transmitter which outputs ultrasonic sound pulses and the other is receiver which listens for reflected waves. Information technology'south basically a SONAR which is used in submarines for detecting underwater objects.

Here are its main specifications:

Operating Voltage 5V DC
Operating Current 15mA
Operating Frequency 40KHz
Min Range 2cm / ane inch
Max Range 400cm / 13 feet
Accuracy 3mm
Measuring Bending <15°
Dimension 45 x xx 10 15mm

HC-SR04 Ultrasonic Sensor Pinout

Here's the pinout of the sensor:

The sensor has iv pins. VCC and GND get to 5V and GND pins on the Arduino, and the Trig and Echo go to whatsoever digital Arduino pivot. Using the Trig pin we send the ultrasound moving ridge from the transmitter, and with the Echo pin we mind for the reflected signal.

How the HC-SR04 Ultrasonic Altitude Sensor Works?

It emits an ultrasound at 40 000 Hz which travels through the air and if at that place is an object or obstruction on its path It will bounce back to the module. Considering the travel time and the speed of the sound you lot can calculate the distance.

How Ultrasonic Sensor Working Principle - Explained

In social club to generate the ultrasound we need to prepare the Trig pin on a High State for 10 µs. That will transport out an 8 cycle ultrasonic burst which volition travel at the speed of sound. The Echo pins goes loftier right abroad later that viii cycle ultrasonic outburst is sent, and information technology starts listening or waiting for that wave to be reflected from an object.

If in that location is no object or reflected pulse, the Echo pin will time-out later on 38ms and become back to low land.

Ultrasonic Sensor Diagram

If we receive a reflected pulse, the Echo pin will become down sooner than those 38ms. According to the amount of time the Echo pin was High, we can determine the distance the sound wave traveled, thus the distance from the sensor to the object.

For that purpose nosotros are using the following basic formula for calculating altitude:

Distance = Speed x Time

Nosotros really know both the speed and the fourth dimension values. The time is the corporeality of time the Repeat pin was High, and the speed is the speed of audio which is 340m/s. There's i additional step we need to do, and that'due south separate the stop result by ii. and that'due south because nosotros are measuring the elapsing the sound moving ridge needs to travel to the object and bounce dorsum.

Let's say the Echo pin was HIGH for 2ms. If we desire the become the distance issue in cm, nosotros can convert the speed of sound value from 340m/s to 34cm/ms.

Distance = (Speed x Time) / 2 = (34cm/ms 10 i.5ms) / ii = 25.5cm.

Then, if the Repeat pivot was Loftier for 2ms (which we measure using the pulseIn() function), the distance from the sensor to the object is 34cm.

How to Connect HC-SR04 Ultrasonic Sensor to Arduino

Here's how we demand to connect the HC-SR04 sensor to an Arduino board.

HC-SR04 Ultrasonic Sensor Arduino Connection - Wiring

The Footing and the VCC pins of the module needs to exist continued to the Ground and the 5 volts pins on the Arduino Board respectively and the trig and echo pins to any Digital I/O pin on the Arduino Board.

You can get these components need for this tutorial from any of the sites below:

  • Ultrasonic Sensor HC-SR04 …………Amazon / Banggood / AliExpress
  • Arduino Lath …………………………… Amazon / Banggood / AliExpress
  • Breadboard and Jump Wires ………Amazon / Banggood / AliExpress

Disclosure: These are affiliate links. As an Amazon Associate I earn from qualifying purchases.

HC-SR04 Ultrasonic Sensor Arduino Code

Here'south a code for measuring distance using the HC-SR04 ultrasonic sensor and Arduino.

          

/* Ultrasonic Sensor HC-SR04 and Arduino Tutorial by Dejan Nedelkovski, www.HowToMechatronics.com */ // defines pins numbers const int trigPin = nine; const int echoPin = x; // defines variables long elapsing; int distance; void setup () { pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input Series.begin(9600); // Starts the serial advice } void loop () { // Clears the trigPin digitalWrite(trigPin, LOW); delayMicroseconds(two); // Sets the trigPin on Loftier country for 10 micro seconds digitalWrite(trigPin, HIGH); delayMicroseconds(ten); digitalWrite(trigPin, Depression); // Reads the echoPin, returns the sound wave travel fourth dimension in microseconds duration = pulseIn(echoPin, HIGH); // Calculating the distance distance = duration * 0.034 / ii; // Prints the distance on the Serial Monitor Serial.print("Altitude: "); Serial.println(altitude); }

Lawmaking language: Arduino ( arduino )

Code Explanation

Commencement nosotros take to ascertain the Trig and Echo pins. In this case they are the pins number ix and 10 on the Arduino Board and they are named trigPin and echoPin. Then we need a Long variable, named "elapsing" for the travel time that we will get from the sensor and an integer variable for the altitude.

          

// defines pins numbers const int trigPin = 9; const int echoPin = 10; // defines variables long duration; int distance;

Lawmaking linguistic communication: Arduino ( arduino )

In the setup nosotros accept to ascertain the trigPin as an output and the echoPin equally an Input and also first the serial communication for showing the results on the serial monitor.

          

void setup () { pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin every bit an Input Serial.begin(9600); // Starts the series communication }

Lawmaking language: Arduino ( arduino )

In the loop kickoff we have to make sure that the trigPin is clear and so you have to set that pin on a Depression State for just two µs. Now for generating the Ultra sound wave we accept to ready the trigPin on Loftier State for 10 µs.

          

// 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);

Code language: Arduino ( arduino )

Using the pulseIn() function we read the travel time and put that value into the variable "duration". This function has two parameters, the kickoff one is the proper name of the Repeat pin and for the 2d is the state of the pulse we are reading, either High or Low.

          

// Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echoPin, High);

Code language: Arduino ( arduino )

In this case, nosotros need this prepare to it HIGH, as the HC-SR04 sensors sets the Echo pivot to Loftier afterward sending the 8 cycle ultrasonic outburst from the transmitter. This actually starts the timing and once we receive the reflected sound wave the Echo pin will become to Depression which stops the timing. At the finish the function volition return the length of the pulse in microseconds.

For getting the altitude we will multiply the elapsing by 0.034 and divide it by 2 as we explained this equation previously.

          

// Calculating the distance distance= duration*0.034/ii; // Prints the distance on the Serial Monitor Serial.impress("Distance: "); Series.println(distance);

Code language: Arduino ( arduino )

At the finish we will print the value of the distance on the Serial Monitor.

Arduino Ultrasonic Sensor and LCD Display Example

Here's another example how to use the ultrasonic sensor with Arduino and display the results on an LCD.

Arduino Ultrasonic Sensor and LCD Display Example

You lot tin connect the ultrasonic sensor and the LDC as following:

Arduino ultrasonic sensor and the LDC circuit diagram

The code measuring the distance is pretty much the same equally the basic example. Here, instead of printing the results on the serial monitor nosotros impress them on the LCD. If you demand more than details how to employ and connect an LCD with Arduino you tin can check my item tutorial for information technology.

          

/* Ultrasonic Sensor HC-SR04 and Arduino Tutorial past Dejan Nedelkovski, www.HowToMechatronics.com */ #include <LiquidCrystal.h> // includes the LiquidCrystal Library LiquidCrystal lcd (1, 2, iv, 5, half-dozen, 7) ; // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7) const int trigPin = 9; const int echoPin = 10; long duration; int distanceCm, distanceInch; void setup () { lcd.begin(xvi, 2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and superlative) of the brandish pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); } void loop () { digitalWrite(trigPin, Depression); delayMicroseconds(ii); digitalWrite(trigPin, High); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distanceCm = duration * 0.034 / 2; distanceInch = duration * 0.0133 / 2; lcd.setCursor(0, 0); // Sets the location at which subsequent text written to the LCD will be displayed lcd.print("Distance: "); // Prints string "Altitude" on the LCD lcd.impress(distanceCm); // Prints the distance value from the sensor lcd.impress(" cm"); delay(10); lcd.setCursor(0, ane); lcd.print("Distance: "); lcd.print(distanceInch); lcd.print(" inch"); delay(10); }

Code language: Arduino ( arduino )

Example Code Using the NewPing Library

There are actually a simpler and better style to plan the Arduino to measure altitude using the HC-SR04 ultrasonic sensor, and that's using the NewPing library.

In the previously explained code nosotros manually triggered the sensor and measured the received signal pulse duration. Then according to those results we calculated the distance based on information technology. Using the NewPing library nosotros can become the distance with just a unmarried line of code.

Hither's an example code:

          

#include <NewPing.h> #ascertain TRIGGER_PIN nine #define ECHO_PIN x #define MAX_DISTANCE 400 // Maximum distance we want to mensurate (in centimeters). NewPing sonar (TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE) ; // NewPing setup of pins and maximum distance. void setup () { Serial.brainstorm(9600); } void loop () { filibuster(50); // Wait 50ms between pings (about twenty pings/sec). 29ms should be the shortest delay between pings. int distance = sonar.ping_cm(); // Send ping, get distance in cm and impress issue (0 = outside set altitude range) Serial.impress("Distance: "); Serial.print(distance); Serial.println("cm"); }

Code language: Arduino ( arduino )

It is much simpler than the previous example. We just use the call the ping_cm() method on the NewPing sonar object and we go the distance in centimeters. If we want the distance in inches nosotros can use the ping_in() instead.

The library as well has few other useful features. For example, with the ping_median(iterations [, max_cm_distance]) method, we tin get more accurate results, as it returns a median, or the heart value from several measurements. With the iterations parameter we set the number of samples the program will take for calculating the middle value. The default value is 5 iterations. The ping_median() returns the received pulse duration in microseconds.

Still, for more info and details yous can check the NewPing wiki page.

Improving HC-SR04 Distance Sensor Accuracy with DHT22 Temperature Sensor

The HC-SR04 sensor is fairly accurate, just as it work depends on the speed of sound, for more than accurate results we should also accept the air temperature into consideration. The speed of sound tin significantly modify as the air temperature changes. For example, at 20°C, the speed of sound is effectually 340m/south, but at -20°C the speed of sound is around 315m/south. The relative humidity affects the speed as well.

So, if we employ this sensor to measure distances at various temperatures we should implement a temperature bounty, and we tin do that with the following formula:

Velocity = 331.4 + 0.6 10 Temperature + 0.0124 x Relative_Humidity

Here's an example:

Along the HC-SR04 ultrasonic sensor, nosotros will employ the DHT11 / DHT22 sensor to measure the temperature and the humidity of the environs, and adjust the speed of sound value accordingly.

Arduino lawmaking

          

/* Instance fabricated by Dejan, How To Mechatronics, https://howtomechatronics.com/ */ #include <NewPing.h> // https://bitbucket.org/teckel12/arduino-new-ping/wiki/Habitation #include "dht.h" // https://github.com/RobTillaart/DHTlib #ascertain TRIGGER_PIN ix #define ECHO_PIN 10 #define MAX_DISTANCE 400 #define dht22 5 // DHT22 temperature and humidity sensor NewPing sonar (TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE) ; dht DHT; // Creats a DHT object int readDHT, temp, hum; float speedOfSound, altitude, duration; void setup () { Serial.begin(9600); } void loop () { filibuster(100); // Read temperature and humidity from DHT22 sensor readDHT = DHT.read22(dht22); // Reads the data from the sensor temp = DHT.temperature; // Gets the values of the temperature hum = DHT.humidity; // Gets the values of the humidity speedOfSound = 331.4 + (0.6 * temp) + (0.0124 * hum); // Calculate speed of audio in m/s duration = sonar.ping_median(10); // ten interations - returns duration in microseconds duration = duration/1000000; // Convert mircroseconds to seconds altitude = (speedOfSound * elapsing)/2; distance = distance * 100; // meters to centimeters Serial.impress("Distance: "); Serial.print(distance); Serial.println("cm"); }

Code language: Arduino ( arduino )

So, kickoff we are reading the temperature and humidity values from the DHT22 sensor, and then utilize those values to calculate the speed of sound. Then we get the elapsing the sound wave travel in microseconds, convert them to seconds and calculate the distance from the sensor to the objects in centimeters.

HC-SR04 Dimensions and 3D Model

I fabricated a 3D model of the HC-SR04 ultrasonic sensor in instance you lot need 1 when making your side by side projection with information technology. Yous can download it in from the link below.

HC-SR04 Ultrasonic Sensor 3D Model

You can observe and download this 3D model at Thangs.

Here are the dimensions of the HC-SR04 sensor:

Delight keep in heed that they can sometimes differ a little bit depending on the manufacturer.

Determination

So, we have covered pretty much everything that we demand to know about using the HC-SR04 Ultrasonic sensor with Arduino. It's a great sensor for many DIY electronics projects where we need a non-contact distance measuring, detection of presence or objects, level or position something etc.

I already mentioned the projects that I have fabricated with this sensor at the beginning of the postal service. Hither are some other absurd projects using the HC-SR04 sensor and Arduino:

  • Touchless Automated Move Sensor Trash Can
  • Hand Gesture Smart Low-cal Command
  • Mini Acoustic Levitation
  • Obstruction Avoiding Robot

I hope you enjoyed this tutorial and learned something new. Feel costless to ask whatsoever question in the comments department beneath and don't forget to check out my full collection of thirty+ Arduino Projects.

Source: https://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/

Posted by: ackerfainjusly.blogspot.com

0 Response to "Can I Use Arduino To Trigger An Scmos Camera"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel