I wanted the sensor to be high when I press the button, but when DigitalRead does low, the LED turns on, when it does high, the LED goes off. Whereas when the digitalRead is high, isn't it supposed to turn on the led?

int pirPin = 8;
int ledPin = 13;
int state;
unsigned long previousMillis = 0;        // will store last time LED was updated
const long interval = 1000;

void setup() {
  pinMode(pirPin, INPUT);
  pinMode(ledPin, OUTPUT);


void loop() {

  unsigned long currentMillis = millis();
  state = digitalRead(pirPin);

  if (state == HIGH) {
    previousMillis = currentMillis;
    if (currentMillis - previousMillis >= interval) {
      digitalWrite(ledPin, LOW);
      digitalWrite(ledPin, HIGH);
  • 1
    What about the button? Have you connected a button to pirPin or a PIR sensor?
    – chrisl
    Apr 10, 2021 at 16:53
  • 1
    "isn't it supposed to turn on the led?" - how should we know that? It's your code. You tell us what it's supposed to do. If you want the LED to go on, the code is wrong. Apr 10, 2021 at 17:10
  • This way the led lights up.But I don't understand why the led turns on when I make low
    – Sabri73
    Apr 10, 2021 at 18:39
  • @ chrisl. yes i connected.code already works this way.I'm wondering why the led lights up when it's low
    – Sabri73
    Apr 10, 2021 at 19:06
  • 1
    Please provide a schemtic or wiring diagram of your circuit. And please better describe your problem. What behavior did you expect? What did actually happen? I currently don't understand you. "I make it low" is not really a helpful description of what you are doing
    – chrisl
    Apr 10, 2021 at 23:15


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.