I want arduino code for TSOP ir read data with using Attiny1606 controller

need help on arduino code for attiny 1606 microcontroller with ir receive code

I moved your topic to an appropriate forum category @akshay-369.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Have you tried to find any examples online? Downloading any libraries?

If you want someone to write the code for you, there is a 99.99% chance no one will do that for you unfortunately. What are you planning to do with the code? Is it simply just to receive IR signals?

I tried many of codes and examples but not getting results

I want basic code to read ir signal with Attiny 1606 microcontroller. If someone help me with this

const int irPin = 8;

void setup() {

Serial.begin(115200);
pinMode(irPin, INPUT);
}

void loop() {
int key = getIrKey();

if(key != 0)
Serial.println(key);
}

int getIrKey(){
int len = pulseIn(irPin,LOW);
int key, temp;
key = 0;
//Serial.print("len=");
//Serial.println(len);
if(len > 5000) {
for(int i=1;i<=32;i++){
temp = pulseIn(irPin,HIGH);
if(temp > 1000)
key = key + (1<<(i-17));
}
}
if(key < 0 )
key = -key;

delay(250);
return key;
}

I try this code... This code I got from one YouTube video link.

They use this code and got different ir data from multiple remotes but when I try it not works

Another tip, when pasting or writing code in the forum, use the tab above labelled:

< CODE / >

makes it much easier to read!

Code is correct?

The code may be correct but, use the code tags in your post. Here is an example:

const int irPin = 8;

void setup() {

Serial.begin(115200);
pinMode(irPin, INPUT);
}

void loop() {
int key = getIrKey();

if(key != 0)
Serial.println(key);
}

int getIrKey(){
int len = pulseIn(irPin,LOW);
int key, temp;
key = 0;
//Serial.print("len=");
//Serial.println(len);
if(len > 5000) {
for(int i=1;i<=32;i++){
temp = pulseIn(irPin,HIGH);
if(temp > 1000)
key = key + (1<<(i-17));
}
}
if(key < 0 )
key = -key;

delay(250);
return key;
}

this isn't nice to read.

If you use the < CODE / > tag in the tab above (where you see the settings, emojis, upload etc) it looks like this:

const int irPin = 8;

void setup() {

Serial.begin(115200);
pinMode(irPin, INPUT);
}

void loop() {
int key = getIrKey();

if(key != 0)
Serial.println(key);
}

int getIrKey(){
int len = pulseIn(irPin,LOW);
int key, temp;
key = 0;
//Serial.print("len=");
//Serial.println(len);
if(len > 5000) {
for(int i=1;i<=32;i++){
temp = pulseIn(irPin,HIGH);
if(temp > 1000)
key = key + (1<<(i-17));
}
}
if(key < 0 )
key = -key;

delay(250);
return key;
}

which is easier to read. formatting also help the code for readability, which you can do easily by going to edit -> auto format in the arduino IDE, so it takes the code from that to this (with a tiny bit of formatting from myself too):

const int irPin = 8;


void setup() {

  Serial.begin(115200);
  pinMode(irPin, INPUT);
}


void loop() {

  int key = getIrKey();

  if (key != 0) { 
    Serial.println(key);
  }
}

int getIrKey() {

  int len = pulseIn(irPin, LOW);
  int key, temp;
  key = 0;
  //Serial.print("len=");
  //Serial.println(len);
  if (len > 5000) {
    for (int i = 1; i <= 32; i++) {
      temp = pulseIn(irPin, HIGH);
      if (temp > 1000) {
        key = key + (1 << (i - 17));
      }
    }
  }
  if (key < 0) { 
    key = -key;
  }

  delay(250);
  return key;
}

I think you should copy the last code I have pasted there and try that instead.

Whenever you try and upload the code, what output do you get? What happens when you try and compile your code?

This code should also work on a 1606. It was tested on a 1604 tinyAVR. The MegaTinycore (GitHub - SpenceKonde/megaTinyCore: Arduino core for the tinyAVR 0/1/2-series - Ones's digit 2,4,5,7 (pincount, 8,14,20,24), tens digit 0, 1, or 2 (featureset), preceded by flash in kb. Library maintainers: porting help available!) from Spence Konde is required.

#include <Arduino.h>
#include <Streaming.h>

Print &cout {Serial};

#define DECODE_NEC
#define IR_RECEIVE_PIN 0

#if !defined(RAW_BUFFER_LENGTH)
  // Maximum length of raw duration buffer. Must be even. 100 supports up to 48 bit codings inclusive 1 start and
  // 1 stop bit.
  #define RAW_BUFFER_LENGTH 100   // #define RAW_BUFFER_LENGTH  112    // MagiQuest requires 112 bytes.
#endif

#define EXCLUDE_UNIVERSAL_PROTOCOLS   // Saves up to 1000 bytes program space.
#define EXCLUDE_EXOTIC_PROTOCOLS      // saves around 650 bytes program space if all other protocols are active

#include <IRremote.hpp>

void setup() {
  // Serial.swap(1);   // Swap Pins swapped TX is pin 8
  Serial.begin(115200);
  delay(10000);

  cout << (F("START " __FILE__ " from " __DATE__ "\r\nUsing library version " VERSION_IRREMOTE)) << endl;
  cout << (F("Enabling IRin...\n"));
  IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
  cout << (F("Ready to receive IR signals of protocols: "));
  printActiveIRProtocols(&Serial);
  cout << (F("at pin ")) << (IR_RECEIVE_PIN) << endl;;
}
void loop() {
  if (IrReceiver.decode()) {
    switch (IrReceiver.decodedIRData.command) {
      case 0x19: cout << ("0 "); break;
      case 0x45: cout << ("1 "); break;
      case 0x46: cout << ("2 "); break;
      case 0x47: cout << ("3 "); break;
      case 0x44: cout << ("4 "); break;
      case 0x40: cout << ("5 "); break;
      case 0x43: cout << ("6 "); break;
      case 0x07: cout << ("7 "); break;
      case 0x15: cout << ("8 "); break;
      case 0x09: cout << ("9 "); break;
      case 0x16: cout << ("*\n"); break;
      case 0x0D: cout << ("#\n"); break;
      case 0x18: cout << ("Arrow UP\n"); break;
      case 0x5A: cout << ("Arrow RIGHT\n"); break;
      case 0x52: cout << ("Arrow DOWN\n"); break;
      case 0x08: cout << ("Arrow LEFT\n"); break;
      case 0x1C: cout << ("OK\n"); break;
      default: IrReceiver.printIRResultShort(&Serial);
    }
    delay(65);
    IrReceiver.resume();   // Receive the next value
  }
}

A remote control with NEC protocol was used.

1 Like