Skip to content

Instantly share code, notes, and snippets.

@siuying
Created November 2, 2011 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save siuying/b3a95a1bdc7bcbb9a9bd to your computer and use it in GitHub Desktop.
Save siuying/b3a95a1bdc7bcbb9a9bd to your computer and use it in GitHub Desktop.
Arduino distant measure using US-100
// Use US-100 to find distance and display into LCD
// LCD Ref: http://www.dfrobot.com/wiki/index.php?title=I2C/TWI_LCD1602_Module_%28SKU:_DFR0063%29
// US-100 Ref: http://www.doc88.com/p-610738935623.html
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Setup of LCM1602 driven by PCF85741
// A4 - SDA
// A5 - SCL
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Setup US-100
// D2 - ECHO
// D3 - TRIG
unsigned int ECHO = 2;
unsigned int TRIG = 3;
// used for calculate distance
unsigned long time_echo_us = 0;
unsigned long len_mm = 0;
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
pinMode(ECHO, INPUT);
pinMode(TRIG, OUTPUT);
}
void loop() {
digitalWrite(TRIG, HIGH);
delayMicroseconds(50);
digitalWrite(TRIG, LOW);
time_echo_us = pulseIn(ECHO, HIGH);
if (time_echo_us > 1) {
len_mm = (time_echo_us * 34 / 100) / 2;
lcd.clear();
lcd.print("Dist: ");
lcd.print(len_mm, DEC);
lcd.println("mm");
}
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment