

The XHM131 Light Sensor w Relay is a versatile electronic component designed to detect ambient light levels and activate a relay based on user-defined thresholds. This component is ideal for automating devices such as lights, fans, or other appliances, making it a popular choice for home automation, industrial applications, and DIY projects. Manufactured in China, the XHM131 combines a light-dependent resistor (LDR) for light detection and a relay module for switching high-power devices.








The XHM131 module has a total of 6 pins and terminals for input and output connections. Below is the pinout description:
| Pin/Terminal | Label | Description | 
|---|---|---|
| 1 | VCC | Connect to a 5V DC power supply. | 
| 2 | GND | Ground connection. | 
| 3 | OUT | Digital output pin that indicates the light detection status (HIGH or LOW). | 
| 4 | NC | Normally Closed terminal of the relay. | 
| 5 | COM | Common terminal of the relay. | 
| 6 | NO | Normally Open terminal of the relay. | 
Below is an example of how to use the XHM131 with an Arduino UNO to control an LED based on ambient light levels.
// Define the pin connected to the XHM131 OUT pin
const int lightSensorPin = 2;  
// Define the pin connected to the relay (NO terminal)
const int relayPin = 13;  
void setup() {
  pinMode(lightSensorPin, INPUT);  // Set the light sensor pin as input
  pinMode(relayPin, OUTPUT);       // Set the relay pin as output
  digitalWrite(relayPin, LOW);     // Ensure the relay is off initially
  Serial.begin(9600);              // Initialize serial communication
}
void loop() {
  int lightStatus = digitalRead(lightSensorPin);  // Read the light sensor output
  
  if (lightStatus == HIGH) {
    // If light is detected, activate the relay
    digitalWrite(relayPin, HIGH);
    Serial.println("Light detected: Relay ON");
  } else {
    // If no light is detected, deactivate the relay
    digitalWrite(relayPin, LOW);
    Serial.println("No light detected: Relay OFF");
  }
  
  delay(500);  // Add a small delay for stability
}
Relay Not Activating
Erratic Behavior
Relay Stuck in One State
Arduino Not Detecting Output
Q: Can the XHM131 detect specific light wavelengths?
A: No, the XHM131 uses an LDR, which responds to general ambient light levels and is not wavelength-specific.  
Q: Can I use the XHM131 with a 3.3V microcontroller?
A: The module requires a 5V power supply, but the OUT pin can be connected to a 3.3V logic input with proper level shifting.  
Q: Is the relay safe for inductive loads like motors?
A: Yes, but use a flyback diode across the load to protect the relay from voltage spikes.
This concludes the documentation for the XHM131 Light Sensor w Relay.