Playing with Raspberry Pi (19) - Experiment of Infrared Remote Control

Playing with Raspberry Pi (19) - Experiment of Infrared Remote Control

Infrared remote control is a very common electronic appliance in life. Televisions, air conditioners, stereos and other electrical appliances can be controlled by remote control. In this blog, we try to conduct infrared control experiments through infrared generators and infrared signal receivers, and handle the control of LED lights through infrared remote buttons on the Raspberry Pi.

1. Preparation before the experiment

In this experiment, we will use an infrared remote control to make LED lights. The components that need to be prepared are: infrared remote control, infrared receiver and a two-color LED light. As shown in the figure below:

As long as the battery is installed in the infrared remote control, the infrared signal will be triggered when we click any button, and the infrared receiving module can receive the infrared signal. Can be extinguished.

First of all, we can connect the infrared receiving sensor to the Raspberry Pi so that we can test the infrared receiving function. The connection is as follows:

raspberry pie Infrared receiver
5V VCC
GND GND
GPIO27 (BCM code) DO

Next, we need to install the infrared driver library lirc and Python-lirc on the Raspberry Pi, and execute the following commands on the Raspberry Pi terminal:

sudo apt update

sudo apt install lirc

sudo apt-get install liblircclient-dev

sudo apt-get install python3-lirc 



After that, you need to configure the infrared interface IO, and execute on the Raspberry Pi terminal:

sudo nano /boot/config.txt


nano is a text editor under Linux system. After opening this configuration file, we need to find the following two lines of configuration items:

# Uncomment this to enable the lirc-rpi module

#dtoverlay=lirc-rpi,gpio_out_pin=17,gpio_in_pin=18,gpio_in_pull=up


Uncomment the second line and modify it as follows:

dtoverlay=lirc-rpi,gpio_in_pin=27,gpio_in_pull=up


It should be noted that here we use the GPIO27 pin (BCM code) of the Raspberry Pi as the receiving pin of the infrared signal, and it is set as a pull-up resistor by default. In this experiment, we don't need to send out infrared signal, so the output pin of infrared signal can not be configured. After the modification is completed, use Control + x to exit the editor. When exiting, you will be asked whether to save the modification. Press the Y key to save it.

Next, we also need to configure the hardware interface of lirc, and use the following command to open the configuration file on the Raspberry Pi terminal:

sudo nano /etc/lirc/lirc_options.conf


Find the following two lines in the configuration file:

driver = devinput

device = auto


amend as below:

driver = default

device = /dev/lirc0


After that, restart the Raspberry Pi, and we can test the infrared reception function.

2. Reception of infrared signals

After the Raspberry Pi restarts, enter the following command in the terminal to test the infrared signal:

mode2 -d /dev/lirc0


It should be noted that if you are prompted that lirc0 cannot be initialized, you need to stop the service first and then execute the above command. Use the following command to stop the service:

sudo service lircd stop


If you successfully enter the test mode, you can try to use the infrared remote control to press some buttons on the Raspberry Pi, and the output as shown in the figure below appears on the terminal, indicating that the infrared signal has been successfully received.

It is not enough for us to only be able to receive signals. We cannot recognize these signals amicably. To recognize these signals as meaningful keys, we also need to encode the signals. First, execute the following command in the terminal to view all available encodings:

irrecord -l


The terminal will output the following information:

There are many codes available. We can choose 3 codes to use in this experiment. For example, we choose KEY_0, KEY_1 and KEY_2 to bind to the 0, 1 and 2 buttons corresponding to our remote control.

Now let's burn the code and execute the following command in the terminal:

irrecord -d /dev/lirc0 ~/lircd.conf


After that, the terminal will output a lot of description text, as shown in the following figure:

If you are interested, you can read these introductory texts and press Enter.

If it is the first time to encode and burn, the terminal will prompt the file of "Enter name of remote (only ascii, no spaces)", we need to give a name to the burning file, it should be noted that the name can only have ascii characters and cannot There are spaces. After entering the name, press Enter, the terminal will output a prompt of "Press RETURN now to start recording.", we press Enter again to enter the burning stage.

The process of burning is a bit cumbersome. At the beginning, we need to analyze the key interval of the remote control. We can press the keys on the remote control at will, and the terminal will output a "." every time a key is pressed. Note that you must be patient. Don't stay loose. After pressing a full line, the terminal will prompt "Please keep on pressing btns like described above", continue the previous operation until the terminal displays "Please enter the name for the next btn(press <ENTER> to finish recording)", that is Indicates that the key interval time has been analyzed, and we can start formal recording. At this time, start to enter the key we need to record, such as KEY_0, and then the terminal prompts as follows:

At this time, we can click the "0" button on the remote control, and then record KEY_1 and KEY_2 in the same way. After all recordings are completed, press Enter again. Mainly pay attention, at this time, the terminal will prompt to save and verify, you should press and hold any button on the remote control as soon as possible until the terminal prompts that the recording is successful, as shown in the following figure:

After the recording is completed, a recording file of *.lircd.conf will be generated in the current folder and copied to the /etc/lirc/lircd.conf.d folder, as follows:

sudo cp 0_1_2.lircd.conf /etc/lirc/lircd.conf.d/0_1_2.lircd.conf


There is one more detail to pay attention to. There is a file named devinput.lircd.conf in the /etc/lirc/lircd.conf.d folder by default. By default, the configuration of this file will be read and renamed devinput.lircd .dist will do. After entering the /etc/lirc/lircd.conf.d folder, execute the following command:

sudo mv devinput.lircd.conf devinput.lircd.dist


You can also open the recorded *.lircd.conf file to see if there are two strings of hexadecimal numbers after each recorded key, as shown below:

Then you need to delete the last string of the same hexadecimal number, otherwise it cannot be used normally. The deleted file is as follows:

Now, if nothing else, we can process these three key signals normally, and enter the following command in the terminal to start the daemon process:

sudo service lircd restart 

lircd --nodaemon --device /dev/lirc0 --driver default


Open another terminal and enter the following command to test the remote control keys:

sudo irw


At this point, press the 0, 1 and 2 buttons on the remote control, and the terminal can respond normally, as shown in the following figure:

3. Using the lirc driver library in Python

Do you still remember the python3-lirc library we installed before, it's time to use it, first create a Python file, let's name it 19.infrared.py for now , put /etc/lirc in the Raspberry Pi terminal The /irexec.lircrc file is renamed to lircrc. It should be noted that it is the lircrc file without a suffix. Next, we need to perform some configuration according to the requirements, open this file, and configure the infrared keys to be processed by the Python program, as shown in the following figure:

As shown in the figure, the signals to be processed are configured by the begin and end groups, in which prog needs to be configured as the Python program to process this signal. Fill in our program file name here, and the button is used to set the signal to be processed. The key, config is used to configure the data passed to the Python program. More configuration methods can be obtained through the following links:

https://github.com/tompreston/python-lirc

After the configuration is complete, modify the 19.infrared.py file as follows, and a simple test can be performed:

# 导入lirc库
import lirc
# 进行初始化 这里设置blocking为False会避免阻塞,不接收的信号直接抛弃
sockid = lirc.init("19.infrared.py", blocking=False)
while True:
    #获取按键信息
    btn = lirc.nextcode()
    if len(btn):
        print(btn)

lirc.deinit()

Run the above code, press 0, 1 or 2 on the infrared remote control, you can see that the program will print out an array, which stores our key values.

Now, in principle, you can already do a lot of things with the IR remote control and the IR receiver, you can enter all the buttons you need to use, and then just do a calculator with an LCD screen, or through the remote control Control light switches, upload temperature and barometer data, and more. In this experiment, we use two-color LED as an example, and I hope you can be more creative.

The two-color LED has 3 pins. The experiment about the two-color LED lamp was introduced in a blog earlier in this series. If you feel unfamiliar, you can turn it forward. Here we connect directly:

Bi-color LED raspberry pie
GND GND
S GPIO17 (BCM code, corresponding to physical code 11)
- GPIO18 (BCM code, corresponding to physical code 12)

The sample code is as follows:

#coding:utd-8

# 导入lirc 和 GPIO库
import lirc
import RPi.GPIO as GPIO
# 初始化lirc
sockid = lirc.init("19.infrared.py", blocking=False)

#初始化GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)

# 灭灯
def clear():
    GPIO.output(11, GPIO.LOW)
    GPIO.output(12, GPIO.LOW)

# 设置红灯亮
def red():
    GPIO.output(11, GPIO.HIGH)
    GPIO.output(12, GPIO.LOW)

# 设置绿灯亮
def green():
    GPIO.output(11, GPIO.LOW)
    GPIO.output(12, GPIO.HIGH)

clear()

# 监听红外信号
while True:
    btn = lirc.nextcode()
    if len(btn):
        command = btn[0]
        if command == '0':
            clear()
        if command == '1':
            red()
        if command == '2':
            green()
lirc.deinit()

Run the code and try using the IR remote to control the color and switch of the lights!

4. Take a break

At this point in this series of blogs, we have introduced a lot of colorful sensors. Have you ever thought about combining these sensors to create something fun? Now that you have the ability to use the infrared remote control to remotely control the program, try to put your Let your idea come true!

Focus on technology, understand love, be willing to share, be a friend

QQ:316045346

{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/2340880/blog/5310343