Components Require
- 1. Arduino Uno Board
- 2. Ultrasonic Sensor HC-SR04
- 3. 0.96″ OLED Display
- Breadboard
- 5. Connecting Wires
- 6. 5V Power Supply
Ultrasonic Sensor HC-SR04:
Description:
The HC-SR04 ultrasonic sensor uses sonar to determine the distance to an object like bats do. It offers excellent non-contact range detection with high accuracy and stable readings in an easy-to-use package.
From 2cm to 400 cm or 1” to 13 feet. Its operation is not affected by sunlight or black material like sharp rangefinders are (although acoustically soft materials like cloth can be difficult to detect). It comes complete with the ultrasonic transmitter and a receiver module.
0.96″ I2C OLED Display:
This is a 0.96 inch blue OLED display module. The display module can be interfaced with any microcontroller using SPI/IIC protocols. It is having a resolution of 128×64. The package includes display board, display,4 pin male header pre-soldered to board.
OLED (Organic Light-Emitting Diode) is a self light-emitting technology composed of a thin, multi-layered organic film placed between an anode and cathode. In contrast to LCD technology, OLED does not require a backlight. OLED possesses high application potential for virtually all types of displays and is regarded as the ultimate technology for the next generation of flat-panel displays.
Circuit: Interfacing Ultrasonic Sensor with Arduino & OLED
CODE
/*
* Created by ArduinoGetStarted, https://arduinogetstarted.com
*
* Arduino – Ultrasonic Sensor HC-SR04
*
* Wiring: Ultrasonic Sensor -> Arduino:
* – VCC -> 5VDC
* – TRIG -> Pin 9
* – ECHO -> Pin 8
* – GND -> GND
*
* Tutorial is available here: https://arduinogetstarted.com/tutorials/arduino-ultrasonic-sensor
*/
int trigPin = 9; // TRIG pin
int echoPin = 8; // ECHO pin
float duration_us, distance_cm;
void setup() {
// begin serial port
Serial.begin (9600);
// configure the trigger pin to output mode
pinMode(trigPin, OUTPUT);
// configure the echo pin to input mode
pinMode(echoPin, INPUT);
}
void loop() {
// generate 10-microsecond pulse to TRIG pin
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// measure duration of pulse from ECHO pin
duration_us = pulseIn(echoPin, HIGH);
// calculate the distance
distance_cm = 0.017 * duration_us;
// print the value to Serial Monitor
Serial.print(“distance: “);
Serial.print(distance_cm);
Serial.println(” cm”);
delay(500);
}