Ultrasonic Distance Sensor & Display

A fun way to measure distance

BeginnerFull instructions provided1 hour2,149
Ultrasonic Distance Sensor & Display

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
idIoTware Shield
idIoTware Shield
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
128*64 i2c Oled Monochrome display
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Ultasonic Distance Sensor & Display Code

Arduino
#include<NewPing.h>
#include"U8glib.h"

#define TRIGGER_PIN 3
#define ECHO_PIN 2
#define MAX_DISTANCE 200

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST); // Fast I2C / TWI

void setup() {
  u8g.setRot180();
}

void draw(int distance) {
  u8g.setFont(u8g_font_fur30);
  u8g.setPrintPos(0, 63);
  u8g.print(distance);
  u8g.print("cm");
}

void loop() {
  int dist = sonar.ping_cm();
  u8g.firstPage();
  do {
    draw(dist);
  }
  while (u8g.nextPage());

  delay(100);
}

Credits

Rithwik Rayani

Rithwik Rayani

1 project • 2 followers
Rupin Chheda

Rupin Chheda

35 projects • 83 followers
Resident Maker at CuriosityGym! Electronics Engineer, CAD Modeller, Educator
IdiotWare Team

IdiotWare Team

20 projects • 16 followers
The Idiotware Shield is a learning platform for quickly bringing to life hundreds of Arduino projects, whether you’re a novice or expert.
Aditya Ray

Aditya Ray

1 project • 4 followers

Comments