Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino Nano and NRF24L01 Based Ultrasonic Distance Measurement System

View and Edit Project In Cirkit DesignerImage of Arduino Nano and NRF24L01 Based Ultrasonic Distance Measurement System

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

9V Battery

NRF24L01 Adapter

Arduino Nano

Wiring Details

HC-SR04 Ultrasonic Sensor

9V Battery

NRF24L01 Adapter

Arduino Nano

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.