How To Make Communication Between Arduino And Raspberry Pi Using Nrf24l01 Module (Part-I)

Briefing on making the Arduino and raspberry pi talking with each other using NRF24L01 wirelessly.

How To Make Communication Between Arduino And Raspberry Pi Using Nrf24l01 Module (Part-I)

Ultrasonic with Arduino Uno:

1. Summary:

In this blog, we will understand, Introduction to Ultrasonic Sensor and Arduino-Uno also how the Ultrasonic sensor works with Arduino Uno and how to implements it for the communication. How it works by writing necessary code and Getting Basic idea about the Nrf24L01 and its channel, What’s its purpose? So, let’s begin…

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

 
NOTE: The speed of sound can be calculated based on a variety of atmospheric conditions, including temperature, humidity, and pressure. It should be noted that ultrasonic sensors have a cone of detection, the angle of this cone varies with distance.
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:
The HCSR04 can be triggered to send out an ultrasonic burst by setting the TRIG pin to HIGH. Once the burst is sent the ECHO pin will automatically go HIGH. This pin will remain HIGH until the burst hits the sensor again. You can calculate the distance to the object by keeping track of how long the ECHO pin stays HIGH. The time ECHO stays HIGH is the time the burst spent traveling. Using this measurement in equation 1 along with the speed of sound will yield the distance traveled.
To use equation 1 the speed of sound needs to determined, which is 343 meters per second at standard temperature and pressure. To convert this into more useful form use equation 2 to change from meters per second to microseconds per centimeter. Then equation 3 can be used to easily compute the distance in centimeters.
4.5. Wiring/Connection the HCSR04 to a Microcontroller:
The HCSR04 has 4 pins:
  1. The HCSR04 has 4 pins: VCC is a 5v power supply. This should come from the microcontroller.
  2. GND is a ground pin. Attach to ground on the microcontroller.
  3. TRIG should be attached to a GPIO pin that can be set to HIGH.
  4. ECHO is a little more difficult. The HCSR04 outputs 5v, which could destroy many microcontroller GPIO pins (the maximum allowed voltage varies).
In order to step down the voltage use a single resistor or a voltage divider circuit. Once again, this depends on the specific microcontroller you are using, you will need to find out its GPIO maximum voltage and make sure you are below that.
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.
Reset Pin: To reset the microcontroller.
Analog Pins:A0-A5, used to measure analog voltage in the range of 0-5V.
Digital I/O pins:Total 14, out of which 6 provides PWM output.
NOTE: PWM means Pulse Width Modification. It is a technique for getting analog result with digital means. Digital control is used to create square waves, a signal switched between ON and OFF.

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.

NOTE: TTL means transistor-to-transistor logic. Serial communication at a TTL level will always remain between the limit of 0V and Vcc which is often 5V or 3.3V. A logic HIGH (‘1’) is represented by the Vcc, while a logic LOW (‘0’) is 0V.
External Interrupt: Pin 2 and 3 to trigger an interrupt.
NOTE: SPI means Serial Peripheral Interface.
SPI Pins:
  • 10 (SS): Slave Select.
  • 11 (MOSI): Master Out Slave In.
  • 12 (MISO): Master In Slave Out.
  • 13 (SCK): Serial Clock.
NOTE: Inbuilt 13: – To turn on the inbuilt LED.
IIC Pin:
  • A4: (SDA) Serial Data line.
  • A5: (SCL) Serial Clock line.
NOTE: IIC means Inter-Integrated Circuit. Or say TWC i.e. Two Wire Circuit interface.IIC is a Half-Duplex Communication. TWC (peripheral) provides an interface to components on a unique two-wire bus, consisting of one clock line and one data line.
AREF Pin:
Reference
Analogue:
It allows us to feed the Arduino a reference voltage from an external (power) supply.
5.2. Arduino Uno Technical Specifications:
MicrocontrollerATmega328-8-bit AVR family microcontroller
Operating Voltage5V
Recommended Input Voltage7-12V
Input Voltage limit6-20V
Analogue Input Pins6 (A0-A5)
Digital I/O Pins14
DC Current on I/O Pins40mA
DC Current on 3.3V Pin50mA
Flash Memory32KB (0.5KB is used for Bootloader)
SRAM2KB
EEPROM1KB
Frequency (clock speed)16MHz
5.3. Installation of Arduino Ide for implementation of code:
Here is a link to Download and proceed further according to the instruction mention there.
5.4. Code for Ultrasonic Sensor in Arduino IDE:
#include “NewPing.h”
  • We need to add NewPing.h library
Search NewPing.h in Library Manager
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:
1) GND Pin: Ground pin can be used as a reference for identifying the other pins.
2) Vcc Pin: It supplies power for the module. This can be anywhere from 1.9 to 3.9V.
NOTE: We can connect the 3.3V output from Arduino. Remember connecting it to the 5V pin will likely to destroy nRF24L01 module.
3) CE (Chip Enable): It is an active HIGH Pin. When selected the nRF24L01 will either transmit or receive, will depend upon which mode it is currently in.
4) CSN Pin: It is an active LOW pin. It is normally kept HIGH. When this pin goes LOW, the nRF24L01 begins listening on its SPI port for data and processes it accordingly.
5) SCK: Accept clock pulse provided by the SPI bus master.
6) MOSI: It is SPI input to nRF24L01.
7) MISO: It is SPI output to nRF24L01.
8) IQR: It is an interrupt pin that can alert the master when new data is available to process.
6.2. nRF24L01 Technical Specifications:
Frequency Range2.4GHz ISM Band
Max Air Data Rate2Mb/s
Modulation FormatGFSK
Max Output Power0dBm
Operating Supply Voltage1.9-3.6V
Max Operating current13.5mA
Min Current (Standby mode)26µA
Logic Inputs5V Tolerant
Communication Range800+ (line of sight)
NOTE: ISM means Industrial, Scientific and Medical (ISM) radio bands reserved internationally for the radio frequency (RF) energy for Industrial, Scientific and Medical purpose other than communicational.
NOTE: GFSK means Gaussian Frequency-Shift Key. It filters the data pulses with a Gaussian filter to make the transition smother. This filter has the advantage of reducing sideband power, reducing interference with neighbor channels, at the cost of increasing inter-symbol interface.
NOTE: The SPI bus uses a concept of a Master and Slave, in most common applications where Arduino is the Master and the nRF24L01 trans-receiver module is the Slave.
NOTE: Unlike the IIC bus the number of Slaves on the SPI bus is limited on the Arduino Uno can use a maximum of two SPI Slaves i.e. 2 nRF24L01.
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.