Ultrasonic Range Finder With an ATtiny85 (With Shield)




About: I've always taken stuff apart, almost always put it back together... I enjoy tinkering around with pretty much anything.

I’m here to show you how to use a HC-SR04 Ultrasonic Range Finder with an ATtiny85 as well as programming the ATtiny85 using the wonderful shield that randofo created.

List of materials:
ATtiny85 Programming Library
Arduino Uno
HC-SR04 Ultrasonic Range Finder and Library
Jumper Wires

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: Program the Arduino

Before you connect your shield to the Arduino Uno, you’ll want to make sure you’ve put your Arduino Uno into it’s ISP mode, to do this load up your Arduino IDE and once it’s loaded go to File > Examples > ArduinoISP, now connect your Arduino up to your PC and upload the ISP sketch.

Step 2: Connect the Shield

Take your Arduino Uno and clip the shield in place, then take your ATtiny85 IC and put it in the correct way into the DIP-8 Socket on the shield.

Step 3: Program the ATtiny85

Now that you’ve put the Arduino into its Programming state, and connected the shield, it’s time to program the ATtiny85 chip, now I’ll show you how to make the LED on the board blink.

Go to this link and follow those instructions to get the core library for ATtiny’s loaded into the Arduino IDE, Once you’ve done that then copy the code below into the IDE and upload it (Once you’ve selected the correct board.)

  Turns on an LED on for one second, then off for one second, repeatedly.
  This example code is in the public domain.

void setup() {                
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(0, OUTPUT);     

void loop() {
  digitalWrite(0, HIGH);   // set the LED on
  delay(1000);              // wait for a second
  digitalWrite(0, LOW);    // set the LED off
  delay(1000);              // wait for a second

Step 4: The Real Magic

If you managed to get this far without causing any issues or having any problems you’re doing good, it’s time to hook up the HC-SR04 now.

We’ll start off with the power circuit first.

Connect jumpers from VCC (Pin 8 on the ATtiny) and Ground (Pin 4) to the breadboard, make sure they’re 2 holes apart (See image 7 for reference).

Now connect two more jumpers between the VCC and Ground and hook one up to Analogue Input 3 (Pin 2) and another to Analogue Input 1 (Pin 7).

Now connect your HC-SR04 Ultrasonic Range Finder, having the ‘eyes’ facing you the four pins should read “VCC, TRIG, ECHO, GND” TRIG goes to Analogue Input 1 and ECHO goes to Analogue Input 3.

Step 5: The Code

You’ll need to get the HC-SR0R Library, Once you’ve gotten the library install it (Make sure the Arduino IDE is closed).

Once everything is configured and wired up, upload the following sketch.

#include "Ultrasonic.h"

int LED1 = 0;  // LED1 Pin
int TRIG = 2; // Trigger Pin
int ECHO = 3; // Echo Pin
int Range; // The range of the object from the HC-SR04 Ultarsonic Module
int Dist; // The Distance value

Ultrasonic ultrasonic(TRIG,ECHO); // Create and initialize the Ultrasonic object.

void setup() {
  pinMode(LED1, OUTPUT);
  Dist = 2;

void loop() {
  //Range = ultrasonic.Ranging(CM); // Range is calculated in Centimeters. 
  Range = ultrasonic.Ranging(INC); // Range is calculated in Inches.
  if (Range < Dist) { 
    digitalWrite(LED1, HIGH);
  } else if (Range > Dist) { 
    digitalWrite(LED1, LOW);


If an object comes within 2 inches of the range finder the LED will light up.

There is a lot of room for improvement like for example every now and again the LED will light up for no reason, I believe this could be from a false reading from the range finder, but all in all, this is a pretty good ATtiny85 Project to get you started.

Make It Glow Challenge

Participated in the
Make It Glow Challenge

Be the First to Share


    • Made with Math Contest

      Made with Math Contest
    • Multi-Discipline Contest

      Multi-Discipline Contest
    • Robotics Contest

      Robotics Contest

    16 Discussions


    2 years ago

    Download link for the HC-SR0R Library

    is not working: can you provide a good link plse? Or can we use the regular HC-SR04 library from Arduino?

    1 reply

    Reply 2 years ago



    5 years ago on Introduction

    I am working on a project which requires that a very powerful bulb gets brighter as a person aproaches it.

    How could I modify this to give an output voltage which increases in a linear fashion from 0 to 10 volts?

    There are many projects on instructables and elsewhere that either use the distance information to output to a digital display or light up a few leds in steps as with car reverse sensors, but I have not found anything which will give a continous analogue output voltage neccessary to operate a lighting dimmer. Can anyone point me in the right direction?

    1 reply

    Reply 4 years ago

    That sounds like a cool project. One way to do that would be to store values for max distance and min distance. A digital to analog output could control the light. If the subject(Range) is at max distance(maxDist), the analog value would be set to 0(0 volts), and if he's at min distance(minDist) it would be set to 255 (5 volts). Assuming the analog resolution is 8 bits. Your formula could be:

    AnalogOut = ((1 - (Range - minDist) / (maxDist - minDist)) * 255)

    if (Range < minDist) AnalogOut = 255

    You need an amplifier circuit with a gain of 2 so you can get the voltage from the 5 volts max from the analog pin to 10 volts. It would need to source enough current for the light.

    In a nutshell you calculate where the subject is within min and max and get that percentage. Subtract from 1 because 0 range is 100%. Multiply by 255 to scale it to the DAC, send it out the DAC, then amplify the output.

    If you don't have a DAC I bet you could use pulse width modulation and smooth it with a low pass filter. Heck you don't even need to smooth it or use an amplifier circuit if you use a mosfet or solid state relay triggered by the 5v with a 10v supply(or whatever your light requires) across the load rails of the mosfet or SSR. I'd use PWM and try to find a mosfet that works. And probably an IR light and sensor rather than the ultrasonic sensor for a wider radius but IDK.

    I hope this isn't overwhelming but it's something you can work with. Have fun with it.


    5 years ago on Step 5

    Great tutorial! But I missed the HC-SR0R Library, the link is broken. Would you like to update it? I can't find the suitable library for this project. Thanks.


    7 years ago on Introduction

    where can i purchase jumper wires like the ones shown??? Ive been looking for sooo long. Thanks in advance

    2 replies

    Reply 7 years ago on Introduction

    Dealextreme has them as well. good quality and very cheap

    Well the base of this project is for you to do something when an object or something else gets within a certain range, other than that it's only limited by your imagination and I/O pins


    Reply 7 years ago on Introduction

    I wrote a short program that uses if_then_else statements to determine and energize one of three LEDs (green, yellow, red) depending on the distance and installed it on the back of my motorhome. Works great. I even ended up using an RGB (3n1)led to minimize the footprint. I used inches and the light stays green until 3ft then goes to blinking yellow until distance is less than 2ft. THEN red. One NOTE: the HC-SR04 is voltage sensitive and must not exceed the rated 5vcc. Unlike the arduino that can handle some variations. Its and expensive lesson.

    some autobody shops sell ultrasonic parking guides, I'd advise you go check those out, I'm not sure that this could do the job.


    Reply 7 years ago on Introduction

    A useful application would be in the drone community or other RC flying platforms.


    7 years ago on Introduction

    Adafruit.com also has them. They have both male: https://www.adafruit.com/products/153 and female: https://www.adafruit.com/products/266.