Arduino Nano and NRF24L01 Based Ultrasonic Distance Measurement System
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit integrates an HC-SR04 Ultrasonic Sensor, an NRF24L01 Adapter, and an Arduino Nano microcontroller to measure distances and transmit the data wirelessly. The Arduino Nano is powered by a 9V Battery and communicates with the NRF24L01 Adapter using SPI protocol. The HC-SR04 Ultrasonic Sensor is interfaced with the Arduino Nano to trigger ultrasonic pulses and measure the reflected signals to calculate distances to objects.
Component List
HC-SR04 Ultrasonic Sensor
- Pins: VCC, TRIG, ECHO, GND
- Description: This sensor measures distances by emitting ultrasonic waves and measuring the time taken for the echo to return.
9V Battery
- Pins: -, +
- Description: Provides power to the Arduino Nano.
NRF24L01 Adapter
- Pins: CE, CSN, SCK, MOSI, MISO, IRQ, VCC, GND
- Description: A wireless communication module that allows the Arduino Nano to transmit data using the NRF24L01 chip.
Arduino Nano
- Pins: D1/TX, D0/RX, RESET, GND, D2, D3, D4, D5, D6, D7, D8, D9, D10, D11/MOSI, D12/MISO, VIN, 5V, A7, A6, A5, A4, A3, A2, A1, A0, AREF, 3V3, D13/SCK
- Description: A compact microcontroller board based on the ATmega328P, which is used to control the sensor and wireless module.
Wiring Details
HC-SR04 Ultrasonic Sensor
- VCC: Connected to Arduino Nano 5V
- TRIG: Connected to Arduino Nano D3
- ECHO: Connected to Arduino Nano D2
- GND: Connected to Arduino Nano GND
9V Battery
- - (Negative): Connected to Arduino Nano GND
- + (Positive): Connected to Arduino Nano VIN
NRF24L01 Adapter
- CE: Connected to Arduino Nano D9
- CSN: Connected to Arduino Nano D10
- SCK: Connected to Arduino Nano D13/SCK
- MOSI: Connected to Arduino Nano D11/MOSI
- MISO: Connected to Arduino Nano D12/MISO
- IRQ: Not connected
- VCC: Connected to Arduino Nano 5V
- GND: Connected to Arduino Nano GND
Arduino Nano
- D13/SCK: Connected to NRF24L01 Adapter SCK
- D12/MISO: Connected to NRF24L01 Adapter MISO
- D11/MOSI: Connected to NRF24L01 Adapter MOSI
- D10: Connected to NRF24L01 Adapter CSN
- D9: Connected to NRF24L01 Adapter CE
- D3: Connected to HC-SR04 Ultrasonic Sensor TRIG
- D2: Connected to HC-SR04 Ultrasonic Sensor ECHO
- 5V: Connected to HC-SR04 Ultrasonic Sensor VCC and NRF24L01 Adapter VCC
- GND: Common ground for all components
- VIN: Connected to 9V Battery +
Documented Code
// Libraries to include
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
// Define pins
// Ultrasonic pins
const int echoPin = 2;
const int trigPin = 3;
// NRF24L01 pins
const int CE_PIN = 9;
const int CSN_PIN = 10;
RF24 radio(CE_PIN, CSN_PIN);
// Define address
const byte address[6] = "00001";
void setup() {
// Setup ultrasonic sensor
pinMode(trigPin, OUTPUT); // Sets the trigger pin as an output
pinMode(echoPin, INPUT); // Sets the echo pin as an input
Serial.begin(9600); // Starts serial communication
// Setup for NRF24L01
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN); // Sets power amplifier level to minimum
radio.stopListening(); // Sets NRF24L01 module as transmitter
}
void loop() {
// Clear the trigger pin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Set the trigger pin on HIGH state for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echoPin; pulseIn returns the sound wave travel time in microseconds
long int duration = pulseIn(echoPin, HIGH);
// Calculate the distance
// Speed of sound in cm/us is approximately 0.0343
// Divide by 2 because the pulse travels to the object and back
float distance = (duration * 0.0343) / 2;
// Print distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
// Send distance value to receiver
radio.write(&distance, sizeof(distance));
// Delay between measurements
delay(1000);
}
This code initializes the HC-SR04 Ultrasonic Sensor and NRF24L01 module, measures the distance to objects using the ultrasonic sensor, and transmits the distance data wirelessly via the NRF24L01 module. The setup() function configures the pins and initializes serial communication and the wireless module. The loop() function triggers the ultrasonic sensor, reads the echo, calculates the distance, and sends the data to a receiver.