5vdc
Published

Distance Detection Circuit with HC-SSR04 and Micro OLED

This is a prototype that measures distances in meters and inches, and prints the distances on a screen.

IntermediateFull instructions provided283
Distance Detection Circuit with HC-SSR04 and Micro OLED

Things used in this project

Hardware components

Arduino UNO & Genuino UNO
Arduino UNO & Genuino UNO
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×1
SparkFun Micro OLED Breakout
SparkFun Micro OLED Breakout
×1

Software apps and online services

Arduino IDE
Arduino IDE
sparkfun micro oled library

Story

Read more

Schematics

screen hookup

HC-SR04 hookup

Code

Code

C/C++
#include <Wire.h>

#include <SPI.h>

#include <SFE_MicroOLED.h>
#define PIN_RESET 9  // Connect RST to pin 9
#define PIN_DC    8  // Connect DC to pin 8
#define PIN_CS    10 // Connect CS to pin 10
#define DC_JUMPER 0
MicroOLED oled(PIN_RESET, PIN_DC, PIN_CS); // SPI declaration
//MicroOLED oled(PIN_RESET, DC_JUMPER);    // I2C declaration

int SCREEN_WIDTH = oled.getLCDWidth();
int SCREEN_HEIGHT = oled.getLCDHeight();


#define trigPin 3
#define echoPin 2


void setup() 
{
 // Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  oled.begin();
 oled.clear(PAGE);
 oled.display(); 
 oled.clear(PAGE);
  oled.clear(ALL);
//arduino  logo 
  oled.pixel(12,11);
  oled.pixel(13,11);
  oled.pixel(14,11);
  oled.pixel(15,11);
  oled.pixel(19,11);
  oled.pixel(20,11);
  oled.pixel(21,11);
  oled.pixel(22,11);
  oled.pixel(11,12);
  oled.pixel(16,12);
  oled.pixel(18,12);
  oled.pixel(20,12);
  oled.pixel(23,12);
  oled.pixel(11,13);
  oled.pixel(12,13);
  oled.pixel(13,13);
  oled.pixel(14,13);
  oled.pixel(16,13);
  oled.pixel(17,13);
  oled.pixel(18,13);
  oled.pixel(19,13);
  oled.pixel(20,13);
  oled.pixel(21,13);
  oled.pixel(23,13);
  oled.pixel(11,14);
  oled.pixel(16,14);
  oled.pixel(18,14);
  oled.pixel(20,14);
  oled.pixel(23,14);
  oled.pixel(12,15);
  oled.pixel(13,15);
  oled.pixel(14,15);
  oled.pixel(15,15);
  oled.pixel(19,15);
  oled.pixel(20,15);
  oled.pixel(21,15);
  oled.pixel(22,15);
  oled.circle(17, 13, 8); 
  oled.setFontType(0);  // Set the text to small (10 columns, 6 rows worth of characters).
oled.setCursor(11, 30);
oled.print("ARDUINO");
  oled.display();
  oled.clear(PAGE);
  //arduino logo
  delay(2000);
}
void loop() 
{
  oled.clear(PAGE);
  oled.clear(ALL);

  
  long duration, distance;
  digitalWrite(trigPin, LOW);        
  delayMicroseconds(2);              
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);           
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  
//Serial.print(distance/100.0);
// Serial.println(" m");

//Serial.print(distance/2.54);
//Serial.println(" in.");


  oled.setFontType(0);  // Set the text to small (10 columns, 6 rows worth of characters).
oled.setCursor(0, 0);  // Set the text cursor to the upper-left of the screen.
oled.print(distance/100.0); // print distance in meters
oled.print(" m    ");
oled.print(distance/2.54); // Print distance in inches
oled.print(" in.");
oled.display();

  delay(100);
}

Credits

5vdc

5vdc

1 project • 0 followers

Comments