This is a distance calculator(range 30cm), made with arduino, ultrasonic sensor and oled display... It is a beginner level project....
Things used in this project
Hardware components
HARDWARE LIST
1 Arduino UNO
1 DFRobot Gravity I2C OLED-2864 Display
1 SparkFun Ultrasonic Sensor - HC-SR04
8 Jumper wires (generic)
Story
Just made it for you all :) enjoy!! You are free to ask anything(related to it)
Schematics
Circuit diagram
Connect according to it :)
Code
Code
Arduino
Upload it with arduino ide
CODE
#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);
}
The article was first published in hackster, December 13, 2022
cr: https://www.hackster.io/jasrajdevs/arduino-distance-calculator-2f793c
author: jasrajdevs
License
All Rights
Reserved
0