Help writing a simple sketch, at power up send 2 RC5 IR sequences

Hi, I'm new to the forum and also new to the Arduino but have a strong background in electronics.

I've been researching the ATtiny chips and I have a project that I would like to attempt although I confess I'm a little clueless and not sure where to start. Hope someone can give me some pointers.

My requirements:

I have two devices and I want the second device to turn on and switch to the correct input using IR when the first device is turned on.

At power on:
A 12v trigger signal is available when device A is switched on. I want this 12v Signal to power up the ATtiny, and transmit over IR an RC5 IR code to power up device B, and then a moment later send a second RC5 IR code to set device B it to the correct input.

At power off:
When the 12v signal drops, I want an RC5 IR code sent to turn off device B.

Ideally I don't want to use an external power source for this, I want to use the 12v Trigger to also power the ATtiny chip. My idea was to use a buck converter to drop the 12v to 5.1v and use a 220uf capacitor on the 5.1v. This should have enough voltage stored to keep the ATtiny powered up long enough to issue the power off IR signal when the trigger signal drops to 0v.

I have the hex RC5 codes already, so hoping don't I don't need to build a receiver / decoder to capture the IR commands.

Hope that makes sense, appreciate any help.

Cheers

You should start with your background in electronics. What are two devices? How do the first and second differ in how they turn on? What does "switch to the correct input'" mean? How is correctness decided? What does IR do with any of the two devices? Who determines which device turns on first?

Hi

Device A is a streamer, it's trigger out goes high to 12v when streaming starts. Device B is an AV Receiver with no trigger in so I have to resort to IR, correct input would be the audio input into which the streamer is plugged in. Basically I want a seamless experience without having to manually turn on the AVR and selecting CD.

When the music stops, the 12v trigger drops to 0v and I want the AVR to switch off at this point.

Hope that clears it up.

1 Like

Not sure I understand this statement. If you are intending to send IR commands between two devices, they obviously some form of IR sender and receiver device is needed? If you are saying that you don't want to build a sender and receiver from scratch, then there are ready made modules available. If you are saying that you don't want to have to program an IR decoder, then there are also IR libraries such as the Arduino IRremote library available that should make it easier to program sending codes between devices.

At the receiving end you have to have some device powered up to receive the IR signal. That would suggest putting the second device into deep sleep. I think you will also need an IR receiver module to detect the IR carrier.

Hi, sorry for the confusion. I meant that in order to capture the commands that I need in my project from the remote, I don't need to build a decoder as I already have these commands in RC5 Hex.

Yes I will need the ATtiny to send IR commands so will have a IR transmitter on one of it's pins. Device 2 already has a receiver.

Hi, yes that's right, device 2 is always powered up but usually in standby when not in use. Yes I understand I will need a an IR transmitter connected to the ATtiny.

Ok, so if I understand correctly, your device 2 has an IR decoder built in, and you have the hex codes that it recognizes. Is that right?

Yes, it's an Av receiver that has an RC5 remote. I have the HEX codes for all the button sequences. eg. Power on = 0x10,0x7B.

This is the draft circuit I'm thinking of....

PB4 is checking the status of the trigger and the diode and capacitor network keeps the supply up for a couple for a second or so after the 5v drops.

Ok, well if it's the ATTiny85 for example, I believe the IRremote.h library works with that at least for transmitting. If that works, then the RC5 transmission would be pretty simple - a single command for each transmission. But I have no experience with the ATTiny parts. I know there can be a question as to the processor's clock speed, and how that is set. Maybe someone else here has already done this.

From the library's GitHub readme


I've not tried using that library with ATtinys.

Hi, thanks all for your contributions so far.

I think this should work as I have seen other examples of a ATtiny85 being used as an IR remote control. I can't find any example of projects that fit my use case but this one is quite simple and does something very similar.

Infrared Remote in a USB Cable | Arduino Project Hub
by @goranv

I'll write up the flowchart on how I want this to work and and tidy up the diagram.

Edit: updated diagrams:

I've managed to get co-pilot to create the code for me, is this likely to work ?

#include <IRremote.h>
#include <avr/sleep.h>
#include <avr/power.h>

// Define RC5 codes (address and command)
#define RC5_ADDRESS 0x10
#define RC5_POWER_ON 0x7B
#define RC5_SELECT_AV 0x5E
#define RC5_STANDBY 0x7C

// Define pin mappings
#define IR_PIN PB4
#define MONITOR_PIN PB3

IRsend IrSender;

void enterSleepMode() {
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);
    sleep_enable();
    sleep_mode();
    sleep_disable();
}

void setup() {
    // Initialize pins
    DDRB |= (1 << IR_PIN); // Set IR_PIN as output
    DDRB &= ~(1 << MONITOR_PIN); // Set MONITOR_PIN as input

    // Initialize IR sender
    IrSender.begin(IR_PIN);

    // Set IR frequency to 36 kHz
    IrSender.enableIROut(36);

    // Disable ADC to save power
    ADCSRA &= ~(1 << ADEN);

    // Reduce clock speed to 1 MHz
    CLKPR = (1 << CLKPCE); // Enable clock prescaler change
    CLKPR = (1 << CLKPS1) | (1 << CLKPS0); // Set clock prescaler to divide by 8 (1 MHz)
}

void sendRC5(uint8_t address, uint8_t command) {
    // Send RC5 code using IRremote library
    IrSender.sendRC5((address << 6) | command, 13); // 13 bits for RC5
}

void loop() {
    // Start first RC5 transmission (Power On AVR)
    sendRC5(RC5_ADDRESS, RC5_POWER_ON);
    _delay_ms(5000); // Wait 5 seconds

    // Start second RC5 transmission (Select AV input)
    sendRC5(RC5_ADDRESS, RC5_SELECT_AV);

    // Monitor the state of MONITOR_PIN
    while (PINB & (1 << MONITOR_PIN)) {
        // If MONITOR_PIN is high, continue monitoring
        _delay_ms(100);
    }

    // Start last RC5 transmission (Put AVR into Standby)
    sendRC5(RC5_ADDRESS, RC5_STANDBY);

    // Enter idle mode but continue to monitor high/low state of MONITOR_PIN
    while (1) {
        if (PINB & (1 << MONITOR_PIN)) {
            // If MONITOR_PIN is high, continue monitoring
            _delay_ms(100);
        } else {
            // If MONITOR_PIN is low, enter sleep mode
            enterSleepMode();
        }
    }
}

int main(void) {
    setup();
    while (1) {
        loop();
    }
}

What are you intending to use to build the code: Arduino IDE, Visual Code + PlatformIO, something else?
That code doesn't look like it was written with the intention of using the Arduino framework, where main() would be hidden from you. Your top level would be setup(), which is run once on entry to your code, and loop() which is called again every time it returns.

Hi Dave

I was hoping I could just use a USB Pluggable ATTINY Development Board For ATtiny13A/ATtiny25/ATtiny45/ATtiny85 and then program it straight from USB.

Then remove the Chip and solder up ?

Also added your comments into co-pilot and now have setup and loop in there, how does it look now:

#include <Infrared4Arduino.h>
#include <avr/sleep.h>
#include <avr/power.h>

// Define RC5 codes (address and command)
#define RC5_ADDRESS 0x10
#define RC5_POWER_ON 0x7B
#define RC5_SELECT_AV 0x5E
#define RC5_STANDBY 0x7C

// Define pin mappings
#define IR_PIN 4
#define MONITOR_PIN 3

IRsendPWM IrSender;

void enterSleepMode() {
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);
    sleep_enable();
    sleep_mode();
    sleep_disable();
}

void setup() {
    // Initialize pins
    pinMode(IR_PIN, OUTPUT); // Set IR_PIN as output
    pinMode(MONITOR_PIN, INPUT); // Set MONITOR_PIN as input

    // Initialize IR sender
    IrSender.begin(IR_PIN);

    // Set IR frequency to 36 kHz
    IrSender.enableIROut(36);

    // Disable ADC to save power
    ADCSRA &= ~(1 << ADEN);

    // Reduce clock speed to 1 MHz
    CLKPR = (1 << CLKPCE); // Enable clock prescaler change
    CLKPR = (1 << CLKPS1) | (1 << CLKPS0); // Set clock prescaler to divide by 8 (1 MHz)
}

void sendRC5(uint8_t address, uint8_t command) {
    // Send RC5 code using Infrared4Arduino library
    IrSender.sendRC5(address, command);
}

void loop() {
    // Start first RC5 transmission (Power On AVR)
    sendRC5(RC5_ADDRESS, RC5_POWER_ON);
    delay(5000); // Wait 5 seconds

    // Start second RC5 transmission (Select AV input)
    sendRC5(RC5_ADDRESS, RC5_SELECT_AV);

    // Monitor the state of MONITOR_PIN
    while (digitalRead(MONITOR_PIN) == HIGH) {
        // If MONITOR_PIN is high, continue monitoring
        delay(100);
    }

    // Start last RC5 transmission (Put AVR into Standby)
    sendRC5(RC5_ADDRESS, RC5_STANDBY);

    // Enter idle mode but continue to monitor high/low state of MONITOR_PIN
    while (1) {
        if (digitalRead(MONITOR_PIN) == HIGH) {
            // If MONITOR_PIN is high, continue monitoring
            delay(100);
        } else {
            // If MONITOR_PIN is low, enter sleep mode
            enterSleepMode();
        }
    }
}

I know AI generated code is frowned upon but this is all I know. Also I read another similar thread in which it turned out Infrared4Arduino.h was a better library for my use case so changed that too.

What you've got at the moment is source code. That has to be converted to the machine code that the ATtiny85 understands. So you need something to convert your source code to machine code. That conversion process is called 'building' the code. The ArduinoIDE is the best bet if you are new to this process.

Why did you change that to this:

Assuming you are going to get the Arduino IDE. Just see if your code compiles as a first sanity check. Then report back here if you have any further questions, or error messages from the compiler that you don't understand.

Yes will look into IDE, I think i get the concepts but still working through the detail and trying to get my head around it all.

I haven't programmed anything like this since the around 1997 when I used to program my own PIC16C54s, I'm sure things have moved on a fair bit.

The source code looks pretty good, quite interesting using co-pilot to come up with that. I submitted a the above flowchart and diagram and provided some prompts, pretty easy. Do you think it will work ?

I will be ordering a Digispark kickstarter development board ATTINY85 module to try this on.

Indeed. It's now very easy to get started. A matter of minutes rather than days. Although starting with the ATtiny85, rather than something more standard, like an Uno R3, may offer some challenges. I never managed to get an ATtiny85 development board to work with the Arduino IDE. Probably user error. I just put my ATtiny85 on a bread board to program it, using the "Arduino as ISP" example sketch. Since then I built my own ATtiny85 dev board which I program using an ICSP programmer.