Jump to content
Electronics-Lab.com Community

IR sending and receiving data -- Ameba Arduino


Recommended Posts

IR (Infrared) is often used in our remote controller to control TV, fans and light etc for its easy-to-use and affordibility. However, we can also use it for some other purposes like sending and receiving sensor data in a close range, and Realtek's Ameba RTL8722 equiped with dual band WiFi and BLE 5.0 also comes with special designed registers just for IR transmissions, here is demo,

 

IR – Infra-Red Receiver And Emitter

Preparation

  • Ameba x 1
  • Grove - Infrared Receiver x 1
  • Grove - Infrared Emitter x 1

Principle
Infra-red refers to the invisible light with wavelength 770nm~1mm. It is commonly used in our life, for example the remote control in our home.
In general, the infra-red emitter and receiver specify the frequency used in transmission. There are some widely used frequency regulations such as NEC, Philips RC5, RC6, RCMM, Toshiba, Sharp, JVC, Sony SIRC,...etc. Among them, NEC uses 38KHz frequency and is commonly used in appliances.
The demodulator in the IR receiver outputs 0V when it receives signals with specified frequency, otherwise it outputs 3.3V(or 5V). For the IR emitter, it needs to emit signal with specified frequency (generally PWM signal), since IR receiver would only respond to the specified frequency,

The figure below shows a complete Infra-red signal emitted from the IR emitter:
1

  1. First it sends the start signal, which contains a start high and a start low. Start high is PWM signal with frequency 38KHz. Start low is digital signal output, usually we output 0V signal for start low.
  2. Then it sends the data a byte at a time. A byte is sent in the order from LSB (Least Significant Bit) to MSB (Most Significant Bit). A bit 1 is represented by a 560us-long PWM signal, and stop for 1.69 ms. A bit 0 is represented by a 560us-long PWM signal, and stop for 560 us.
  3. When the data transmission is finished, send a bit 1 as the stop bit.

At the receiver side, through demodulation, the received PWM signals are converted to general digital input:
2
And those signals other than PWM signals, are put at 3.3V (or 5V).

Example: IR receiver

For implementation, Ameba uses a GPIO Interrupt pin and hardware Timer 4.
Open the example in "File" -> "Examples" -> "AmebaIRSendRecv" -> "recv"
In this example, we use Grove Infrared Receiver, other infra-red receiver works similarly. In general, we will use 3 pins: VCC(connects to 3.3V), GND, RX. In the example, we connect RX to D3 (which has GPIO Interrupt function).
Wiring diagram:
3

 

RTL8710 Wiring diagram:
3

Compile the code and upload to Ameba, then press reset.
If you have NEC remote control in hand, you can test that whether the Ameba IR receiver works. Or you can follow next example to use another Ameba board to make an IR emitter yourself. Note that the received messages of the IR receiver will be shown on the serial monitor.
4

Example: IR emitter

A typical IR emitter has 2 pins. However, the Grove Infrared Emitter has 3 pins: VCC, GND and TX. In practice, we can only use GND and TX.
To transmit 38KHz signal in real time, Ameba uses TX of UART to emit the signal. Hence, when we are using Ameba to emit IR signal, D0 pin (UART RX) would be unavailable.
Following is the wiring diagram:
5

 

However, since the TX of UART is usually on 3.3V, this leads to unnecessary power consumption. Therefore, we rearrange the wires to connect Ameba UART TX to the GND pin of Grove Infrared Emitter, and connect the signal wire of emitter to 3.3V:
6

RTL8710 Wiring Diagram:
6

In this case, when the emitter is not emitting signal, GND and TX are both at 3.3V, voltage difference is 0.
Open the example "File" -> "Examples" -> "AmebaIRSendRecv" -> "send".
Compile and upload to Ameba, and press reset.

In this example, the IR emitter emits signal every 2 seconds. You can test it with the previous IR receiver example.

Code Reference

We refer to Grove infra-red Send Recv library to design our API. The original source code can be found here. Although the API of Ameba looks similar to that of Arduino, the implementation details is different.

 

  • IR receiver
    First, we have to specify the pin used to receive data. Since it needs GPIO interrupt, we have to select a pin with GPIO interrupt function. We use D3 pin in the example.
    IR.Init(pinRecv)
    In the loop, we keep checking if there is incoming signal.
    IR.IsDta()
    When we receive signal, put the data into user buffer.
    IR.Recv(dta)
    The format of the buffer:

     

    • Byte 0: Data length of whole packet.
    • Byte 1: Length of start high, unit is 50us.
    • Byte 2: Length of start low, unit is 50us.
    • Byte 3: The stop length after the PWM signal when the data bit is 1, unit is 50us.
    • Byte 4: The stop length after the PWM signal when the data bit is 0, unit is 50us.
    • Byte 5...: Data.

 

  • IR emitter
    Use Send() API to transmit data. The first argument is the data to be transmitted, and the second argument is the frequency(unit is 1K), in the example we use 38KHz.
    IR.Send(dtaSend, 38)
    The transmission format of the data:

     

    • Byte 0: Data length of whole packet.
    • Byte 1: Length of start high, unit is 50us.
    • Byte 2: Length of start low, unit is 50us.
    • Byte 3: The stop length after the PWM signal when the data bit is 1, unit is 50us.
    • Byte 4: The stop length after the PWM signal when the data bit is 0, unit is 50us.
    • Byte 5: Length of data to transmit.
    • Byte 6...: Data to transmit.
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
  • Create New...