Digital distance measuring device using ultrasonic sensor SR04, OLED display and Arduino

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

Leave a Reply

Your email address will not be published. Required fields are marked *