Subhajit
Published © GPL3+

Social Distancing tool using Ultrasonic sensor and Arduino

We can easily maintain social distancing with this circuit. It shows the distance in OLED & starts buzzer when the distance becomes low.

IntermediateFull instructions provided6 hours13,930
Social Distancing tool using Ultrasonic sensor and Arduino

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Gravity I2C OLED-2864 Display
DFRobot Gravity I2C OLED-2864 Display
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Ultrasonic sensor HCSR04 Arduino distance measurement circuit

Ultrasonic sensor HCSR04 Arduino distance measurement circuit diagram

Code

Arduino Ultrasonic sensor 0.96-inch OLED Code

Arduino
Arduino Ultrasonic sensor code for 0.96-inch OLED display
/**********************************************************************************
 *  TITLE: Ultrasonic sensor HCSR04 and Arduino distance measurement (0.96" I2C OLED)
 *  Click on the following links to learn more. 
 *  YouTube Video: https://youtu.be/ajQh_uQ7nNI
 *  Related BLOG : https://easyelectronicsproject.com/arduino/ultrasonic-sensor-arduino-01
 *  by Tech StudyCell
 **********************************************************************************/


#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// define pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int greenLedPin = 13;
const int buzzerPin = 12;

// define Trigger Distance in CM
const int trigDistance = 15; //change this value from 2 to 400

// define variables
long duration;
int distance;
uint32_t delayMS;

void setup() {
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  pinMode(buzzerPin, OUTPUT); // Sets the greenLedPin as an Output
  pinMode(greenLedPin, OUTPUT); // Sets the redLedPin as an Output
  Serial.begin(9600); // Starts the serial communication
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)
  // init done
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.clearDisplay(); // Clear display buffer
  delayMS = 1000;
}

void ledOnOff(int distance){
  if(distance < trigDistance){
    digitalWrite(greenLedPin, LOW);   // turn the GREEN LED off
    digitalWrite(buzzerPin, HIGH);   // turn the RED LED on
    delay(100);
    digitalWrite(buzzerPin, LOW);   // turn the RED LED off
    delay(100);
    digitalWrite(buzzerPin, HIGH);   // turn the RED LED on
  }
  else{
    digitalWrite(greenLedPin, HIGH);   // turn the GREEN LED on
    digitalWrite(buzzerPin, LOW);   // turn the RED LED off
  }
}

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

  if (distance < trigDistance){
      ledOnOff(distance);
      display.setTextSize(1);
      display.setCursor(1,1);
      display.print("STAY BACK!"); 
      display.println();
      display.setTextSize(2);
      display.setCursor(15,15);  
      display.print(distance);     
      display.display();
      delay(delayMS);
      display.clearDisplay();
  }
  else{
      ledOnOff(distance);
      display.setTextSize(1);
      display.setCursor(1,1);
      display.print("Distance:"); 
      display.println();
      display.setTextSize(2);
      display.setCursor(15,15);  
      display.print(distance);     
      display.display();
      delay(delayMS);
      display.clearDisplay();
  }
}

Arduino Ultrasonic sensor 1.3-inch OLED Code

Arduino
Arduino Ultrasonic sensor code for 1.3-inch OLED display
/**********************************************************************************
 *  TITLE: Ultrasonic sensor HCSR04 and Arduino distance measurement (1.3" I2C OLED)
 *  Click on the following links to learn more. 
 *  YouTube Video: https://youtu.be/ajQh_uQ7nNI
 *  Related BLOG : https://easyelectronicsproject.com/arduino/ultrasonic-sensor-arduino-01
 *  by Tech StudyCell
 **********************************************************************************/

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>

#define OLED_RESET 4
Adafruit_SH1106 display(OLED_RESET);

// define pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int greenLedPin = 13;
const int buzzerPin = 12;

// define Trigger Distance in CM
const int trigDistance = 15; //change this value from 2 to 400

// define variables
long duration;
int distance;
uint32_t delayMS;

void setup() {
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  pinMode(buzzerPin, OUTPUT); // Sets the greenLedPin as an Output
  pinMode(greenLedPin, OUTPUT); // Sets the redLedPin as an Output
  Serial.begin(9600); // Starts the serial communication
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  display.begin(SH1106_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)
  // init done
  display.setTextSize(6);
  display.setTextColor(WHITE);
  display.clearDisplay(); // Clear display buffer
  delayMS = 1000;
}

void ledOnOff(int distance){
  if(distance < trigDistance){
    digitalWrite(greenLedPin, LOW);   // turn the GREEN LED off
    digitalWrite(buzzerPin, HIGH);   // turn the RED LED on
    delay(100);
    digitalWrite(buzzerPin, LOW);   // turn the RED LED off
    delay(100);
    digitalWrite(buzzerPin, HIGH);   // turn the RED LED on
  }
  else{
    digitalWrite(greenLedPin, HIGH);   // turn the GREEN LED on
    digitalWrite(buzzerPin, LOW);   // turn the RED LED off
  }
}

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

  if (distance < trigDistance){
      ledOnOff(distance);
      display.setTextSize(2);
      display.setCursor(1,1);
      display.print("STAY BACK!"); 
      display.println();
      display.setTextSize(5);
      display.setCursor(15,25);  
      display.print(distance);     
      display.display();
      delay(delayMS);
      display.clearDisplay();
  }
  else{
      ledOnOff(distance);
      display.setTextSize(2);
      display.setCursor(1,1);
      display.print("Distance:"); 
      display.println();
      display.setTextSize(5);
      display.setCursor(15,25);  
      display.print(distance);     
      display.display();
      delay(delayMS);
      display.clearDisplay();
  }
}

Credits

Subhajit

Subhajit

46 projects • 121 followers
I have studied Electrical Engineering. I do love to make different DIY electronics projects & share it on my YouTube channel Tech StudyCell.

Comments