Pocket-Sized Ultrasonic Ruler




About: I'm a student at UW-Milwaukee studying computer science with a passion for electronics. I'm always working on a project or thinking of new ideas. If you have any questions about any of my instructables, or w...

Today everything is digitalized. Most of us don't bother to waste our time using maps, going to the record store, or even going to the DVD store anymore. So... why are so many people using tape-measures and rulers? Yes, they are reliable, but they are certainly not convenient. With innovations, such as ultrasonic range sensors, we have entered a time where you don't have to bother getting out your old tape-measurer. Using this pocket-sized ultrasonic ruler, you can simply point at any object, click a button, and the distance will be displayed on the 8 LED display. And you don't have to pay hundreds of dollars, like most commercially available ultrasonic measurers. This ruler is the smallest (5cm by 7cm) and cheapest (about 5 USD) ultrasonic measuring device available today.

Although the ultrasonic range sensor is widely used, I haven't seen many DIY devices that simply act as rulers. Its main application seems to be in robotics. However, in robotics typically the precision of the sensor is not fully used. I found that this sensor is so precise that its ability to determine distance is enough to make a ruler. This prototype is accurate to +/- one centimeter, which is great for quick measurements and can be more efficient than a physical ruler.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Parts

You will need these parts:

-Attiny85 (with socket)

-74hc595 Shift Register

-7805 Voltage Regulator

-HC-SR04 Ultrasonic Range Sensor

-Eight 330 Ohm Resistors

-One Tactile-Switch Button

-One Two-way Slide Switch

-Eight LEDs

-One Indicator LED (with 2k resistor)

-Perfboard (5cm by 7cm)

-9v Battery (with connector)

You will need these tools:


-Soldering Iron (with rosin)

-Another Arduino (for programming attiny)

-Hot Glue Gun

You will need these skills:

-How to program an attiny

-How a 74hc595 works

-Basic arduino programming skills

Step 2: Pre-Making (Planning)

Before we get into the schematic, lets go over exactly what we will be making. This step is essential since there are several changes you may want to make to this design before even breadboarding the circuit. My design will use the following:

1. A eight LED binary display (controlled with a shift register)

2. 9v Battery

3. Will read a distance measured in CENTIMETERS

The "binary display" is basically what it sounds like. The value from this measuring device will be displayed in binary. If you feel that this is a problem, you could replace the binary display with a 7-segment display or an lcd. However, I personally like the style of the binary display and it helps me to be more fluent in reading binary which is why I chose it. (This DOES limit your distance to 255 though)

I used a 9v battery since I love how easy it is to interchange 9v batteries via their connectors; however, 2 or 3 AA or AAA batteries would give you a longer battery life. Or to make this even more portable you could use coin batteres (such as a 2032).

Once you have given these issues some thought we can move onto breadboarding the circuit!

Step 3: Prototype the Prototype (Breadboarding)

Lets build the circuit on a breadboard before we think about making anything on perfboard. Be sure to check out the next two steps for the schematic and code. As I said in the first step, I expect that you already know how to program an attiny and how a shift register works.

As a reference you can see the pictures attached to this step, since it is the real circuit; however, it is rather hard to see with all the wires and where they go.

Once you are finished, be sure to keep your breadboarded circuit together since we still have to test its measuring capabilities and it is nice to have it around for when we solder!

Step 4: Schematic

In the attached picture you will see the schematic I made for this circuit. For simplicity I'll explain it in three parts:

1. Voltage Regulator

2. The 74HC595 and LEDs

3. The HC-SR04

The circuit for the voltage regulator is straight forward. All you have to do there is connect the positive terminal of the battery to the VIN (Voltage IN) and the GROUND to the negative terminal. (Be sure to connect a two-way switch before as an ON/OFF switch). You may add two capacitors to smoothen out the current through the voltage regulator, but that is not necessary in this circuit.

For the 74HC595, connect the clock line to attiny's physical pin 5 and the data line to pin 6. Then all the outputs of the shift register to the LED's positive leads (via 330 ohm resistors). For the "latch" pin on the shift register, we are going to connect a button. This is so that when the user wants to know the distance, it will be latched out when the button is pressed. (It saves us pins on the attiny). So we will attach a button (which is attached to ground via a 10k ohm resistor) which when pressed is connected to 5v.

The HC-SR04 only has four pins to worry about. Connect the ground to ground and VCC to 5v. Then connect the TRIG pin to the attiny's physical pin 7 and the ECHO pin to pin 3.

When you have finished implementing the circuit in this schematic, you can upload the code shown in the following step!

Step 5: The Code

The program for this circuit is quite small and simple. I'm not going to take the time to explain how the code interacts with the HC-SR04 since it has been done many times before (give it a google search), but I'll go over it briefly.

Basically, all the code does is every 500 milliseconds, the distance between the HC-SR04 and an object infront of it is shifted out to the shift register via the data and clock pins attached to the attiny85. When the user presses the button on the device, they are actually activating the "latch" on the 74hc595. This illuminates the need for attaching the latch pin to the attiny and attaching a button to the attiny85. Some good ol' discrete logic!

The code is attached to this step below feel free to use it anyway you'd like.

Step 6: Testing (How to Read Binary)

Before we think about soldering a final implementation of our circuit, lets make sure it is accurate. To measure, I taped a tape measure to the ground and checked if the number on the ultrasonic ruler matched the values on the tape measure. Keep in mind, the ruler may be off by + or - one centimeter. But this is expected, so don't be worried.

For those of you who are not familiar with binary:

As I mentioned before our device will read out values in binary. Don't worry if you do not know how to read binary since it is easy.

So... we have eight LEDs here. Lets call each LED a "bit" for now. And lets name the "bit" closest to the HC-SR04 is the Most Significant Bit (MSB). Now I'm going to assign each of these "bit"s a value. The MSB will have a value of 1 (2 to the power of 0), and each bit to the right of that will be the following: 2, 4, 8, 16, 32, 64, 128. Whenever an "bit" is ON just add its value to the answer . Now lets think of these "bit"s as LEDs again.

Lets read a value with the ultrasonic ruler so that some LEDs become lit. In the first attached picture on this step, you see that the 6th LED is ON. So lets just see what value the 6th LED has and add it to our answer. As I mentioned before, the value for the 6th "bit" is 32. 32 + 0 = 32. So our ruler is telling us that it is 32cm away from an object. If this is still a foggy idea to you, there are plenty of resources online on binary.

Step 7: Planning the Perfboard

"Planning makes perfect!". Well that's not really how the phrase goes... but I know planning makes a great perfboard layout! So before we solder lets check out how we can use the perfboard space to make the most useable design. For my layout, I didn't have all that many options. The main concerns for laying out this project on perfboard are:

1. Having space for one row of 8 LEDs (for the binary display)

2. Ensuring the HC-SR04 can face outwards straightly

3. Basic component placement (finding a good place for the 74hc595 and attiny85)

Once you have met those guidelines for your board you are all set to begin soldering!

Step 8: Soldering

Time to put the circuit together one last time and lets make sure you don't have to fix it later. While soldering be sure to:

-Keep all ICs and LEDs flush against the perfboard.

-Make sure no resistor leads are able to create any shorts (hot glue helps for this)

-Refer back to the schematic often. I often keep my breadboarded circuit close by to make sure I don't misread anything.

Once you are finished you may want to go back to the "Testing" step and make sure your device is returning the proper values. If it is we can move on to the next step!

Step 9: A Case (Optional)

Finally, we can finish our device by creating a case. I decided to leave my device case-less, since like most people I don't have a 3d printer or laser cutter or any other sort of CNC. However, if you do have any of those tools, I'd love to see what kind of cases you can come up with.

If you are looking to make a simple case without any expensive tools, I usually make a small acrylic back panel for my "pocket-sized' projects. Simply cut a piece of acrylic to the dimensions of your perfboard, and screw it in as a back panel.

Step 10: Measure Stuff!

Now you can put away all those old rulers and tape measurers and enter the electronic world of measuring! Next time you are asked to take a quick measurement, all you have to do is click a button instead of searching for those old measuring tools. I hope you learned crafty ways to use the 74HC595, attiny85 and the HC-SR04. If you think this instructable is worthy, give me a vote for either the Epilog, Battery Powered, or Sensor Contests! Thank you for your interest in my project!

Sensors Contest

Second Prize in the
Sensors Contest

Battery Powered Contest

Participated in the
Battery Powered Contest

Epilog Challenge VI

Participated in the
Epilog Challenge VI



  • Indoor Lighting Contest

    Indoor Lighting Contest
  • Make It Fly Challenge

    Make It Fly Challenge
  • Growing Beyond Earth Maker Contest

    Growing Beyond Earth Maker Contest

47 Discussions


3 years ago

great project

can i place your project on my website.

i'm working on a website which is related to electrical projects.

i also mention your name, link and other info.

plz reply


3 years ago

Hello, i have some problems with your code. I use the Arduino IDE 1.6.5 and if I upload the code, this message will come:

Arduino: 1.6.5 (Windows 7), Platine: "ATtiny, ATtiny85, 8 MHz (internal)"

ultrasonic_ruler.ino: In function 'void setup()':
ultrasonic_ruler:15: error: 'Serial' was not declared in this scope
'Serial' was not declared in this scope

Please help me...


4 years ago on Introduction

How can I program ultrasonic HC-SR04 with AVR?please help......SOS

I want to use it as a meter


4 years ago on Step 5

The arduino IDE still won't let me upload this code, I've tried many versions and using an UNO and a Tiny AVR Programmer but no luck... I would really like to finish this project because I've bought all the pieces. I've tried using different releases of the IDE but that hasn't helped. If anyone knows what might be wrong or has any advice/input/a gut feeling or maybe had this problem please help! :)


Reply 5 years ago

I used an arduino mega 2560 to program the attiny85. An Attiny85 is an arduino chip that can be programmed just like any other arduino, but it only has 5 i/o pins.


Reply 5 years ago on Introduction

Thanks for answering i will start my project as soon as i get all the components :-)


Reply 5 years ago on Introduction

Thank you! And hmm when you say "number led panel", do you mean a 7-segment display? (Such as a digital alarm clock display?) If you do, than yes you may certainly use one; however, you will need one or two 74HC595s in your circuit (depending on the number of digits you want to use). You could even attach a 8x8 LED matrix to this circuit since all you would need is two 74HC595s to drive it.

Timofte Andreibergerab

Reply 5 years ago

you can use an i2c 8x1 lcd display instead of 7segments display... a nokia display is out of discussion because it uses SPI comunication and needs too many pins. of course it can be done with atmega328, but it's not gonna be as fun as attiny85


Reply 5 years ago

Yes you may use an uno for this, however, one of the purposes of this device was to make it very small and portable. For testing purposes the uno would do just fine but I suggest getting some attiny chips later on. Thanks!


Reply 5 years ago on Introduction

Thanks for answering i will start my project as soon as i get all the components :-)