kilerin
kilerin

Reputation: 3

ESPHome YAML configuration to control the built-in LED of a mercury switch sensor (turn it on/off)

I've hooked up a mercury tilt sensor (as the one reported in https://microcontrollerslab.com/mercury-tilt-switch-interfacing-pic/) to an ESP32. I want the built-in LED of the mercury switch sensor to turn ON when the sensor is in the ON state, and turn OFF when the sensor is in the OFF state.

This is my YAML configuration:

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO4
      mode: INPUT_PULLUP
      inverted: true
    name: "Mercury Switch"
    on_press:
      then:
        - logger.log: "Mercury Switch Turned On!"
        - output.turn_on: builtin_led
    on_release:
      then:
        - logger.log: "Mercury Switch Turned Off!"
        - output.turn_off: builtin_led

output:
  - platform: gpio
    pin: GPIO13
    id: builtin_led

Currently, the sensor correctly detects the ON and OFF states, but the LED stays ON all the time.

Basically I want the ESPHome code version of this PlatformIO code.

#include <Arduino.h>

// Definizione dei pin
const int mercurySwitchPin = 4;
const int ledPin = 13;         

void setup() {
  // Configura i pin
  pinMode(mercurySwitchPin, INPUT_PULLUP); 
  pinMode(ledPin, OUTPUT);                
}

void loop() {
  int switchState = digitalRead(mercurySwitchPin);
  if (switchState == LOW) {
    digitalWrite(ledPin, HIGH); 
  } else {
    digitalWrite(ledPin, LOW); 
  }
}

Upvotes: 0

Views: 61

Answers (0)

Related Questions