Project tutorial

Distance Sensor and OLED © GPL3+

Short and easy, read distance 2 - 400 cm and plot it in I2C OLED screen.

  • 22,053 views
  • 22 comments
  • 65 respects

Components and supplies

Apps and online services

About this project

How to use OLED screen

Here you have my previous tutorial about it:

  • #includes and #defines, before run setup():
#include <SPI.h>   //we need all those nasty libraries for OLED
#include <Wire.h> 
#include <Adafruit_GFX.h> 
#include <Adafruit_SSD1306.h>  
#define OLED_RESET 4 // this is the reset pin, IM NOT USING IT
Adafruit_SSD1306 display(OLED_RESET); 
  • In the setup() function:
 display.begin(SSD1306_SWITCHCAPVCC, 0x3C); 
//initialize with the I2C addr 0x3C (128x64) 
 display.clearDisplay();  // clear the display before start
  • In loop() function:
display.setCursor(22,20);        //x,y coordinates
 display.setTextSize(3);         //size of the text
 display.setTextColor(WHITE);    //if you write BLACK it erases things
 display.println(distance);      //print our variable
 display.setCursor(85,20);       //set size,print the units (cm/in)
 display.setTextSize(3); 
 #ifdef CommonSenseMetricSystem//if theres#define CommonSenseMetricSystem
 display.println("cm");        //print "cm" in oled
 #endif 
 #ifdef ImperialNonsenseSystem//if there´s#define ImperialNonsenseSystem
 display.println("in");       //print "in" in oled
 #endif 
 display.display();          //you need to actually display all that data
 delay(500);                 //wait!, human speed
 display.clearDisplay();      //clear black the display

How to use HC-SR04 ultrasonic range

Here you have the datasheet, the HC module sends a burst of pulses and then measures the time that the ultrasound´s echo takes to return to its original place.

  • Make a pulse for HC trigger, the HC will do a pulse burst:
 long duration, distance; //our beloved variables
 digitalWrite(trigPin, LOW);  //PULSE ___|---|___ 
 delayMicroseconds(2);  
 digitalWrite(trigPin, HIGH); 
 delayMicroseconds(10);  
 digitalWrite(trigPin, LOW); 
  • PulseIn() is a rare used function, detects the length of the step high (__----__) or low (----___---)

We use #ifdef for only compiling the parts that we need.

duration = pulseIn(echoPin, HIGH); //
 #ifdef CommonSenseMetricSystem 
 distance = (duration/2) / 29.1; 
 #endif 
 #ifdef ImperialNonsenseSystem 
 distance = (duration/2) / 73.914; 
 #endif 
  • And this for debugging in case your Oled is not working:
Serial.println(distance);//debug 

Code

Distance ultrasound measure and displayArduino
Main code
//CODE BY Javier Muñoz Sáez,05/11/2016 questions to javimusama@gmail.com
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define CommonSenseMetricSystem
//#define ImperialNonsenseSystem

#define trigPin 13
#define echoPin 12

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //initialize with the I2C addr 0x3C (128x64)
  display.clearDisplay();

}

void loop() {
  long duration, distance;
  
  digitalWrite(trigPin, LOW);  //PULSE ___|---|___
  delayMicroseconds(2); 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW);
  
  duration = pulseIn(echoPin, HIGH);
  
  #ifdef CommonSenseMetricSystem
  distance = (duration/2) / 29.1;
  #endif
  #ifdef ImperialNonsenseSystem
  distance = (duration/2) / 73.914;
  #endif

  display.setCursor(22,20);  //oled display
  display.setTextSize(3);
  display.setTextColor(WHITE);
  display.println(distance);
  display.setCursor(85,20);
  display.setTextSize(3);
  
  #ifdef CommonSenseMetricSystem
  display.println("cm");
  #endif
  #ifdef ImperialNonsenseSystem
  display.println("in");
  #endif
  
  display.display();

  delay(500);
  display.clearDisplay();
 
  Serial.println(distance);//debug
  
 

}

Schematics

Schematic
Oenoct2uzp1uptbebuxk

Comments

Similar projects you might like

Arduino Mp3 player + Distance sensor = FUN

Project tutorial by javier muñoz sáez

  • 21,576 views
  • 23 comments
  • 46 respects

Use a PC Fan as a Sensor

by Daniel Porrey

  • 48,775 views
  • 22 comments
  • 100 respects

Displaying Sensor Values on LCD

by Wimpie van den Berg

  • 42,769 views
  • 4 comments
  • 36 respects

Liquid Level Sensing Using a Laser ToF Sensor

Project tutorial by Team ProtoCentral

  • 22,160 views
  • 16 comments
  • 42 respects

Parking Sensor with Arduino

Project showcase by VINICIUS LOPES

  • 9,934 views
  • 4 comments
  • 17 respects
Add projectSign up / Login