distance – Blog eTechPath https://blog.etechpath.com Sat, 08 Apr 2023 19:45:45 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://blog.etechpath.com/wp-content/uploads/2017/08/BrandLogo12-150x150.png distance – Blog eTechPath https://blog.etechpath.com 32 32 Interfacing SHARP 2Y0A21 Distance Sensor with Arduino https://blog.etechpath.com/interfacing-sharp-2y0a21-distance-sensor-with-arduino/ https://blog.etechpath.com/interfacing-sharp-2y0a21-distance-sensor-with-arduino/#respond Tue, 21 Feb 2023 08:44:53 +0000 https://blog.etechpath.com/?p=1201 Introduction:

In this tutorial, I will explain how to interface SHARP 2Y0A21 infrared distance sensor with Arduino board and display the output in serial monitor.

SHARP 2Y0A21

SHARP 2Y0A21 sensor works on infrared technology, it consists of a transmitter and a receiver diode which calculates the distance between the sensor and the target by measuring time interval between transmitted and received wave.

SHARP infrared distance sensor
SHARP 2Y0A21 Sensor Specifications:
  1. Working voltage: 4.5 – 5.5 Vdc
  2. Distance measuring range: 10cm – 80cm (10cm dead band)
  3. Output type: Analog (voltage)
  4. Output: 0 to 3.1v (3.1v at 10cm to 0.3v at 80cm)
  5. Operating Temperature: -10 to +60 °C (Storage: -40 to +70 °C)

Circuit Diagram:

Code:

//SHARP 2Y0A21 IR Distance Sensor Test (10-80cm) 
// Author: Pranay Sawarkar
// www.eTechPath.com

#define sensor A2 

void setup() 
{
  Serial.begin(9600);
}

void loop() 
{
  float volts = analogRead(sensor)*0.0048828125;
  int distance = 30*pow(volts, -1.173);
  delay(1000); 
  
  if ((distance >=10)&&(distance <=80))
  {
    Serial.print("Distance:");
    Serial.println(distance);
    Serial.print("Volts:");
    Serial.println(volts);   
  }
  else
  Serial.println("Out of range");
}
]]>
https://blog.etechpath.com/interfacing-sharp-2y0a21-distance-sensor-with-arduino/feed/ 0
Digital distance measuring device using ultrasonic sensor SR04, OLED display and Arduino https://blog.etechpath.com/digital-distance-measuring-device-using-ultrasonic-sensor-sr04-oled-display-and-arduino/ https://blog.etechpath.com/digital-distance-measuring-device-using-ultrasonic-sensor-sr04-oled-display-and-arduino/#respond Sat, 05 May 2018 14:48:07 +0000 https://blog.etechpath.com/?p=573 About:

In this project i will explain how to interface SR04 ultrasonic sensor module with Arduino and display result on OLED display.




Components:

Arduino

SR04 Ultrasonic sensor module

OLED display i2c

Wires and basic tools




Circuit Diagram:

Description: 

  1. Connect SR04 ultrasonic sensor power to arduino +5v and GND also connect Trigger and Echo pin to arduino pin 9 and pin 8 as shown in above circuit diagram.
  2. In same way connect OLED power to arduino and connect SCL & SDA of oled display to arduino pin A5 and pin A4.
  3. Download arduino code and all libraries form download section, compile the code and upload it to your arduino.
  4. If you are new to ultrasonic sensors, do visit this post for basic study “SR04 Ultrasonic Sensor Module Basics”





Code:

/******************************************************************
 * Project: Printing utrasonic sensor data on OLED display
 * Author: Pranay Sawarkar
 * Website: www.etechpath.com
 *
 * This example is for a 128x32 pixel monocrome display using i2c
 *
 *****************************************************************/
#include <Ultrasonic.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Ultrasonic ultrasonic(9, 8);
#define OLED_RESET 4   //(optional)
Adafruit_SSD1306 display(OLED_RESET);
#if (SSD1306_LCDHEIGHT != 32)     // change it to 64 if you are using 128x64 pixel OLED
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
void setup()
{
  Serial.begin(9600);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  //display.display();
  //delay(2000);
  display.clearDisplay();
}
void loop ()
{
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.print("Distance: ");
  display.println(ultrasonic.distanceRead());
  display.setCursor(80,0);
  display.print("cm");
  display.display();
  delay(1000);
  display.clearDisplay();
}

 



Downloads:

SR04 Ultrasonic Sensor Module Basics

SR04 Ultrasonic Module Library 

Adafruit GFX Library

Adafruit_SSD1306 OLED Library

Arduino Code

]]>
https://blog.etechpath.com/digital-distance-measuring-device-using-ultrasonic-sensor-sr04-oled-display-and-arduino/feed/ 0
How to interface HC-SR04 Ultrasonic Sensor with Arduino https://blog.etechpath.com/how-to-interface-hc-sr04-ultrasonic-sensor-with-arduino/ https://blog.etechpath.com/how-to-interface-hc-sr04-ultrasonic-sensor-with-arduino/#respond Tue, 03 Apr 2018 23:35:22 +0000 https://blog.etechpath.com/?p=555 About:

HC-SR04 is an Ultrasonic sensor module which includes ultrasonic transmitter, receiver and control circuit in one small PCB.  HC-SR04 has distance measurement ability ranging from 2cm to 400cm. These small units are very accurate and its ranging accuracy can be reach to 3mm.




How HC-SR04 Works:

Ultrasonic modules work on principle similar to radar or sonar. It generates high frequency signal of 40kHz towards object and receives echo signal from subjects surface. Then it can calculate object distance using time interval between the sending of signal and the receiving of echo.




Theory:

To measure the object distance form sensor body we need to send a trigger pulse of 10uS, module will detect the trigger and will send out one 8cycle ultrasonic sound bust of 40kHz towards subject and receives echo of transmitted signal from objects surface. Now you can calculate distance from time interval between sending of signal and receiving of signal.

Distance Calculation:

Time=distance/speed

t = s / v

s = t . v

Speed of sound is 340m/s

v=340m/s = 0.034cm/uS

So, in order to calculate distance in cm, we need to multiply time with 0.034. But in this case time is distance between sensor and object in one way only and in our case the time value we will get from echo will be double because the sound wave needs to travel towards objects and bounce back to sensor. So, in our case the formula will be,

s = t . v/2

s = t . 0.034/2

Circuit Diagram:

Circuit Diagraam

Basic Code:

/*
* Ultrasonic Sensor HC-SR04 and Arduino Tutorial
*
* Crated by Pranay Sawarkar,
* www.eTechPath.com
*
*/
// defines arduino pin numbers
const int trigPin = 9;
const int echoPin = 8;
// defines variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
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;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}







]]>
https://blog.etechpath.com/how-to-interface-hc-sr04-ultrasonic-sensor-with-arduino/feed/ 0