Trouble testing a hc-sr501 using esp32-c3-mini

Currently having trouble testing the hc-sr501 PIR sensor on a esp32-c3-mini microcontroller. Haven't been able to properly test it, since i haven't managed to figure out to which pin the sensor should be connected to. (I've tried all of them and haven't gotten any kind of response on the serial monitor)

Current code on the IDE:

#define PIN_SENSOR 5
const int PIN_TO_SENSOR = 5; // GPIO19 pin connected to OUTPUT pin of sensor
int pinStateCurrent   = LOW;  // current state of pin
int pinStatePrevious  = LOW;  // previous state of pin

void setup() {
  Serial.begin(9600);            // initialize serial
  pinMode(PIN_TO_SENSOR, INPUT); // set ESP32 pin to input mode to read value from OUTPUT pin of sensor
}

void loop() {
  pinStatePrevious = pinStateCurrent; // store old state
  pinStateCurrent = digitalRead(PIN_TO_SENSOR);   // read new state

  if (pinStatePrevious == LOW && pinStateCurrent == HIGH) {   // pin state change: LOW -> HIGH
    Serial.println("Motion detected!");
    // TODO: turn on alarm, light or activate a device ... here
  }
  else
  if (pinStatePrevious == HIGH && pinStateCurrent == LOW) {   // pin state change: HIGH -> LOW
    Serial.println("Motion stopped!");
    // TODO: turn off alarm, light or deactivate a device ... here
  }
}

Honestly any help is appreciated.

how do you power your module?

is it 5 or 19 ?

USB connected to my notebook

I meant the hc-sr501 (a link to your module's doc would help too)

5v and gnd from the esp32-c3, and i tried using pin 5 since the code originally was created for a esp32, and the esp32-c3 doesn't have a gpio19 pin

if you mean the datasheet i don't have the link anymore since i already downloaded the pdf file, and since i'm a new user on the website i can't upload files

found it

OK - many of those modules require indeed 5V and have a 3.3V compatible output

which esp32-c3-mini do you have ? does it have a 5V OUTPUT ?

I'm not sure which esp32-c3 i have, but it does have a 5V output

it says it's a super mini on the back if it helps

Something like that?

try connecting your sensor to pin GPIO5 / A3 / D3 and run this code

const byte sensorPin = D3;  // GPIO5

void setup() {
  Serial.begin(115200);
  pinMode(sensorPin, INPUT);
}

void loop() {
  Serial.println(digitalRead(sensorPin));
  delay(10);
}

open the Serial plotter at 115200 bauds and see if you get any reaction (some modules require a rest time of 30 seconds to "sense" their environment)

Alright, new problem, i was about to try the code but now my IDE can't find the port i'm connected to. This didn't happen before and reinstalling ch341ser.inf did not fix it.

reboot ?

The computer or the board?

the computer

didn't work

can't help much on that...

fair enough, thanks for the code. if it works i'll mark the solution

the code is not rocket science but should be enough to see if things work

1 Like