NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Sensors, Actuators, and Robotics » SparkFun IR remote

September 07, 2015
by scootergarrett
scootergarrett's Avatar

I just finished up the code to work with this simple IR remote. The output of the sensor is connected to PB0. It uses pin change interrupt and determines the length of time the signal is held low, which represents the 1's and 0's. Currently all it dose is print to the uart the code of the last button pressed and if the button is held down.

Two collums, last button hit see button map 1/4 way down and if the a button is held (1,0) Here is the code:

// for NerdKits with ATmega328 //
// http://techdocs.altium.com/display/FPGA/NEC+Infrared+Transmission+Protocol
// https://learn.sparkfun.com/tutorials/ir-control-kit-hookup-guide?_ga=1.262663887.2144683174.1398553503

const uint16_t BUTTON_POWER = 0xD827;
const uint16_t BUTTON_A = 0xF807;
const uint16_t BUTTON_B = 0x7887;
const uint16_t BUTTON_C = 0x58A7;
const uint16_t BUTTON_UP = 0xA05F;
const uint16_t BUTTON_DOWN = 0x00FF;
const uint16_t BUTTON_LEFT = 0x10EF;
const uint16_t BUTTON_RIGHT = 0x807F;
const uint16_t BUTTON_CIRCLE = 0x20DF;

#define CutOffTime 60
volatile uint16_t IR_ComBits;
volatile uint16_t IR_LastButtonHit;
volatile int8_t IR_CurrentBit;
volatile uint8_t IR_SamplePosition;
volatile bool IR_HeldDown;

void IR_Remote_init()
{
    // Set up PB0 for pin change interrupts //
    DDRB &= ~(1<<PB0);
    PORTB |= (1<<PB0);
    PCICR |= (1<<PCIE0);
    PCMSK0 |= (1<<PCINT0);

    // Set up the clock (needs review but works for now) //
    TCCR1B |= (1<<CS12) | (1<<WGM12);

    sei();          // Enable interrupts

    return;
}

/// Interrupt code ///
ISR(PCINT0_vect)
{
    // Interrupt code ...
    if(!(PINB & (1<<PB0)))
    {
        ++IR_SamplePosition;
        if(TCNT1 > 200)
        {
            IR_SamplePosition = 0;
            IR_ComBits = 0;
            IR_CurrentBit = 15;
        }

        if(IR_SamplePosition > 16)
        {
            if(TCNT1 > CutOffTime)
                IR_ComBits |= (1<<IR_CurrentBit);
            --IR_CurrentBit;
            if(IR_CurrentBit == -1)
            {
                IR_LastButtonHit = IR_ComBits;
            }
        }

        if(TCNT1 > 300)
        {
            IR_HeldDown = true;
        }

    }

    TCNT1 = 0;                      // Re set the clock to 0  (needs review but works for now)
    return;
}

void uart_write16(uint16_t x)
{
    // wait for empty receive buffer //
    while ((UCSR0A & (1<<UDRE0))==0);
    // send LSBF //
    UDR0 = x;

    // wait for empty receive buffer //
    while ((UCSR0A & (1<<UDRE0))==0);
    // send MSBL //
    UDR0 = *((char*)&x + 1);

    return;
}

int main()
{
    // Start up the serial port //
    uart_init();
    FILE uart_stream = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);           // Not always needed
    stdin = stdout = &uart_stream;

    IR_Remote_init();

    while(1)
    {
        fprintf_P(&uart_stream, PSTR("%04X  %i"), IR_LastButtonHit, (int8_t)IR_HeldDown);
        IR_LastButtonHit = 0;
        IR_HeldDown = 0;
        uart_write16(0x0A0D);
        delay_ms(100);
    }

    while(1);
    return 0;
}
December 10, 2015
by lnino
lnino's Avatar

Hey scootergarrett, thanks a lot for sharing your project.

I have bought this IR-Kit too, some time ago. I played a Little with my arduino and this kit.

I was able to use the remote control to turn a LED on and off. Like it is showed in the tutorial. But I was never able to record a IR Signal from my TV remote, for turning the TV on or off with the Arduino. Did you had luck with that?

Maybe I shall give it another try. And try to use it on my Nerdkit.

Do you have some Pictures of your project you can share with us?

December 13, 2015
by scootergarrett
scootergarrett's Avatar

Within this project I never intended to do any thing other than use the Spark Fun remote with the NerdKit. Other users have done some interesting things with Nerdkits IR remotes that communicate with TV and such

December 14, 2015
by lnino
lnino's Avatar

Thanks for the link.

I will give it a try and maybe it works this time. :-)

Post a Reply

Please log in to post a reply.

Did you know that a motor's no-load current at a given voltage is much less than it's resistance would suggest? Learn more...