## Introduction: Interface Ultrasonic Sensor With OLED Using ArduinoUNO

Holla!!

This is my another Instructable on electronics. In this instructable, we will learn how to Interface Ultrasonics sensor with Arduino UNO and displaying the values in OLED.

Let's begin!

## Step 1: Components Required

Here is the list of components required to get started with the Instructable,

Hardware Components

• Arduino Uno
• Ultrasonic sensor(HC-SR04)
• 0.96” SSD1306 OLED
• Jumper Wires
• Micro USB Cable

Software Components

• Arduino IDE

## Step 2: ​How It Works – Ultrasonic Sensor

It emits an ultrasound at 40 000 Hz which travels through the air and if there is an object or obstacle on its path It will bounce back to the module. Considering the travel time and the speed of the sound you can calculate the distance.

The HC-SR04 Ultrasonic Module has 4 pins, Ground, VCC, Trig and Echo. The Ground and the VCC pins of the module needs to be connected to the Ground and the 5 volts pins on the Arduino Board respectively and the trig and echo pins to any Digital I/O pin(5 and 6) on the Arduino Board.

In order to generate the ultrasound, you need to set the Trig on a High State for 10 µs. That will send out an 8 cycle sonic burst which will travel at the speed sound and it will be received in the Echo pin. The Echo pin will output the time in microseconds the sound wave traveled.

## Step 3: Calculation

• Speed of sound in air - 340m/s(0.034cm/microsecond)
• Time(duration) = distance/speed
• distance = time * 0.034

You divide by two because it goes out and back so the time would be double that of a one-way travel

• distance = ( time * 0.034) / 2

## Step 4: Ultrasonic Connection

The connection of Ultrasonic sensor is very simple and the connection has followed.

1. Vcc - connected tothe 5V pin of Arduino.
2. GND - connected to the GND pin of Arduino.
3. Trig - connected to the Digital pin 5 of Arduino.
4. Echo - connected to the Digital pin 6 of Arduino.

## Step 5: OLED Connection

For OLED connections follow the scheme below.

• CS pin of the OLED is connected to Digital Pin 8 of the ArduinoUNO.
• DC of the OLEDis connected to Digital Pin 9 of the ArduinoUNO.
• Reset of the OLED is connected to Digital Pin 10 of the ArduinoUNO.
• SDA of the OLEDis connected to Digital Pin 11 of the ArduinoUNO.
• SCK of the OLEDis connected to Digital Pin 12 of the ArduinoUNO.
• VDD of the OLED is connected to Digital 5v of the ArduinoUNO.
• GND of the OLED is connected to ground (GND) pin of the ArduinoUNO.

For further library setup, check my previous Instructable.

"Interfacing OLED Display Using ArduinoUNO"

## Step 6: CODE

```#include <U8glib.h>

U8GLIB_SSD1306_128X64 u8g(12, 11, 8, 9, 10);
const int trigPin = 5;
const int echoPin = 6;
long duration;
int distance;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}

void loop(){
u8g.firstPage();

/* Keep looping until finished drawing screen */
do
{

u8g.setFont(u8g_font_osb18);

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

char ultr_str[8];
itoa(distance,ultr_str,10);
u8g.drawStr(0, 20, "Dist:");
u8g.drawStr(60, 20, ultr_str);
u8g.drawStr(75, 20, "cm");
delay(1000);

} while(u8g.nextPage());
}```

## Step 7: OUTPUT

Thank YOU!!

Let's meet with another cool Instructable. Hope you liked it.