Light sensor using LM393

I have a LM393 sensor hooked up to an ESP32 dev board and flashed it so it shows up on my network. The light sensor seems to be working fine since I can get the LED to go on or off depending on if I cover it or not.

My problem is getting it detected in Home Assistant. I created a sensors.yaml file and also added the line “sensor: !include sensors.yaml” in the configuration.yaml file.

I assume I need to add some lines in both the sensors.yaml file and the yaml file that gets uploaded to the device but I am not sure what I need to add.

Can anyone point me in the right direction please? :slight_smile:

So what code/firmware did you put on the ESP? ESPHome?

I added the ESP32 board using the ESPHome add-on in HA and it generated a yaml file to upload.

HA can see the ESP because I am able to download the logs, etc wirelessly so I think that part is woking correctly.

You have to write additional YAML for your sensor. Not familiar with that specific sensor, but just do some googling and perusing the ESPHOME website and I’m sure you’ll be on your way.

Yea I’ve been googling it with no luck so far. The LM393 is not on the ESP site either…perhaps I need to use a different LDR.

First question - what do you want to do with this sensor?

LM393 breakout boards can have 3 or 4 pins. One will be VCC, one GND and the others A0 and/or D0.

D0 can simply be used as a GPIO input. The turn-on point is adjusted usually by a trimpot on the breakout board.

A0 will be a voltage relative to VCC based on the light level. You can read the voltage using one of the ESPs analogue inputs.

Docs for both are below:

Thanks for the info—I’ll check it out.
I simply want to be able to see if I left the lights on in the garage and thought this would be a fun little project to get my feet wet with the ESP32 board.

My LM393 has 3 pins and seems to be working the way I have it wired up. I adjusted the pot so the LED goes out when I cover the sensor. Just not sure how to get that info into HA.

Wired like this: the VCC is going to the 3.3V pin on the ESP, the ground is going to ground and the DO is going to pin 23. I guessed on the DO port so not sure if that’s correct.

So add this to the end of your yaml:

binary_sensor:
  - platform: gpio
    pin: GPIO23
    name: "Garage Light Sensor"  
    device_class: light

Awesome! Does this go in my sensors.yaml or does it get uploaded to the ESP itself?

Add it to your yaml in ESPHome. Once compiled HA will discover the sensor. You can then use it in your HA automation.

I added those lines and uploaded it to the ESP32 but HA didn’t detect anything…

Go into developer tools, into the states tab, then filter entities starting with garage.

Can you see it there? If not, remove it from the ESPHome integration (NOT the ESPHome add-on) and re-add it.

We are in the dark (lol)… Please post your YAML file for the device (properly formatted for the forum- use the </> button) and a schematic of how you connected it to the ESP32.

The Texas Instruments LM393 is a dual voltage comparator that is used in lots of breakout boards. For example, Flame Sensor, Humidity Sensor, Sound Sensor, Speed Sensor, Vibration Sensor and light sensor.

To ESPHome, it is simply an analog device. The output of a comparator may drive a transistor giving a digital output to the 4th pin. Or not.

I had not added it as an integration so now it shows up! Thanks!

It is opposite though so do I add “inverted: true” to the yaml to reverse that?

I had to invert the pin because it was reading opposite but it is all good now!

The only minor issue I have is that somehow I ended up with 2 friendly names…it shows up as “garage_light_sensor Garage Light Sensor” so I need to remove the first part but when I edit the yaml file it doesn’t like me to remove that…

could you give me the code you are using for your esphome please ? I’ve been struggling for a couple of days to get my lm393 with lightdiode to work properly with my esp8266 (its previously worked fine with an esp32 i was using it for home assistant glow… but i’ve since repurposed that esp device etc so i can’t go back to it) i simply want it to show the amount of light in the format of voltage (and i can do some fancy filters to change that into lux or something)

i’ve also tried it as a binary sensor but it again, I can’t get it to change regardless of what I try)

here is my code



sensor:
  - platform: adc
    pin: GPIO17
    name: "Illuminance"
    device_class: voltage
    unit_of_measurement: v
    update_interval: 5s


binary_sensor:
  - platform: gpio
    pin: GPIO5
    name: "Garage Light Sensor"  
    device_class: light

I just get ON all the time if its connected to the binary sensor pin or 1 if its connected to the ADC pin :frowning:

Some dont have an analog output and some do. Does yours have a DO pin or AO pin? If it has a potentiometer to adjust the brightness level, it most likely doesnt have AO so you cant measure voltage/light level. Those sensors just output a High or Low signal (ON/OFF) and the potentiometer is how you set the brightness threshold inorder for the sensor to send that On/Off signal.

Your code and presumably your wiring are wrong. You ca nt have the DO pin on the sensor going to 2 gpio pins.
It doesnt do Analog so remove the sensor in your code for adc and remove the wire from pin 17.

If the binary sensor still isnt working, first make sure your on the right pin. GPIO5 is D1 on the esp8266. Are you using 3.3v or 5v to power the sensor?

You can make your own light sensors and theyre super easy and it takes 2 components, an LDR and a resistor

See how it has the option for “3 Pin” and “4 Pin”? The 3 pin is only DO - Digital Out - a.k.a ON/OFF. The 4 pin has DO and AO -Analog Out a.k.a voltage level out.

yeah so mines got 3 pins, connected to a0 and it didnt do much. I’ve given up on my esp8266 for now and hooked same thing up to a spare esp32, I now get LED’s lighting up on the sensor when i put light towards it etc. so its now working fine under esphome. So i’m guessing it was something wonky with the 8266.
but yeah i now realize that i would need the 4 pin version to get some kind of analogue output of voltage. which is a shame, but not that big a deal as I can just tweak the pot to the amount of light needed and use that. its currently reversed (light = OFF) so i’ll have to add that inverted: true line i guess

Theres nothing wonky with the esp8266. The most basic function of any esp board is reading and sending digital signals from its gpio. If it wasnt working with tbat board, then you were doing something wrong, which i why i asked those specific questions. Youve got pins mixed up, a problem with how your powering it…