Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

šŸ‘‰ Ready to explore and create with us? Click [here] and join today!

Lesson 07: Infrared Speed Sensor Module

In this lesson, you will learn how to use the Raspberry Pi Pico W to interface with an infrared speed sensor module. By connecting the sensor to GPIO 16, you will detect obstructions in real-time. The program monitors the sensor output, and when an obstruction is detected, it prints ā€œObstruction detectedā€ to the console. If there’s no obstruction, it prints ā€œUnobstructed.ā€

Required Components

In this project, we need the following components.

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

Universal Maker Sensor Kit

94

Universal Maker Sensor Kit

You can also buy them separately from the links below.

Component Introduction

Purchase Link

Raspberry Pi Pico W

BUY

Infrared Speed Sensor Module

BUY

Breadboard

BUY

Wiring

../_images/Lesson_07_Speed_pico_bb.png

Code

from machine import Pin
import time

# Set GPIO 16 as an input pin to read the speed sensor
speed_sensor = Pin(16, Pin.IN)

while True:
    if speed_sensor.value() == 1:
        print("Obstruction detected")
    else:
        print("Unobstructed")

    time.sleep(0.1)  # Short delay to reduce CPU usage

Code Analysis

  1. Import Libraries:

    This code begins by importing necessary libraries. The machine library is used to interact with the GPIO pins, and the time library is for adding delays in the program.

    from machine import Pin
    import time
    
  2. Sensor Configuration:

    The infrared speed sensor is connected to GPIO 16. It’s set as an input, meaning the Pi Pico W will read data from this pin.

    speed_sensor = Pin(16, Pin.IN)
    
  3. Main Loop:

    The while True: loop creates an infinite loop. Inside this loop, the program continuously checks the sensor’s value.

    If speed_sensor.value() is 1, it means the sensor detects an obstruction. If it is 0, then there is no obstruction.

    while True:
        if speed_sensor.value() == 1:
            print("Obstruction detected")
        else:
            print("Unobstructed")
    
  4. Delay to Reduce CPU Usage:

    A short delay of 0.1 seconds is introduced in each iteration of the loop. This reduces the CPU usage by preventing the loop from running too rapidly.

    time.sleep(0.1)
    
  5. More

    If an encoder is mounted on the motor, the rotational speed of the motor can be calculated by counting the number of times an obstruction passes the sensor within a specific period.

    ../_images/Lesson_07_Encoder_Disk1.png