How To Make Communication Between Arduino And Raspberry Pi Using Nrf24l01 Module (Part-I)
Ultrasonic with Arduino Uno:
1. Summary:
2. Required Components:
- Ultrasonic Sensor
- Arduino Uno
- Jump wires
- Arduino cable
3. Required Software:
- Arduino IDE
4. Introduction to Ultrasonic Sensor:
4.1. How Ultrasonic Sensors Work:
Ultrasonic Sensor is sound wave transmitting and sensing electronic device. It uses sound to determine the distance between the sensor and the closest object in its path. The sensor sends out a sound wave at a specific frequency. It then listens for that specific sound wave to bounce off of an object and return. The sensor keeps track of the time between sending the sound wave and the sound wave returning. To know how fast something is going and how long it is traveling you can find the distance traveled with equation 1.
Equation 1. d = v × t
4.2. The HCSR04 Specifications are listed below:
- Power Supply: +5V DC
- Quiescent Current: <2mA
- Working current: 15mA
- Effectual Angle: <15deg
- Ranging Distance: 2400cm
- Resolution: 0.3 cm
- Measuring Angle: 30deg
- Trigger Input Pulse width: 10uS
4.3. Timing Chart and Pin Explanations:
- It has four pins, The VCC and GND pins are the simplest the power the HCSR04.
- These pins need to be attached to a +5-volt source and ground respectively.
- One single control pin: the TRIG pin.
- The TRIG pin is responsible for sending the ultrasonic burst.
- This pin should be set too HIGH for 10 μs, at which point the HCSR04 will send out an eight-cycle sonic burst at 40 kHz.
- After a sonic burst has been sent the ECHO pin will go HIGH.
- The ECHO pin is the data pin it is used in taking distance measurements.
- After an ultrasonic burst is sent the pin will go HIGH, it will stay high until an ultrasonic burst is detected back, at which point it will go LOW.
4.4. Taking Distance Measurements:
4.5. Wiring/Connection the HCSR04 to a Microcontroller:
- The HCSR04 has 4 pins: VCC is a 5v power supply. This should come from the microcontroller.
- GND is a ground pin. Attach to ground on the microcontroller.
- TRIG should be attached to a GPIO pin that can be set to HIGH.
- ECHO is a little more difficult. The HCSR04 outputs 5v, which could destroy many microcontroller GPIO pins (the maximum allowed voltage varies).
4.6. Disadvantage and Drawbacks:
- When an object has an uncertain surface then it has the possibility that sound wave may not reach up to the ECHO pin such that data may be lost or accurate data may not receive.
- It may also possible that the object has distorted surface due to which sound waves from TRIG hits at distance d1 and time t1 and the sound waves received at ECHO at distance d2 and time t2.
- It is also possible there is another object O2 present other than intended object O1 in surrounding due to which distance received at ECHO is not accurate and sometimes not actual which may lead to the wrong prediction.
- And also, Angles are important to be set.
5. Introduction to Arduino Uno:
5.1. Pin Configuration:
Power Pins:
- Vin: Input voltage to Arduino, when using an external power source (6-12V)
- 5V: Regulated power supply used to power microcontroller and other components on
the board. - 3.3V: 3.3V supply generated by on-board voltage regulated maximum current draw in 50mA.
- GND: Ground pin.
PWM Pins: 3,5,6,9,10,11. Each provides 8-bit PWM output.
Serial Rx: It is used to transmit TTL data.
Serial Tx:It is used to receive TTL data.
SPI Pins:
- 10 (SS): Slave Select.
- 11 (MOSI): Master Out Slave In.
- 12 (MISO): Master In Slave Out.
- 13 (SCK): Serial Clock.
IIC Pin:
- A4: (SDA) Serial Data line.
- A5: (SCL) Serial Clock line.
AREF Pin:
Analogue:
5.2. Arduino Uno Technical Specifications:
Microcontroller | ATmega328-8-bit AVR family microcontroller |
Operating Voltage | 5V |
Recommended Input Voltage | 7-12V |
Input Voltage limit | 6-20V |
Analogue Input Pins | 6 (A0-A5) |
Digital I/O Pins | 14 |
DC Current on I/O Pins | 40mA |
DC Current on 3.3V Pin | 50mA |
Flash Memory | 32KB (0.5KB is used for Bootloader) |
SRAM | 2KB |
EEPROM | 1KB |
Frequency (clock speed) | 16MHz |
5.3. Installation of Arduino Ide for implementation of code:
5.4. Code for Ultrasonic Sensor in Arduino IDE:
- We need to add NewPing.h library
Further code:
// Hook up HC-SR04 with Trig to Arduino Pin 10, Echo to Arduino pin 13 // Maximum Distance is 400 cm #define TRIGGER_PIN 4 #define ECHO_PIN 2 #define MAX_DISTANCE 400 NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); float duration, distance; int iterations = 5; void setup() { Serial.begin (9600); }
void loop() { duration = sonar.ping_median(iterations); // Determine distance from duration // Use 343 metres per second as speed of sound distance = (duration / 2) * 0.0343; // Send results to Serial Monitor Serial.print("Distance = "); if (distance >= 400 || distance <= 2) { Serial.println("Out of range"); } else { Serial.print(distance); Serial.println(" cm"); delay(500); } delay(500); }
5.5. Output:
6. Introduction to nRF24L01 (Trans-receiver):
6.1. Pin Configuration:
6.2. nRF24L01 Technical Specifications:
Frequency Range | 2.4GHz ISM Band |
Max Air Data Rate | 2Mb/s |
Modulation Format | GFSK |
Max Output Power | 0dBm |
Operating Supply Voltage | 1.9-3.6V |
Max Operating current | 13.5mA |
Min Current (Standby mode) | 26µA |
Logic Inputs | 5V Tolerant |
Communication Range | 800+ (line of sight) |
6.3. RF Channel Frequency:
- The nRF24L01 trans-receiver module transmits and receives data on a certain frequency called Channel. A band of frequency clustered around 2.4GHz has been designated.
- The channel could be any frequency in the 2.4 GHz ISM band or to be precise, it could be between 2.400GHz to 2.525GHz.
- That means total 125 channels (Network) are possible at independent modems in one place.
- Each channel will occupy 1MHz.
Conclusion: After going through this tutorial you might have got the brief idea about the Ultrasonic sensors and Arduino-Uno, fun experimenting by connecting them and fetching the data back on Arduino-Uno, also more elaborating the trans-receiver(nRF24L01). My approach was to provide easy and fun learning. Do give Suggestions and Corrections.