Skip to content

Commit 92d0ace

Browse files
committed
Add IR Remote project to module 3
1 parent 79cff4d commit 92d0ace

10 files changed

Lines changed: 184 additions & 0 deletions

03_sensors/03_09_ir_remote.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Infrared Remote Control
2+
3+
An infrared(IR) remote control is a device with buttons and an IR emitter. Pressing down different buttons will make the infrared emitter send infrared pulses unique for each button. Infrared remote control technology is widely used in electronic products such as TV, air conditioning, etc. The receiver decodes those pulses into a binary code so the receiving device knows which button was pressed on the remote.
4+
5+
![IR Remote Component](../images/ir_remote_component.png)
6+
7+
> NOTE: pull out the plastic tab before you use the remote. If the plastic tab is not present the remote might have run down the battery while sitting.
8+
9+
>Save the tab and put it back after you are done with the remote.
10+
11+
12+
## New Concepts
13+
14+
- IR Remote
15+
- IR Receiver
16+
17+
### IR Remote
18+
19+
An IR Remote sends a unique value to the IR Receiver for each key pressed. This particular IR Remote sends the following values for each key.
20+
21+
![IR Remote key/value table](../images/ir_remote_key_value_table.png)
22+
23+
### IR Receiver
24+
25+
An infrared(IR) receiver is a component which can receive the infrared light and convert it into a data signal. That data signal returns the key or code value sent by the IR Remote when a button is pressed.
26+
27+
![IR Receiver Component](../images/ir_receiver_component.png)
28+
29+
## Component List
30+
31+
![Component List](../images/03_09_components.png)
32+
33+
## Circuit
34+
35+
### Wiring Diagram
36+
37+
> Disconnect all power before building the circuit. Reconnect once verified.
38+
39+
![Wiring Diagram](../images/03_09_wiring_diagram.png)
40+
41+
### Schematic Diagram
42+
43+
![Schematic Diagram](../images/03_09_schematic_diagram.png)
44+
45+
## Code
46+
47+
```python
48+
from irrecvdata import irGetCMD
49+
50+
def commandHandler(hexValue):
51+
print(hex(hexValue))
52+
53+
recvPin = irGetCMD(21, commandHandler)
54+
```
55+
56+
The `irGetCMD` class listens to the pin specified in the constructor and calls the `commandHandler` callback when it decodes a button press.
57+
58+
## Key Concepts
59+
60+
- **Key Codes**: Each button value is encoded as a hex value or code.
61+
62+
## Further Exploration
63+
64+
- Add code to turn a light on and off when the power button is pressed.

03_sensors/code/Infrared_Remote.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from irrecvdata import irGetCMD
2+
3+
def commandHandler(hexValue):
4+
print(hex(hexValue))
5+
6+
recvPin = irGetCMD(21, commandHandler)

03_sensors/code/irrecvdata.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from machine import Pin, Timer
2+
import time
3+
4+
5+
"""
6+
Class to read signals from the IR Remote
7+
8+
gpioNum - The number of the GPIO pin the IR Receiver is connected to
9+
commandHandler - The callback function that will be called after a command has been
10+
decoded from the IR Remote.
11+
The Callback function must take an integer in as a parameter, this integer is the
12+
hex code for the button pressed.
13+
"""
14+
class irGetCMD(object):
15+
def __init__(self, gpioNum, commandHandler):
16+
self.irRecv = Pin(gpioNum, Pin.IN, Pin.PULL_UP)
17+
self.irRecv.irq(trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, handler=self.__logHandler)
18+
self.logList = []
19+
self.start = 0
20+
self.cmdHandler = commandHandler
21+
self.timer = Timer(0)
22+
23+
def __logHandler(self, source):
24+
thisComeInTime = time.ticks_us()
25+
if self.start == 0:
26+
self.start = thisComeInTime
27+
self.timer.deinit()
28+
self.timer.init(mode=Timer.ONE_SHOT, period=120, callback=self.readCommand)
29+
return
30+
self.logList.append(time.ticks_diff(thisComeInTime, self.start))
31+
self.start = thisComeInTime
32+
33+
def readCommand(self, t):
34+
if len(self.logList) < 67:
35+
print("Error reading command")
36+
self.reset()
37+
else:
38+
bufferedBits = []
39+
for i in range(3, 66, 2):
40+
if self.logList[i] > 800:
41+
bufferedBits.append(1)
42+
else:
43+
bufferedBits.append(0)
44+
45+
irValue=0x00000000
46+
for b in bufferedBits:
47+
irValue = irValue<<1
48+
irValue += b
49+
50+
self.reset()
51+
self.cmdHandler(irValue)
52+
53+
def reset(self):
54+
self.logList = []
55+
self.index = 0
56+
self.start = 0
57+

images/03_09_components.png

74.9 KB
Loading

images/03_09_schematic_diagram.png

55.3 KB
Loading

images/03_09_wiring_diagram.png

171 KB
Loading

images/ir_receiver_component.png

18.4 KB
Loading

images/ir_remote_component.png

107 KB
Loading
80.8 KB
Loading

reference_code/irrecvdata.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from machine import Pin, Timer
2+
import time
3+
4+
5+
"""
6+
Class to read signals from the IR Remote
7+
8+
gpioNum - The number of the GPIO pin the IR Receiver is connected to
9+
commandHandler - The callback function that will be called after a command has been
10+
decoded from the IR Remote.
11+
The Callback function must take an integer in as a parameter, this integer is the
12+
hex code for the button pressed.
13+
"""
14+
class irGetCMD(object):
15+
def __init__(self, gpioNum, commandHandler):
16+
self.irRecv = Pin(gpioNum, Pin.IN, Pin.PULL_UP)
17+
self.irRecv.irq(trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, handler=self.__logHandler)
18+
self.logList = []
19+
self.start = 0
20+
self.cmdHandler = commandHandler
21+
self.timer = Timer(0)
22+
23+
def __logHandler(self, source):
24+
thisComeInTime = time.ticks_us()
25+
if self.start == 0:
26+
self.start = thisComeInTime
27+
self.timer.deinit()
28+
self.timer.init(mode=Timer.ONE_SHOT, period=150, callback=self.readCommand)
29+
return
30+
self.logList.append(time.ticks_diff(thisComeInTime, self.start))
31+
self.start = thisComeInTime
32+
33+
def readCommand(self, t):
34+
if len(self.logList) < 67:
35+
print("Error reading command")
36+
self.reset()
37+
else:
38+
bufferedBits = []
39+
for i in range(3, 66, 2):
40+
if self.logList[i] > 800:
41+
bufferedBits.append(1)
42+
else:
43+
bufferedBits.append(0)
44+
45+
irValue=0x00000000
46+
for b in bufferedBits:
47+
irValue = irValue<<1
48+
irValue += b
49+
50+
self.reset()
51+
self.cmdHandler(irValue)
52+
53+
def reset(self):
54+
self.logList = []
55+
self.index = 0
56+
self.start = 0
57+

0 commit comments

Comments
 (0)