Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

Arduino Nano Based LoRa-Enabled Weather Station with Ultrasonic Distance Measurement

Image of Arduino Nano Based LoRa-Enabled Weather Station with Ultrasonic Distance Measurement

Circuit Documentation

Summary

This circuit integrates an Arduino Nano microcontroller with an HC-SR04 Ultrasonic Sensor, a LoRa Ra-02 SX1278 module, and an AM2302 Humidity and Temperature Sensor. The purpose of the circuit is to measure distance using the ultrasonic sensor, read humidity and temperature data, and transmit this information wirelessly using the LoRa module. The Arduino Nano serves as the central processing unit, interfacing with the sensors and handling data acquisition and communication.

Component List

Arduino Nano

  • Microcontroller board based on the ATmega328P
  • Offers a variety of digital and analog I/O pins
  • Capable of serial communication and supports external peripherals

HC-SR04 Ultrasonic Sensor

  • Ultrasonic distance measuring module
  • Operates on a 5V supply
  • Provides 2cm to 400cm non-contact measurement functionality

LoRa Ra-02 SX1278

  • Long-range wireless communication module
  • Operates in the 433 MHz frequency band
  • Utilizes LoRa spread spectrum modulation technology

AM2302 Humidity and Temperature Sensor

  • Digital signal output temperature and humidity sensor
  • Combines calibrated humidity and temperature sensing elements
  • Simple communication via a single data pin

Wiring Details

Arduino Nano

  • GND connected to GND of all other components
  • 5V supplies power to the HC-SR04 Ultrasonic Sensor and AM2302 Sensor
  • D2 connected to DI00 of LoRa Ra-02 SX1278
  • D3 connected to TRIG of HC-SR04 Ultrasonic Sensor
  • D4 connected to ECHO of HC-SR04 Ultrasonic Sensor
  • D8 connected to DATA of AM2302 Humidity and Temperature Sensor
  • D9 connected to RST of LoRa Ra-02 SX1278
  • D10 connected to NSS of LoRa Ra-02 SX1278
  • D11/MOSI connected to MOSI of LoRa Ra-02 SX1278
  • 3V3 supplies power to LoRa Ra-02 SX1278
  • D12/MISO connected to MISO of LoRa Ra-02 SX1278
  • D13/SCK connected to SCK of LoRa Ra-02 SX1278

HC-SR04 Ultrasonic Sensor

  • GND connected to GND of Arduino Nano
  • VCC connected to 5V of Arduino Nano
  • TRIG connected to D3 of Arduino Nano
  • ECHO connected to D4 of Arduino Nano

LoRa Ra-02 SX1278

  • GND connected to GND of Arduino Nano
  • NSS connected to D10 of Arduino Nano
  • MOSI connected to D11/MOSI of Arduino Nano
  • MISO connected to D12/MISO of Arduino Nano
  • SCK connected to D13/SCK of Arduino Nano
  • 3.3V connected to 3V3 of Arduino Nano
  • RST connected to D9 of Arduino Nano
  • DI00 connected to D2 of Arduino Nano

AM2302 Humidity and Temperature Sensor

  • VDD connected to 5V of Arduino Nano
  • DATA connected to D8 of Arduino Nano
  • GND connected to GND of Arduino Nano

Documented Code

#include <NewPing.h>
#include <Wire.h>
#include <SPI.h>
#include <LoRa.h>
#include <DHT.h>

#define trigPin 3
#define echoPin 4
#define MAX_DISTANCE 593
#define DHTPIN 8 // What pin we're connected to
#define DHTTYPE DHT21 // DHT 21 (AM2301)

DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor for normal 16mhz Arduino
float hum; // Stores humidity value
float temp; // Stores temperature value

// Define variables:
long duration;
int distance;

void setup() {
  // Define inputs and outputs
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  // Begin Serial communication at a baudrate of 9600:
  Serial.begin(9600);
  dht.begin();
  while (!Serial);
  Serial.println("LoRa Sender");
  
  if (!LoRa.begin(433E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
  LoRa.setTxPower(20);
  LoRa.setSpreadingFactor(12);
  LoRa.setSignalBandwidth(62.5E3);
}

void loop() {
  // Read data and store it to variables hum and temp
  hum = dht.readHumidity();
  temp = dht.readTemperature();
  int roundedHum = round(hum);
  float roundedTemp = round(temp * 10) / 10.0;

  // Clear the trigPin by setting it LOW:
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);

  // Trigger the sensor by setting the trigPin high for 10 microseconds:
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the echoPin. pulseIn() returns the duration (length of the pulse) in microseconds:
  duration = pulseIn(echoPin, HIGH);

  // Calculate the distance:
  distance = duration*0.034/2;

  String line2 = "T:"+String(roundedTemp,1)+ "C"+" H: "+String(roundedHum)+"%";
  // Combine messages with a separator
  String message = "Dist. "+ String(distance) + " cm " + "\\n" + line2;
  // Send data via LoRa
  LoRa.beginPacket();
  LoRa.print(message);
  LoRa.endPacket();

  // Print the distance on the Serial Monitor (Ctrl+Shift+M):
  Serial.print("Dista. = ");
  Serial.print(distance);
  Serial.print(" cm");
  Serial.println("");
  Serial.print("Humidity: ");
  Serial.print(hum);
  Serial.print("%, Temperature: ");
  Serial.print(temp);
  Serial.println(" Celsius");
  LoRa.endPacket();

  delay(9000);
}

This code is designed to run on the Arduino Nano and handles the interfacing with the HC-SR04 Ultrasonic Sensor and AM2302 Humidity and Temperature Sensor, as well as the communication with the LoRa Ra-02 SX1278 module. It reads the distance from the ultrasonic sensor, the humidity and temperature from the AM2302 sensor, and sends this data wirelessly via LoRa.