Sensor

๐Ÿ‘€ Infrared Sensor (Obstacle Avoidance Module)

๐Ÿ“Œ Description

An infrared sensor is like the robotโ€™s eyes. It sends out invisible light and checks if it bounces back. If something is in front of it, the sensor knows thereโ€™s an obstacle.

๐ŸŽฏ Use

In our projects, infrared sensors are used to:

  • Detect walls or objects in front of the robot.
  • Help the robot avoid crashes.
  • Make smart decisions, like stopping or turning when something is close.

๐Ÿ‘‰ Think of the infrared sensor as the robotโ€™s radar โ€” it helps the robot โ€œseeโ€ without real eyes.

Module parameters

Pin name Description
G GND (Power Input Negative)
V VCC (Power Input Cathode)
S Digital signal pins
A Analog signal pins
  • Supply voltage: 3.3V/5V
  • Connection: 2.54mm pin header
  • Installation method: double screw fixing

๐Ÿ–ฅ๏ธ Code Explanation

import machine
import time

pin17 = machine.Pin(17, machine.Pin.IN)   # Sensor connected to pin 17
pin26 = machine.Pin(26, machine.Pin.OUT)  # LED connected to pin 26

while True:
    if pin17.value() == 0:        # If sensor detects an obstacle
        pin26.value(1)            # Turn LED ON
        time.sleep_ms(200)        # Keep it on for 0.2 seconds
    else:
        pin26.value(0)            # Turn LED OFF

Step by Step

  1. import machine & import time โ†’ Tools to control pins and add pauses.
  2. pin17 = machine.Pin(17, machine.Pin.IN) โ†’ Pin 17 reads the sensorโ€™s signal (input).
  3. pin26 = machine.Pin(26, machine.Pin.OUT) โ†’ Pin 26 controls the LED (output).
  4. while True: โ†’ Loop that repeats forever.
  5. if pin17.value() == 0: โ†’ Checks if the sensor sees an obstacle.
  6. pin26.value(1) โ†’ Turns the LED on (signal that something is detected).
  7. time.sleep_ms(200) โ†’ Keeps the LED on for 0.2 seconds.
  8. else: โ†’ If no obstacle is detectedโ€ฆ
  9. pin26.value(0) โ†’ Turns the LED off.

๐Ÿ‘‰ Together, this makes the LED blink when the sensor detects an obstacle.


โœ… Conclusion of the Test

  • If the LED lights up when something is placed in front of the sensor, the program is working correctly.
  • This test shows how the board can read input from a sensor and react with an output (LED).
  • Later, instead of just turning on a light, the robot could stop, turn, or change direction when it sees an obstacle.
On this page