In this article, we connect a KY-032 Obstacle Detector to a Raspberry Pi Pico, any Rp2040 type board will be suitable. We actually tested this with a Cytron Maker Pi Pico
We will use Micropython for these examples but of course you can use the Arduino IDE as well if you have Raspberry Pi Pico support enabled
The module produces a 38KHz square wave primarily made-up by the 555 Timer and VR1. This 38KHz pulse is used to switch on/off the IR LED. If there is an object in front of the IR LED, the infrared pulses are reflected and some of the reflected IR is detected by the IR receiver. This would tell the module that there is an obstacle and will pull the signal pin S to LOW. If there is no obstacle, pin S will be HIGH.
VR2 is used to adjust the distance the obstacle is detected. The module has a detection range of 2~40cm.
The EN pin and EN jumper are provided to have control over the module detection. Put a jumper on EN to have the module always enabled. If you want to have control over the module enable/disable, remove the jumper and connect the EN pin to HIGH to enable or LOW to disable.
- Working voltage: 3.3V to 5VDC
- Working current: ≥ 20mA
- Operating temperature: -10°C to +50°C
- Detection distance: 2 to 40cm
- IO Interface: 4-pin (EN /out / + / GND)
- Output signal: TTL level
- LOW level if obstacle detected
- HIGH if no obstacle detected
- Adjustment: two single-turn variable resistors
The sensor looks like this
Parts Required
You can connect to the module using dupont style jumper wire.
Name | Link | |
Raspberry Pi Pico | ||
37 in one sensor kit | ||
Connecting cables |
Schematic/Connection
Pico | SENSOR |
---|---|
GPIO27 | S |
3v3 | + |
GND | – |
Code Examples
Basic example
from machine import Pin, Timer from time import sleep # Initialization of GPIO27 as input sensor = Pin(27, Pin.IN, Pin.PULL_DOWN) # Continuous loop while True: if sensor.value() == 0: print("Obstacle detected") else: print("No obstacle detected") print("-------------------") sleep(0.5)
REPL Output
Draw a thick black line on a piece of paper and move the sensor over it or just cover it with a black/dark object. You may have to adjust the potentiometer to adjust the sensitivity
MicroPython v1.17 on 2021-09-02; Raspberry Pi Pico with RP2040
Type “help()” for more information.
>>> %Run -c $EDITOR_CONTENT
No obstacle detected
————————————-
No obstacle detected
————————————-
No obstacle detected
————————————-
No obstacle detected
————————————-
Obstacle detected
————————————-
Links
https://github.com/getelectronics/rp2040learning/tree/main/Sensor%20Kit/KY-032%20OBSTACLE%20DETECTOR