@tutorial: Homemade ultrasonic distance sensor

We will make a homemade ultrasonic distance sensor and see how these modules work. The working principle is quite simple but the circuit could get a bit tricky. We send a ultrasonic wave with the transmitter speaker, the wave hits an obstacle and bounce back to the receiver speaker. We calculate the time between the sent and received pulse, and by knowing the speed of sound which is constant, we can calculate the distance.

External Link: https://electronoobs.com/eng_arduino_tut36.php

by: ELECTRONOOBS on 2026-08-11

~part list

1 x Arduino NANO LINK eBay??

2 x 40KHz speakers: LINK eBay

1 x MAX232: LINK eBay

1 x LM324: LINK eBay

3 x 5k potentiometer: LINK eBay

3 x 100k resistor: LINK eBay

3 x 10k resistor: LINK eBay

1 x 100R resistor: LINK eBay

5 x 100nF capacitor: LINK eBay

5 x 10nF capacitor: LINK eBay

1 x BC557 PNP: LINK eBay

1 x drilled PCB: LINK eBay

PCB pins: LINK eBay

Wires LINK eBay


Wire, soldering iron, solder, etc...

electronoobs Homemade ultrasonic distance sensor 2133
~PART 1 - The commercial module

To begin this project, I first analyze this commercial ultrasonic distance sensor below, the SR04. What I can see is that it ahs 4 pins, ground, power, trigger pin and echo. It has some components on the back, and on the front part, the main components, two ultrasonic speakers. One as transmitter and the other one as receiver. Each time we apply a pulse to the trigger pin, the transmitter speaker creates a 40kHz sound wave.

electronoobs Homemade ultrasonic distance sensor 2136

We have to analyze how it works and see the signals. For that, I've connected the trigger pin to and Arduino UNO and applied a 10us pulse each 100ms. I connect the TX and RX speakers to the oscilloscope and this is what I see. The green line is the TX signal. It is a 40KHz square wave applied to the transmitter speaker and that will send a sound wave burst. It has 8 cycles. Then, the yellow line is the received signal and it is the bounced sound wave. The time between the sent and received signal will give us information about the distance to the object.

electronoobs Homemade ultrasonic distance sensor 2138

As you can see below, there is a direct relation between the distance and the delay time between the sent and received pulse. That delay time is the tiem it took the sound wave to go to the object and get back. So we haev to divide that by 2. Then we multiply it by the speed of sound and we get the distance.


EXAMPLE:

Measured delay = 588us. Speed of sound = 340m/s

Distance = [(588us / 1000000) / 2] x 34000cm/s ? 10cm


electronoobs Homemade ultrasonic distance sensor 2141

Ok, but the commercial module works like this. It receives the trigger signal. Waits 250us then it sends 8 cycles of 40KHz sound wave and puts the echo pin to high. That wave hits an object and bounce back. The receiver detects that sound and then it puts the echo pin to low. That creates a pulse and the width of that pulse is related to the measured distance. That's what we haev to implement today for our project.


electronoobs Homemade ultrasonic distance sensor 2143
~PART 2 - Homemade module

First of all, this below, is the schematic that I've created for this project. You will need two ultrasonic speakers, one 1uad-OPAMP, the MAX232 voltage shifter the Arduino and a few more components such as PNP transistor, resistors and capacitors. I first mount this schematic on a breadboard for tests.

electronoobs Homemade ultrasonic distance sensor 2146

I have pins for trigger and echo just as the commercial module. Then the microcontroller connected to the lever shifter and it will apply 8 cycles of 40kHz signal after the trigger pulse, applied to the transmitter speaker. The good thing of using the MAX232 is that it has a voltage doubler and inverter. So I apply 5 V square signal to it and I can get over 30V peak to peak at the output that goes to the speaker and that will increase range. As you can see I send the square signal and receive that on the other speaker and it will change with the distance.

electronoobs Homemade ultrasonic distance sensor 2148
~PART 3 - Final PCB

Now I mount everything on the PCB. Arduino in the middle and the amplified transmitter speaker on one side and the receiver on the other. We have potentiometers so we could set the treshold voltage for each amplified stage of the OPAMP se could get a good wave that will go to the Arduino. Now we haev to program the microcontroller.

electronoobs Homemade ultrasonic distance sensor 2151
~PART 4 - Final code

The code is quite easy and short. First we set the registers for the ports so we define D3, D4, D5 and D10 as outputs. Those are TX out1, TX out2, Vcc activate and echo out pins. Then we set pin D9 and D8 to be able to fire an interruption.


We go to the interruption vector and if D8 is high, that means the trigger pin was activated so we can start the code. That will activate the loop in the infinite loop. We first have to add that delay of 250us we haev seen before. In the code that is 150us because after tests on the oscilloscope I saw a 100 extra delay so I've reduced the 250 delay to 150.

Ok, after the delay we create 8 cycles of 40KHz on Tx out1 and Tx out 2 pins and that will send the 40KHz sound wave. But first we turn LOW the Vcc activate pin so the PNP connected to the MAX232 will be activated and the IC will be supplied. Then using a 12us delay, we create, more or less, a 40KHz signal. We do taht 8 times and then we put the Vcc activate back to high and we turn to HIGH the echo pin. Now when we detect the bounced signal we tutn echo pin to low and the pulse is over.

~PART 5 - Test the module

After I've uploaded the code to my module I've made the enxt connections between the homemade module and another Arduino UNO. I’ve connected an LCD to the Arduino UNO and connect D3 and D4 to the trigger and echo pins of my homemade module. Now I upload this next code to the second Arduino and place an object in front of the sensor. This code will only apply a trigger pulse and measure the delay between the sent and received echo and then print the distance on the LCD.

electronoobs Homemade ultrasonic distance sensor 2160

Make the connections above and uplaod next code to the Arduino uno. This code will measure the width of the echo pulse and divide that by 2. Next we multiply taht by 34000cm/s and we het the distance in cm and print it on the LCD. REMEMBER! you have to isntall i2c liquid crystal librery in order to work with the i2c LCD.



Leave a comment

Please login in order to comment.

Comments

ADVERTISERS
ADVERTISERS
PCBWAY