Skip to content
TheChatArchiveThe Chat Archive
Qwenqwen3-30b-a3b· Alibaba (Qwen)Hugging Face LMArena Human Preference 140K

write code for arduino, i want it to have a PIR sensor in PWM3,that sensor shoudl…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Claudeclaude-sonnet-4-20250514-thinking-32k’s answer.
  1. User

    write code for arduino, i want it to have a PIR sensor in PWM3,that sensor shoudl trigger PWM 13 if motion is detected

  2. Qwen Assistant

    Here's the Arduino code to detect motion using a PIR sensor connected to digital pin 3 and trigger a digital HIGH signal on pin 13 (note: pin 13 on the Arduino Uno is not a PWM pin, but you can use digitalWrite to control it). If you need PWM on pin 13, ensure you're using a board that supports it (e.g., Arduino Mega).

    // Define pin numbers
    const int pirPin = 3;     // PIR sensor connected to digital pin 3
    const int ledPin = 13;    // Output pin (e.g., for an LED or relay)
    
    void setup() {
      // Set PIR sensor as input and LED pin as output
      pinMode(pirPin, INPUT);
      pinMode(ledPin, OUTPUT);
    
      // Optional: Enable internal pull-up resistor for the PIR sensor
      // pinMode(pirPin, INPUT_PULLUP);
    }
    
    void loop() {
      // Read the PIR sensor value
      int sensorValue = digitalRead(pirPin);
    
      // If motion is detected (HIGH), turn on the LED/actuator
      if (sensorValue == HIGH) {
        digitalWrite(ledPin, HIGH);
      } else {
        digitalWrite(ledPin, LOW);
      }
    
      // Optional: Add a small delay to stabilize readings
      delay(100);
    }
    

    🔧 Notes:

    1. Pin 13 on Arduino Uno:

      • This is a digital pin, not a PWM pin. If you need PWM (variable brightness/speed), use a PWM-capable pin like 3, 5, 6, 9, 10, or 11 (on Uno).
      • Example: Replace ledPin = 13 with ledPin = 9 and use analogWrite(ledPin, 255) for PWM.
    2. PIR Sensor Wiring:

      • Connect the PIR sensor's VCC to 5V, GND to GND, and OUT to digital pin 3.
    3. Optional Pull-Up Resistor:

      • Some PIR sensors require a pull-up resistor. Uncomment pinMode(pirPin, INPUT_PULLUP); if needed.
    4. Adjust Sensitivity:

      • Use the PIR sensor's built-in potentiometers to adjust detection range and sensitivity.

    📌 Example for PWM Output (if needed):

    If you want to use PWM on a different pin (e.g., pin 9), modify the code:

    const int ledPin = 9;  // PWM-capable pin
    
    void loop() {
      if (digitalRead(pirPin) == HIGH) {
        analogWrite(ledPin, 255);  // Full brightness
      } else {
        analogWrite(ledPin, 0);    // Off
      }
    }
    

    Let me know if you need further adjustments! 🚀

  3. User

    yes please modify pin 13 to stay on for 10 seconds

  4. Qwen Assistant

    Here's the modified Arduino code to make the LED on pin 13 stay on for 10 seconds after motion is detected by the PIR sensor connected to pin 3.


    ✅ Key Features:

    • Motion detection via PIR sensor on pin 3.
    • LED on pin 13 turns on when motion is detected.
    • LED remains on for 10 seconds after the last motion is detected.
    • Uses millis() for non-blocking timing.
    • Works with Arduino Uno or similar boards (note: pin 13 is not a PWM pin, but digitalWrite is used to control it).

    🧠 How It Works:

    • The code tracks the last time motion was detected using millis().
    • The LED is on if the last motion was within the last 10 seconds.
    • If no motion is detected for 10 seconds, the LED turns off.

    🔧 Code:

    // Define pin numbers
    const int pirPin = 3;     // PIR sensor connected to digital pin 3
    const int ledPin = 13;    // LED or output device on digital pin 13
    
    // Variable to store the time of the last motion detection
    unsigned long lastMotionTime = 0;
    
    void setup() {
      // Set PIR sensor as input and LED pin as output
      pinMode(pirPin, INPUT);
      pinMode(ledPin, OUTPUT);
    
      // Turn LED off initially
      digitalWrite(ledPin, LOW);
    }
    
    void loop() {
      // Read the PIR sensor value
      int sensorValue = digitalRead(pirPin);
    
      // If motion is detected, update the last motion time
      if (sensorValue == HIGH) {
        lastMotionTime = millis();
      }
    
      // Check if 10 seconds have passed since the last motion
      if (millis() - lastMotionTime < 10000) {
        // LED is on
        digitalWrite(ledPin, HIGH);
      } else {
        // LED is off
        digitalWrite(ledPin, LOW);
      }
    
      // Optional: Add a small delay to reduce CPU usage
      delay(100);
    }
    

    📝 Notes:

    • Pin 13: This is a digital pin, not a PWM pin. If you need variable brightness, use a PWM-capable pin (e.g., pin 9, 10, or 11 on Arduino Uno).
    • PIR Sensor: Make sure it's wired correctly:
      • VCC → 5V
      • GND → GND
      • OUT → Digital Pin 3
    • Adjustability: You can change 10000 to any number of milliseconds (e.g., 5000 for 5 seconds).

    🔄 Optional: Use PWM (if needed)

    If you want to use PWM (e.g., for fading the LED), change the pin and use analogWrite:

    const int ledPin = 9;  // PWM-capable pin (e.g., 9 on Arduino Uno)
    
    // In loop:
    if (millis() - lastMotionTime < 10000) {
      analogWrite(ledPin, 255); // Full brightness
    } else {
      analogWrite(ledPin, 0);   // Off
    }
    

    Let me know if you'd like to add features like sound alerts, logging, or multiple sensors! 🚀

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.