Basic IR send with Trinket

Adafruit's tiny microcontroller platform. Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Basic IR send with Trinket

Post by adafruit_support_rick »

Thanks!

It should work unmodified on 8MHz 328's. Nothing in there for 84's, though.

User avatar
Funman1
 
Posts: 376
Joined: Fri Nov 28, 2014 2:13 am

Re: Basic IR send with Trinket

Post by Funman1 »

HMM I just got gibberish on my 8mhz Trinket Pro when I was making a project back in December but it worked fine on my Uno @ 16mhz I may try again but didn't have any luck back in December.

User avatar
JayTee427
 
Posts: 16
Joined: Fri Mar 13, 2015 12:59 am

Re: Basic IR send with Trinket

Post by JayTee427 »

Guy's just a quick update.

It was all user error. :( A library install issue with the two different IDE's (two different IDE's, one latest for Uno and another 1.0.5 trinket).

Apologies for the false alarm! Once I found that and cleaned it up, it compiled without issue!

Now to put it back together and actually test it!

Huge thanks again Rick for your time and effort. Many thanks Funman1 for your feedback and replies as well!
-jt

User avatar
Funman1
 
Posts: 376
Joined: Fri Nov 28, 2014 2:13 am

Re: Basic IR send with Trinket

Post by Funman1 »

Awesome news!!
Rick if you have a chance can you figure out why the library decodes garbage when used with a 8mhz trinket pro using adafruits IR receiver?

User avatar
JayTee427
 
Posts: 16
Joined: Fri Mar 13, 2015 12:59 am

Re: Basic IR send with Trinket

Post by JayTee427 »

Funman1, did you get IR codes to be successfully sent/received with the trinket?

I didn't get a chance to finally get the transmitter rigged up and actually test mine out till a few days ago. Although it compiled fine, and I can see the TX LED blink on the trinket. The receiver (TV) doesn't respond.

I noticed a discrepancy in my sleep time count, so I changed the trinket to run at 16mhz rather than the default 8mhz thinking that it was causing the problem. Running at 16mhz fixed my sleep timing (not crucial), but the sent code still doesn't seem to work.

I haven't had a chance to build up a quick decoder on the ardruino to recieve the trinkets TX signal and see what I get, hopefully this weekend. I just wanted to check with you to see if you had any luck actually using it, outside of the compilation success we had discussed!

Many thanks!
-jt

User avatar
Funman1
 
Posts: 376
Joined: Fri Nov 28, 2014 2:13 am

Re: Basic IR send with Trinket

Post by Funman1 »

I did not test a trinket perse'
I used a bare attiny 85 to test sending at 8mhz and that worked....
.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Basic IR send with Trinket

Post by adafruit_support_rick »

I tested the code out on a trinket. I don't actually have an IR transmitter, so I looked at the output on a scope. Don't know why it doesn't work for you. Can you see the IR transmitter light up, using a digital camera?

User avatar
JayTee427
 
Posts: 16
Joined: Fri Mar 13, 2015 12:59 am

Re: Basic IR send with Trinket

Post by JayTee427 »

Rick, I did a quick test with a multi-meter and saw the voltage on the IR command send. I swapped the IR LED for a colored LED, and saw it blink. Then popped the IR LED back on, and using the camera saw the IR blink as well.

I found my adafruit IR receiver component this morning but haven't had a chance to get a decoding sketch/setup yet. Once I get it together, hopefully I can figure out what it thinks it's sending.

For what it's worth, the same sketch (but with standard IRremote lib) and components on the Ardruino work without issues.

Thanks again!
-j

User avatar
JayTee427
 
Posts: 16
Joined: Fri Mar 13, 2015 12:59 am

Re: Basic IR send with Trinket

Post by JayTee427 »

Got the IR decoder sketch and receiver all setup on the ardruino this morning. I point the remote at it, hit the power button (same code set to send from the trinket), and it's receiving it as expected:

Code: Select all

20DF10EF
Decoded NEC: 20DF10EF (32 bits)
Raw (68): -320 8850 -4450 550 -600 550 -550 550 -1650 550 -600 550 -550 550 -550 550 -600 550 -550 550 -1650 600 -1650 550 -550 600 -1650 550 -1650 600 -1650 550 -1650 600 -1650 550 -600 550 -550 550 -550 550 -1650 600 -550 550 -600 550 -550 550 -550 550 -1650 600 -1650 550 -1650 600 -550 550 -1650 600 -1650 550 -1650 600 -1650 550 
Unfortunately, no matter how close, far, or number of times I try with the trinket, it doesn't receive anything, not even as a malformed or unknown protocol.

I don't own a scope, so I don't have the ability to capture what it's actually sending.

If anyone has any more ideas or things to try, I'm all ears! Even simple non-library based sketch to send my power on signal. I'm more than happy to test and post results. Otherwise I think it's time to toss the trinket and buy another Uno...

User avatar
JayTee427
 
Posts: 16
Joined: Fri Mar 13, 2015 12:59 am

Re: Basic IR send with Trinket

Post by JayTee427 »

Rick,

After doing a bunch of reading and digging around into anything that could possibly be wrong. It seems like it might be timer related?

I found several guys troubleshooting different IR sending issues, not specific to the library or Attiny85's, and alot of it came down to timers/timing. Some folks with issues on the timers in general and needing to get the PWM set to send at 38kHz, other folks dealing with digitalWrite having 3.5us delays on executing causing issues!

I found one guy that had a snippet of code specifically for Attiny85 timers to generate 38Khz for IR: http://www.ernstc.dk/arduino/38khz_timer.htm

Not sure if any of that can help with the timer tweaks you made in IRremoteInt.h for IR_USE_TIMER1_8 ??

Code: Select all

//Attiny85 , running @ 8MHZ
// ATMEL ATTINY45 / ARDUINO
//
//                           +-\/-+
//  Ain0       (D  5)  PB5  1|    |8   VCC
//  Ain3       (D  3)  PB3  2|    |7   PB2  (D  2)  INT0  Ain1
//  Ain2       (D  4)  PB4  3|    |6   PB1  (D  1)        pwm1
//                     GND  4|    |5   PB0  (D  0)        pwm0
//                           +----+
 void setup(){
 DDRB |= (1<<PB0); //Set pin PB0 as output
 TCNT0 = 0;
 TCCR0A=0;
 TCCR0B=0;
 
 TCCR0A |=(1<<COM0A0); //Timer0 in toggle mode Table 11-2
 TCCR0A |=(1<<WGM01); //Start timer 1 in CTC mode Table 11.5
 TCCR0B |= (1 << CS00);// Prescaler table 11.6
 OCR0A=104; //CTC Compare value 
 }
 void loop(){
 }
I have no idea if any of this is useful. I looked at the library and can see where you added the "IR_USE_TIMER1_8" for the Trinket, but outside of that all the timer setup is unfortunately way over my head.

Again, not sure if any of this is the issue or applicable. It just seems that if the timing is slightly off with all the IR functions, it doesn't quite work. So possibly just a timer setup issue? I noticed that my delay(10000) wasn't giving me a actual 10sec delay, untill I ran the Trinket @ 16MHz. per the Adafruit F_CPU code. Regardless, I tried the IR send with both 8MHz and 16MHz and it didn't seem to make a difference.

Thanks again for all your help!
-j

User avatar
JayTee427
 
Posts: 16
Joined: Fri Mar 13, 2015 12:59 am

Re: Basic IR send with Trinket

Post by JayTee427 »

Funman1, while digging around I found someone having a similar issue with memory, so they disabled specific protocols in the library that they weren't using! I haven't tried it, but it seems like that does the trick for a few folks on a different ATtiny with small ammount of mem.

Code: Select all

Take a look in irrecv.h. I disabled most protocols to save memory:

#undef COMPILE_DECODE_NEC
#undef COMPILE_DECODE_SONY
#undef COMPILE_DECODE_RC6
#undef COMPILE_DECODE_JVC


Funman1 wrote:Well good news and bad..
Looks like the sending code WORKS AWESOME on my ATTiny 85's!!!!
It actually sends real code and not just garbage!!! (Was getting garbage on 8mhz 328 Chips Still have not tested that)
but still won't compile on the ATTiny 84's

Now the sad news, the Receive part of the code is so huge it does not really fit on the ATtiny :(
I got it to squeak on there but have no functionality with a few bytes left over :(

So really the only two things left I wanted were the 84's and; to work on 328's (and other chips at 8mhz) untested right now. Figured I would ask before trying :)

Rick you're the MAN!!!

User avatar
JayTee427
 
Posts: 16
Joined: Fri Mar 13, 2015 12:59 am

Re: Basic IR send with Trinket

Post by JayTee427 »

Whoo Hoo! I figured it out and got it to work!!! A bit of reading all day, looking at the library, and some playing around..

It was a timing issue ;)

Problem: The internal oscillator on the ATtiny runs at 8MHz. The library, checks for F_CPU, then defaults to 16MHz. No bueno for Trinket!

Start with Rick's modified IRremote library (Thanks again Rick!!).

Edit the IRremoteInt.h. Find "#define SYSCLOCK" and change it from 16000000 to 8000000 (the ATtiny85's 8Mhz)

Code: Select all

#ifdef F_CPU
#define SYSCLOCK F_CPU     // main Arduino clock
#else
#define SYSCLOCK 8000000  //  change to 8MHz instead of default 16MHz - JT
#endif
Works perfect for me now.

As a side note, even if you try to run Trinket at 16Mhz like I did earlier, it's doubled in software.

Code: Select all

#include <avr/power.h>
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
The timer/oscillator is still 8MHz. So regardless of trying the 16MHz tweak, it will still get it wrong and fail. I haven't tried going back and running the trinket @16MHz and forcing SYSCLOCK to 8MHz though, not sure what you'd end up with.

Hope this helps everyone else out. It now works for me.

I'm sending IR codes from the trinket using the Adafruit Trinket IDE and Ricks IRremote library, to an Ardruino Uno with an IR reciever running the 1.61 IDE / Ken's latest IRremote library from git-hub.

Huge thanks again to Rick for modifying the library timer. and Funman1 for all the feedback!
-jt

User avatar
hiduino
 
Posts: 862
Joined: Sat Sep 01, 2012 7:05 pm

Re: Basic IR send with Trinket

Post by hiduino »

Nope, it uses the F_CPU value, so changing the SYSCLOCK to 8000000 does nothing. For your simple startup sketch, it is most likely selecting Trinkt 8MHz from the boards is what works for your delay(10000) to work correclty.

The real issue is that the IRremote is changing the Timer 0 settings and cause the delay() to not work. So in a more complex program where delay() is used it will not work correctly.

On a separate note, the example from the http://www.ernstc.dk site is running the timer0 in a CTC mode, instead of PWM. Which kind gets to the same 38KHz pulse signal.

I measured the output of PB1 on an oscope and it was reading 32 us period, or 31.25 KHz PWM. So this is a bit off from 38KHz.

I setup a test in CTC mode instead of PWM and was able to get a 38KHz freq. I still need to do more testing, but here is what I tried.

Code: Select all

// defines for timer1 (8 bits)
#elif defined(IR_USE_TIMER1_8)
#define TIMER_RESET
#define TIMER_ENABLE_PWM     (TCCR1 |= _BV(COM1A0))
#define TIMER_DISABLE_PWM    (TCCR1 &= ~(_BV(COM1A0)))
#define TIMER_ENABLE_INTR    (TIMSK = _BV(OCIE1A))
#define TIMER_DISABLE_INTR   (TIMSK &= _BV(OCIE1A))
#define TIMER_INTR_NAME      TIMER1_COMPA_vect
#define TIMER_CONFIG_KHZ(val) ({ \
const uint8_t pwmval = SYSCLOCK / 2000 / (val) - 1; \
TCCR1 = _BV(CTC1) | _BV(COM1A0) | _BV(CS10); \
OCR1C = pwmval; \
})
#define TIMER_COUNT_TOP      (SYSCLOCK * USECPERTICK / 1000000)
#if (TIMER_COUNT_TOP < 256)
#define TIMER_CONFIG_NORMAL() ({ \
TCCR1 = _BV(CS10); \
OCR1A = TIMER_COUNT_TOP; \
TCNT1 = 0; \
})
#else
#define TIMER_CONFIG_NORMAL() ({ \
TCCR1 = _BV(CS11); \
OCR1A = TIMER_COUNT_TOP / 8; \
TCNT1 = 0; \
})
#endif
#if defined(CORE_OC1A_PIN)
#define TIMER_PWM_PIN        CORE_OC1A_PIN  /* Teensy */
#else
#define TIMER_PWM_PIN        1  /* Arduino Duemilanove, Diecimila, LilyPad, etc */
#endif

User avatar
JayTee427
 
Posts: 16
Joined: Fri Mar 13, 2015 12:59 am

Re: Basic IR send with Trinket

Post by JayTee427 »

Thanks Hiduino!

I'll definitely defer to your expertise here, I'm a n00b, and the timers are beyond me. (That's why I've been posting to the forums asking for help/insights) That was simply my interpretation of what I thought was occurring, and what I did.

For what it's worth, what I did finally got the IR command sent successfully, when it wasn't working when I first started.

If I revert SYSCLOCK back to 16000000 in the library, it's trying to send something, but it's not the NEC IR code I'm trying to send. I change that to 800000 as I mentioned. My code is sent/received successfully. No other changes.

More than happy to test anything out if it helps thought!
thx again!

User avatar
hiduino
 
Posts: 862
Joined: Sat Sep 01, 2012 7:05 pm

Re: Basic IR send with Trinket

Post by hiduino »

@adafruit_support_rick, It appears the IRremoteInt.h file is incorrectly setting the PWM mode for PB1(OC1A). I measured the output of PB1 on an oscope and it was reading 32 us period, or 31.25 KHz PWM.

This code is setting only one part of the PWM, OCR1A. It is incorrectly setting the OCR1B register, which belongs to PB4(OC1B).

Code: Select all

#define TIMER_CONFIG_KHZ(val) ({ \
  const uint8_t pwmval = SYSCLOCK / 2000 / (val); \
  TCCR1 = _BV(PWM1A) | _BV(CS10); \
  OCR1A = pwmval; \
  OCR1B = pwmval / 3; \
})
It should be setting OCR1C to pwmval*2 and the pwmval needs to be 1 less, to get a proper %50 duty 38KHz signal on PB1.

Code: Select all

#define TIMER_CONFIG_KHZ(val) ({ \
  const uint8_t pwmval = SYSCLOCK / 2000 / (val) - 1; \
  TCCR1 = _BV(PWM1A) | _BV(CS10); \
  OCR1A = pwmval; \
  OCR1C = pwmval*2; \
})
With this change it now correctly measures a 38KHz signal.

However, there still is an issue with delay() not working after calling an IR send routine.

Locked
Please be positive and constructive with your questions and comments.

Return to “Trinket ATTiny, Trinket M0”