[PYTHON] I learned how the infrared remote control works with Raspberry Pi

I had never been aware of the mechanism of remote controls for TVs and air conditioners that are familiar to me, but I learned how to use infrared remote controls for electronic work. I also tried to check how communication is actually done using Raspberry Pi.

** What I used **

Infrared remote control communication format

First of all, there are roughly three communication formats for infrared remote controllers.

Since the remote control used this time was in NEC format, we will focus on this.

NEC format

In the NEC format, T = 562 [μs] is one time unit. The data '0' and '1' are represented in this way.

format0&1.jpg

And the whole communication is roughly divided into four codes. format.jpg

Now, let's actually use Raspberry Pi and check if this is the case.

Infrared remote control analysis

We will analyze the infrared remote controller labeled Car mp3, which is often included in electronic work starter kits. <img width="150" alt="carMp3.jpg ", src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/551611/1c27be81-e637-922e-e676-f516271f3ba6.jpeg "> First, we will make a circuit.

1. 1. Creating a circuit

The infrared receiver module used this time is VS1838B. vs1838b.jpg The pins of VS1838B are OUT on the left, GND in the middle, and VCC on the right. Since Min of VCC is 2.7V and Max is 5.5V, connect it to 3.3V without inserting a resistor. This time, connect OUT to PIN 21.

circuit.jpg

2. Source code creation

This is the source code that outputs the time (μs) when the signal is ON / OFF.

irAnalysis.py


import RPi.GPIO as GPIO
import time

IR_PIN = 21

def timeMeasure():
    maxTime = 1 #sec
    prevState = GPIO.input(IR_PIN)
    startTime = time.perf_counter()
    while time.perf_counter() - startTime < maxTime:
        nowState = GPIO.input(IR_PIN)
        if nowState != prevState:
            endTime = time.perf_counter()
            elapsedTime = (endTime - startTime) * 1000000 #microsec
            return round(elapsedTime)
    return None

def analysis():
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(IR_PIN, GPIO.IN)
    GPIO.wait_for_edge(IR_PIN, GPIO.BOTH)
    try:
        while True:
            elapsedTime = timeMeasure()
            if elapsedTime is None:
                break
            else:
                print(elapsedTime)

    except KeyboardInterrupt:
        GPIO.cleanup(IR_PIN)
    GPIO.cleanup(IR_PIN)

if __name__ == '__main__':
    analysis()

3. 3. Output analysis

I ran the program I mentioned earlier and pressed number 0 on the infrared remote controller. Since the remote controller was in NEC format, check the output while comparing it with the format.

First of all, in NEC format, the reader code can be received first.

Reader code

Because T is 562 μs 16T 16*562=8992(μs)

8T 8*562=4496(μs) It becomes.

The first two lines of output look like this.

python


9338
4294

The values are close to each other.

Next is the custom code.

Custom code

'0': 1T ON, 3T OFF When ON is 562 μs and OFF is 562 μs

'1': 1T ON, 1T OFF When ON is 562 μs and OFF is 1686 μs

So this time the custom code is 0000 0000 1111 1111.

From the third line onward to the middle of the output

python


484 #'0'
461
543 #'0'
478 
546 #'0'
478
543 #'0'
480
545 #'0'
486
548 #'0'
471
538 #'0'
479
546 #'0'
478
544 #'1'
1604
546 #'1'
1596
544 #'1'
1600
544 #'1'
1600
536 #'1'
1602
544 #'1'
1601
485 #'1'
1578
496 #'1'
1596

Next is the data code.

Data code

The data code is 0110 1000, followed by the inverted 1001 01111.

python


520 #'0'
466
529 #'1'
1587
328 #'1'
1543
335 #'0'
426
449 #'1'
1589
527 #'0'
468
512 #'0'
442
463 #'0'
460
------Invert from here--------
521 #'1'
1573
506 #'0'
467
539 #'0'
484
532 #'1'
1586
460 #'0'
438
527 #'1'
1603
479 #'1'
1575
464 #'1'
1588

Repeat code

It seems that 1T, 71T, 16T, 4T are repeated first, and then 1T, 172T, 16T, 4T are repeated. This is a little different from the format, but it may be different for each product.

python


472
40106 
9203
2024
---------------
234
96804
9194
2128
----repetition----
466
96856
9161
2097
----repetition----
451
96858
9212
2168

It was found that the communication was performed according to the NEC format, although the numerical values were slightly different except for the repeat code.

Recommended Posts

I learned how the infrared remote control works with Raspberry Pi
I tried using the DS18B20 temperature sensor with Raspberry Pi
[Raspberry Pi] Stepping motor control with Raspberry Pi
Servo motor control with Raspberry Pi
Control the motor with a motor driver using python on Raspberry Pi 3!
I tried to automate the watering of the planter with Raspberry Pi
Infrared remote control reception with Intel Edison
Use python on Raspberry Pi 3 to light the LED with switch control!
I tried L-Chika with Raspberry Pi 4 (Python edition)
Observe the Geminids meteor shower with Raspberry Pi 4
I tweeted the illuminance of the room with Raspberry Pi, Arduino and optical sensor
Using the digital illuminance sensor TSL2561 with Raspberry Pi
GPGPU with Raspberry Pi
Display images taken with the Raspberry Pi camera module
Try to visualize the room with Raspberry Pi, part 1
How to use the Raspberry Pi relay module Python
When I tried to do socket communication with Raspberry Pi, the protocol was different
Control brushless motors with GPIOs on Raspberry Pi Zero
Take the value of SwitchBot thermo-hygrometer with Raspberry Pi
Log the value of SwitchBot thermo-hygrometer with Raspberry Pi
DigitalSignage with Raspberry Pi
I wanted to run the motor with Raspberry Pi, so I tried using Waveshare's Motor Driver Board
Control the display of RGB LED matirix electric bulletin board freely with Raspberry Pi 3B +
I tried to move ROS (Melodic) with the first Raspberry Pi (Stretch) at the beginning of 2021
It was great to edit the Python file in the Raspberry Pi with Atom's remote function
I tried to simulate how the infection spreads with Python
Play with the Raspberry Pi Zero WH camera module Part 1
I tried running Movidius NCS with python of Raspberry Pi3
I tried connecting Raspberry Pi and conect + with Web API
Production of temperature control system with Raspberry Pi and ESP32 (1)
I made a resource monitor for Raspberry Pi with a spreadsheet
I made a surveillance camera with my first Raspberry PI.
I sent the data of Raspberry Pi to GCP (free)
Mutter plants with Raspberry Pi
I talked to Raspberry Pi
I made a web server with Raspberry Pi to watch anime
I want to run the Python GUI when starting Raspberry Pi
Periodically log the value of Omron environment sensor with Raspberry Pi
I made an npm package to get the ID of the IC card with Raspberry Pi and PaSoRi
Use vl53l0x with Raspberry Pi (python)
Serial communication with Raspberry Pi + PySerial
OS setup with Raspberry Pi Imager
Try L Chika with raspberry pi
I investigated how the scope looks
I liked the tweet with python. ..
VPN server construction with Raspberry Pi
Try moving 3 servos with Raspberry Pi
Using a webcam with Raspberry Pi
A memo to simply use the illuminance sensor TSL2561 with Raspberry Pi 2
[I touched the Raspberry Pi (1)] I summarized the basic operations of Minecraft Pi Edition (2015.5.23 pre-release)
[Electronic work] I made a Suica touch sound detector with Raspberry Pi
Note: I want to do home automation with Home Assistant + Raspberry Pi + sensor # 1
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
I connected the thermo sensor to the Raspberry Pi and measured the temperature (Python)
Logging the value of Omron environment sensor with Raspberry Pi (USB type)
I tried to control the network bandwidth and delay with the tc command
Production of temperature control system with Raspberry Pi and ESP32 (2) Production of transmission device