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 **
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.
In the NEC format, T = 562 [μs] is one time unit. The data '0' and '1' are represented in this way.
And the whole communication is roughly divided into four codes.
Now, let's actually use Raspberry Pi and check if this is the case.
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.
The infrared receiver module used this time is VS1838B. 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.
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()
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.
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.
'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.
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
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