###### tags: `Arduino` `WingSync`
# 電控傳感模組
零件列表
| 名稱 | 耗能 | 功能|
| -------- | -------- | -------- |
|RF24| 108mA(max)| 無線收發|
|mpu6050| 1.5mA |三軸傾角|
|ms5611| 1.4mA |壓力偵測/溫度 | 0x77
|mpl3115A2| <1mA|壓力偵測/溫度|
|dht22| 5mA |溫溼度
|sd card| 100mA |資料儲存
|neo6mv2| 67mA |GPS
|Arduino nano |19mA|主控|
|LD1117S33|穩壓晶片|
|LD1117S50|穩壓晶片|
## RF24
[lib](https://github.com/nRF24/RF24)
使用2.4GHz進行通訊,SPI協定,共有4個強度可選,正電源只能接3.3V,單筆資料傳輸上限是32位元組(下面的程式是從main複製出來的,不完整)
```cpp=
//發射端
#include <SPI.h>
#include "RF24.h"
RF24 rf24(8,9); // CE腳, CSN腳
const byte addr[] = "00001"; //傳輸位址
float msg[8] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; //共32byte 上限32byte
void setup(){
if(!rf24.begin()){
Serial.println("Not responding");
delay(100);
}
rf24.setChannel(83); // 設定頻道編號
rf24.openWritingPipe(addr); // 設定通道位址
rf24.setPALevel(RF24_PA_MIN); // 設定廣播功率
rf24.setDataRate(RF24_2MBPS); // 設定傳輸速率
rf24.stopListening(); // 停止偵聽;設定成發射模式
}
void loop(){
rf24.write(&msg, sizeof(msg)); // 傳送資料
}
```
```cpp=
//接收端
#include <SPI.h>
#include "RF24.h"
RF24 rf24(8, 7); // CE腳, CSN腳
const byte addr[] = "00001";
const byte pipe = 1; // 指定通道編號
float msg[8] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,}; //共32byte 上限32byte
void setup() {
Serial.begin(115200);
while(!rf24.begin()){
Serial.println("fail");
}
rf24.setChannel(83); // 設定頻道編號
rf24.setPALevel(RF24_PA_MIN);
rf24.setDataRate(RF24_2MBPS);
rf24.openReadingPipe(pipe, addr); // 開啟通道和位址
rf24.startListening(); // 開始監聽無線廣播
Serial.println("nRF24L01 ready!");
}
void loop() {
if (rf24.available(&pipe)) {
rf24.read(&msg, sizeof(msg));
for (auto &i : msg) {
Serial.print(i);
Serial.print(' ');
}Serial.print(pToH(msg[3], Sp));
Serial.print('\n');
}
delay(10);
}
```
||接腳|
|-|-|
|Vcc|3.3V
|GND|GND
|CE|8
|CSN|9
|MISO|12
|MOSI|11
|SCK|13
|IRQ|空接

## mpu6050
[lib](https://github.com/Harrysome/MPU6050)
三軸加速度感測器,手刻函式庫,輸出為XYZ傾角,使用I2C協定,addr為0x68
```cpp=
#include <MPU6050.h>
#incldue <Streaming.h>
MPU6050 mpu;
const int add = 0x68; //the addr of MPU6050
const int sen = 0; //the sensitivity from 0 ~ 6
void setup(){
mpu.begin(add, sen);
}
void loop(){
mpu.read(); //read all the value
Serial << "( x, y, z) = ( " << mpu.Xaxis() << ", " << mpu.Yaxis() << ", " << mpu.Zaxis() << ")" << endl;
}
```
||腳位|
|-|-|
|Vcc|5V|
|GND|GND|
|SCL|A5|
|SDA|A4|
## ms5611
溫度壓力感測器,使用I2C協定,電壓需使用3.3V,addr為0x77
```cpp=
#include "MS5611.h"
MS5611 MS5611(0x77);
void setup(){
MS5611.begin();
}
/*
There are 5 oversampling settings, each corresponding to a different amount of milliseconds
The higher the oversampling, the more accurate the reading will be, however the longer it will take.
OSR_ULTRA_HIGH -> 8.22 millis
OSR_HIGH -> 4.11 millis
OSR_STANDARD -> 2.1 millis
OSR_LOW -> 1.1 millis
OSR_ULTRA_LOW -> 0.5 millis Default = backwards compatible
*/
void loop(){
MS5611.setOversampling(OSR_ULTRA_HIGH);
MS5611.read();
p1 = MS5611.getPressure();
t1 = MS5611.getTemperature();
Serial << "T1 : " << t1 << " P1 : " << p1 << endl;
}
```
||腳位|
|-|-|
|Vcc|3.3V|
|GND|GND|
|SCL|A5|
|SDA|A4|
## dht22
溫溼度感測器,使用數位信號傳輸
```cpp=
#include <DHT22.h>
const int data = 2;
DHT22 dht22(data);
void setup(){
Serial.begin(115200);
}
void loop(){
Serial << "T3: " << dht22.getTemperature() << " °C, humidity: " << dht22.getHumidity() << " %." << endl;
}
```
||腳位|
|-|-|
|Vcc|5V|
|GND|GND|
|data|2|
## mpl3115
溫度壓力感測器,使用I2C協定
```cpp=
#include <Adafruit_MPL3115A2.h>
Adafruit_MPL3115A2 mpl;
void setup(){
mpl.begin();
mpl.setMode(MPL3115A2_BAROMETER); //unit
}
void loop(){
mpl.startOneShot();
t2 = mpl.getLastConversionResults(MPL3115A2_TEMPERATURE); msg[2] = t2;
p2 = mpl.getLastConversionResults(MPL3115A2_PRESSURE); msg[4] = p2;
Serial << "T2 : " << t2 << " P2 : " << p2 << endl;
}
```
||腳位|
|-|-|
|Vcc|3.3V|
|GND|GND|
|SCL|A5|
|SDA|A4|
## sd card (暫不使用)
使用SPI協定,但使用的模組和RF24衝突
## neo6mv2 GPS (暫不使用)
使用UART協定,但測試結果出現錯誤
# Ref
my git
https://github.com/Harrysome/WingSync-SensorRemote
streaming.h
https://www.arduinolibraries.info/libraries/streaming