Sensor ultrassônico com NRF24LE1 (Parte 11/14)

Ultrasonic sensor with NRF24LE1 (Part 11/14)

Today we are going to make a very interesting sensor interface with NRF module .

Have you ever heard bats communicating with each other? We can't hear them because they use ultrasonic frequency to communicate. Ultrasonic frequencies cannot be heard by human ears. Humans can only hear sound between the frequency of 20 to 20 KHz, while the ultrasonic frequency is above 20 KHz and expands up to several GHz.

Protótipo de interface NRF24LE1 com sensor ultrassônico

Fig. 1: NRF24LE1 interface prototype with ultrasonic sensor

Ultrasonic waves have different travel speeds in air, liquids and gases. In air, they generally travel at 340 m/s. They have a tendency to be reflected from a solid surface. Due to this functionality, they are used to measure distances. If the time between transmission and reception is known, the distance from the solid surface of the transmitter can be calculated.

We will use the HCSR04 ultrasonic sensor which has a built-in transmitter, receiver and control circuit. We will interface this sensor with the NRF24LE1 using input/output pins.

HCSR04 has 4 pins:

1. VCC – 5V

2. Trig – Trigger input pin

3. Echo – Output Pin

4. Gnd – Earth

Some specifications of this sensor are:

· Distance measurement range from 2 cm to 400 cm

· Working frequency – 40Khz

· Uses a trigger pulse of 10 uS (microseconds).

· Transmits eight 40 kHz pulses when triggered

· Receives an echo and emits a high pulse with a range proportional to the distance.

· 60 mS delay must be given between the next trigger.

Distance calculation

Imagem da interface NRF24LE1 com sensor ultrassônico

Fig. 2: Image of the NRF24LE1 interface with ultrasonic sensor

There are two ways to measure distance:

1. First determine the time between transmission and reception of the echo. Then use the formula = Time * speed / 2. In air speed is 340 m/s.

2. Calculate the time the sensor output pin is high. Then use the predetermined formula, distance in cm = time/58 or distance in inches = time/148. The time will be upon us.

We will be using the second method for distance calculation.

Please make sure you have read our previous articles on Timers and NRF24LE1 .

The NRF module will be used to activate the sensor with a 10 us pulse. After triggering, the sensor will send a 40 KHz signal and wait for the echo. If the echo is received, it will emit a loud pulse. This high pulse will have a band width proportional to the distance.

In NRF, pin0 of port0 or P00 is used as output pin to drive the sensor, while pin1 of port0 or P01 is used as input pin to receive the high pulse from the sensor.

We are using Timer0 to measure the period of time during which the output pin remains high. Timer0 is initialized in Mode1, which is 16-bit mode. This means we can measure time up to (0.75 * 65536) = 49152 uS. To calculate the time measured by timer0 we use time = (timer0 register value * 0.75) uS. To calculate the distance to the solid object we can use distance in cm = time/58.

We wrote the code so you understand how it works.

Project source code

###

 //Program to
/* Copyright (c) 2009 Nordic Semiconductor. All rights reserved.
*
*The information contained herein is the confidential property of Nordic * ASA Semiconductor. Terms and conditions of use are described in detail * in the NORDIC SEMICONDUCTORS STANDARD SOFTWARE LICENSE AGREEMENT. * * Licensees are granted free and non-transferable use of the information. NO * WARRANTY OF ANY KIND is provided. This title should NOT be removed from the * the file. * *$LastRevisionAmended: 133$ */ #include //standard I/O library #include "nrf24le1.h" // I/O header file for NRF24LE1 #include "hal_uart.h" // library containing serial communication functions #include "hal_clk.h" // library containing clock functions #include //standard library #include //library containing string functions #include //library containing mathematical functions #include "hal_delay.h" // header file containing delay functions sfr16 DPTR = 0X82; //functional register to store value // Putchar repeated to print a string void putstring(char *s) { while(*s != 0) putchar(*s++); } //function to convert integer to string array empty itoa(int num, char* str) { int i= 0, count = 0; internal temperature = num; if(num == 0) { str(i++) = '0'; str(i) = ''; turn back; } while (temperature!=0) { int rem = temp% 10; temperature = temperature/10; count++; } str(count --) = ''; while(num!= 0) { int rem = num% 10; str(count --) = (rem > 9) ? (rem - 10) + '1' : rem + '0'; num = num/10; } turn back; }

###

Circuit diagrams

Circuit Diagram-NRF24LE1-Interface-Ultrasonic Sensor

Project video

Related Content

Back to blog

Leave a comment

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