Arduino-based DIY ultrasonic ruler with ATtiny85 microcontroller

Measuring the distance to an object in the path of a person, equipment, vehicle, or moving object is used in a wide variety of applications such as robot motion control, vehicle control, cane for the blind, medical equipment, etc. Various distance measurement methods are available today, but measurement with an ultrasonic sensor is one of the cheapest among other options.


Arduino-based DIY ultrasonic ruler with ATtiny85 microcontroller


In this project, we are going to use an HC-SR04 ultrasonic sensor with an ATtiny85 microcontroller programmed with Arduino IDE and an OLED display module to create a digital ultrasonic ruler. This board can be used for distance measurement, object detection and many other applications.


An ultrasonic ruler diagram using the ATtiny85 is shown below.


Arduino-based DIY ultrasonic ruler with ATtiny85 microcontroller


The image above shows a schematic diagram of the interaction of the ultrasonic sensor and OLED display with the Attiny85 chip. The interface between the OLED display and the ATtiny85 must be implemented using the I2C protocol. Therefore, the SCL pin (PB2) of the ATtiny85 is connected to the SCL pin of the OLED display. Likewise, the SDA pin (PB0) of the ATtiny85 is connected to the SDA pin of the OLED display. The Echo and Trig pins of the ultrasonic sensor are connected to the PB3 and PB4 pins of the ATtiny85, respectively. Finally, we used a 9V battery to power all the components.Then this 9V is converted to 5V using an AMS117-5V voltage regulator.


The board and final assembly might look like this.


Arduino-based DIY ultrasonic ruler with ATtiny85 microcontroller


Arduino-based DIY ultrasonic ruler with ATtiny85 microcontroller


The ATtiny85 microcontroller was programmed in the Arduino IDE as Digispark. The following is the program code.



#include "TinyWireM.h"
#include "TinyOzOLED.h"
const int trigPin = 4;
int echoPin = 3;
int duration, cm;
void setup() {
  TinyWireM.begin();
  OzOled.init();
  OzOled.clearDisplay();  
  OzOled.setNormalDisplay();
  OzOled.sendCommand(0xA1);
  OzOled.sendCommand(0xC8);
  pinMode(trigPin, OUTPUT);
}
void loop()
{
//OzOled.clearDisplay(); 
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
cm = microsecondsToCentimeters(duration);
displayOLED();
delay(1000);
OzOled.clearDisplay(); 
}
void displayOLED() {
  OzOled.printString("Distance:", 3, 3);
  OzOled.printNumber(cm, 0, 12, 3);
  OzOled.printString("(In CM)", 5, 5);
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}


After PCB assembly and ATtiny85 programming, we can test the ultrasonic distance ruler. To do this, apply a voltage of 9 V to the board and wait until the measured distance is displayed on the OLED display.


Arduino-based DIY ultrasonic ruler with ATtiny85 microcontroller


You can use a scale or measuring tape to check if the measured distance is correct.


Previous Post Next Post

Contact Form