Skip to content

Commit 15f0e83

Browse files
authored
Merge pull request #2784 from adafruit/ir_remote_receiver
example code for ir remote receiver
2 parents 9142c83 + 3d2da42 commit 15f0e83

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
/*
6+
* Based on the SimpleReceiver.cpp from the
7+
* Arduino-IRremote https://github.com/Arduino-IRremote/Arduino-IRremote.
8+
* by Armin Joachimsmeyer
9+
************************************************************************************
10+
* MIT License
11+
*
12+
* Copyright (c) 2020-2023 Armin Joachimsmeyer
13+
*
14+
*/
15+
16+
#include <Arduino.h>
17+
18+
#include <IRremote.hpp> // include the library
19+
#define IR_RECEIVE_PIN 5
20+
21+
void setup() {
22+
Serial.begin(115200);
23+
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
24+
}
25+
26+
void loop() {
27+
/*
28+
* Check if received data is available and if yes, try to decode it.
29+
* Decoded result is in the IrReceiver.decodedIRData structure.
30+
*
31+
* E.g. command is in IrReceiver.decodedIRData.command
32+
* address is in command is in IrReceiver.decodedIRData.address
33+
* and up to 32 bit raw data in IrReceiver.decodedIRData.decodedRawData
34+
*/
35+
if (IrReceiver.decode()) {
36+
if (IrReceiver.decodedIRData.protocol == UNKNOWN) {
37+
IrReceiver.printIRResultRawFormatted(&Serial, true);
38+
IrReceiver.resume();
39+
} else {
40+
IrReceiver.resume();
41+
IrReceiver.printIRResultShort(&Serial);
42+
IrReceiver.printIRSendUsage(&Serial);
43+
}
44+
Serial.println();
45+
}
46+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2024 ladyada for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import board
6+
import pulseio
7+
import adafruit_irremote
8+
9+
# IR receiver setup
10+
ir_receiver = pulseio.PulseIn(board.D5, maxlen=120, idle_state=True)
11+
decoder = adafruit_irremote.GenericDecode()
12+
13+
def decode_ir_signals(p):
14+
codes = decoder.decode_bits(p)
15+
return codes
16+
17+
while True:
18+
pulses = decoder.read_pulses(ir_receiver)
19+
try:
20+
# Attempt to decode the received pulses
21+
received_code = decode_ir_signals(pulses)
22+
if received_code:
23+
hex_code = ''.join(["%02X" % x for x in received_code])
24+
print(f"Received: {hex_code}")
25+
except adafruit_irremote.IRNECRepeatException: # Signal was repeated, ignore
26+
pass
27+
except adafruit_irremote.IRDecodeException: # Failed to decode signal
28+
print("Error decoding")
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2024 ladyada for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
#
5+
# Product demo for the IR Remote Receiver with an ESP32-S2 TFT
6+
7+
import board
8+
import terminalio
9+
import displayio
10+
import digitalio
11+
import pulseio
12+
import adafruit_irremote
13+
try:
14+
from fourwire import FourWire
15+
except ImportError:
16+
from displayio import FourWire
17+
from adafruit_display_text import label
18+
from adafruit_st7789 import ST7789
19+
20+
displayio.release_displays()
21+
22+
# TFT display setup
23+
spi = board.SPI()
24+
tft_cs = board.TFT_CS # Adjust to your CS pin
25+
tft_dc = board.TFT_DC # Adjust to your DC pin
26+
display_bus = FourWire(spi, command=tft_dc, chip_select=tft_cs)
27+
display = ST7789(
28+
display_bus, rotation=270, width=240, height=135, rowstart=40, colstart=53
29+
)
30+
31+
# Enable the display backlight
32+
backlight = board.TFT_BACKLIGHT # Adjust to your backlight pin
33+
backlight = digitalio.DigitalInOut(backlight)
34+
backlight.switch_to_output()
35+
backlight.value = True
36+
37+
# Display initial text
38+
text_area = label.Label(terminalio.FONT, text="Waiting\nfor IR...", color=0xFFFFFF)
39+
text_area.anchor_point = (0.5, 0.5)
40+
text_area.anchored_position = (240 / 2, 135 / 2) # Assuming display size is 240x135
41+
text_area.scale = 3 # Scale the text size up by 3 times
42+
43+
display.root_group = text_area
44+
45+
# IR receiver setup
46+
# Adjust to your IR receiver pin
47+
ir_receiver = pulseio.PulseIn(board.D5, maxlen=120, idle_state=True)
48+
decoder = adafruit_irremote.GenericDecode()
49+
50+
def decode_ir_signals(p):
51+
codes = decoder.decode_bits(p)
52+
return codes
53+
54+
while True:
55+
pulses = decoder.read_pulses(ir_receiver)
56+
try:
57+
# Attempt to decode the received pulses
58+
received_code = decode_ir_signals(pulses)
59+
# Update display with the received code
60+
if received_code:
61+
hex_code = ''.join(["%02X" % x for x in received_code])
62+
text_area.text = f"Received:\n{hex_code}"
63+
print(f"Received: {hex_code}")
64+
except adafruit_irremote.IRNECRepeatException: # Signal was repeated, ignore
65+
pass
66+
except adafruit_irremote.IRDecodeException: # Failed to decode signal
67+
text_area.text = "Error decoding"

0 commit comments

Comments
 (0)