Simple Ultrasonic Range Finder – ATtiny85

5,503 views

Many times in current automation, you’ve probably seen that practically every robot reverses its route when it encounters a stumbling block. And, if you’re interested in robotics, you’ve probably figured out that this is because of the sensor that is wired in the robot, which is commonly referred to as an ‘ultrasonic sensor.’

Ultrasonic sensors assist in the detection of high-pitched pulse impulses or vibrations that are inaudible to humans. Humans can hear frequencies between 20Hz to 20KHz, as we all know. Ultrasonic sensors can detect values that are outside of this range. So, here in this article, we are going to make Ultrasonic Range Finder With an ATtiny85 (With Shield)

ultrasonic-range-finder-attiny85

PCBWay commits to meeting the needs of its customers from different industries in terms of quality, delivery, cost-effectiveness, and any other demanding requests. As one of the most experienced PCB manufacturers in China. They pride themselves to be your best business partners as well as good friends in every aspect of your PCB needs.

Hardware Components

S.noComponentValueQty
1.ArduinoUNO1
2.controllerATtiny851
3.Jumper Wires1
4.Ultrasonic SensorHC-SR041
5.LED1

ATtiny85 Pinout

Ultrasonic Range Finder Circuit

Code

#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);
  }

Working Explanation

In this Ultrasonic Range Finder With an ATtiny85 (With Shield) we are using HC-SR04 to find the range. The transmitter and receiver are integrated into the HC-SR04. The sensor’s working voltage is roughly 5 volts. It can theoretically measure distances ranging from 2 cm to 450 cm. However, in practice, it is estimated to be between 2 and 80 cm. The angle of measurement is less than 15 degrees. It can readily run at a 40Hz frequency. The sensor’s operational current range is also less than 15mA.

Now you make connections properly and power the circuit, The LED will light up when an object is within 2 inches of the range finder.

Code Explanation

  • First, you need to download and include the library for the ultrasonic sensor.
  • Now define the pins that are connected with Arduino. Also, define integers that you will use in the further code.
  • In the void loop, define LEd as your output. Also, define your distance range and save it in the predefined integer.
  • In a void loop, calculate the range in centimeters and in inches. Moreover, provide the condition, that if the distance is greater than that range, LEd will turn On, else, it will remain OFF.

Application and Uses

  • Robots, avoid obstacles and detect the path.
  • For the devices, that are used to map the objects.
  • In the security devices like alarm circuits, etc.