Sending serial using IR

Hi there. I have been trying to send serial data using an IR led for TX and a 38khz IR receiver for RX. i have used the following code for the TX arduino(taken from here:

volatile byte pulse = 0;

ISR(TIMER2_COMPB_vect) { // Interrupt service routine to pulse the modulated pin 3
  pulse++;
  if (pulse >= 8) { // change number for number of modulation cycles in a pulse
    pulse = 0;
    TCCR2A ^= _BV(COM2B1); // toggle pin 3 enable, turning the pin on and off
  }
}

void setIrModOutput() { // sets pin 3 going at the IR modulation rate
  pinMode(3, OUTPUT);
  TCCR2A = _BV(COM2B1) | _BV(WGM21) | _BV(WGM20); // Just enable output on Pin 3 and disable it on Pin 11
  TCCR2B = _BV(WGM22) | _BV(CS22);
  OCR2A = 51; // defines the frequency 51 = 38.4 KHz, 54 = 36.2 KHz, 58 = 34 KHz, 62 = 32 KHz
  OCR2B = 26;  // deines the duty cycle - Half the OCR2A value for 50%
  TCCR2B = TCCR2B & 0b00111000 | 0x2; // select a prescale value of 8:1 of the system clock
}

byte val = 10;

void setup() {
  setIrModOutput();
  TIMSK2 = _BV(OCIE2B); // Output Compare Match B Interrupt Enable

  Serial.begin(300);
}

void loop() {
  
  static unsigned long timer = millis();
  if (millis() - timer > 1000) {
    timer = millis();

    Serial.write(val);
  }
}

and the following for the RX arduino-

void setup() {
  Serial.begin(300);
}

void loop() {
  if (Serial.available()){
    byte val = Serial.read();

    Serial.println(val);
  }
}

the signal makes it through but is very unreliable (often wrong and cant transmit quickly.) i have noticed that the RX light is flashing spontaneously suggesting it is receiving a signal even when i am not sending one)

Is the serial protocol simply unsuitable for IR transmition or could i change my circuit/ code to improve it. I have tried using the IR librarys but they seem to concentrate on standard remote controls and take up a lot of memory so i wanted a ‘lighter’ simpler option which i could use to send simple commands from one arduino to another wirelessly. Thanks.

Screen Shot 2018-09-20 at 12.35.44.png

Not looked at the code .... but You could try running at a slower baud rate and/or what about two way communications with a check sum to see if the data is corrupted - if it is sent again .

You could try running at a slower baud rate

300 is the lowest on the arduino IDE so i thought it was the lowest you could go. is the number arbitrary?the specific numbers in the IDE are a mystery to me at the moment... if it is arbitrary then how might one view the results? i could send the data at a lower baudrate between arduinos and then switch to a higher baudrate to send the result to my computer perhaps?

what about two way communications with a check sum to see if the data is corrupted

this is a nice idea but sounds pretty advanced. i will look into it. im suprised i cant find an IR serial library.... thanks for the reply.

You may need a more sophisticated sender routine, because the receiver has a delay of several (100?) µs when switching to high (low?) level. See exact details in the IRremote library code.

If you only use IRsend, the library footprint is quite low. I’d try to encode the transmitted chars with start and stop bits into raw data, and send it using the library.

DrDiettrich: You may need a more sophisticated sender routine, because the receiver has a delay of several (100?) µs when switching to high (low?) level. See exact details in the IRremote library code.

If you only use IRsend, the library footprint is quite low. I'd try to encode the transmitted chars with start and stop bits into raw data, and send it using the library.

thanks for this.I need to send and receive data. I have looked into the IRLibrary again and tried to reduce the footprint by disabling protocols in the IRremote.h file but even after disabling all of them exept the one i am using (NEC) the size of the compiled program is exactly the same as when i use the unmodified IRremote.h file. any idea what might be going on here? thanks/.

The IRremote library consists of 2 classes. You don't have to modify anything, only use the right (IRsend) class. The footprint may be high if sending also uses floating point math, that costs about 4KB. Show your code, up to and including Setup().