Skip to content

Commit 4df862a

Browse files
authored
Merge pull request #2846 from adafruit/ir_transceiver
adding examples for the stemma ir transceiver
2 parents cd779f7 + 21f1186 commit 4df862a

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

STEMMA_IR_Transceiver_Examples/Arduino_STEMMA_IR_Transceiver/.uno.test.only

Whitespace-only changes.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
/*
6+
* Based on the SimpleReceiver.cpp and SimpleSender.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+
#define IR_SEND_PIN 6
21+
22+
void setup() {
23+
Serial.begin(115200);
24+
while (!Serial)
25+
;
26+
Serial.println("Adafruit STEMMA IR Transceiver Demo");
27+
IrReceiver.begin(IR_RECEIVE_PIN);
28+
IrSender.begin(IR_SEND_PIN); // Start with IR_SEND_PIN -which is defined in PinDefinitionsAndMore.h- as send pin and enable feedback LED at default feedback LED pin
29+
Serial.print("IRin on pin ");
30+
Serial.print(IR_RECEIVE_PIN);
31+
Serial.print(", IRout on pin ");
32+
Serial.println(IR_SEND_PIN);
33+
}
34+
35+
uint8_t sCommand = 0x34;
36+
uint8_t sRepeats = 0;
37+
38+
void loop() {
39+
/*
40+
* Check if received data is available and if yes, try to decode it.
41+
* Decoded result is in the IrReceiver.decodedIRData structure.
42+
*
43+
* E.g. command is in IrReceiver.decodedIRData.command
44+
* address is in command is in IrReceiver.decodedIRData.address
45+
* and up to 32 bit raw data in IrReceiver.decodedIRData.decodedRawData
46+
*/
47+
if (IrReceiver.decode()) {
48+
if (IrReceiver.decodedIRData.protocol == UNKNOWN) {
49+
IrReceiver.printIRResultRawFormatted(&Serial, true);
50+
IrReceiver.resume();
51+
} else {
52+
IrReceiver.resume();
53+
IrReceiver.printIRResultShort(&Serial);
54+
IrReceiver.printIRSendUsage(&Serial);
55+
delay(1000);
56+
Serial.println("Sending received command..");
57+
IrSender.sendNEC(IrReceiver.lastDecodedProtocol, IrReceiver.lastDecodedCommand, IrReceiver.repeatCount);
58+
delay(1000);
59+
Serial.print("Sent!");
60+
//Serial.println(IrReceiver.lastDecodedProtocol, IrReceiver.lastDecodedCommand, IrReceiver.repeatCount);
61+
}
62+
Serial.println();
63+
}
64+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
import time
5+
import array
6+
import pulseio
7+
import board
8+
import adafruit_irremote
9+
10+
# Create a 'PulseOut' to send infrared signals on the IR transmitter @ 38KHz
11+
pulseout = pulseio.PulseOut(board.D6, frequency=38000, duty_cycle=2**15)
12+
# Create an encoder that will take numbers and turn them into NEC IR pulses
13+
encoder = adafruit_irremote.GenericTransmit(header=[9000, 4500],
14+
one=[560, 1700],
15+
zero=[560, 560],
16+
trail=0)
17+
# IR receiver setup
18+
ir_receiver = pulseio.PulseIn(board.D5, maxlen=120, idle_state=True)
19+
decoder = adafruit_irremote.GenericDecode()
20+
21+
while True:
22+
pulses = decoder.read_pulses(ir_receiver)
23+
try:
24+
# Attempt to decode the received pulses
25+
received_code = decoder.decode_bits(pulses)
26+
if received_code:
27+
hex_code = ''.join(["%02X" % x for x in received_code])
28+
print(f"Received: {hex_code}")
29+
# Convert pulses to an array of type 'H' (unsigned short)
30+
pulse_array = array.array('H', pulses)
31+
# send code back using original pulses
32+
pulseout.send(pulse_array)
33+
print(f"Sent: {pulse_array}")
34+
except adafruit_irremote.IRNECRepeatException: # Signal was repeated, ignore
35+
pass
36+
except adafruit_irremote.IRDecodeException: # Failed to decode signal
37+
print("Error decoding")
38+
ir_receiver.clear() # Clear the receiver buffer
39+
time.sleep(1) # Delay to allow the receiver to settle
40+
print()

0 commit comments

Comments
 (0)