Monthly Archives: June 2014

Improved Infrared Communications Link using the Vishay TSOP382 (Arduino to Arduino communications)

This is a first in a series of posts where I describe how I built an Arduino-based weather station, positioned outside my house, that transmits temperature and barometric pressure data through a window using an IR link to an Arduino-based receiver…

In a previous posting, I had described my experiments with IR LEDs and an IR photo-transistor.  I finally purchased a miniaturized receiver for infrared remote control systems, the Vishay TSOP38238. This device contains a photo detector and a preamplifier in one miniaturized package, all for less than 2 dollars. It handles a supply voltage of 2.5 V to 5.0 V and therefore can be powered off the Arduino’s 5.0 V power output. The data sheet for this device can be found here: tsop382

I purchased these devices from SparkFun Electronics:

The main advantage of this IR receiver, besides the preamplifier, is that it ignores IR light unless it is modulated (pulsed) at a specific frequency. The last two digits of the part number indicate this carrier frequency in kilohertz and so the TSOP38238 works with a 38 kHz carrier frequency.

My original circuit used a transistor connected to a data pin of an Arduino, with this data pin configured as the transmit pin of a serial port. As the serial data was transmitted from this pin, the transistor turned on and off an IR LED to match the serial data output. Below is a schematic that shows on the left the IR transmitter circuit and on the right the IR receiver circuit. In Transmitter Circuit – Arduino2, the transistor Q1 is connected to the transmit pin of a serial port and switches the IR LEDs on and off. I have added a second transistor, Q2, and this transistor is switched on and off at a constant frequency of 38 kHz. Because Q2 is in series with Q1, the serial data from Q1 is modulated by Q2 at 38 kHz. Now the serial data is transmitted on a 38 kHz carrier and can be detected by the IR receiver circuit, Receiver Circuit – Arduino 1, on the right-hand side of the schematic below.

IR_CircuitsThe IR receiver circuit is taken from the TSOP382 data sheet. The components R1 and C1 are recommended to prevent electrical overstress.

The Arduino sketch to drive the transmitter circuit:

  Author: Terry Field
  Date  : 20 June 2014
  Board: Arduino Uno
  Purpose: This demonstration program uses PWM to 
  output a 38 kHz carrier on pin 11 and serial data on
  pin 2. The 38 kHz output is used to modulate the 
  serial output from pin 2 so that it can be detected
  by an IR receiver. 

#include <SoftwareSerial.h>

const int rxPin = 3;  
const int txPin = 2; 

const long IR_CLOCK_RATE = 38000L;

#define pwmPin 11   // IR Carrier

SoftwareSerial displayPort(rxPin, txPin); // Rx, Tx

void setup()  {
  // set the data rate for the Serial port
  // toggle on compare, clk/1
  TCCR2A = _BV(WGM21) | _BV(COM2A0);
  TCCR2B = _BV(CS20);
  // 38kHz carrier/timer
  pinMode(pwmPin, OUTPUT);

void loop ()

The code in the setup() function above is using a Pulse Width Modulation (PWM) technique to output a 38 kHz square wave on pin 11.

The Arduino sketch to read output from the IR receiver and output it to the default serial port for inspection in the Arduino IDE’s Serial Monitor is below:

  Program: SerialReader
  Author: Terry Field
  Date  : 20 June 2014
  Board: Arduino Uno
  Purpose: This program reads from a serial port and 
           outputs it to another.

#include <SoftwareSerial.h>

const int rxPin = 2;
const int txPin = 255;

// set up a new serial port
SoftwareSerial inputPort =  SoftwareSerial(rxPin, txPin, true);

void setup()  {
  // set the data rate for the SoftwareSerial port
  Serial.println("\nStarting to listen...\n");

void loop() 
  if (inputPort.available())

Make sure you set the Serial Monitor’s baud rate to 9600.

In my next post I will describe a simple protocol to increase the robustness of the data transfer from the transmitter to the receiver.

The photo below shows on the left the transmitter circuit and on the right the receiver circuit:


X11 Forwarding from a Headless Raspberry Pi, to an Ubuntu 12.04 Desktop

I have several Raspberry Pi’s on my network that run headless, i.e. without monitor or keyboard. When I want to run an XWindows application on the Raspberry Pi and have the window appear on my Ubuntu desktop, I run ssh (secure shell client) with the -X option set, and the application’s connection to the X11 display is automatically forwarded from the Raspberry Pi to my desktop’s XWindows server. The connection is encrypted as all ssh connections are.

The command is:

ssh -X user@hostname

It’s that simple. The -X option takes care of setting the DISPLAY environment variable for you.

Note that man page for ssh mentions that enabling X11 forwarding should be enabled with caution for reasons of security. For this X11 forwarding to work, the remote machine has to make a connection back to the local X11 display, and an attacker could take advantage of this connection to perform activities such as keystroke logging. Make sure you trust the machines you use X11 forwarding with.