Medição de distância sem fio usando sensor ultrassônico (Parte 18/23)

Wireless distance measurement using ultrasonic sensor (Part 18/23)

Distance measurement has important applications in automotive and industrial applications. Distance measurement through sensors is useful in detecting obstacles and measuring fluid levels in automotive tanks and containers. It's the distance measurement feature that made it possible to imagine autonomous cars and robots. Without technology that could have made it possible to measure the distance to an object or obstacle, the concept of autonomous driving would never have been thought of. The distance measurement application is also used in industries to check fuel levels in aircraft and commercial transport vehicles. The application is used in robotics to equip autonomous robots to detect obstacles and find an accessible path. This project is also a distance measurement application using ultrasonic sensors.

Protótipo de medidor de distância sem fio baseado em Arduino

Fig. 1: Arduino-based wireless distance meter prototype

Any distance measurement application has a sensor circuit and an actuator or display circuit (to change the path according to obstacle detection or display the distance reading respectively). Often these circuits are connected via a fixed line to transmit data between the two. This project used a 434 RF module to transmit distance reading instead of using a wired bus. This way, the project can be easily implemented in any industrial environment where installing the cable between the sensor circuit and the actuator/display circuit may be risky or more expensive. An RF module has a typical operating range of 50 to 60 meters and can be extended to 300 to 350 meters using an antenna and higher transmit power. In this way, the wireless distance measurement system can be deployed at any location and have supervisory or control operations activated from a remote location.

The project uses an ultrasonic sensor to measure distances (and the sensor used can measure distances from 2 to 400 cm) and is built on the Arduino Pro Mini. A 16X2 LCD is used in the display circuit to show the measured readings. A 434 RF transmitter and receiver form the wireless bridge between the sensor circuit and the display circuit.

Required components

Mr. No. Required components Required quantity
1 RF Rx Module (434 MHz) 1
two RF Tx Module (434Mhz) 1
3 Ultrasonic sensor 1
4 LCD 1
5 1k pot 1
6 10k resistor 1
7 Arduino pro mini development board two
8 Battery – 9V two
9 Test board two
10 Connecting wires

Imagem mostrando o funcionamento do medidor de distância sem fio baseado em Arduino

BLOCK DIAGRAM

Circuit Connections

There are two circuits in the project – Sensor Circuit and Display Circuit. The sensor circuit is built on an Arduino Pro Mini. The ultrasonic sensor is connected to pins 2 and 4 of the Arduino. The ultrasonic sensor has four pins – Ground (Pin 1), Eco (Pin 2), Trigger (Pin 3) and Trigger. The VCC and ground pins are connected to VCC and ground respectively. The Echo pin is connected to pin 4 of the Arduino board, while the Trigger pin is connected to pin 2 of the Arduino board. An RF transmitter is connected directly to the Arduino Pro Mini with pin 2 connected to pin 12 of the board for serial data output and has an antenna connected to pin 4 of the module.

Medição de distância sem fio usando sensor ultrassônico

Fig. 2: Image showing the operation of the Arduino-based wireless distance meter

The other circuit is the display circuit. It has an RF receiver connected to the Arduino Pro Mini with pin 2 connected to pin 11 on the board for serial data input. An antenna is connected to pin 8 of the RF receiver. An LCD is connected to the Arduino board to show the distance reading. The 16X2 LCD display is connected to the Arduino board by connecting its data pins to pins 7 to 4 of the Arduino board. The RS and E pins of the LCD are connected to pins 3 and 2 of the Arduino Pro Mini, respectively. LCD RW pin is grounded.

LCD ARDUINO UNO
LOL 3
R.W. GRND
AND two
D7,D6,D5,D4 7,6,5,4 respectively
The standard code library for interfacing Arduino UNO and Arduino Pro Mini is used in the project to program the LCD with the board.

How the circuit works

The ultrasonic sensor works on the principle of sound wave echo. When a 10usec HIGH pulse is passed to the trigger pin of the sensor, it transmits eight 40KHz waves of HIGH sonic pulse triggers consecutively. A high pulse signal comes out of the echo pin as the ultrasonic wave is transmitted. This wave, when colliding with an obstacle, is reflected back and detected by the sensor. When the wave is detected again, the high pulse signal from the sensor's echo pin is terminated. The signal received from the echo pin is analog in nature. The distance to the obstacle can be measured by measuring the maximum echo pin time. This is the time between transmission and reflection of the sound wave. The distance is given by the formulas -:

Test distance = (high level time × speed of sound (340M/S)) / 2

The time multiplied by the speed is divided by 2, as the time required for the sonic wave to reach the obstacle and return. Therefore, the distance measurement in cm can be given by the formulas –:

Test distance = (high level time × speed of sound (340M/S)) / 2

= (high level time (microsecond) × speed of sound (340M/S)) / 2

= high level time x 340/2000000 m

= high level time x 34,000/2,000,000 cm

= high level time x 34,000/2,000,000 cm

The ultrasonic sensor outputs the high pulse from pin 2, which is detected on pin 12 of the Arduino board. The program code measures the pulse duration and digitizes it into a distance value using the formulas given above. The distance measurement is transmitted serially using the RF transmitter in the form of decimal characters.

Imagem do medidor de distância sem fio baseado em Arduino

Fig. 3: Arduino based wireless distance meter circuit diagram

In the display circuit, the decimal characters corresponding to the distance measurement are received by the RF receiver and passed in series to pin 11 of the Arduino Pro Mini on the receiver side. The Arduino on the receiver side has the built-in program to store the received characters serially in a memory buffer and pass the buffered characters to the 16X2 LCD in a presentable format. Check out the Arduino program code on the transmitter side to learn how the pulse width of the ultrasonic sensor is measured and the measurement is converted to decimal characters for presentation. Then check the program code on the transmitter side to see how the received characters are buffered and displayed on the LCD.

Programming Guide

On the transmitter side Arduino, first the standard libraries are imported. The VirtualWire library is imported to access the Ultrasonic Sensor's analog input.

#include

There are two defined constants “trigpin” mapped to pin 2 where the trigger pin of the ultrasonic sensor is connected and “echopin” where the echo pin of the ultrasonic sensor is connected.

A “Distance” array of type character is created to store characters corresponding to the measured distance value.

A setup function is called where the baud rate of the Arduino board is set to 9600 bits per second using the Serial.begin function.

The vw_setup function is used to set the data rate for serial transmission to 2000 bits per second.

A loop function is called, where the variable “duration” is declared to store the duration of the pulse width, the variable “distance” to store the distance value in meters and the variable “cm” to store the distance value in cm .

The 10uS trigger pulse is generated at the trigger pin by setting the “trigpin” variable to output using the pinMode function. A LOW is passed to the trigger pin to start the pulse trigger for two microseconds followed by a HIGH pulse of 10 microseconds, then the trigger pin is set to LOW to end the pulse trigger.

The echopin is set for input using the pinMode function. The duration of the HIGH pulse on the echo pin is measured using the pulseIn function and stored in the “duration” variable.

Pulse width measurement is converted to distance measurement using time-velocity formulas. The measurement is in meters which is converted to centimeters by multiplying by 100.

The distance measurement in cm is stored in the microcontroller buffer using the Serial.print function along with the string “cm”. The distance measurement in cm is converted to character value and stored in the Distance array using itoa integer to character conversion function.

The LED connected to pin 13 is lit to indicate data transmission is in progress. The distance value is sent serially to the RF transmitter using the vw_send function where the characters are converted to unsigned characters as a parameter. The vw_wait_tx function is used for the microcontroller to request push to talk until all characters are transmitted serially. The LED is off to indicate that the distance reading has been transmitted successfully.

This terminates the loop function and thus the program code on the transmitter side.

On the receiver side Arduino board, standard libraries are imported. Liquidcrystal.h is imported to realize LCD interface and content display. An “lcd” array is created with pins connected to the LCD mapped to the LiquidCrystal object. The VirtualWire library is imported to read serial data from the RF receiver.

Additional global variables are declared – “ledpin” mapped to pin 13 where the receive indicator LED is connected, “Data” to store the integer value of the distance reading and distance array to read one by one character from the distance buffer. received characters.

A setup function is created to execute the initialization code. Inside the function, the Arduino baud rate is set to 9,600 bits per second using the Serial.begin function. The lcd is configured for 16X2 mode using the lcd.begin function. The initial messages flash and the cursor is placed on the first line of the display. The pinMode function is used to set the output of the pins connected to the LCD.

The RF transmitter and receiver module does not have a Push To Talk pin. They are inactive when no data is present to transmit or receive respectively. Therefore, vw_set_ptt_inverted(true) is used to set the push to talk polarity and request the receiver to continue receiving data after fetching the first character. The baud rate for serial input is set to 2000 bits per second using the vw_setup function. Data reception is started using vw_rx_start .

A loop function is called where the “buff” array of unsigned character type is created to retrieve characters from buffer and “buflen” variable is created to keep check the length of the character buffer.
The character buffer is detected using the vw_get_message function, if present, an “i” counter is initialized and the LED connected to pin 13 is turned on by sending a HIGH on pin 13 to indicate successful character buffer detection. The character buffer is stored in the Distance array using the for loop with the counter initialized. The null character is detected in the buffer stream so that no junk value goes to the LCD display. The characters received from the buffer are converted to an integer value and stored in the “Data” variable. The value of the variable along with the relevant strings included are passed to the microcontroller's buffer and passed to the LCD for display in a presentable format . This ends the loop function and therefore the program code on the receiver side.

Project source code

###


 #include

 const int trigPin = 2;

 const int echoPin = 4;

 char Distance(4); void setup {

 Serial.begin(9600);

 vw_setup(2000);

 }

 void loop {

 int duration, distance, cm;

 pinMode(trigPin, OUTPUT);

 digitalWrite(trigPin, LOW);

 delayMicroseconds(2);

 digitalWrite(trigPin, HIGH);

 delayMicroseconds(10);

 digitalWrite(trigPin, LOW);

 pinMode(echoPin, INPUT);

 duration = pulseIn(echoPin, HIGH);

 distance=duration*34/200000;

 //Convert meter to cm

 cm = distance*100

 Serial.print(cm);

 Serial.print("cm");

 Serial.println;

 delay(100);
 //Convert integer value to character value

 itoa(cm,Distance,10);

 digitalWrite(13, true); // Turn on a light to show transmitting

 vw_send((uint8_t *)Distance, strlen(Distance));
 
vw_wait_tx ; // Wait until the entire message is gone

 digitalWrite(13, false); // Turn off a light after transmission

 delay(200);

 }

 #include

 #include

 LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

 // LED's
 int ledPin = 13;

 // Sensors

 int Date;

 // RF Transmission container

 char Distance(4);

 void setup {

 Serial.begin(9600);

 lcd.begin(16, 2);

 lcd.print("ENGINEERS GARAGE");

 lcd.setCursor(0, 1);

 // sets the digital pin as output

 pinMode(ledPin, OUTPUT);

 pinMode(9, OUTPUT);

 pinMode(8, OUTPUT);
 //VirtualWire

 // Initialize the IO and ISR

 // Required for DR3100

 vw_set_ptt_inverted(true);

 // Bits per sec

 vw_setup(2000);

 // Start the receiver PLL running

 vw_rx_start ;

 } // END void setup

 void loop {

 uint8_t buf(VW_MAX_MESSAGE_LEN);

 uint8_t buflen = VW_MAX_MESSAGE_LEN;

 // Non-blocking

 if (vw_get_message(buf, &buflen))
 {

 int i;

 // Turn on a light to show received good message

 digitalWrite(13, true);

 // Message with a good checksum received, dump it.

 for (i = 0; i 

 Distance(buflen) = '';
 
// Convert Sensor1CharMsg Char array to integer

 Data = atoi(Distance);

 // DEBUG

 Serial.print("distance = ");

 Serial.print(Data);

 Serial.println(" cm ");

 lcd.setCursor(0, 2);

 lcd.print("Distance = ");

 lcd.print(Data); // change the analog out value:

 lcd.print("cm ");

 }
}

###

Circuit diagrams

Arduino based wireless distance meter

Project video

Related Content

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.