Introduction: Pocket-Sized Ultrasonic Ruler

Picture of Pocket-Sized Ultrasonic Ruler

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.

Step 1: Parts

Picture of 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)

Picture of 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)

Picture of 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

Picture of 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

Picture of 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)

Picture of 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

Picture of 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

Picture of 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)

Picture of 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!

Picture of 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!


Abhi909 (author)2016-01-31

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

gnarnhammer (author)2015-10-18

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...

BurakTutal (author)2015-04-26

Nice Project. I have made a smilar one. But mine is talking according to distance :) You can check my project. :)

sabam2 (author)2015-02-11

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

I want to use it as a meter

StraitEdge (author)2014-09-14

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! :)

Rowdyrobs made it! (author)2014-07-18

I made one with a few modifications!

bergerab (author)Rowdyrobs2014-07-21

That's too cool! I love the PCB. It looks like you used two ATMega328s? Can you share exactly what you did? Great work and thanks for sharing!

Rowdyrobs (author)bergerab2014-09-09

Yes, I used two 328s - one for the for the LCD & Ultrasonic Sensor, and the other for all my leds. I wrote up my code so that a single led turns on every 2inches. Looking back I should have used a shiftregister (to control the leds) but I was running out of time and did not have time to order any. I won first place at my wife's family reunion for adult project hahaha. Thanks for the idea.

bergerab (author)Rowdyrobs2014-09-09

Nice! That's definitely an original way to make it. Also there are some great modules out there that allow you to control LCDs using I2C which could save you like.. 6 pins. But either way your version looks awesome!

Drone0 (author)2014-06-17


is it possible to use number led panel replacing line of led?

Raphango (author)Drone02014-07-05

Take a look at this instructable:

Here's the video of it actually working:

agangwani (author)Raphango2014-07-11

Which arduino did you used, arduino uno or arduino mega?

Please tell me

Thanks :)

bergerab (author)agangwani2014-07-11

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.

agangwani (author)bergerab2014-08-16

and Congrats for winning the competition :-)

agangwani (author)bergerab2014-08-16

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

Raphango (author)agangwani2014-07-12

Arduino Uno. The thing you see upon it is a arduino uno prototyping board. =)

agangwani (author)Raphango2014-08-16

Thanks :-) Hope i will get all the required components and it works :-)

Drone0 (author)Raphango2014-07-07


bergerab (author)Drone02014-06-17

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 Andrei (author)bergerab2014-06-18

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

Drone0 (author)bergerab2014-06-17

oh i see
thank you for your answer.

agangwani (author)2014-07-11

Can i use an arduino uno for this?

Please tell me

Thanks :)

bergerab (author)agangwani2014-07-11

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!

agangwani (author)bergerab2014-08-16

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

Antzy Carmasaic (author)2014-07-20

Nice project. Congrats on winning the sensor's contest.

How much range do you get with HC-SR04? I'm trying to use one but it only gives range of about 30-40cm instead of the promised 400cm.

bergerab (author)Antzy Carmasaic2014-07-21

I believe the datasheet said it has a distance of 200cm. However, a good way to troubleshoot is to hookup your SR04 to a spare microcontroller, and write code that outputs the sensor's data to the serial console. If it is still reading 30-40cm, I'd buy a new sensor. If it reads up to 200cm, then it is probably just your 74hc595 acting up. If so, check for shorts and the schematic again! Thanks for commenting

Antzy Carmasaic (author)bergerab2014-07-21

I tried it with an Arduino and got reading on serial for 30-40cm only. I guess it is busted. Have ordered new ones. Thanks for your help :)

StraitEdge (author)2014-07-18

When I try and upload the sketch I get the error message that 'Serial' was not declared in this scope for the "Serial.begin (9600);" line, I copied and pasted this example so I'm not sure where I went wrong or how to fix this. Help please! :)

bergerab (author)StraitEdge2014-07-21

Make sure you are using the latest version of the arduino IDE. Here is a link to the updated version:

Thanks for the comment!

padayattiljosemon (author)2014-07-09

simple and cool

brmarcum (author)2014-07-06

Congrats on winning 2nd place with this little gem in the Sensors Contest! Should've been higher, but it is hard to beat a fingerprint scanner

bergerab (author)brmarcum2014-07-07

Thanks! Haha yeah right after I saw the LED Beer Pong and that Finger print scanner I knew it would be a tough contest, but it was still awesome to win something!

brmarcum (author)2014-06-14

Great device, great design, and great tutorial. Well written and easy to follow. I now have a use for the Attiny85 I bought a while ago.

bergerab (author)brmarcum2014-06-14

Thank you very much! If you have any issues or suggestions please let me know

IBH (author)bergerab2014-06-14

How can I a lcd and make it display measurement in inches??

Raphango (author)IBH2014-07-05

Take a look at this instructable:

Here's the video of it actually working:

bergerab (author)IBH2014-06-15

1. To use an LCD, you will need to use a shift register (such as the 74HC595) with the attiny. The process is described at this instructable:

2. To make the LCD display inches, in the code you will have to add this line "distance = distance * 0.393701" right before you see "shiftOut". Also you will have to replace the "shiftOut" line with something such as this: "lcd.print(distance)" . (replace the word "lcd" with whatever you named your object of the Liquid Crystal Library).

Thanks for your interest in the project, don't hesisate to ask more questions!

bezo88 (author)2014-06-24

great project. I wanted some help with it. I connected everything as per your guide and uploaded the code to the attiny85 but it does not seem to be working right. it is not displaying in binary, for anything under 10cm it is turning Qa on the 74hc595 on, under 12cm it is turning Qb on, 60cm it turned on Qa,Qb,Qc.

bergerab (author)bezo882014-06-28

Sorry I couldn't respond sooner, but I could help you troubleshoot. Be sure that all of your LEDs are functioning properly. (check for any burnt out LEDs). Then be sure there are no shorts to the "CLR" or "RST" pins on the 74HC595. I believe those pins are close the the VCC and GND so just make sure they don't connect to the other pins. Did you use my exact code? Thanks for your interest!

Oliverhall (author)2014-06-22

Cool little project! ;)

You can buy the HC-SR04 ultrasonic range sensor for cheap on this webstore:

They ship from Canada.

bergerab (author)Oliverhall2014-06-23

Nice link! I actually ordered mine from an ebay seller from china and got two HC-SR04s for under $3. Pretty good deal but you've gotta have patience haha

_soapy_ (author)2014-06-18

While quite a nice idea, this isn't really practical as a replacement for a tape measure.
These are already sold as "distance estimators" and are prone to error from reflections, & of course, are at best within 10mm. Try cutting a board to that then fitting it!
Nice Instructable though.

Dark Solar (author)2014-06-18

Although this is definitely a useful item, I use tape measures and rulers as much as possible specifically because they do not require power cells. Two thumbs up for pioneering!!

AJMansfield (author)2014-06-17

Except, of course, that measuring is actually only a secondary function of rulers and tape measures. The real purpose of those devices is locating points at known dimentions, and, in the case of rulers, drawing straight lines.

bergerab (author)AJMansfield2014-06-17

Good call! You're right it certainly cannot replace some of the ability of rulers, however, it is a pretty handy device!

m1keo (author)2014-06-15

May I ask what the range of it is?

bergerab (author)m1keo2014-06-15

200 centimeters that is. Sorry

bergerab (author)m1keo2014-06-15

Yes of course! I believe the sensor has a max range of 200 and the binary display can only display 255 digits. Thanks!

nodcah (author)2014-06-15

Nice instructable! Great use of the ATtiny85! =D

bergerab (author)nodcah2014-06-15

Thank you! If you do the project I'd love to see some pictures!

About This Instructable




Bio: 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 ... More »
More by bergerab:Flame Controlled MIDI ControllerSOLO Cup SpeakerBackpack Anti-Theft Alarm
Add instructable to: