Nearsightness Alert | MindPlus Coding Kit for Arduino Started Tutorial E13

0 4248 Easy

Project 2 Nearsightness Alert

Nearsightness is epidemic. Aside from genetic factors, it is mainly caused by bad habits. We know we shouldn’t read a book with a bow-back or humpback, but we just can’t help it when we sit in front of the desk.

For our health and eye protection, let’s make a simple nearsightedness alert device using buzzer and ultrasonic sensor.


Task Navigation

1. Learn about ultrasound

2. Making a nearsightness alert device

Key Points Analysis

1. Understanding Ultrasonic Sensors

The current mainstream ranging sensors are ultrasonic ranging sensors, infrared ranging sensors, laser ranging sensors and radar sensors. Among them, the ultrasonic sensor is used for long range stationary plane ranging. The ranging range of ordinary ultrasonic sensors is about 2cm - 450cm.

We can clearly see that there are 4 corners on the physical ultrasonic sensor: VCC -- 5V power pin, Trig -- trigger end, Echo -- receiving end, and GND -- ground.


Among the dual-probe sensors in the picture, one is used to send ultrasound and the other is used to receive. The single-probe ultrasonic sensor in the middle can do both by its own. This sensor is the first four-pin sensor we deal with, and the wiring is relatively special. We need to pay special attention when we connect the hardware later.

2. Ultrasound

We already know in the sound-activate light section that sound is generated by the vibration of objects. The number of vibrations per second is the frequency of the sound. The unit is Hertz. Human can produce sounds ranging from 20Hz to 8kHz, while human ears can hear sounds between 20 to 2000 Hz. The sound below 20 Hz is called low frequency sound, and the sound above 20 kHz is called ultrasound. Ultrasonic waves can propagate through any gases, liquids, and solids, at various speeds. The propagation speed in the air is C = 340m / s.

3. Ultrasonic ranging mechanism


The mechanism of ultrasonic ranging is using the propagation speed of ultrasonic wave in the air as a given condition, and measure the time difference between sending ultrasonic wave and receiving the reflection from the obstacle, which is used calculate the actual distance between the sending point and obstacle.

The process starts when ultrasonic wave transmitter sends ultrasonic waves in a certain direction and starts timing; ultrasonic waves travel in the air and returns upon meeting any obstacles; ultrasonic receiver receives the returning ultrasonic waves and stops timing. Distance L = C * T / 2 (L: distance, C: propagation speed in the air, T: total time). The value of C is related to temperature.


Command List



Hardware connection


Connect UNO and ultrasonic sensors

(Note the wire matching and relative positions)


1. Write the program

1) First, find the ultrasonic command in the Arduino function module.


2) Maintain a healthy sitting posture, then use the serial port to print the data read by the ultrasonic sensor, in order to determine the correct distance when sitting posture is healthy.


Please pay attention to the ultrasonic sensor’s test direction (probe direction) is unobstructed.


With initial experimental data, we can set up alert conditions.

2. Reference program


3. Program results

When the head is buried, the buzzer goes off for a second and stops. The buzzer stops upon posture change.

4. Program analysis

According to the test results of the serial port, with the position of ultrasonic sensor fixed, the distance measured when sitting healthily is more than 300cm, and the distance of an unhealthy is a little more than 50cm. So we set 50cm as the threshold value.

The buzzer sound is a little loud and we need no more than an alert, thus a 1s buzz duration.

Pay attention that we utilized variables properly to store the detected distance value. This is to battle the problem of repeated detection causing insensitivity in the conditional. Try comparing the effects of the following error programs.


Further Reading

Application of Ultrasonic Ranging

As a typical non-contact measurement method, sonic ranging has been widely used in many occasions, such as industrial automatic control, construction engineering measurement and robot vision recognition.

Compared to to other ranging methods, sonic wave ranging has far less time measurement accuracy than laser ranging or microwave ranging, as sonic wave travels much slower then light and radio waves in the air. Therefore, the ultrasonic ranging system is easy to implement with a simple structure and low cost. Plus ultrasonic wave is not affected by smoke, air visibility and other factors during the propagation process, and is widely used in various occasions.

However, ultrasonic ranging has many limitations in practical applications, which all affect the accuracy of ultrasonic ranging. First, the ultrasonic wave is greatly attenuated in the air. Due to the difference in measurement distance, echo signal would fluctuate, causing high errors in the measurement of arrival time. Second, ultrasonic pulse echoes are greatly widened in the receiving process, which affects the resolution of the ranging, especially when applied to close range measurement. Ambient temperature, wind speed, and other factors will also affect the measurement accuracy to a certain extent. These factors all limit the application of ultrasonic ranging in some situations that require high measurement accuracy. It is of great practical significance in providing solutions to improve the ultrasonic ranging’s measurement accuracy.

Further Exercise

icon Program 5-2 E13.rar 165KB Download(1)
All Rights