Trains.com

Subscriber & Member Login

Login, or register today to interact in our online community, comment on articles, receive our newsletter, manage your account online and more!

Arduino Application: Using a TV Remote to Trigger Events on a Model Railroad

2434 views
2 replies
1 rating 2 rating 3 rating 4 rating 5 rating
  • Member since
    January 2010
  • 170 posts
Arduino Application: Using a TV Remote to Trigger Events on a Model Railroad
Posted by nycmodel on Sunday, April 22, 2018 11:56 AM

I was surfing around the Internet looking for additional ways I could incorporate an Arduino into my model railroad. I had already used an Arduino, as described in this Forum, to control both crossing gates and flashers at two separate road crossings. What caught my interest were several Internet articles about using a TV remote control with an Arduino.
One in particular, at http://howtomechatronics.com/tutorials/arduino/control-any-electronics-with-a-tv-remote-arduino-ir-tutorial/, is an excellent tutorial explaining the technical aspects of IR remotes and contains a number of examples using the Arduino. The Arduino is used to translate the infrared signal from the transmitter to numerical codes that correspond to the pushing of each button on the remote. Fortunately, as with many Arduino libraries, someone had already worked out the hard part of counting and translating the IR pulses that the TV remote transmits. The IRremote library can be downloaded from the above mentioned site. I chose to use the 2.1.0 – Stable Release. Save the zip file to your pc and note the location. Most people save it in the Downloads directory. Then open the Arduino IDE and from the Sketch menu choose Import Library, Add Library. From the directory you downloaded the zip file to select Arduino-IRremote-2.1.0. The IRremote library should now appear in your Arduino IDE Library list and you can add it to your program. If  IRremoteINT.h should also appear in your program just delete it from the program.
The main component used is a TSOP4838 IR photo sensor. There are numerous TSOP4838 IR photo sensors available but the one I chose (see link below) is mounted on a pc board with mounting holes and comes with a color coded cable and connector. The cable ends are easily plugged into the Arduino connector. The pc board also has a blue LED to indicate that the sensor is powered up and a yellow LED that flashes when a signal is received.
With only 3 lines to connect to the Arduino, vcc, ground and a single signal output pin, it couldn’t be any easier.  Using example code provided and a spare DVD remote, I was quickly reading the output from the remote and displaying the corresponding codes via the Arduino serial monitor. It would be straight forward to drive various output pins based on the numerical code received. Now that I had the classic solution in search of a problem I needed to come up with a use for this on my model railroad. My two road crossings were already animated and the flasher crossing had an ITT crossing bell sound module added to the existing Arduino driven circuitry. What was missing was the 2 long one short 1 long diesel horn sound. Yeah, I know you DCC sound guys are laughing since this is a straight forward application of DCC sound systems, but I am DC with no need or desire to convert and try to fit decoders and speakers into my existing fleet of locomotives. Besides, my layout is nowhere near complex enough to warrant DCC. I had experience with the ITT sound modules so why not get a diesel horn road crossing sound module and trigger it with the IR remote and send the sound to speakers under each of the 2 road crossings depending on which remote control button was pressed. After making note of the various HEX codes displayed when I pushed various remote buttons, I decided to use the Left and Right arrow buttons to trigger sound at the two road crossings. Left Arrow for the crossing on the left of the layout as I looked at them and Right Arrow for the crossing on the right of the layout.  I suppose you could also add descriptive labels to your remote buttons.
/*
IR_recv_sound
2/14/18
IRremote library API commands that we will use
  IRrecv irrecv(pin)       object to receive IR code
  irrecv.enableIRin()      creates a receiver object
  irrecv.decode(&results)  starts listening for an IR code
                                               returns 1 if there is a received code available,
                                               otherwise 0
                                               we will use only the field "value" as in
                                               results.value
  irrecv.resume()          this must be called after irrecv.decode() to resume
                                  listening for a code
*/
#include <IRremote.h>
int RECV_PIN = 11;   //this pin will receive the IR signal from the IR decoder
IRrecv irrecv(RECV_PIN);
decode_results results;
 
void setup()
pinMode(2,OUTPUT); //set pin 2 to output   This will set the spst relay to turn on
                                                                the sound card
pinMode(3,OUTPUT); //set pin 3 to output   This will set the dpdt relay to choose
                                                               output speaker
digitalWrite(2, LOW); //initialize both output pins LOW (off)
digitalWrite(2, LOW);
Serial.begin(9600);
irrecv.enableIRIn();  //Start the receiver
}
 
void loop() {
if (irrecv.decode(&results)){   //is there a signal to decode?
  Serial.println(results.value, HEX);
  switch(results.value){    //we will use a Toshiba DVD remote with the following
                                        codes
    case 0xA25D8A75: 
      Serial.println("Left arrow");
      digitalWrite(3, LOW);  //select speaker for crossing #1 via dpdt relay
      delay(200); // allow relay contacts to settle
      digitalWrite(2, HIGH);  //activate the sound card for half a second via spst
                                          relay
      delay(500);
      digitalWrite(2, LOW);
      break;
    case 0xA25DB24D:
      Serial.println("Right arrow");
      digitalWrite(3, HIGH); //select speaker for crossing #2 via dpdt relay
      delay(200); // allow relay contacts to settle
      digitalWrite(2, HIGH);  //activate the sound card for half a second via spst relay
      delay(500);
      digitalWrite(2, LOW);
      break;
    default:
      Serial.println("Undefined");
   } 
  irrecv.resume(); //Receive the next value
}
delay(100);
}
The code is documented and pretty much describes what is going on. Note that hexadecimal numbers are indicated in the Arduino code by placing 0x in front of the number.
To test the code, I assembled 3 LEDS on the breadboard.
 

When I push the Left Arrow button on the remote the red LED flashes for half a second (this will be the sound board “plays 1 time and turns off” momentary connection). Also the yellow LED illuminates indicating that the left speaker is chosen. Pressing the Right Arrow button on the remote triggers the same red LED flash but now illuminates the green LED to indicate that the right speaker is chosen. The code includes a 0.2 second delay to allow the relay contacts to settle before the sound card is activated.
Here is how the final circuit looks. This application only uses three of the digital pins available on the Arduino Uno. That leaves 8 additional pins to trigger other actions based on the remote control buttons pressed.
 
Components:

1.      Obviously, an Arduino. I used an UNO.

2.      IR Detector.  https://www.amazon.com/SunFounder-Infrared-Sensitive-Receiver-Raspberry/dp/B012RVHSKE/ref=pd_day0_23_3?_encoding=UTF8&pd_rd_i=B012RVHSKE&pd_rd_r=NFMSQ0BKPE9K585DY6YZ&pd_rd_w=PxMhO&pd_rd_wg=57RK4&psc=1&refRID=NFMSQ0BKPE9K585DY6YZ#feature-bullets-btf

3.      2N2222 NPN transistors (2)

4.      1 K resistors (2)

5.      1N4002 diode (2)

6.      DPDT 12 volt relay

7.      SPST 12 volt relay

8.      ITT diesel horn sound module GL200-1 Diesel Horn 1. http://www.ittproducts.com/ 

9.      IR remote control. I used a spare Toshiba DVD remote but any IR remote control should work. Universal remotes are cheap and readily available online.

Just off the top of my head I can think of a couple of other possible IR Remote applications. Press a remote key and activate a servo to set a train order signal. If you use servos for turnouts then why not write code to input multiple consecutive numbers followed by the Enter key. Each turnout would have a unique number. Type that number and press Enter to throw the turnout. 
Notes:
The IR codes received will vary with the make and type of IR remote. I tried six different makes and types of remote controls and they all had different codes for each button. Holding a button down will often result with multiples of the initial code being sent as long as the button is depressed. Some remotes will send the initial code followed by repeating transmissions of “FFFFFFFF”.
The IR system is line of sight. I was able to trigger the circuit from as far as 30 feet away. It could be further than that but I ran out of room.
The IRremote library also contains a “send” command that enables an Arduino to act as an IR transmitter by using an IR LED.
 
Wayne
  • Member since
    January 2010
  • 170 posts
Posted by nycmodel on Sunday, April 22, 2018 12:03 PM

I apologize for the formatting of the comments in my Arduino code. I still haven't figured out how to get this right when copying Word text to the Forum.

  • Member since
    December 2004
  • From: Pa.
  • 3,361 posts
Posted by DigitalGriffin on Monday, April 23, 2018 12:10 PM

Option 1:
You do have the option of playing back wave files with the SD card reader version of arduino.  You can use any audio rip from youtube of trains for background sounds.

Option 2:

I had some left over computer speakers from a 5.1 setup.  I thought about hooking into JRMI and running their 3D sound set that allows up to 256 sound streams using OpenAL Soft 3D Positional audio, including doppler shift, movement reverb, mountains.   Reading a com port for the arduino is no issue.

I was actually playing with it this weekend.  Turned down low it's quite believable.  The authors of JMRI were clever to think of this.

Don - Specializing in layout DC->DCC conversions

Modeling C&O transition era and steel industries There's Nothing Like Big Steam!

Subscriber & Member Login

Login, or register today to interact in our online community, comment on articles, receive our newsletter, manage your account online and more!

Users Online

There are no community member online

Search the Community

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
Model Railroader Newsletter See all
Sign up for our FREE e-newsletter and get model railroad news in your inbox!