Controlling arduino with IR remote control. Universal remote control for Arduino Connecting the IR remote control to arduino

Good day everyone (or night, as you wish), let’s start with a lyrical introduction. Nowadays many people have houses TV with remote control, tuner, DVD player . Many people (and families) cannot imagine their home life without a remote control in their hand. Agree - how great it is to be the master of home appliances, and at any moment dictate your will to these pieces of hardware. In this article, we would like to look at remote control technology in more depth, and give some examples of application for our needs.

So, what do we need as components for our experiment? As an option ready-made IR remote control and IR receiver modules are sold. But we don’t want to wait and pay money, so we will act more hardcore. Let's take as a basis a remote control of unknown origin, we also have a piece of a board from a Chinese tuner on which an infrared receiver is soldered. In the photo below you can see these components. To be honest, the remote control was found among unnecessary junk in the office desk, and the board with the IR receiver was taken from the nearest radio repair shop.

Well then, as I used to say Hannibal - "Forward to Carthage" . We just need to unsolder the receiver and connect it to the board Arduino according to the following scheme...

  • Connecting an IR receiver

IR receiver which was soldered from the board does not have any markings, it is just another unknown Chinese radio component, of which thousands were produced. In short, we can say - in one building it combinesphotodiode, preamplifier and driver . The output is formed by a normalTTL signal without padding, suitable for further processing by the microcontroller. The carrier frequency is possibly (!) 36 kHz, but now this is not so important... Let's just try to connect it to the boardArduino , the conditional diagram will give the desired pinout of this device. In the diagram below, highlighted in red is the shape of the body in which ours is madeIR receiver , highlighted in green - pinout on which it is connected to the boardArduino .

Attention!!! On the Internet there are many pinout diagrams for devices in such a package (TSOP). The pinouts given in this article may not coincide with any found on the Internet, but this is how everything is connected to us. If the IR receiver starts to heat up when connected, immediately turn off the assembled circuit; this means that the connection pinout does not correspond to the actual one, and you will have to select it almost at random. This is exactly what we did, because the circuits found on the Internet were not suitable for our IR receiver. Here, in general, the main thing is not to burn the Arduino board, proceed with caution!!!

  • IRremote library

So everything is connected. There is a library to read codes from the remote control IRremote.h, it is with its help that we will study our remote control, or rather the button codes. In the future, we will use the read codes for our own purposes. The sketch with which button codes will be read is presented in the examples of this library, it is called IRrecvDemo. Attention!!! When compiling the sketch, it gives an error; at the very beginning, you need to add two more plug-in libraries:

#include "boarddefs.h" //Additional library #include "IRremote.h" #include "IRremoteInt.h" //Additional library int RECV_PIN = 2; //Pin for connecting the output signal from the IR receiver //Create an instance of the IRrecv class, passing the pin for connecting the signal from the IR receiver as a parameter IRrecv irrecv(RECV_PIN); decode_results results; //Variable for saving the received code of the pressed button void setup() ( Serial.begin(9600); irrecv.enableIRIn(); //Enabling the IR receiver to work ) void loop() ( if (irrecv.decode(&results)) //If an event occurred/the button was pressed ( Serial.println(results.value, HEX); //Output the code of the pressed button in hexadecimal form to the port monitor irrecv.resume(); //Read the next value/button ) delay(100 ); )

After the sketch has been uploaded to the boardArduino(we use Arduino Nano on the shield I/O Wireless Shield for Nano ), can be opened port monitor and see what codes appear when you press the buttons on the remote control. The result of the sketch is shown in the screenshot below:

By the way, as port monitor We use our proven software, if anyone is interested, you can read the article and download Serial Monitor Pro.

#define KEY_ONOFF 0x807F807F //On/Off button #define KEY_MUTE 0x807F48B7 //Mute button #define KEY_1 0x807F00FF //Button 1 #define KEY_2 0x807FE01F //Button 2 #define KEY_3 0x807F609F //K button 3 #define KEY_4 0x807F20DF //Button 4 #define KEY_5 0x807FD02F //Button 5 #define KEY_6 0x807F50AF //Button 6 #define KEY_7 0x807F10EF //Button 7 #define KEY_8 0x807FF00F //Button 8 #define KEY_9 0x807F708F //Button 9 #define KEY_0 0x807FC837 //Button 0

And now, in general, everything is ready for the final test - this will be an elementary test of controlling the on/off of relay modules. Here's a small task:

  • We use two relay modules
  • We bind relay No. 1 to button “1” of the remote control
  • Relay No. 2 is tied to button “2” of the remote control
  • Turning on any of the relay modules is done by pressing the button to which it is assigned
  • Turning off any of the relay modules is also done by pressing the button to which it is assigned
  • Pressing the On/Off button unconditionally turns off both relay modules (if they were turned on, or one of them is turned on)

A sketch that implements the above task:

#include "boarddefs.h" //Additional library #include "IRremote.h" #include "IRremoteInt.h" //Additional library #define KEY_ONOFF 0x807F807F //On/Off button #define KEY_1 0x807F00FF //Button 1 #define KEY_2 0x807FE01F //Button 2 #define RELOUT1 3 //Output port for relay 1 #define RELOUT2 4 //Output port for relay 2 int RECV_PIN = 2; IRrecv irrecv(RECV_PIN); decode_results results; static boolean REL1_ONOFF = false; static boolean REL2_ONOFF = false; void setup() ( pinMode(RELOUT1, OUTPUT); pinMode(RELOUT2, OUTPUT); Serial.begin(9600); irrecv.enableIRIn(); // Start the receiver ) void loop() ( if (irrecv.decode(&results )) ( switch(results.value) ( ​​case(KEY_ONOFF): REL1_ONOFF = false; REL2_ONOFF = false; break; case(KEY_1): if(REL1_ONOFF) REL1_ONOFF = false; else REL1_ONOFF = true; break; case(KEY_2): if(REL2_ONOFF) REL2_ONOFF = false; else REL2_ONOFF = true; break; ) irrecv.resume(); ) digitalWrite(RELOUT1, REL1_ONOFF); digitalWrite(RELOUT2, REL2_ONOFF); delay(100); )

And at the end of the article there is a video that demonstrates the work of both sketches. If you wish and have creative imagination, you can expand the fleet of plug-ins and manage everything in a more advanced way. In our article, we tried to give a basic example of the application of this technology. Thank you for your attention and enjoy watching!!!

Please enable javascript for comments to work.

The IR Receiver module in combination with an IR remote control will allow you to easily implement remote control of the Arduino board.

It is nothing more than a VS1838B IR receiver with the manufacturer’s recommended harness installed on the board.

To work with this module out of the box, you need a remote control with a frequency of 38 kHz.

The advantage of this board is the push-in connector, which allows you to replace the IR receiver with another one operating at the frequency required for your project without soldering.

Main technical characteristics:

Supply voltage: 2.7 - 5.5V

Modulation frequency: 38kHz

Temperature range: - 20 ... + 80°C

Interface: Digital

Connecting to Arduino

The module is equipped with a three-pin 2.54mm connector

: connects to GND pin

: connects to +5V output

: connects to digital pin (D2 in example)

An example of working in the Arduino environment

To work with this module you need to install the IRRemote library

Download, unpack and put it in the libraries folder in the Arduino folder. If the Arduino IDE was open at the time of adding the library, reboot the environment.

Reading remote control buttons

To read the remote control readings, fill in the sketch below. It will output the encoding of the pressed buttons to the port.

As an example, we will use the remote control, as in the picture, because This type of remote control is included in the set

You can read about the differences in the operating logic of various remote controls in the original article from a member of our community under the nickname

Sample code:

#include int RECV_PIN = 2; IRrecv irrecv(RECV_PIN); //Create an object for receiving a signal from a specific port decode_results results; //Variable storing the result void setup () { Serial // Start receiving) void loop() ( if (irrecv.decode(&results)) //When receiving a signal... { Serial.println(results.value); //...output its value to the serial port irrecv.resume(); ) )

You should see the following in the port monitor:

By holding each button for almost a second, we get about 10 codes. The first one is the button code. And after it, a standard code begins to appear, which reports that the button is stuck.

Controlling Arduino board with remote control

Let's make the LED on the Arduino board (D13) light up when the first button is encoded and turn off when the second button is encoded.

Sample code:

// Tested on Arduino IDE 1.0.3#include int RECV_PIN = 2; int LED = 13; IRrecv irrecv(RECV_PIN); decode_results results; void setup () { Serial.begin(9600); irrecv.enableIRIn(); // Start the receiver pinMode(LED, OUTPUT); ) void loop() ( if (irrecv.decode(&results)) ( Serial.println(results.value); if (results.value == 16769565) // When receiving encoding 1( digitalWrite(LED, HIGH); // Turn on the LED) if (results.value == 16761405) // When receiving encoding 2( digitalWrite(LED, LOW); // Turn off the LED) irrecv.resume(); // Get the next value } }

The issue of remote or remote control of electrical equipment has always been and will be relevant, regardless of whether there are automation tools in the system or not. To organize remote control, it does not matter whether it is needed; it all depends on the necessary functions assigned to the controlled device. In this article you will learn general information about ways to remotely control a microcontroller.

Kinds

There are two main types of remote communication:

Wired. When control of actuators located in one room (or not in one room) is carried out from a dispatch console or from a push-button station located in another place. In this case, an electrical wire connection is provided between control circuits and actuators (relays, contactors that turn on mechanisms such as motors or systems, for example, lighting).

Wireless. This option does not require electrical connection of control and executive circuits. In wireless circuits there are two devices: a transmitter or remote control (RC) and a receiver, which is part of the controlled circuit. Wireless control, in turn, is common in two versions:

    By optical signal. There are such systems in every home, so you control the operation of the TV, air conditioner and other household appliances.

    By radio signal. There are already a number of options: Bluetooth, ZigBee, Wi-Fi, 433 MHz receivers and transmitters and other variations on this theme.

It is worth noting that with modern technical means you can control the microcontroller both from a remote control and via the Internet on a local network or with access from anywhere in the world.

IR remote control

Let's start our consideration with the simplest and most classic option. Control the device by transmitting a code from a sequence of flickering IR LEDs to an opto-receiver installed on the device. It is worth noting that the IR spectrum is not visible to the human eye, but most photo and video cameras see it.

Since most cameras see IR radiation, you can check. To do this, simply point the remote control so that the emitter faces the camera and press the buttons. Typically, a white glow with a purple tint is visible on the screen.

This type of control has an obvious drawback - you must point the remote control towards the receiver. And if the batteries in the remote control are dead, then you still have to aim, since the activations become less and less frequent.

The advantages are simplicity and high maintainability of both the transmitter and the receiver. You can find parts by disassembling broken remote controls and TVs in order to use them in your own projects.

A typical sensor looks like this. Since an optical signal is being received, it is necessary to exclude triggers from extraneous light sources, such as the sun, lighting lamps, and others. It is also worth noting that the IR signal is received mainly at a frequency of 38 kHz.

Here are the characteristics of one of the IR sensors:

    carrier frequency: 38 kHz;

    supply voltage: 2.7 - 5.5 V;

    current consumption: 50 µA.

And its connection diagram:

Any remote control with a similar operating principle can be used; remote controls from:

    TVs;

    DVD players;

    radio tape recorder;

    from modern lighting devices, such as smart chandeliers and LED strips, etc.

Here is an example of using such a sensor:

In order for the microcontroller, in our case Arduino, to understand the signal from the sensor, you need to use the IRremote.h library. For an example of how to read a signal from a sensor, here is the code for recognizing them by reading the serial port of the microcontroller from the Arduino IDE:

#include "IRremote.h" // connect the library for working with the IR signal.

decode_results results;

Serial.begin(9600); // set the speed of the COM port

Serial.println(results.value, HEX); // print data

As a result, when you flash the Arduino and start shining the remote control at the receiver, we will see the following picture in the serial port monitor:

These are the codes that the buttons send in hexadecimal. This way, you can find out which button on the remote control sends which code, so there are no specific requirements for the remote control you use, because you can recognize and bind any one. By the way, this is an idea for a project for a learning universal remote control; these were sold before. But now, in the age of the Internet, the amount of equipment controlled in this way is decreasing every year.

And using this code you can recognize signals and control the load:

#include "IRremote.h"

IRrecv irrecv(2); // indicate the pin to which the receiver is connected

decode_results results;

irrecv.enableIRIn(); // start receiving

if (irrecv.decode(&results)) ( // if the data arrived

switch (results.value) (

digitalWrite(13, HIGH);

digitalWrite(13, LOW);

irrecv.resume(); // accept the following command

The main thing in the code is recognition through the Switch function, sometimes called a “switchcase”. It is analogous to if branches, but has a more beautiful form for perception. Case - these are options, “if such a code arrives, then...” In the code, pin 13 is controlled for certain signals. Let me remind you that the built-in LED on the ARDUINO board is connected to pin 13, i.e. the author of the code controlled the LED.

You can control anything using a high or low digital pin, through a power transistor (which we covered in two articles earlier) with a DC load, or through a triac and driver for it with a DC load, you can also use relays and contactors, in in general, a whole field for imagination.

For use with microcontrollers, transmitters with operating frequencies of 433 MHz or 315 MHz are common; there may be other frequencies, depending on the specific board, but these are the most common. The system consists of two nodes - a receiver and a transmitter, which is logical.

In the picture, the transmitter is shown at the top right, and the receiver is at the bottom left. Their search name: Radio module 433MHz, MX-05V/XD-RF-5V (receiver and transmitter).

The pinout, as is often the case in modules, is written on the board, like the transmitter:

It’s not so obvious on the receiver, because Data on the printed circuit board is written above two pins; in fact, one of them is not used.

As an example, we provide a diagram and code for turning on an LED from one Arduino board connected to another similar board, wirelessly. The receiver and transmitter are connected identically to both boards:

Device

Module

Arduino pins.

Receiver

Transmitter

2

First, let's write a transmitter program:

#include

RCSwitch mySwitch = RCSwitch(); // create an object to work with front-com

void setup() (

mySwitch. enableTransmit(2); // tell the program which pin the information channel is connected to

void loop() (

mySwitch.send(B0100, 4);

delay(1000);

mySwitch.send(B1000, 4);

delay(1000);

}

The transmitter can transmit binary code, but its values ​​can be written in decimal form.

mySwitch.send(B0100, 4);

mySwitch.send(B1000, 4);

these are the transfer commands, mySwitch is the name of the transmitter that we specified at the beginning of the code, and send is the transfer command. The arguments to this function are:

Transmittername.send(value, size of a packet of pulses sent on the air);

B1000 - the symbol B means binary, it could be written as the number 8, i.e. in decimal notation. Another option was to write “1000” as a string (in quotes).

#include

RCSwitch mySwitch = RCSwitch();

pinMode(3, OUTPUT);

mySwitch.enableReceive(0);

if(mySwitch.available())(

int value = mySwitch.getReceivedValue();

if(value == B1000)

digitalWrite(3, HIGH);

else if(value == B0100)

digitalWrite(3, LOW);

mySwitch.resetAvailable();

Here we declare that the Value variable stores the received value in the line mySwitch.getReceivedValue(). And the fact that the receiver is connected to the 2nd pin is described here mySwiitch.enableReceive(0).

The rest of the code is elementary, if a signal of 0100 is received, then we move pin number 3 to a high state (log. one), and if 1000, then to a low state (log. zero).

Interesting:

In the line mySwitch.enableTransmit(0) we tell the program that a receiver is connected to the 2nd pin and the receiving mode is turned on. The most attentive ones have noticed that the argument of this method is not the pin number “2”, but “0”, the fact is that the enableTransmit(number) method takes not the pin number, but the interrupt number, and in atmega328, which is set to , on the second pin (PortD pin PD2) there is an interrupt with number zero. You can see this in the Atmega pinout applicable to the Arduino board; the pin numbers are written in pink squares.

This method of transmission and reception is very simple and cheap; a pair of receiver and transmitter costs about $1.5 at the time of writing.

Wi-Fi, Adruino and ESP8266

Let's begin with ESP8266 is a microcontroller with hardware Wi-Fi support, it is sold both as a separate chip and soldered on a board, like an Arduino. It has a 32-bit core and is programmable via a serial port (UART).

Boards usually have 2 or more free GPIO pins and there are always pins for firmware; this must be done via a USB to serial adapter. Controlled by AT commands, a complete list of commands can be found on the official ESP8266 website and github.

There is also a more interesting option, NodeMCU boards, they have the ability to flash firmware via USB, because... The USB-UART converter is already on the board, usually made on a CP2102 chip. Node MCU is a firmware, something like an operating system, project based on the Lua scripting language.

The firmware can execute Lua scripts, either by receiving them over the serial port or by reproducing algorithms stored in Flash memory.

By the way, it has its own file system, although it does not have directories, i.e. only files without folders. Not only scripts, but also various data can be stored in memory, i.e. the board can store information recorded, for example, from sensors.

The board works with the following interfaces:

It has a whole host of functions:

    encryption module;

    task Manager;

    real time clock;

    Internet clock synchronization protocol SNTP;

  • ADC channel (one);

    play audio files;

    generate a PWM signal at the outputs (up to 6);

    use sockets, there is support for FatFS, that is, you can connect SD cards and so on.

Here is a short list of what the board can work with:

    ADXL345 accelerometers;

    magnetometers HMC5883L;

    L3G4200D gyros;

    temperature and humidity sensors AM2320, DHT11, DHT21, DHT22, DHT33, DHT44;

    temperature, humidity, atmospheric pressure sensors BME280;

    temperature, atmospheric pressure sensors BMP085;

    many displays operating on I2C, SPI buses. With the ability to work with different fonts;

    smart LEDs and LED controllers - WS2812, tm1829, WS2801, WS2812.

Another interesting thing is that on the website https://nodemcu-build.com/ you can assemble the firmware yourself from the necessary modules, thus you will save space by excluding unnecessary elements from it for your useful code. And you can upload this firmware to any ESP8266 board.

In addition to using the Lua language, you can program the board using the Arduino IDE.

The ESP8266 board can be used as a standalone device or as a module for wireless communication with Arduino.

A review of all the functions and features of this board will take a whole series of articles.

So this board is an excellent option for remote control via Wi-Fi. The scope of application is colossal, for example, using a smartphone as a control panel for a homemade radio-controlled car or quadcopter, even setting up networks for the whole house and controlling every socket, lamp, etc. If only there were enough pins.

The simplest option for working with a microcontroller is to use one ESP8266 board. Below is a diagram of a simple Wi-Fi outlet.

To assemble this circuit you will need a relay module, or a regular relay connected to the pin via a transistor. To get started, you will need the RoboRemoFree smartphone program (https://www.roboremo.com/). In it you will configure the connection to the ESP and create an interface for controlling the outlet. To describe how to use it, you need to write a separate article, so we will omit this material for now.

We load the following firmware into the ESP using the ESPlorer program (a program for working with the board)

WiFi AP Setup

wifi.setmode(wifi.STATIONAP)

cfg.ssid="ESPTEST"

cfg.pwd="1234567890"

wifi.ap.config(cfg)

my_pin_number = 1

Gpio.mode(my_pin_number, gpio.OUTPUT)

gpio.mode(my_pin_number, gpio.OPENDRAIN)

sv=net.createServer(net.TCP)

function receiver(sck, data)

if string.sub (data, 0, 1) == "1" then

if string.sub (data, 0, 1) == "0" then

sv:listen(333, function(conn)

conn:on("receive", receiver)

conn:send("Hello!")

Create HTTP Server

http=net.createServer(net.TCP)

function receive_http(sck, data)

local request = string.match(data,"([^\r,\n]*)[\r,\n]",1)

if request == "GET /on HTTP/1.1" then

Gpio.write(my_pin_number, gpio.HIGH)

gpio.write(my_pin_number, gpio.LOW)

if request == "GET /off HTTP/1.1" then

Gpio.write(my_pin_number, gpio.LOW)

gpio.write(my_pin_number, gpio.HIGH)

sck:on("sent", function(sck) sck:close() collectgarbage() end)

local response = "HTTP/1.0 200 OK\r\nServer: NodeMCU on ESP8266\r\nContent-Type: text/html\r\n\r\n"..

"NodeMCU on ESP8266"..

"

NodeMCU on ESP8266

"..

"On Off"..

sck:send(response)

http:listen(80, function(conn)

conn:on("receive", receive_http)

print("Started.")

Now you can control the program either from the Roboremo program, or through any web browser, to do this you need to type the IP address of the board in the address bar in the wi-fi point mode 192.168.4.1.

There is a fragment in the code:

"NodeMCU on ESP8266"..

"

NodeMCU on ESP8266

"..

"On Off"..

""

This is a kind of response that is given to the browser when accessing the board. It contains HTML code, i.e. a simple WEB page similar to the one on which you are now reading this article.

Here is this page launched in the browser of a smartphone running Android OS. What is described above is not a complete instruction, since it would take up a huge amount of space. If you are interested in this information, write comments and we will definitely conduct a review and write an article about working with it.

Publications on the topic

  • My Talking Angela download for PC My Talking Angela download for PC

    We recommend downloading My Talking Angela to your computer for those who were fond of Tamagotchi as a child. The principle of the game is almost the same, but it has become...

  • Rating of popular browsers Rating of popular browsers

    After reading the rating of the best browsers 2017-2018 for windows xp, 7, 8, 10 and devices with the Android operating system, you can find out...