Spark core IR communication?

How to send some text over Infrared?
can I use Serial1 to do that?
any tutorial for that? THANKS! :blush:

Have you searched for e.g. “infrared” already?

1 Like

To save you the trouble of having to click the search button, take a look at this topic:

I did search on the google
is it same if I use the Serial1 or Serial2?

That should work just the same - you might just want to redim the resistors for 3.3V rather than 5V.

The only problem with a setup like this is that it has absolutely no fault tolerance/recovery logic built in.
If your signals get muddled up (e.g. stray light from other IR sources, obstacles, …) your receiver won’t be able to recover the data and may even carry errors forward after the interference has gone - and it won’t even know the data is wrong.

This is why there are more sophistcated encoding solutions used even in simple IR remote controls.

One possible Arduino workaround is even addressed in this follow-up article

BTW: To get Spark specific search results, the “Magnifier” search in the top right corner of this forum page works quite well :wink:


In general, using un-modulalted IR will get you very short range and lots of errors & interference. So using some sort of checksum is advisable. This approach is similar to IRDA. With the right sensors good baud rates are achievable at short ranges.

CIR (Consumer IR e.g. TV IR remote control) uses modulated IR signalling and can achieve much longer ranges. However, IR receivers are not usually ‘tuned’ for serial style comms and use quite a few different protocols. It is possible to use this approach to get up to 2400 baud with common receivers and a bit faster with specialised receivers.

Some commercial solutions send serial using CIR at 2400 baud e.g. DISNEY glow with the show & some Laser tag systems also. (Both can get very good range)

If you use one of the common IR protocols, like NEC, you can send 4 bytes in each signal and you could use one for a checksum.

Because it uses the light spectrum, IR comms is nearly always half-duplex (unless both directions are shielded from each other) and needs line of sight.


Hi, Thanks for reply
I am using TSOP4138 receiver and Vishay TSAL6100 irLED
is it ok for long range communication? what speed should I use, 2400?

here is my code:

const int irTX = D1;
const int irRX = D0;

char receive[64] = "";

void setup() {
Spark.variable("receive", receive, STRING);

void loop() {
if (Serial1.available()){
        receive = (char);

but it is getting this error:

In file included from ../inc/spark_wiring.h:29:0,
from ../inc/application.h:29,
from ircommunication.cpp:2:
../../core-common-lib/SPARK_Firmware_Driver/inc/config.h:12:2: warning: #warning "Defaulting to Release Build" [-Wcpp]
#warning "Defaulting to Release Build"
ircommunication.cpp: In function 'void loop()':
ircommunication.cpp:13:17: error: incompatible types in assignment of 'char' to 'char [64]'
Spark.variable("receive", receive, STRING);
make: *** [ircommunication.o] Error 1

As for your error you’d need to do something like

  while (Serial2.available() && i < 62)
  { // read as long there are bytes, but no more than 63
    receive[i++] = (char);  // one byte at a time
  receive[i] = '\0';       // terminate string
  Serial2.write(receive);  // echo back
  Serial.write(receive);   // send via USB
  i = 0;


1 Like

Hi @5h177y

It looks like you are using a consumer IR receiver with 30-50kHz bandpass filter and a bare IR LED transmitter. How are you modulating the IR LED transmitter with the 30-50kHz carrier then? Your receiver is expecting an IR signal with a 38kHz carrier and I don’t see how you are providing it.

1 Like

The TSAL6100 is probably one of the best IR LEDs/emitters to use. (Narrow angle / more range)

TSOP4138 wouldn’t be the best, in my opinion. A quick check of the data sheet shows it is designed for short bursts. If you are using an IR receiver go for 2400 BAUD or less. (Unless you are using specialised devices which can get up to 9600).

As a general comment, using a higher modulation frequency (56kHz) should provide better results.

Long range = “how long is a piece of string”. Perhaps if you outline the details & requirements of your project, it may be possible to make more relevant suggestions.

I noticed you were using a 64 byte buffer in your code. That might be streching things a bit. Start using 4, 8 or 16 bytes with error checking and increase until it fails.

As has been mentioned already, for an IR receiver you need to generate a modulated signal and turn it on and off to match the bitstream. However, there are good reasons why companies have created IR protocols to achieve optimum performance.

Check out this site for a solid intro to IR signals (CIR only). SB Projects

Lots of interesting IR related topics on our own blog as well.

Using something like the NEC IR Protocol might be a better approach for you & should work with your existing receiver @ 38kHz modulation frequency!


Thanks ScruffR!
No compile error, but it shows some unrecognizable characters…

serial monitor:


my code:

#include "Serial2/Serial2.h"

char read[8] = "";
char write[8] = "hello";
int i = 0;

void setup() {
Spark.variable("read", read, STRING);

void loop() {

while (Serial2.available() && i < 8){
    read[i++] = (char);
read[i] = '\0'; 

You need to reset i = 0; otherwise you’ll corrup your memory beyond read[8]!

And if you still got the problems with your wrong characters there might be several reasons.

  1. You are not using your hardware correctly - as mentioned by @bko in post #9.
  2. Your sender got an issue.
  3. You got interference corrupting your signal.

I’d suspect 1. to be the most likely because of the reasons Brian pointed out. These are valid points you shouldn’t just ignore.

Thanks for the advice!

  1. Can I use a 56kHz receiver instead?
  2. I will find a way to determine it
  3. If got interference, it will show “?”

No matter what modulation frequency you’d use, you’ll have to provide this particular frequency for the receiver.
And since we don’t see your transmitter code there is no way to tell if you do.

Your code doesn’t seem to have any logic that would support this statement