Sensor

๐Ÿ“ก IR Receiving

๐Ÿ“Œ Description

An IR receiver is like the robotโ€™s ears. It listens for invisible light signals sent by an infrared remote control (like a TV remote). Each button on the remote sends a special code, and the robot can react to those codes.

๐ŸŽฏ Use

In our projects, IR receivers are used to:

  • Control the robot wirelessly with a remote.
  • Turn lights, motors, or sounds on and off.
  • Give commands without touching the robot.

๐Ÿ‘‰ Think of the IR receiver as the robotโ€™s remote control listener โ€” it hears signals and follows them.

Module parameters

Pin name Description
G GND (Power Input Negative)
V VCC (Power Input Cathode)
S Digital signal pins
  • Supply voltage: 3.3V/5V
  • Connection: PH2.0 terminal
  • Installation method: screw fixing

๐Ÿ–ฅ๏ธ Code Explanation

import machine
import irremote

pin26 = machine.Pin(26, machine.Pin.OUT)   # LED connected to pin 26
value = 0
Mode = 0
ir_rx = irremote.NEC_RX(17, 8)             # IR receiver on pin 17

while True:
    if ir_rx.any():                        # If a signal is received
        value = hex(ir_rx.code[0])         # Read the code in hex format
    if value == hex(0x1c):           # If the code matches button signal
        Mode = 1 - Mode                    # Toggle Mode (switch between 0 and 1)
        value = 0                          # Reset value
    if Mode == 0:
        pin26.value(0)                     # LED OFF
    if Mode == 1:
        pin26.value(1)                     # LED ON

Step by Step

  1. import machine, irremote โ†’ Tools for pins and infrared communication.
  2. pin26 = machine.Pin(26, machine.Pin.OUT) โ†’ Pin 26 controls the LED.
  3. ir_rx = irremote.NEC_RX(17, 8) โ†’ Sets up the IR receiver on pin 17.
  4. while True: โ†’ Loop that repeats forever.
  5. if ir_rx.any(): โ†’ Checks if a signal is received.
  6. value = hex(ir_rx.code[0]) โ†’ Reads the code from the remote in hex format.
  7. if value == hex(0x1c): โ†’ If the code matches the chosen button (example: โ€œOKโ€ button).
  8. Mode = 1 - Mode โ†’ Switches between Mode 0 and Mode 1 (like ON/OFF).
  9. if Mode == 0: โ†’ LED OFF
  10. if Mode == 1: โ†’ LED ON

๐Ÿ‘‰ Together, this program makes the LED toggle ON and OFF each time the remote button is pressed.


โœ… Conclusion of the Test

  • If pressing the chosen button on the remote makes the LED turn on and off alternately, the program is working correctly.
  • This test shows how the robot can receive infrared signals and react to them.
  • Later, instead of just controlling an LED, the robot could use the remote to move motors, play sounds, or change modes.

button Hex-raw hex-command
1 0xba45ff00 0x45
2 0xb946ff00 0x46
3 0xb847ff00 0x47
4 0xbb44ff00 0x44
5 0xbf40ff00 0x40
6 0xbc43ff00 0x43
7 0xf807ff00 0x7
8 0xea15ff00 0x15
9 0xf609ff00 0x9
* 0xe916ff00 0x16
0 0xe619ff00 0x19
# 0xf20dff00 0xd
Up 0xe718ff00 0x18
Left 0xf708ff00 0x8
Right 0xa55aff00 0x5a
Down 0xad52ff00 0x52
Ok 0xe31cff00 0x1c

On this page