How to make a DIY digital ruler using an ultrasonic sensor
Process of this Digital Ruler
When activated in this project, the ultrasonic sensor calculates the distance to the barrier in front of it. This distance is then taken by the Arduino board and displayed in centimeters and inches on the LCD screen.
OK, let’s do this project step by step. The required components are as follows.
- Arduino Nano board x 1 — Amazon / Our Store
- Ultrasonic sensor x 1 — Amazon / Our Store
- LCD display x 1 — Amazon / Our Store
- I2C module x 1 — Amazon / Our Store
- Male header x 1 — Amazon / Our Store
- Female header x 1 — Amazon / Our Store
- Dot board — Amazon /
- Circuit wires — Amazon / Banggood
- Jumper wires — Amazon / Our Store
- Foam board — Amazon /
- Cardboard — Amazon
Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.
Step 1
Firstly, identify these components.
Arduino Nano board
Ultrasonic sensor
LCD display
I2C module
Male header
Female header
Dot board
Circuit wires
Jumper wires
Foam board or Cardboard
Step 2
Secondly, cut the foam board or cardboard as follows.
Step 3
Thirdly, solder the female and male headers onto the dot board as shown below.
Step 4
Next, solder this circuit using wires. To do this, use the circuit diagram below.
Step 5
Now, adjust the 4.5 x 2.25-inch piece to fit this circuit and glue it.
Step 6
Then, glue the that piece and the 2.25 x 2.25-inch piece to the 4.5 x 2.25-inch piece.
Step 7
Next, adjust the foam board pieces to fit the LCD and the ultrasonic sensor. After, glue them. For that, use the pictures below.
Step 8
Then, connect them to the Arduino board. To do this, use the circuit diagram above.
Step 9
Now, glue a 2.25 x 2.25-inch piece and a 4.5 x 2.25-inch piece to the base foam board piece as shown below. After, glue the top piece of this digital ruler.
Step 10
Now, connect this project to the computer using a USB cable. Ok, let’s create the project for this project. It is as follows.
/*How to make a digital rular
* https://srituhobby.com
*/
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C dis(0x27, 16, 2);
#define Trig 2
#define Echo 3
void setup() {
pinMode(Trig, OUTPUT);
pinMode(Echo, INPUT);
dis.init();
dis.backlight();
dis.setCursor(2, 0);
dis.print("MEASURE YOUR");
dis.setCursor(4, 1);
dis.print("DISTANCE");
delay(2000);
dis.clear();
}
void loop() {
digitalWrite(Trig, LOW);
delayMicroseconds(4);
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
long t = pulseIn(Echo, HIGH);
long inch = t / 74 / 2;
long cm = t / 29 / 2;
dis.setCursor(0, 0);
dis.print("Distance: ");
dis.print(cm );
dis.print("cm ");
dis.setCursor(0, 1);
dis.print("Distance: ");
dis.print(inch );
dis.print("inch ");
}
Code explanation
Firstly, the I2C library is included. After, an object called “lcd” was created for this library. It includes the I2C address and the LCD length and width.
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C dis(0x27, 16, 2);
Secondly, ultrasonic sensor pins are defined.
#define Trig 2
#define Echo 3
In the setup function, Ultrasonic sensor pins are set as input and output pins.
pinMode(Trig, OUTPUT);
pinMode(Echo, INPUT);
Also, the LCD will start and it will show “Measure your distance”.
dis.backlight();
dis.setCursor(2, 0);
dis.print("MEASURE YOUR");
dis.setCursor(4, 1);
dis.print("DISTANCE");
delay(2000);
dis.clear();
In the loop function, the distances are obtained from the ultrasonic sensor and printed on the LCD.
void loop() {
digitalWrite(Trig, LOW);
delayMicroseconds(4);
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
long t = pulseIn(Echo, HIGH);
long inch = t / 74 / 2;
long cm = t / 29 / 2;
dis.setCursor(0, 0);
dis.print("Distance: ");
dis.print(cm );
dis.print("cm ");
dis.setCursor(0, 1);
dis.print("Distance: ");
dis.print(inch );
dis.print("inch ");
}
Step 11
OK, now select board and port, Afterward, upload this code to the Arduino board.
Step 12
Lastly, connect the power source to the digital ruler. For that, use a potential of 7.4v to 12v.
OK, enjoy this project. The full video guide is given below. So, we will meet in the next tutorial. Have a good day.