Hackster is hosting Hackster Holidays, Ep. 5: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 5 on Friday!
jasrajdevs
Published © GPL3+

Arduino Distance Calculator

This is a distance calculator(range 30cm), made with arduino, ultrasonic sensor and oled display... It is a beginner level project....

BeginnerFull instructions provided30 minutes320
Arduino Distance Calculator

Things used in this project

Story

Read more

Schematics

Circuit diagram

Connect according to it :)

Code

Code

Arduino
Upload it with arduino ide
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
int a;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

const int trigPin = 9;
const int echoPin = 10;
long duration;
int distanceCm, distanceInch;

void setup() {
  


pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
}

void loop() {
  
digitalWrite(trigPin,HIGH);

delayMicroseconds(10);

digitalWrite(trigPin,LOW);

duration = pulseIn(echoPin, HIGH);
distanceCm= duration*0.034/2;
if(distanceCm>30){
  distanceCm=0;
}
if(distanceCm!=a){
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.print("Distance:-");
display.setCursor(0,17);
display.print(distanceCm);
display.display();  
}

a = distanceCm;
delay(100);
}

Credits

jasrajdevs

jasrajdevs

7 projects • 10 followers
Developing since 2020 july..... I am a student.

Comments