【问题标题】:NRF24L01 modules not connecting - hardware or coding issue?NRF24L01 模块未连接 - 硬件或编码问题?
【发布时间】:2022-06-11 08:54:37
【问题描述】:

我正在尝试使用连接到两个单独的 Arduino UNO 的两个 NRF24L01 模块将数据从超声波传感器传输到 LCD 屏幕,用于自行车的距离传感器。由于我对 Arduino 比较陌生,最初不知道如何使用 NRF24L01 模块,所以我遵循了来自 https://www.electroniclinic.com/nrf24l01-multiple-transmitters-and-single-receiver-for-sensor-monitoring-using-arduino/ 的教程 ,将其实现到我的代码中,但它不起作用。我多次确保 NRF24L01 的接线对于 Arduino UNO 是正确的,并试图更改无线电频道,但没有用。对于网站上的其他人来说,它似乎在工作,但对我来说却不是。两个程序都编译得很好。我的代码是否有问题,或者我的 NRF24L01 可能无法正常工作?

这是我的代码:

发射器:

#include <RF24Network.h>
#include <RF24Network_config.h>
#include <Sync.h>
#include <RF24.h>
#include <SPI.h>
RF24 radio(5, 4); // radio module, pins 5,4 for (CE, CSN)
RF24Network network(radio);

const uint16_t this_node = 01; //address of arduino
const uint16_t node00 = 00;

//setting up pins for each device
const int buzzerPin = 10;
const int aheadTrigPin = 9;
const int aheadEchoPin = 8;

//initialize variables for ultrasonic sensor and data
unsigned long data[3];
unsigned long aheadDistance;
unsigned long aheadDuration;

void setup() {
  Serial.begin(9600);
  SPI.begin();
  radio.begin();
  network.begin(69, this_node);
  radio.setDataRate(RF24_2MBPS);

  pinMode(buzzerPin, OUTPUT);
  pinMode(aheadTrigPin, OUTPUT);
  pinMode(aheadEchoPin, INPUT);
}

void loop() {
  //clears the "trig" condition for ultrasonic sensor
  digitalWrite(aheadTrigPin, LOW);
  delayMicroseconds(2);

  //releases sound waves from ultrasonic sensor for 10 microseconds
  digitalWrite(aheadTrigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(aheadTrigPin, LOW);

  //reads "echo" pin to return sound wave travel time in microseconds
  aheadDuration = pulseIn(aheadEchoPin, HIGH);

  //calculating distance of ultrasonic sensor, in inches (distance = velocity * time)
  aheadDistance = (aheadDuration * 0.034 / 2) * 2.54;
  Serial.println(aheadDistance);
  
  //activates buzzer to alert rider if danger values are met
  if(aheadDistance <= 100) {
    tone(buzzerPin, 255);
  }
  else {
    noTone(buzzerPin);
  }
  //send data to main component
  network.update();
  data[0] = aheadDistance; 
  
  RF24NetworkHeader header(node00);
  bool ok = network.write(header, &data, sizeof(data)); // send data
  Serial.println("sent to NRF24 server");
  delay(200);
  }
  

我将数组作为数据发送的原因是因为我目前正在努力让另一个发射器将数据发送到单个接收器。

接收者:


    #include <LiquidCrystal_I2C.h>
    #include <Wire.h>
    #include <RF24Network.h>
    #include <RF24Network_config.h>
    #include <Sync.h>
    #include <RF24.h>
    #include <SPI.h>
    
    //sets up timer for the LCD display to constantly run
    
    //sets up OLED screen
    LiquidCrystal_I2C display(0x27,20,4);
    
    RF24 radio (10, 9); // CE, CSN, defines new RF24 radio
    RF24Network network(radio);
    
    const uint64_t this_node = 00;
    
    //data variables that will be collected from other arduinos
    unsigned long data[3];
    unsigned long aheadDistance;
    unsigned long groundDistance;
    unsigned long backAheadDistance;
    
    void setup()
    {
      Serial.begin(9600);
      display.init(); // intialize lcd
      display.backlight(); //open backlight of lcd
      display.clear();
    
      //initialize starting screen
      display.setCursor(0, 0);
    
      //set up radio module (reciever)
      SPI.begin();
      radio.begin();
      network.begin(69, this_node);
      radio.setDataRate(RF24_2MBPS);
    
    }
    
    //method to update screen with current data values
    void displayDistance() {
      display.setCursor(0, 0);
      display.print("Bike Sensor");
      display.setCursor(0, 1);
      display.print("Front: ");
      display.print(aheadDistance);
      display.print(" in");
      display.setCursor(0, 2);
      display.print("Ground: ");
      display.print(groundDistance);
      display.print(" in");
      display.setCursor(0, 3);
      display.print("Back: ");
      display.print(backAheadDistance);
      display.print(" in");
      display.display();
    }
    
    void connectionLost() // activates when main component is unable to connect to others
    {
      display.clear();
      display.setCursor(0, 1);
      display.print("Connection");
      display.setCursor(0, 2);
      display.print("Lost");
      display.display();
    }
    
    void loop()
    {
      network.update();
      while(network.available()) {
        RF24NetworkHeader header;
        Serial.println("connection found");
        network.read(header, &data, sizeof(data));
        Serial.println("data recieved");
    
        if(header.from_node == 01) {
          backAheadDistance = data[0];
        }
        if(header.from_node == 02) {
          aheadDistance = data[1];
          groundDistance = data[2];
        }
        displayDistance();
        delay(100);
      }
      connectionLost();
      Serial.println("no connection found");
      delay(200);
    }
      //updates OLED screen and actually displays it on the module

【问题讨论】:

    标签: c++ arduino


    【解决方案1】:

    你的 nRF24L01 可能已经死了。我有很多。有些失败。我推荐 EByte 的那些,尤其是 ML01DP5ML01SP4。这些都很好。 还有一个鲜为人知的硬件问题:永远不要让 FIFO 缓冲区满 4 毫秒。当然很难知道。因此,只需经常冲洗它们以作为预防措施。我以这种方式修复了我的代码,现在它很健壮。

    【讨论】:

      猜你喜欢
      • 2016-04-15
      • 1970-01-01
      • 2012-07-24
      • 2014-04-02
      • 1970-01-01
      • 2016-12-26
      • 2014-03-20
      • 1970-01-01
      • 2015-02-07
      相关资源
      最近更新 更多