首 頁 > 產品展示 > ZigBee 通訊模組/ 感測器 > RF 無線通訊模組 > nRF24L01+ 無線通訊模組 | |
|
▲ nFR24L01 2.4G無線模組
▲ nFR24L01 2.4G無線模組
▲nFR24L01 2.4G無線模組接腳
表 Arduino SPI接腳對照圖
Arduino Board | MOSI | MISO | SCK | SS (slave) | SS (master) |
Uno or Duemilanove | 11 or ICSP-4 | 12 or ICSP-1 | 13 or ICSP-3 | 10 | - |
Mega1280 or Mega2560 | 51 or ICSP-4 | 50 or ICSP-1 | 52 or ICSP-3 | 53 | - |
Leonardo | ICSP-4 | ICSP-1 | ICSP-3 | - | - |
Due | ICSP-4 | ICSP-1 | ICSP-3 | - | 4, 10, 52 |
表 Arduino開發板外部插斷接腳圖
Board | int.0 | int.1 | int.2 | int.3 | int.4 | int.5 |
Uno, Ethernet | 2 | 3 | | | | |
Mega2560 | 2 | 3 | 21 | 20 | 19 | 18 |
Leonardo | 3 | 2 | 0 | 1 | 7 | |
首先,上述提到Arduino SPI接腳與Arduino開發板外部插斷接腳之後,我們完成下表之nFR24L01 2.4G無線模組發射端接腳表與下下表之nFR24L01 2.4G無線模組接收端接腳表。
表 nFR24L01 2.4G無線模組發射端接腳表
nFR24L01 2.4G無線模組發射模組 | ||
接腳 | 接腳說明 | Arduino開發板接腳 |
1 | VCC 3.3V | Arduino +3.3V |
2 | GND | Arduino GND板接腳 |
3 | MOSI | Arduino Digital Pin 11 |
4 | MISO | Arduino Digital Pin 12 |
5 | SCK | Arduino Digital Pin 13 |
6 | CE | Arduino Digital Pin 8 |
7 | CSN | Arduino Digital Pin 7 |
8 | IRQ(Using Interrupt 0) | Arduino Digital Pin 2 |
▲透視圖 ▲nFR24L01 2.4G無線模組 ▲Arduino UNO開發版 |
表 nFR24L01 2.4G無線模組接收端接腳表
nFR24L01 2.4G無線模組接收模組 | ||
接腳 | 接腳說明 | Arduino開發板接腳 |
1 | VCC 3.3V | Arduino +3.3V |
2 | GND | Arduino GND板接腳 |
3 | MOSI | Arduino Digital Pin 51 |
4 | MISO | Arduino Digital Pin 50 |
5 | SCK | Arduino Digital Pin 52 |
6 | CE | Arduino Digital Pin 8 |
7 | CSN | Arduino Digital Pin 53 |
8 | IRQ(Using Interrupt 1) | Arduino Digital Pin 3 |
▲透視圖 ▲nFR24L01 2.4G無線模組 ▲Arduino Mega 2560開發板 |
先前我們已將Arduino 開發板的驅動程式安裝好之後,我們打開Arduino 開發板的開發工具:Sketch IDE整合開發軟體,撰寫一段程式,如下表所示之nFR24L01 2.4G無線模組發射測試程式,我們就可以讓nFR24L01 2.4G無線模組發射送出資料,透過2.4Ghz的無線電波傳送資料。
nFR24L01 2.4G無線模組發射測試程式(ping_client) |
/** * A Mirf example to test the latency between two Ardunio. * * Pins: * Hardware SPI: * MISO -> 12 * MOSI -> 11 * SCK -> 13 * * Configurable: * CE -> 8 * CSN -> 10 * * Note: To see best case latency comment out all Serial.println * statements not displaying the result and load * 'ping_server_interupt' on the server. */ #include <SPI.h> #include <Mirf.h> #include <nRF24L01.h> #include <MirfHardwareSpiDriver.h> byte num = 0 ; byte num1 = 0 ; void setup() { Serial.begin(9600); /* * Setup pins / SPI. */ /* To change CE / CSN Pins: * * Mirf.csnPin = 9; * Mirf.cePin = 7; */ /* Mirf.cePin = 7; Mirf.csnPin = 8; */ Mirf.spi = &MirfHardwareSpi; Mirf.init(); Mirf.payload = 1; // this means length of send data /* * Configure reciving address. */ /* * To change channel: * * Mirf.channel = 10; * * NB: Make sure channel is legal in your area. */ Mirf.config(); Mirf.setTADDR((byte *)"bruce"); Mirf.setRADDR((byte *)"UNO"); Serial.println("Beginning ... "); } void loop() { unsigned long time = millis(); // Serial.println("get time data finish..."); // Mirf.setTADDR((byte *)"bruce"); // Serial.println("set target server finish..."); if (num >= 255) num = 0 ; transmit((char)num) ; Serial.print(num); Serial.print(" S " ); num ++ ; if (!Mirf.isSending() && Mirf.dataReady()) { Mirf.getData((byte *) &num1); Serial.print("/"); Serial.print(num1); Serial.print(" R "); Serial.print("\n"); } else { Serial.print("\n"); } /* Serial.print("sending data: "); Serial.print(time); Serial.print(" to server finish...\n") ; while (Mirf.isSending()) { // wait data sending....until finished } Serial.println("data sending..finished"); delay(10); while (!Mirf.dataReady()) { //Serial.println("Waiting"); if ( ( millis() - time ) > 1000 ) { Serial.println("Timeout on response from server!"); return; } } Mirf.getData((byte *) &time1); Serial.print("Ping: "); Serial.print(time1); //Serial.print("and cost time : "); // // Serial.print(time1 - time); Serial.print(" ; Get echo from server ... \n") ; Serial.print("==============\n\n\n") ; delay(1000); */ } void transmit(char st) { byte c; // Serial.println("Now enter char sending"); c = st; Mirf.send(&c); while( Mirf.isSending() ) ; } void transmit( float v) { byte c; char buf[10]; dtostrf(v,9,3,buf); for( int i=0 ; i<8 ; i++ ) { c = buf; Mirf.send(&c); while( Mirf.isSending() ) ; } } // sends a string via the nRF24L01 void transmit(const char *string) { byte c; for( int i=0 ; string!=0x00 ; i++ ) { c = string; Mirf.send(&c); while( Mirf.isSending() ) ; } } // send a CR/LF sequence via the nRF24L01 void transmitlf(void) { byte c; c = '\r'; Mirf.send(&c); while( Mirf.isSending() ) ; c = '\n'; Mirf.send(&c); while( Mirf.isSending() ) ; } void waitforsending() { unsigned long t1 = millis() ; unsigned long t2 = millis() ; while(Mirf.isSending()) { t2 = millis() ; Serial.println(t2-t1) ; if ((t2-t1) >=200 ) return ; } } void waitforreceiving() { unsigned long t1 = millis() ; unsigned long t2 = millis() ; while(Mirf.dataReady()) { t2 = millis() ; Serial.println(t2-t1) ; if ((t2-t1) >=200 ) return ; } } |
原始碼資料網站:https://github.com/brucetsao/arduino_RFProgramming
將Arduino 開發板的驅動程式安裝好之後,我們打開Arduino 開發板的開發工具:Sketch IDE整合開發軟體,撰寫一段程式,如下表所示之nFR24L01 2.4G無線模組接收測試程式,我們就可以讓nFR24L01 2.4G無線模組接收由發射端送出的資料,透過2.4Ghz的無線電波來接收資料。
nFR24L01 2.4G無線模組接收測試程式(ping_server) |
/** * An Mirf example which copies back the data it recives. * * Arduino UNO Pins: * Hardware SPI: * MISO -> 12 * MOSI -> 11 * SCK -> 13 * * Arduino MEGA 2560 use Pins: * Hardware SPI: * MISO -> 50 * MOSI -> 51 * SCK -> 52 * Configurable: * CSN -> 7 * CE -> 8 */ #include <Wire.h> #include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line display #include <SPI.h> #include <Mirf.h> #include <nRF24L01.h> #include <MirfHardwareSpiDriver.h> #define ledpin 7 byte recvdata ; void setup() { // pinMode(ledpin,OUTPUT) ; Serial.begin(9600); lcd.init(); // initialize the lcd // Print a message to the LCD. lcd.backlight(); lcd.print("RF Monitor!"); /* * Set the SPI Driver. */ Mirf.spi = &MirfHardwareSpi; /* * Setup pins / SPI. */ Mirf.init(); /* * Configure reciving address. */ Mirf.setRADDR((byte *)"bruce"); Mirf.setTADDR((byte *)"UNO"); /* * Set the payload length to sizeof(unsigned long) the * return type of millis(). * * NB: payload on client and server must be the same. */ Mirf.payload =1; /* * Write channel and payload config then power up reciver. */ Mirf.config(); Serial.println("Listening..."); pinMode(ledpin,OUTPUT) ; } void loop() { lcd.setCursor(0, 1) ; lcd.print(" "); lcd.setCursor(0, 1) ; lcd.print(millis()) ; // Serial.println(millis()) ; /* * A buffer to store the data. */ // byte data[Mirf.payload]; // Serial.println("now looping..."); /* * If a packet has been recived. * * isSending also restores listening mode when it * transitions from true to false. */ Serial.println("Now waiting Get Data"); if (!Mirf.isSending() && Mirf.dataReady()) { // if(Mirf.dataReady()){ Serial.println("Get packet before"); /* * Get load the packet into the buffer. */ Mirf.getData(&recvdata); Serial.println("GOt packet after"); /* * Set the send address. */ lcd.setCursor(0, 2) ; lcd.print(" "); lcd.setCursor(0, 2) ; lcd.print(millis()) ; lcd.setCursor(0, 3) ; lcd.print(" "); lcd.setCursor(2, 3) ; lcd.print(recvdata) ; /* for (int i = 0 ; i < Mirf.payload; i++) { lcd.setCursor(2+(i)*4, 2) ; lcd.print(data,HEX) ; // lcd.print((unsigned long)*data) ; // use for unsign long } */ // delay(10); } } void messagecoming() { digitalWrite(ledpin, HIGH) ; delay(3); digitalWrite(ledpin, LOW) ; delay(3); } |
原始碼資料網站:https://github.com/brucetsao/arduino_RFProgramming
網友也可以在作者YouTube頻道(https://www.youtube.com/user/UltimaBruce )中,在網址: https://www.youtube.com/watch?v=ubFQfczrsIE&feature=youtu.be,看到本次實驗- nFR24L01 2.4G無線模組接收測試程式結果畫面。
網友可以由下圖,看到本次實驗- nFR24L01 2.4G無線模組接收測試程式結果畫面。
上述內容就是本篇主要介紹nFR24L01 2.4G無線模組在一對一的模式,如何傳輸資料,後續我們會再介紹nFR24L01 2.4G無線模組在一對多的模式傳輸資料,敬請網友密切注意本專欄喔。
作者介紹
曹永忠 (Yung-Chung Tsao):目前為自由作家、台灣資訊傳播學會秘書長,專研於軟體工程、軟體開發與設計、物件導向程式設計,商品攝影及人像攝影。長期投入資訊系統設計與開發、企業應用系統開發、軟體工程、新產品開發管理、商品及人像攝影等領域,並持續發表作品及相關專業著作。 Email:prgbruce@gmail.com ,Line ID:dr.brucetsao Arduino部落格:http://taiwanarduino.blogspot.tw/ 範例原始碼網址:https://github.com/brucetsao/arduino_RFProgramming 臉書社群(Arduino.Taiwan):https://www.facebook.com/groups/Arduino.Taiwan/ Arduino活動官網:http://arduino.kktix.cc/ Youtube:https://www.youtube.com/channel/UCcYG2yY_u0m1aotcA4hrRgQ |
歡迎光臨 終身學習:智慧電子(物聯網)含金融證卷 (http://finance.freebbs.tw/) | Powered by Discuz! 5.0.0 |