BytesOfProgress

ESP32 Sensors & WiFi

09th October 2023 / 02:30 PM

Today I tried out the ESP32 for the first time. I managed to attach a PIR Sensor-Module and an LED to the ESP32. The LED lights up as soon as the PIR Sensor-Module recognizes something is moving. I also got the ESP32 to connect to a WiFi Network.

First, I started with the obligatory "Hello World"

  void setup() {
    // Set the Serial Monitor to 115200!
    Serial.begin(115200);
  }

  void loop() {
    Serial.println("Hello from ESP32");
    delay(1000);
  }

PIR Sensor-Module

After that I attached the PIR Sensor-Module and the LED:

Then I programmed it to turn on the LED when the PIR Sensor Module recognizes something moving:

  int ledPin = 14;
  int sensorPin = 27;

  void setup() {
    pinMode(ledPin, OUTPUT);
    pinMode(sensorPin, INPUT);
  }

  void loop() {
    int sensorValue = digitalRead(sensorPin);

    if (sensorValue == HIGH) {
      digitalWrite(ledPin, HIGH); // Turning on LED if the Sensor is "HIGH"
    }
    else {
      digitalWrite(ledPin, LOW); // Turning off LED if the Sensor is "LOW"
    }
  }

Connecting to WiFi

I also managed to connect the ESP32 to a Wifi-Network:

 // Connecting an ESP32 to a WiFi Network
  #include <WiFi.h> 

  // Entering network credentials
  const char* ssid = "SSID";
  const char* password = "PASS";

  void setup(){

    Serial.begin(115200); // You need this Value to find out ESP32's Local IP-Address.

    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);
    Serial.println("\nConnecting to WiFi Network ..");

    while(WiFi.status() != WL_CONNECTED){
        Serial.print(".");
        delay(100);
    }

    Serial.println("\nConnected to the WiFi network");
    Serial.print("Local IP: ");
    Serial.println(WiFi.localIP());
  }

  void loop(){
    // Nothing yet...
  }



back