目的:
環境:
Win10、Arduino IDE 2.3.7、ESP32 NODEMCU-32S、1838 紅外線接收模組、NWT 威技電風扇(WPF-14P7)遙控器- 單純的紅外線接收,是一種光電二極體(photodiode、PD),可將接收到光,轉換成電。
雖然長得很像 LED,但 LED 是發光二極體(light-emitting diode),作用是將電轉換成光。
兩者引腳雖然都用長短區分正負,但內部的PN接面剛好相反,所以從內部PN接面來看,
發光二極體工作時,是施加順向偏壓。
光電二極體工作時,是施加逆向偏壓。
通常用來偵測物體。 - 一般紅外線遙控器,使用的載波頻率(Carrier Frequency、可想成背載電磁波進行傳送)是 38KHz,所以下面這種三支引腳的封裝,是將原本兩支腳的光電二極體,加上濾波取得載波頻率為38KHz 的訊號,對訊號解調(demodulation)後,再轉成以 HIGH 或 LOW 訊號輸出。
三支引腳分別是接正極(+)、接地(-)、輸出訊號(S)。 - 最後一種,是做成插上就可使用的電路板模組,我使用的是這種,我用的模組上面除了有三支引腳的紅外線接收器,還接好了電阻,以及一個 LED,紅外線接收器收照訊號時,LED 會閃爍。
引腳線路接法:
- 3.3V (ESP32) ---------- 正極(+) (1838 IR module)
- GND (ESP32) ---------- 負極(-) (1838 IR module)
- GPIO33 (ESP32) ---------- 訊號(S) (1838 IR module)
- 這邊使用 IRremote 這個 library 處理紅外線訊號
「Tools」->「Manage Libraries...」 - 找到 IRremote 進行安裝
安裝好的 library 會在 sketchbook location 裡的 libraries 資料夾,如果在設定裡改了 sketchbook location 路徑,Arduino IDE 會認為沒安裝,若要在「Manage Libraries...」裡移除已安裝的 library,也是要在原本安裝的路徑下才能辨別。
sketchbook location 預設路徑
C:\Users\%UserName%\Documents\Arduino\
程式碼:
#include <IRremote.h>
// 連接紅外線接收模組訊號引腳(S引腳)的 GPIO
const int IR_RECEIVE_PIN = 33;
void setup() {
//LED_BUILTIN 是開發板內建的 LED,GPIO 2
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK); // Start the receiver
delay(2000);
Serial.println("IR Receiver Ready. Point your remote at the sensor and press buttons");
}
void loop() {
// 檢查是否收到紅外線訊號
if (IrReceiver.decode()) {
Serial.println(IrReceiver.decodedIRData.decodedRawData, HEX); // Print "old" raw data
IrReceiver.printIRResultShort(&Serial); // Print complete received data in one line
IrReceiver.printIRSendUsage(&Serial); // Print the statement required to send this data
IrReceiver.resume(); // Enable receiving of the next value
}
}遙控器須對準紅外線接收器,如果沒對準,可能每次都出現不一樣的結果。
若不確定遙控器有無送出訊號,可透過相機(手機)鏡頭,看遙控器發送端,透過鏡頭可看到紅外線。
電源開關
14:32:44.974 -> EF1FEFE 14:32:44.974 -> Protocol=NEC Address=0xFEFE Command=0xF1 Raw-Data=0xEF1FEFE 32 bits LSB first Gap=3276750us Duration=75150us 14:32:45.041 -> Send with: IrSender.sendNEC(0xFEFE, 0xF1, <numberOfRepeats>);
減風量
14:23:42.992 -> FB04FEFE 14:23:42.992 -> Protocol=NEC Address=0xFEFE Command=0x4 Raw-Data=0xFB04FEFE 32 bits LSB first Gap=3276750us Duration=75100us 14:23:43.100 -> Send with: IrSender.sendNEC(0xFEFE, 0x4, <numberOfRepeats>);
加風量
14:25:02.445 -> F906FEFE 14:25:02.445 -> Protocol=NEC Address=0xFEFE Command=0x6 Raw-Data=0xF906FEFE 32 bits LSB first Gap=3276750us Duration=75150us 14:25:02.576 -> Send with: IrSender.sendNEC(0xFEFE, 0x6, <numberOfRepeats>);
14:25:50.912 -> CF3FEFE 14:25:50.912 -> Protocol=NEC Address=0xFEFE Command=0xF3 Raw-Data=0xCF3FEFE 32 bits LSB first Gap=3276750us Duration=75150us 14:25:50.997 -> Send with: IrSender.sendNEC(0xFEFE, 0xF3, <numberOfRepeats>);
轉頭開關
14:27:12.477 -> DF2FEFE 14:27:12.477 -> Protocol=NEC Address=0xFEFE Command=0xF2 Raw-Data=0xDF2FEFE 32 bits LSB first Gap=3276750us Duration=75150us 14:27:12.584 -> Send with: IrSender.sendNEC(0xFEFE, 0xF2, <numberOfRepeats>);
關機定時
14:28:15.624 -> AF5FEFE 14:28:15.624 -> Protocol=NEC Address=0xFEFE Command=0xF5 Raw-Data=0xAF5FEFE 32 bits LSB first Gap=3276750us Duration=75100us 14:28:15.701 -> Send with: IrSender.sendNEC(0xFEFE, 0xF5, <numberOfRepeats>);
開機定時
14:29:05.835 -> 9F6FEFE 14:29:05.835 -> Protocol=NEC Address=0xFEFE Command=0xF6 Raw-Data=0x9F6FEFE 32 bits LSB first Gap=3276750us Duration=75150us 14:29:05.913 -> Send with: IrSender.sendNEC(0xFEFE, 0xF6, <numberOfRepeats>);
- https://zh.wikipedia.org/zh-tw/%E5%85%89%E7%94%B5%E4%BA%8C%E6%9E%81%E7%AE%A1
光電二極體 - 維基百科,自由的百科全書 - https://zh.wikipedia.org/zh-tw/%E7%99%BC%E5%85%89%E4%BA%8C%E6%A5%B5%E7%AE%A1
發光二極體 - 維基百科,自由的百科全書 - https://www.youtube.com/watch?v=wygdW-frlWM
267 光电二极管,光信号接收的关键元件,好好了解 - YouTube - https://www.phy.fju.edu.tw/wp-content/uploads/2024/03/%E5%85%89%E9%9B%BB%E5%AF%A6%E9%A9%971.pdf
光電實驗(一) 發光二極體(LED)與光偵測二極體(PD) - https://www.digikey.tw/zh/articles/the-basics-of-photodiodes-and-phototransistors-and-how-to-apply-them
光電二極體和光電電晶體的基礎知識 | DigiKey - https://www.86duino.com/wp-includes/file/Chapter13-TC.pdf
EduCake 紅外線收發功能實作 - https://www.everlight.com/wp-content/uploads/2021/04/%E7%B4%85%E5%A4%96%E7%99%BC%E5%85%89%E4%BA%8C%E6%A5%B5%E9%AB%94IR-LED%E5%9C%A8%E9%81%99%E6%8E%A7%E5%99%A8%E4%B8%8A%E7%9A%84%E6%87%89%E7%94%A8%E7%B9%81%E4%B8%AD_Ver1.0.pdf
紅外發光二極體(IR LED)在遙控器上的應用 - https://www.reddit.com/r/arduino/comments/17bfddu/which_one_is_the_ir_sensor_i_just_bought_an/
Which one is the IR sensor. I just bought an Arduino set but got confused, which one is sensor and which is receiver??? (First time buyer btw) : r/arduino - https://blog.csdn.net/qq_17351161/article/details/107437382
hx1838 红外遥控(1):接收时序的解码-CSDN博客 - https://www.amebaiot.com.cn/cn/ameba-arduino-infrared/
IR – Infra-red receiver and emitter – Realtek IoT/Wi-Fi MCU Solutions - https://github.com/Arduino-IRremote/Arduino-IRremote
GitHub - Arduino-IRremote/Arduino-IRremote: Infrared remote library for Arduino: send and receive infrared signals with multiple protocols
























