This experiment aims to design a system to detect obstacles using an IR sensor and count them using a Raspberry Pi Pico. The system continuously monitors the IR sensor’s output, increments the count when an obstacle is detected, and displays the total count in real time. The sensor output will be processed in both Embedded C and MicroPython.
Scope:
To develop a reliable system that detects obstacles and counts them using an IR sensor module connected to a Raspberry Pi Pico.
This experiment is useful in obstacle avoidance systems in robotics, automation, security systems, and smart devices.
Prerequisite:
3.1. Hardware:
Pico microcontroller
IR Obstacle Avoidance Sensor Module (with GND, VCC, OUT, ENABLE pins).
Breadboard
Jumper wires
USB cable (to connect the computer)
3 .2. Software:
Thonny Software(For micro python)
MicroPython firmware installed on Raspberry Pi Pico
ubuntu Terminal(For embedded C)
CMake,Pico SDK,GNU Arm Embedded Toolchain,WSL.
Connection Details:
IR Sensor Pins:
VCC: Connect to 3.3V on the Pico.
GND: Connect to GND on the Pico.
OUT: Connect to GPIO 16 on the Pico (for obstacle detection).
ENABLE: Connect to GPIO 14 on the Pico (to enable/disable the sensor).

Working Principle:
This experiment relies on scanning the rows and columns of a matrix keypad. The Pico microcontroller will output signals on rows and read signals from columns to detect which key was pressed.
Keypad Scanning: The rows are activated one by one by sending a LOW signal, while the columns are continuously monitored for any signal change. When a button is pressed, the corresponding row-column intersection generates a unique signal, identifying the pressed key.
UART Serial Communication: The pressed key is sent via UART to the serial console for display.
IR Obstacle Avoidance Sensor Module
This module can adapt to ambient light and includes a pair of infrared emitting and receiving tubes. The emitting tube sends out infrared at a specific frequency, and when the detection direction encounters an obstacle (reflective surface), the receiving tube picks up the reflected infrared. After being processed by the comparator circuit, the green indicator light will turn on, and simultaneously, the signal output interface will produce a digital signal (a low level signal). The detection distance can be adjusted using a potentiometer knob.
Pinout
VCC: This is the positive power supply input from the main control.
GND: Ground connection.
OUT: Digital output. Outputs high level when there is no obstacle, and outputs low level when an obstacle is detected. The detection distance of obstacles can be adjusted by the potentiometer on the module.
Principle
An obstacle avoidance sensor mainly consists of an infrared transmitter, an infrared receiver and a potentiometer. According to the reflecting character of an object, if there is no obstacle, the emitted infrared ray will weaken with the distance it spreads and finally disappear. If there is an obstacle, when the infrared ray encounters it, the ray will be reflected back to the infrared receiver. Then the infrared receiver detects this signal and confirms an obstacle in front. The detection range can be adjusted by the built-in potentiometer.
Application code:
MicroPython:
from machine import Pin
import time
IR_PIN = Pin(16, Pin.IN, Pin.PULL_UP) # IR sensor OUT pin
ENABLE_PIN = Pin(14, Pin.OUT) # IR sensor ENABLE pin
obstacle_count = 0
obstacle_detected = False
stable_delay = 0.1 # Stable detection time (100ms)
debounce_delay = 0.3 # Debounce time (300ms)
ENABLE_PIN.value(1) # Enable the IR sensor
while True:
if IR_PIN.value() == 0 and not obstacle_detected:
obstacle_count += 1
print(f"Obstacle detected! Count: {obstacle_count}")
obstacle_detected = True
time.sleep(stable_delay) # Stable detection
if IR_PIN.value() == 1 and obstacle_detected:
obstacle_detected = False
time.sleep(debounce_delay) # Debouncing logic
time.sleep(0.05) # Loop delay
Embedded C:
#include <stdio.h>
#include "pico/stdlib.h"
#define IR_PIN 16
#define ENABLE_PIN 14
#define STABLE_DELAY 100 // 100ms
#define DEBOUNCE_DELAY 300 // 300ms
volatile int obstacle_count = 0;
volatile uint32_t last_trigger_time = 0;
volatile bool obstacle_detected = false;
void ir_sensor_isr(uint gpio, uint32_t events) {
uint32_t current_time = to_ms_since_boot(get_absolute_time());
if (gpio_get(IR_PIN) == 0 && !obstacle_detected && (current_time - last_trigger_time) > STABLE_DELAY) {
obstacle_count++;
printf("Obstacle detected! Count: %d\n", obstacle_count);
obstacle_detected = true;
last_trigger_time = current_time;
}
if (gpio_get(IR_PIN) == 1 && obstacle_detected && (current_time - last_trigger_time) > DEBOUNCE_DELAY) {
obstacle_detected = false;
}
}
int main() {
stdio_init_all();
gpio_init(IR_PIN);
gpio_set_dir(IR_PIN, GPIO_IN);
gpio_pull_up(IR_PIN);
gpio_init(ENABLE_PIN);
gpio_set_dir(ENABLE_PIN, GPIO_OUT);
gpio_put(ENABLE_PIN, 1); // Enable sensor
gpio_set_irq_enabled_with_callback(IR_PIN, GPIO_IRQ_EDGE_FALL | GPIO_IRQ_EDGE_RISE, true, &ir_sensor_isr);
while (1) {
tight_loop_contents();
}
return 0;
}
Hardware Image:

Output:
MicroPython and Embedded C programs print the obstacle count to the console.
The count increments each time an obstacle is detected and decreases or resets when it is removed
Video Demonstration:
Appendix:
Common CMakeLists:
cmake_minimum_required(VERSION 3.12)
# Pull in SDK (must be before project)
include(pico_sdk_import.cmake)
project(pico_experiments C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
if (PICO_SDK_VERSION_STRING VERSION_LESS "1.3.0")
message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.3.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}")
endif()
# Initialize the SDK
pico_sdk_init()
add_compile_options(-Wall
-Wno-format # int != int32_t as far as the compiler is concerned because gcc has int32_t as long int
-Wno-unused-function # we have some for the docs that arent called
)
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
add_compile_options(-Wno-maybe-uninitialized)
endif()
# Hardware-specific examples in subdirectories:
add_subdirectory(expt_10_LCD)
CMakeLists:
cmake_minimum_required(VERSION 3.13)
include(pico_sdk_import.cmake)
project(obstacle_detection_project)
pico_sdk_init()
add_executable(obstacle_detection
main.c
)
target_link_libraries(obstacle_detection pico_stdlib)
pico_add_extra_outputs(obstacle_detection)
References:
| # | Topic | Date & time | Action |
|---|
0% Completed (0/1)