Example Code for Arduino-Infrared Proximity Detection
Last revision 2025/12/30
This example is a test sample for the Gravity Digital Adjustable Infrared Proximity Sensor.
Hardware Preparation
- DFRduino UNO R3 (or similar) x 1
- Gravity: Digital Adjustable Infrared Proximity Sensor (SKU:SEN0239) x 1
Software Preparation
Wiring Diagram
Other Preparation Work
Detection Distance Adjustment
Sample Code
Serialport band rate 9600bps, display in ASCII.
/*!
@file SEN0239.ino
@brief Gravity Digital Adjustable Infrared Proximity Sensor for Arduino
@n [Get the module here]()
@n This example is a test sample.
@n [Connection and Diagram](http://wiki.dfrobot.com.cn/index.php?title=(SKU:SEN0239)%E6%BC%AB%E5%8F%8D%E5%B0%84%E7%BA%A2%E5%A4%96%E7%BA%BF%E5%85%89%E7%94%B5%E5%BC%80%E5%85%B3#.E6.A0.B7.E4.BE.8B.E4.BB.A3.E7.A0.81)
@copyright [DFRobot](https://www.dfrobot.com), 2017
@copyright GNU Lesser General Public License
@author [lijun]([email protected])
@version V1.0
@date 2017-05-15
*/
#define IR 4
void setup()
{
Serial.begin(9600);
Serial.print("switch: ");
pinMode(IR,INPUT);
}
void loop()
{
Serial.println("-------------------------RF-----------------------");
if(digitalRead(IR) == 1)
Serial.println("Switch: YES");
else
Serial.println("Switch: NO ");
Serial.println();
}
Result
Additional Information
- Use constant voltage transformer when using DC power supply, and make sure the power ripple VPP ≤ 10%.
- It is not allowed to connect wires with power on. Please do exactly as the connection diagram indicates.
Was this article helpful?
