Digital Ruler - Arduino Project

Digital Ruler

The digital ruler is a simple Arduino based project which measures the distance between an ultrasound distance sensor and an object and then projects it onto an LCD. It is a simple project which I designed in the span of about 30 to 45 minutes.




All you will need for this project is:


  • An Arduino UNO
  • An LCD (Liquid Crystal Display specifically LCM1602C)
  • An Ultrasound Distance Sensor (Specifically HC-SR04)
  • A Potentiometer (10kΩ)
  • Wires
  • And the open-source Arduino Software (You can download it for free at https://www.arduino.cc/en/Main/Software)
  • A 220  Ω  Resistor
You first wire up your components as shown in the diagram below.



Even though I showed you the diagram of how the device should be wired, you should check what to plug in where on your device. I will explain why everything is wired in the way it is.

Let's start with the Ultrasound Distance Sensor. It first needs to be connected to the circuit. You first connect the Ultrasound Distance Sensor to the GND wire and then you connect it with the 5V wire (These wires have already been connected to the breadboard. This means that you just need to plug the Ultrasound Distance Sensor into the right holes in the breadboard.) You then connect the Ultrasound Distance Sensor's Trig pin into the digital inlet 8 . You then connect the Ultrasound Distance Sensor's Echo pin into the digital inlet 9

The potentiometer is connected very similarly. You first connect it to the GND wire then the 5V wire. You then connect it to the Vee pin on the LCD. This potentiometer is used to change the contrast of the LCD screen.

The LCD screen is connected through many pins. The first pin (GND) connects to the GND wire, the second pin (Vcc) connects to the 5V wire. The third (Vee) connects to the potentiometer. The fourth pin (RS) connects to the digital outlet 12 on the arduino, this is to state where the characters will appear. The fifth pin (R/W) connects to the GND wire. The sixth pin (E) connects to the digital outlet 11 on the arduino, this sets the LCD into a state where it is able to project things. Pins DB0 through DB7 connect to the digital inlets on the Arduino board. The BL+ pin connects to the 5V wire through a 220Ω resistor and the BL- pin connects to the GND wire.

Here are some pictures of how I wired up my arduino.







When your board is wired up you can start coding. Here is the simplest version of the code.

#include <LiquidCrystal.h>          /*You include the LCD library*/
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);          /*Here you set which pins the LCD uses*/

#define trigPin 8          /*Here you define the pins ans trig and echo pin*/
#define echoPin 9

int pos = 0;          /*Here you set your text position variables*/
int inc = 1;

void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
  pinMode(trigPin, OUTPUT);          /*You set your pins as either inpu ot output*/
  pinMode(echoPin, INPUT);
}

void loop() {
  lcd.clear();
  lcd.setCursor(0, 0);

  float distance = measureDistance();

  if (distance >= 500 || distance <= 0){          /*If the distance is out of the sensors range it will say that the object is out of range*/
    lcd.print("Out of range");
  } else {
    lcd.print("Dist: ");          /*You print your distance here*/
    lcd.print(distance);
    lcd.print(" cm");
  }
float measureDistance() {
  float duration, distance;
  digitalWrite(trigPin, LOW);  
  delayMicroseconds(2); 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW);
  
  duration = pulseIn(echoPin, HIGH);
  return (duration/2) / 29.1;
}


Here is the version of the code with your signature.

#include <LiquidCrystal.h>          /*You include the LCD library*/
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);          /*Here you set which pins the LCD uses*/

#define trigPin 8          /*Here you define the pins ans trig and echo pin*/
#define echoPin 9

int pos = 0;          /*Here you set your text position variables*/
int inc = 1;

void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
  pinMode(trigPin, OUTPUT);          /*You set your pins as either inpu ot output*/
  pinMode(echoPin, INPUT);
}

void loop() {
  lcd.clear();
  lcd.setCursor(0, 0);

  float distance = measureDistance();

  if (distance >= 500 || distance <= 0){          /*If the distance is out of the sensors range it will say that the object is out of range*/
    lcd.print("Out of range");
  } else {
    lcd.print("Dist: ");          /*You print your distance here*/
    lcd.print(distance);
    lcd.print(" cm");
  }
 lcd.setCursor(pos, 1); /*Here you animate the bottom text by moving it left and right*/ 
 pos = pos + inc; 
 if (pos == 6 || pos == 0){ 
 inc = inc * (-1);
 } lcd.print("By Someone");
  delay(500);
}

float measureDistance() {
  float duration, distance;
  digitalWrite(trigPin, LOW);  
  delayMicroseconds(2); 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW);
  
  duration = pulseIn(echoPin, HIGH);
  return (duration/2) / 29.1;
}

Komentáre