2017年7月6日 星期四

Raspberry Pi 3 使用紅外線



硬體準備


一個 IR 發射器及一個接收器 (使用Raspberry pi 3 Education Kit)

Demo




LIRC 軟體安裝及設定


Step 1: We need to install LIRC utility
pi@raspberrypi ~ $ sudo apt-get update
pi@raspberrypi ~ $ sudo apt-get install lirc

Step 2: 修改 /boot/config.txt

# Uncomment this to enable the lirc-rpi Module
dtoverlay=lirc-rpi
dtparam=gpio_in_pull=up   #將GPIO Input 均先設為Pull Up 

# Override the defaults for the lirc-rpi module

dtparam=gpio_in_pin=17   #IR接收
dtparam=gpio_out_pin=25 #IR發射


Step 3: 修改 /boot/config.txt

# /etc/lirc/hardware.conf
#
# Arguments which will be used when launching lircd
LIRCD_ARGS="--uinput"

#Don't start lircmd even if there seems to be a good config file
#START_LIRCMD=false

#Don't start irexec, even if a good config file seems to exist.
#START_IREXEC=false

#Try to load appropriate kernel modules
LOAD_MODULES=true

# Run "lircd --driver=help" for a list of supported drivers.
DRIVER="UNCONFIGURED"
# usually /dev/lirc0 is the correct setting for systems using udev
DEVICE="/dev/lirc0"
MODULES="lirc_rpi"

# Default configuration files for your hardware if any
LIRCD_CONF=""
LIRCMD_CONF=""

step 4:

Reboot



快速測試

To perform a quick test to see if LIRC is working, we need to stop the LIRC daemon and start mode2. mode2 shows the the pulse/space length of infrared signals.

pi@raspberrypi ~ $ sudo /etc/init.d/lirc stop

注意: 使用艾鍗的輸入模組, IR接收模組GPIO Input 須設定內部提升電阻

pi@raspberrypi ~ $ mode2 -d /dev/lirc0

When buttons are pressed on your remote, mode2 will give a similar output to what is shown below.

進階IR 程式解析



參考資料:  

艾鍗Raspberry Pi I/O Shield v3.0子板規格



註: 亦可使用Device Tree僅特定GPIO腳,一開機輸入狀態即為pull up後的狀態


/*
* Overlay for enabling gpio's to pull at boot time
* this overlay uses pincctrl to initialize the pull-up register for the the listed gpios
* the compatible="gpio-leds" forces a module probe so the pinctrl does something
*
* To use this dts:
* copy this to a file named gpio_pull-overlay.dts
* modify the brcm,pins, brcm,function, and brcm,pull values
* apt-get install device-tree-compiler
* dtc -@ -I dts -O dtb -o gpio_pull-overlay.dtb gpio_pull-overlay.dts
* sudo cp gpio_pull-overlay.dtb /boot/overlays
* add this line to the end config.txt: dtoverlay=gpio_pull
* reboot
*/

/dts-v1/;
/plugin/;
/ {
  compatible = "brcm,bcm2835", "brcm,bcm2708";
  fragment@0 {
    target = <&gpio>;
    __overlay__ {
       gpio_pins: gpio_pins {
          brcm,pins = <30 31 32 33>; /* list of gpio(n) pins to pull */
          brcm,function = <0 1 0 1>; /* boot up direction:in=0 out=1 */
          brcm,pull = <2 0 1 0>; /* pull direction: none=0, 1 = down, 2 = up */
       };
    };
  };
  fragment@1 {
    target-path = "/soc";
    __overlay__ {
       gpiopull:gpiopull {
          compatible = "gpio-leds";
          pinctrl-names = "default";
          pinctrl-0 = <&gpio_pins>;
          status = "okay";
       };



沒有留言 :

張貼留言