In this chapter you will learn the principle of ultrasonic ranging, how to use infrared photoelectric sensor and how to program Penguin Bot to realize obstacle avoidance by using ultrasonic ranging and optoelectronic switch together.
Principle of Ultrasonic Ranging:
Feature of the module: testing distance, high precision module.
Application of the products: robot obstacle avoidance、object testing distance、liquid testing、public security、parking lot testing.
Main technical parameters:
• voltage used: DC---5V
• static current: less than 2mA
• level output: higher than 5V
• level output: lower than 0
• detection angle: not bigger than 15 degree
• detecting distance: 2cm-450cm
• high precision: up to 0.2cm
Method of connecting lines: VCC, trig (the end of controlling), echo (the end of receiving), GND
How does the module work:
• Apply IO port of TRIG to trigger ranging, give high level signal, at least 10us one time;
• The module sends 8 square waves of 40kz automatically, tests if there are signals returned
automatically;
• If there are signals received, the module will output a high level pulse through IO port of
ECHO, the duration time of high level pulse is the time between the wave sending and receiving.
So the module can know the distance according to the time.
Testing distance= (high level time* velocity of sound (340M/S))/2);
Actual operation:
The Timing diagram is shown below. You only need to supply a short 10uS pulse to the trigger input
to start the ranging, and then the module will send out an 8 cycle burst of ultrasound at 40 kHz and
raise its echo. The Echo is a distance object that is pulse width and the range in proportion .You can
calculate the range through the time interval between sending trigger signal and receiving echo
signal. Formula: uS / 58 = centimeters or uS / 148 =inch; or: the range = high level time * velocity
(340M/S) / 2; we suggest to use over 60ms measurement cycle, in order to prevent trigger signal
to the echo signal.
How to Use
Connect the ultrasonic sensor with NANO R3 Board from pin ECHO to D4, pin TRIG to D5 and pin VCC to GND. Then use get Distance Function in the program to obtain distance data. The unit of ranging data is cm.
Circuit Diagram
A, K are the positive and negative anodes of infrared emitting diode, C, E are the positive and negative anodes of infrared receiving diode. Polarity A connects to high level and Polarity K connects to low level then infrared emitting diode can transmit infrared.
When there is no reflect of infrared, Polarity C and E will be cut off and no current flows, then Polarity E is low level. When there is reflect of infrared, Polarity C and E will be connected and current flows, then Polarity E is low level.
ST188 Infrared Sensor Application
On the main board of Penguin Bot, connect Polarity E of the right ST188 to Pin A1 of UNO board and connect Polarity E of the left ST188 to Pin A0 of UNO board, by measuring voltage from Pin ADC of UNO board to Polarity E Penguin Bot can detect whether there are obstacles in front.
Using analogRead function in the program can read the data of ST188 which is from 0 to 1023.
Principle of Obstacle Avoidance
Ultrasonic sensor detects the distance of the obstacle in front and ST188 detects where there obstacles on the left and right side. If the obstacle is very near then Penguin Bot will make a turn or walk backwards. Otherwise it will continue walking forwards.
Open the Obstacle folder in the current path and launch the .ino file inside.
After opening the .ino file, you will first see the pin definitions for servos. Then the data pins of the ultrasonic sensor are assigned to D4 and D5. The pins of the ST188 sensor are connected to A0 and A1 respectively.
Next, let's sort out the obstacle avoidance logic. When an obstacle is detected ahead while moving, the robot will turn left or right to bypass it. The ultrasonic sensor detects obstacles in front, and the infrared sensor assists in making avoidance decisions.
In the getDistance function, high and low level pulses are used to acquire the time difference, and the distance is calculated with conversion formulas.
obstacleMode():
The obstacleMode() function calls getDistance() to acquire the distance of obstacles ahead. It then determines whether to move straight or perform obstacle avoidance based on the detected distance.