DIY Ultrasonic Radar with Arduino

n ultrasonic sonar is a device that measures the distance to an object by emitting sound waves at a frequency higher than the upper limit of human hearing (usually more than 20KHz). It works by sending out sound waves and then measuring the time it takes for the waves to bounce back after hitting an object. By calculating the time difference between sending and receiving the sound wave, and using the speed of sound in the air, the distance to the object can be determined. In some of my previous videos, you can see several different ways of building such devices with special features. They were all built with additional programs written by Processing applications to display the results on a computer monitor.

This time, I will show you a simple way to build a stand-alone sonar device whose results are displayed as a radar image directly on a TFT color display, so it is often mistakenly called a radar instead of a sonar.

Component List

- Arduino Nano R3 × 1
- TFT display with 240 x 320 pixel resolution and ILI9341 driver chip × 1
- HC-SR04 ultrasonic sensor × 1
- Small 9g servo motor × 1
- 2.2KM resistor × 5
- 3.3KM resistor × 5

Tools used
- Soldering iron
- Lead-free solder wire

The servo motor and ultrasonic transducer were mounted in a box I kept from a previous project and connected to the main box via flat cable.

Designed and assembled

The idea came to me when I stumbled across a picture on the web, and then, after some research, I found the project in question on Github. The original project was made on a 1.8-inch display, and for this purpose, that's a very small display space indeed. So I rewrote the code to work on a larger 3.2-inch TFT display so that the image would show up more clearly.

Now, let's delve deeper into the excellent performance of this device in real application scenarios:

First of all, in order to ensure the accuracy of the measurement, I carefully separated the ultrasonic sensor from the servo motor, and meticulously calibrated the correspondence between the graphic display and the actual distance of the object. You'll notice that the distance displayed on the screen matches the actual distance measured to a remarkable degree.

We then mounted the sensor securely on the servo motor and placed an obstacle to be detected. After powering on, the servo motor first performed a smooth test, followed by a radar-like scanning interface on the display and began to scan accurately.

Obstacles are identified on the screen with eye-catching red dots. The lower left corner of the screen clearly shows the current scanning area, while the right side is updated in real time with the distance between the sensor and the obstacle, in units accurate to centimeters. In order to present the distance information more intuitively, we have specially designed three green arcs, which dynamically adjust with the change of distance, enabling users to grasp the actual distance at a glance. When the nearest obstacle is more than 1 meter away, a yellow warning dot will appear on the last arc to remind the user that the scanning range has been exceeded. The scanning process starts from 180 degrees, gradually narrows down to 0 degrees, and then expands from 0 degrees to 180 degrees, realizing all-round coverage.

To ensure stable operation of the device, we recommend using an external power supply, but an option to power the device through the Arduino's USB port is also available for convenience. In addition, users can easily adjust all the display colors in the code to meet different visual needs according to their personal preferences.

Finally, I would like to give a brief summary of this device. Compared to most of its counterparts, it does not rely on a computer monitor to display the scan results, eliminating the need for additional apps and code hassles. It is simple to make, visually appealing and completely self-contained, making it an ideal choice for both beginners and experienced DIY enthusiasts. However, for better user experience, it is recommended to integrate all components into a compact housing with a tilting front display to simulate a real radar system for a more realistic user experience.

Wiring Diagram

Code


Below is the full source code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include <Servo.h>
#include <SPI.h>
#include "Ucglib.h"          
 
#define  trigPin   6      
#define  echoPin   5       
#define  ServoPin  3        
int Ymax = 240;             
int Xmax = 320;             
 
int Xcent = Xmax / 2;      
int base = 210;             
int scanline = 185;        
 
Servo baseServo;
//Ucglib_ILI9341_18x240x320_SWSPI ucg(/*sclk=*/ 13, /*data=*/ 11, /*cd=*/ 9, /*cs=*/ 10, /*reset=*/ 8);
Ucglib_ILI9341_18x240x320_HWSPI ucg(/*cd=*/ 9, /*cs=*/ 10, /*reset=*/ 8);
 
void setup(void)
{
      ucg.begin(UCG_FONT_MODE_SOLID);
      ucg.setRotate90();            
       
      pinMode(trigPin, OUTPUT);     
      pinMode(echoPin, INPUT);      
      Serial.begin(115200);           
      baseServo.attach(ServoPin);  
     
       
      ucg.setFontMode(UCG_FONT_MODE_TRANSPARENT);
      ucg.setColor(0, 0, 100, 0);
      ucg.setColor(1, 0, 100, 0);
      ucg.setColor(2, 20, 20,20);
      ucg.setColor(3, 20, 20, 20);
      ucg.drawGradientBox(0, 0, 320, 240);
      ucg.setPrintDir(0);
      ucg.setColor(0, 5, 0);
      ucg.setPrintPos(70,120);
      ucg.setFont(ucg_font_logisoso32_tf); 
      ucg.print("Mini Radar");
      ucg.setColor(0, 255, 0);
      ucg.setPrintPos(70,120);
      ucg.print("Mini Radar");
      ucg.setFont(ucg_font_courB14_tf);
      ucg.setColor(20, 255, 20);
      ucg.setPrintPos(90,200);
      ucg.print("Testing...");
      baseServo.write(90);
     
     
      for(int x=0;x<180;x+=5)
          { baseServo.write(x);
            delay(50);
           }
      ucg.print("OK!");
      delay(500);
      ucg.setColor(0,0, 0, 0);
      ucg.setColor(1,0, 0, 0);
      ucg.setColor(2,0, 0, 0);
      ucg.setColor(3,0, 0, 0);
      ucg.drawGradientBox(0, 0, 320, 240);
      delay(10);
     
     
      //ucg.clearScreen();
      cls();
      ucg.setFontMode(UCG_FONT_MODE_SOLID);
      ucg.setFont(ucg_font_helvR08_hr);   // or freedoomr10_tr
   
}
 
void cls()
{
  
  ucg.setColor(0, 0, 0, 0);
  for(int s=0;s<240;s++)
  {
    ucg.drawHLine(0,s,320);
    delay(1);
  }
     
  //ucg.drawBox(0, 0, 160, 60);
 
}
 
int calculateDistance()
{
      long duration;
     
      digitalWrite(trigPin, LOW);
      delayMicroseconds(2);
       
      digitalWrite(trigPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(trigPin, LOW);
       
      duration = pulseIn(echoPin, HIGH);
      
      return duration*0.034/2;
}
 
void fix_font()
{
      ucg.setColor(0, 180, 0);
      ucg.setPrintPos(144,44);
      ucg.print("1.00");
      ucg.setPrintPos(144,100);
      ucg.print("0.60");
      ucg.setPrintPos(144,165);
      ucg.print("0.30");
}
 
void fix()
{
      ucg.setColor(0, 180, 0);
     
      ucg.drawDisc(Xcent, base+1, 3, UCG_DRAW_ALL);
      ucg.drawCircle(Xcent, base+1, 210, UCG_DRAW_UPPER_LEFT);
      ucg.drawCircle(Xcent, base+1, 210, UCG_DRAW_UPPER_RIGHT);
      ucg.drawCircle(Xcent, base+1, 135, UCG_DRAW_UPPER_LEFT);
      ucg.drawCircle(Xcent, base+1, 135, UCG_DRAW_UPPER_RIGHT);
      ucg.drawCircle(Xcent, base+1, 70, UCG_DRAW_UPPER_LEFT);
      ucg.drawCircle(Xcent, base+1, 70, UCG_DRAW_UPPER_RIGHT);
      ucg.drawLine(0, base+1, Xmax,base+1);
      
      ucg.setColor(0, 180, 0);
      
       for(int i= 40;i < 300; i+=2)
       {
        if (i % 10 == 0)
          ucg.drawLine(185*cos(radians(i))+Xcent,base - 185*sin(radians(i)) , 205*cos(radians(i))+Xcent,base - 205*sin(radians(i)));
        else
         ucg.drawLine(195*cos(radians(i))+Xcent,base - 195*sin(radians(i)) , 205*cos(radians(i))+Xcent,base - 205*sin(radians(i)));
       }
 
       ucg.setColor(0,200,0);
       ucg.drawLine(0,0,0,36);
       for(int i= 0;i < 5; i++)
       {
          ucg.setColor(0,random(200)+50,0);
          ucg.drawBox(2,i*8,random(28)+3,6);
       }
 
       ucg.setColor(0,180,0);
       ucg.drawFrame(292,0,28,28);
       ucg.setColor(0,60,0);
       ucg.drawHLine(296,0,20);
       ucg.drawVLine(292,4,20);
       ucg.drawHLine(296,52,20);
       ucg.drawVLine(318,4,20);
         
       ucg.setColor(0,220,0);
       ucg.drawBox(296,4,8,8);
       ucg.drawBox(296,16,8,8);
       ucg.drawBox(308,16,8,8);
       ucg.setColor(0,100,0);
       ucg.drawBox(308,4,8,8);
 
       ucg.setColor(0,90,0);
       ucg.drawTetragon(124,220,116,230,196,230,204,220);
       ucg.setColor(0,160,0);
       ucg.drawTetragon(134,220,126,230,186,230,194,220);
       ucg.setColor(0,210,0);
       ucg.drawTetragon(144,220,136,230,176,230,184,220);
}
 
void loop(void)
{
   
  int distance;
   
  fix();
  fix_font();
 
  for (int x=180; x > 4; x-=2){     
      
      baseServo.write(x);            
       
      
      int f = x - 4;
      ucg.setColor(0, 255, 0);
      ucg.drawLine(Xcent, base, scanline*cos(radians(f))+Xcent,base - scanline*sin(radians(f)));
      f+=2;
      ucg.setColor(0, 128, 0);
      ucg.drawLine(Xcent, base, scanline*cos(radians(f))+Xcent,base - scanline*sin(radians(f)));
      f+=2;
      ucg.setColor(0, 0, 0);
      ucg.drawLine(Xcent, base, scanline*cos(radians(f))+Xcent,base - scanline*sin(radians(f)));
      ucg.setColor(0,200, 0);
      
      distance = calculateDistance();
      
      
      if (distance < 100)
      {
        ucg.setColor(255,0,0);
        ucg.drawDisc(2.2*distance*cos(radians(x))+ Xcent,-2.2*distance*sin(radians(x))+base, 1, UCG_DRAW_ALL);
      }
      else
      {
        ucg.setColor(255,255,0);
        ucg.drawDisc(208*cos(radians(x))+Xcent,-208*sin(radians(x))+base, 1, UCG_DRAW_ALL);
      }
     
            
      
      Serial.print(x);
      Serial.print("    ,   ");
      Serial.println(distance);
      
 
      if (x > 70 and x < 110)  fix_font();
 
 
      ucg.setColor(255,255,  0);
      ucg.setPrintPos(20,230);
      ucg.print("DEG: ");
      ucg.setPrintPos(54,230);
      ucg.print(x);
      ucg.print("  ");
      ucg.setPrintPos(240,230);
      ucg.print("     ");
      ucg.print(distance);
      ucg.print(" cm    ");
       
  }
  //ucg.clearScreen(); 
  delay(50);
  cls();  
  
  fix();
  fix_font();        
   
  for (int  x=1; x < 176; x+=2){    
      baseServo.write(x);            
       
      
      int f = x + 4;
      ucg.setColor(0, 255, 0);
      ucg.drawLine(Xcent, base, scanline*cos(radians(f))+Xcent,base - scanline*sin(radians(f)));
      f-=2;
      ucg.setColor(0, 128, 0);
      ucg.drawLine(Xcent, base, scanline*cos(radians(f))+Xcent,base - scanline*sin(radians(f)));
      f-=2;
      ucg.setColor(0, 0, 0);
      ucg.drawLine(Xcent, base, scanline*cos(radians(f))+Xcent,base - scanline*sin(radians(f)));
      ucg.setColor(0, 200, 0);
       
      distance = calculateDistance();
 
       
      if (distance < 100)
      {
        ucg.setColor(255,0,0);
        ucg.drawDisc(2.2*distance*cos(radians(x))+Xcent,-2.2*distance*sin(radians(x))+base, 1, UCG_DRAW_ALL);
      }
      else
      {
        ucg.setColor(255,255,0);
        ucg.drawDisc(208*cos(radians(x))+Xcent,-208*sin(radians(x))+base, 1, UCG_DRAW_ALL);
      }
            
       
      Serial.print(x);
      Serial.print("    ,   ");
      Serial.println(distance);
      
      if (x > 70 and x < 110)  fix_font();
       
      ucg.setColor(255,255,  0);
      ucg.setPrintPos(20,230);
      ucg.print("DEG: ");
      ucg.setPrintPos(54,230);
      ucg.print(x);
      ucg.print("  ");
      ucg.setPrintPos(240,230);
      ucg.print("     ");
      ucg.print(distance);
      ucg.print(" cm    ");
   
  }
 //ucg.clearScreen(); //
 delay(50);
 cls();
}