Check the 34 other Arduino tutorials

#9 · Control Arduino with remote (HX1838 VS1838)


This video still uses IRremote v2.0 https://github.com/Arduino-IRremote/Arduino-IRremote#converting-your-2x-program-to-the-3x-version shows the steps to make the code suitable for for 3.0

Introduction

There is something magical about a remote control: sending data through the air without being tied to a wire. In this Dutch Arduino tutorial for beginners I'm going to explain how you can make this yourself 😃

Course material

At the bottom of this page you'll find the course material button. This button allows you to download the code, circuit diagram and other files relevant to this Arduino tutorial.

Infrared

The remote control in this tutorial uses infrared. This cannot be perceived by humans. Sometimes you can make this light visible via your phone. Open the camera on your phone. Now point a remote control at the lens. You will then see the light illuminate. The signals from the remote control cannot pass through materials. That is why the remote control will not work if someone is covering the receiver, for example.

Infrared remote visible on camera
Infrared remote visible on camera

Signals

If the receiver only checks whether the light on the remote control is 'on', then we can only send 1 signal. Most remotes have many more buttons. A clever trick has been used to tell the difference between different buttons: blinking. By adjusting the speed of the flashing, the receiver can distinguish many more signals.

HX1838 VS1838 infrared receiver

IR receiver
HX1838 VS1838 infrared receiver

There appears to be an LED on the top of the receiver. However, this is not it. Here is the IR receiver. Furthermore, the receiver has three pins:

  • - or GND is the earth (ground)
  • + or Vcc is 5V
  • S or TRIG is the signal pin

We will use the S pin for receiving the IR signals.

The circuit

HX1838 VS1838 infrared receiver connected to the Arduino
HX1838 VS1838 infrared receiver connected to the Arduino

Push the HX1838 VS1838 into the breadboard and connect the following jumper wires to:

  • The GND on the Arduino -> the left pin of the sensor
  • The 5V on the Arduino -> the middle pin of the sensor
  • The 12 on the Arduino -> the right pin of the sensor

Libraries

We will also use a library in this lesson. This library controls all handling of the IR signals:

1 #include <IRremote.h>

If you don't have these yet, you can install them via the Arduino IDE:

Sketch ▸ Use Library ▸ Manage Libraries

Search here for IRremote, you should find the library of shirriff.

Arduino Code

We start the code by defining two variables:

1 int IrReceiverPin = 12;
2 decode_results results;
  • IrReceiverPin is the pin to which we connected the trigger.
  • results is the variable where we store the received button code. This is of type decode_results This type comes from the IRremote library.

Initialize Receiver

1 IRrecv irrecv(IrReceiverPin);

The next line is a bit more complicated. We added the IRremote library to our project at the beginning. This contains a function that we can use.

In this case we create the variable irrecv and pass the pin to which it is connected. In our case it is stored in IrReceiverPin.

setup()

1 void setup() {
2   Serial.begin(9600);
3   pinMode(LED_BUILTIN, OUTPUT);
4   Serial.println("Starting IR-receiver...");
5 
6   irrecv.enableIRIn();
7   Serial.println("IR-receiver active");
8 
9   digitalWrite(LED_BUILTIN, LOW);
10 }

First, we initialize the serial monitor. Then we set the LED_BUILTIN as output and print the start message to the serial monitor.

We then tell the IR receiver that it can start reading and become active.

loop()

1 // repeat infinitely
2 void loop() {
3   if (irrecv.decode(&results)) {
4 
5     Serial.println(results.value, HEX);
6 
7     irrecv.resume();
8 
9     switch (results.value) {
10         case 0xFF42BD:  // button *
11             digitalWrite(LED_BUILTIN, HIGH);
12             break;
13 
14         case 0xFF52AD:  // button #
15             digitalWrite(LED_BUILTIN, LOW);
16             break;        
17     }
18 
19   }
20 
21   delay(100);         
22 }

Reading sensor()

1 irrecv.decode(&results)

This rule monitors whether a new signal has been received. What happens here is that the decode function gets &results as parameter. This is the memory address where the variable is stored. In this way the function can store the received IR value here. The result is a hexdecimal value. In this case the value consists of 6 positions for example: 0xFF52AD. At the beginning you will see 0x. Indicates that it is a hexadecimal value.

With irrecv.resume(); we tell the receiver to continue listening for new signals.

Determine button pressed

With switch (results.value) we determine which button has been pressed. On the serial monitor we see code of the pressed button. That way, the 'case' can be set properly. In my case 0xFF42BD for the * button and 0xFF52AD for the # button.

Finally, we wait 100ms for another check to see if a button has been pressed.

Upload code to Arduino

Our program is now ready to be sent to the Arduino . Then open the serial monitor:

Tools ▸ Serial Monitor

Signals received by infrared receiver
Signals received by infrared receiver

Now press different buttons on your remote control. You should now see the received codes in the serial monitor.

#9 &middot; Control Arduino with remote (HX1838 VS1838) schakelschema

Bas van Dijk

About Bas on Tech


My name is Bas van Dijk, entrepreneur, software developer and maker. With Bas on Tech I share video tutorials with a wide variety of tech subjects i.e. Arduino and 3D printing.

Years ago, I bought my first Arduino with one goal: show text on an LCD as soon as possible. It took me many Google searches and digging through various resources, but I finally managed to make it work. I was over the moon by something as simple as an LCD with some text.

With Bas on Tech I want to share my knowledge so others can experience this happiness as well. I've chosen to make short, yet powerful YouTube videos with a the same structure and one subject per video. Each video is accompanied by the source code and a shopping list.