/* OLED_Distance_HC-SR04.ino
HC-SR04 distance sensor connections:
Vcc pin to +5V
Trigger pin to an arduino digital IO
Echo pin to an arduino digital IO
GND pin to GND
Trigger sends a 5V 10 microseconds pulse
transmitted as (40kHz) pulses
Echo outputs a pulse of 150 microseconds to 25 milliseconds
this pulse width relates to distance
Echo pulse times out after 38 milliseconds with no detection
only the return trip is used, so half the total time
distance = (echo_pulse_time/2) * speed of sound
Let's assume we measure an echo pulse time of 500 microseconds
(would be a distance of about 8.54 cm)
at 20C the speed of sound is 343m/sec or 0.0343 cm/microsec
distance (in cm) = (500/2)*0.0343
Other option ...
the sound takes 29.154 microseconds to travel one centimeter
distance (in cm) = (500/2)/29.154
in case you need it: 2.54 centimeters is one inch
Use a box with smooth walls in front of sensor and a ruler
OLED display ...
The 128x64 pixel OLED display uses an SSD1306 driver.
White Color 0.96 Inch Oled Display Module 128*64 I2C
Serial bus (from Walmart online $10.95). Type = JMD0.96A-1
(all white seems to be more readable)
Or ..
Yellow on top 16 rows and blue on bottom 48 rows 128x64 OLED
display using an SSD1306 driver IC (Walmart $8.95), has
1 character less width. Type = GME12864-43
Display has 4 pins (l to r):
1 GND
2 VCC +5V
3 SCL I2C clock line to UNO pin A5
4 SDA I2C data line to UNO pin A4
On an arduino UNO and Nano: A4(SDA), A5(SCL)
(defined in wire.h)
see...
https://www.youtube.com/watch?v=6F1B_N6LuKw
also see DroneBot Workshop ...
https://www.youtube.com/watch?v=7x1P80X1V3E
WOKWI simulator needs some work with out of range
*/
// needed to use the I2C bus for the OLED display
#include <Wire.h>
// use OLED library header
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width in pixels
#define SCREEN_HEIGHT 64 // OLED display height in pixels
// The pins for I2C are defined by the Wire-library.
// On an arduino UNO/Nano: A4(SDA), A5(SCL)
// some UNOs have extra pins just to the left of the AREF pin
// reset pin is pin4 (or -1 if sharing UNO reset pin)
// actually not used any longer, but has to be declared in the
// object/instance creation
#define OLED_RESET -1
// check datasheet or use the "I2C_address_scanner" program
#define SCREEN_ADDRESS 0x3C
// define an object (class instance)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// color of jumper wires will help reconnect the module
#define trigPin 12 // yellow jumper wire to UNO digital IO pin 12
#define echoPin 11 // green jumper wire to UNO digital IO pin 11
float duration, distance;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// SSD1306_SWITCHCAPVCC generates display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
}
void loop() {
// build the buffer
displayDistance();
// display what is in the buffer
display.display();
}
void displayDistance() {
// initiate trigger pin LOW
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// write a 10 microsecond pulse to HC-SR04 trigger pin
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// then measure the response time for the echo
// how many microseconds does it stay HIGH?
duration = pulseIn(echoPin, HIGH);
// calculate distance
// speed of light is 0.0343 cm/microsecond
distance = (duration / 2) * 0.0343;
// there are 2.5 cm per inch
float distanceInch = distance / 2.54;
// result to Serial Monitor (Tools tab)
Serial.print("Distance = ");
// sensor range is specified as 2 to 400 cm
// might need to experiment with this spec.
if (distance >= 400 || distance <= 2) {
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
// show above result on the OLED display ....
// set the color, use white with monochrome displays
// does not effect the yellow/blue OLED's colors
// this OLED has pixel-lines 0 to 15 yellow, the rest blue
display.setTextColor(WHITE);
// set font size
// displays 21 characters per text-line for TextSize(1)
// total 8 text-lines/diplay panel
// displays 10 characters per text-line for TextSize(2)
// total 4 text_lines/display panel
// excess text will wrap to next text-line
display.setTextSize(1);
// clear the buffer
display.clearDisplay();
// sensor range is specified as 2 to 400 cm
if (distance >= 400 || distance <= 2) {
// top left setCursor(0, 0) then for next text-line use
// setCursor(0, 10) or setCursor(0, 8) and so on
display.setCursor(0, 0);
display.print("Out of range ...");
} else {
// set the cursor coordinates
// x = 0, line y = 0
display.setCursor(0, 0);
display.print("Distance: ");
display.print(distance, 1);
display.print(" cm");
// uses about 10 pixels per text-line to display characters well
display.setCursor(0, 10);
display.print("Distance: ");
display.print(distanceInch, 1);
display.print(" inch");
}
// stabilizes OLED display
delay(1000);
}