How to Use the IR Library With an Attiny

58,645

245

35

Published

Introduction: How to Use the IR Library With an Attiny

This Instructable will tell you how to use the IR (infrared) library on an Attiny. I will be using an Attiny85 with an Arduino Uno. I won't cover how to upload sketches to the Attiny just how to use the IR library with it. the best tutorial on uploading sketches I've found is this YouTube tutorial :

(a quck note and warning, You will need to have software serial working for the attiny (follow this instructable : https://www.instructables.com/id/Attiny-serial-monitor-using-arduino-walkthrough/) or google it

Step 1: Getting the Files

Hey Guys this instructables is a bit old and I would recommend using the arduino 1.0.5 Ide and it should work just fine as the newer updated Ides have been changed so this no longer works. Just download the 1.0.5 ide as a zip file from the arduino site and extract it to a different directory to your current ide so they don't interfere with each other.

The main issues that you would come across when trying to use the IR library with an Attiny are errors like this:

C:\Program Files (x86)\Arduino\libraries\IRremote\IRremote.cpp:229: error: 'TCCR2B' was not declared in this scope

this is all to do with different timers on the Attiny than the Atmega chip but we don't need to worry about it unless you want to change the library code (THAT IS SOME HARD STUFF DON'T CHANGE THE CODE) but there are some libraries or the same library just ported to the Attiny so the codes we write is the same as it is for the arduino. you can download the zip file here :

http://gammon.com.au/Arduino/IRremote_Attiny.zip

Just extract the files and put the IRremote file in the libraries folder in the the arduino folder the path should look like

C:\Users\(your user)\Documents\Arduino\libraries

you will also need to download this for the next step:

http://forum.arduino.cc/index.php?PHPSESSID=f55lj4...

Step 2: Fixing the Final Error

Hopefully if you try to upload a sketch utalising the IR library you should now be left with this error

c:/program files (x86)/arduino/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../../avr/lib/avr25/crttn85.o:(.init9+0x2): relocation truncated to fit: R_AVR_13_PCREL against symbol `exit' defined in .fini9 section in c:/program files (x86)/arduino/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/avr25\libgcc.a(_exit.o)

But don't worry if you don't you may likely be using the wrong core so just skip to step 4 (Troubleshooting) and come back to this step.

if you have the error above you are going to need to extract the second download from the last step (tinyPCRELpatch.zip) this step is quite easy just copy the hardware folder to the hardware folder in where you installed the arduino ide (IT IS NOT WHERE WE PUT THE IR LIBRARY FOLDER) it will be some where in your program files (x86) (or the version for your operating system and pc). The path for mine is:

C:\Program Files (x86)\Arduino\hardware

once you copy it it will ask you if you want to replace Id.exe (make sure its only Id.exe and nothing else) select replace and let it copy (it doesn't affect the normal ide so once its there you don't need to worry.

if you are using a mac read reply #4 on this thread:

http://forum.arduino.cc/index.php?PHPSESSID=8t8a2l...

Step 3: A Bit of Code

This step is about code that I used to first get this working it was code that i used on the arduino except different pin numbers due to the less amount of pins on the attiny85. however for this code you will need to use an arduino and the serial monitor just so you can find out the code the buttons use. all the code and can be found here : https://www.instructables.com/id/The-Easiest-Way-to...

Step 4: Troubleshooting

If in step 2 you did not get the different error you probably are using the wrong. the core used for software serial should work but if it doesn't then you can download this one:

https://arduino-tiny.googlecode.com/files/arduino-...

follow the installation guide in the read me file if your computer says that it can't open it just browse for a program and open it in note pad it should open just fine anyway.

If you want to switch back to using the IR library for the Arduino and not the Attiny the you will need to replace the ported library with the regular library which you can download from here:

http://www.mediafire.com/download/jd5j7911amju36g/...

i hope this helps this is my first instructable if there are any problems feel free to message me as i know it van be hard to find a straight tutorial for this on the internet. All the infomation in this tutorial came from these sites:

http://forum.arduino.cc/index.php?topic=180611.0

http://forum.arduino.cc/index.php?PHPSESSID=8t8a2l...

https://www.instructables.com/id/Attiny-serial-moni...

https://www.instructables.com/id/The-Easiest-Way-to...

2 People Made This Project!

Recommendations

  • Creative Misuse Contest

    Creative Misuse Contest
  • Oil Contest

    Oil Contest
  • Water Contest

    Water Contest

35 Discussions

Hi!

I spent a lot of time with the library described in this tutorial... Finaly I managed to compile the code and uploade it to the attiny, but everytime I press a button of my remote, the code is 0. Maybe something is wrong with the library. I tried your library, but it gives an error when it is compiling:

core.a(wiring.c.o): In function `__vector_4':

C:\Users\User\Documents\Arduino\hardware\attiny\cores\tiny/wiring.c:78: multiple definition of `__vector_4'

tiny_IRremote\tiny_IRremote.cpp.o:C:\Users\Deana\Documents\Arduino\libraries\tiny_IRremote/tiny_IRremote.cpp:271: first defined here

I didnt find that __vector_4 anywhere... Can you pease help me?

I am using Arduino IDE 1.0.5 r2, the attiny cores described in this tutorial and the patch too.

Thanks!

I'm using Arduino 1.6.4 and set the board to "Adafruit Trinket 8MHz" (also uses the attiny85). I followed the instructions at https://learn.adafruit.com/adafruit-arduino-ide-setup/overview to add support for the attiny. You still need to "upload using programmer" etc

No "patch" either. This instructable is somewhat dated. Go to 1.6 you won't regret it.

Hello.. I try to follow your instructions but nothing happens :(
Can you PLEASE give some closer instructions how you manage to make it work.. I done some coding, mostly editing and combining some sketches and on arduino uno, all works fine. When I try to upload to attiny85, no errors, but nothing works.. PLEASE HELP :(

Hi,
What other libraries are you running?

tiny_IRremote uses Timer1, and it sounds like another of your libraries is also using that timer.

The error output is referring to line 78 of Arduino/Hardware/attiny/cores/tiny/wiring.c and line 271 of libraries/tiny_IRremote/tiny_IRremote.cpp

Tell me what code is on those lines and I will be able to help more.
If you haven't already got it, look up "Notepad++" and install it. It's great for editing code.

Thank you for the fast response.

I deleted everything. I instaled IDE 1.6.4 and the adafruit boards. The __vector4_ error doesn´t appear anymore. But there is another error. The Trinket uses a bootloader, wich takes 3Kb of the flash memory, leaving 5Kb for the scketch, so I wasn´t able to upload the scketch. I installed the attiny85 on the IDE following this tutorial: (It has allways worked for me with attiny85)

http://highlowtech.org/?p=1695

And I was able to upload the code, but... the same thing... 0. Allways 0... on any button...0... For the reading I am using Software Serial library at 2400 baud rate and works fine.

The IR pin is 4 (on tue attiny). Power comes from an Arduino UNO, IR sensor works ok with arduino and original library.

Any suggestions?

Thanks againg.

Hello IV0 i am getting all buttons as 0 too :/ have you been able to solve the problem? thank you

Hello maths_nerd. I didn't manage to solve the problem. I had to use an atmega328p with the regular IR arduino libaries to finish the proyect. If you have succes with this, please write me, I'm still interested in trying IR with an attiny.

Thank you!

See venumz's comment below

Thank you very much. i have downloaded your above file and did the rest.its worked.again thank you.

can please make video on uploading ir lib to attiny using ardiuno ide

can we apply it for attiny 45

Awesome, you made my day man. I've looked everywhere (literally) and this is the only working code that I have found.

In case anyone have issues, I have used the tiny core from:

https://storage.googleapis.com/google-code-archive...

You need to change the timer for millis in the attiny core:

edit the file tiny/avr/cores/tiny/core_build_options.h
look out for tiny85 section and leave the timer define like:
#define TIMER_TO_USE_FOR_MILLIS 0

Then burn the bootloader "ATtiny85 @ 16MHz (internal PLL;4.3V BOD)"

If you accept donations I would gladly pay you two paints of beer, so let me know!

Thanks man! I'm glad you found it useful :)

That millis() thing is pretty odd. I must've missed it because I converted all my delay() and millis() calls into delayMicroseconds() and micros() in order to make the sketch fit in the flash! xD

Curiously, if you examine the file Arduino\hardware\arduino\avr\cores\arduino\wiring.c there is a preprocessor statement:
`#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
ISR(TIM0_OVF_vect)
#else
ISR(TIMER0_OVF_vect)
#endif`

This is the version of wiring.c which ships with the IDE by default, and it's quite odd that it checks for ATtiny*4 but not ATtiny*5. However in Arduino.h there is a statement which checks for both! I will raise this as an issue on the repo.

As for beer, thanks for the offer (i <3 beer) but the fact someone found my code useful is a reward in and of itself :)
Have a drink for me!

Thanks seejaydee, I have finally a working prototype of an IR receiver for my
linux media center that I can program to work with most IR transmitters.

Your library and the library v-usb for arduino made the trick.

After reading the info that you posted before, I noticed as well differences for the attiny85 regarding the original configuration for the timer in the tiny core I linked before:

/*
For various reasons, Timer 1 is a better choice for the millis timer on the
'85 processor.
*/
#define TIMER_TO_USE_FOR_MILLIS 1

Only the attiny85 was configured to use interrupt1 for the millis timer, all the other micro-controllers where configured to use interrupt0.

Many people are having issues due to that very fact and I wonder why the developer of the core chose that configuration.

Thanks again for your cool code!

0
user
runr

1 year ago

can yoy please make video on this

thank u very much

Its a Nice wqork