LED Sensor
Intro
Here is how I implemented some photo transistors to read the LEDs on my pool salt water generator (SWG).
I have a Hayward that's in a metal case. Made it easier since the case blocks out all ambient light. So the photo transistors see no light, or the light from the LED. The ones I picked also fit just right in the holes in front of the LEDs.
Theory
Its fairly simple. A photo transistor (PT) generates current then it is exposed to light. I picked one below that works with visible light (as opposed to IR). The current creates a voltage across a resistor.
The larger the value used for the resistor, the higher the voltage drop. This means a larger resistor makes the circuit more sensitive to light. I tweaked the value, started with 10k and ended at 56k.
I used an ESP32 running ESPHome to read the PT output as a binary sensor.
With the circuit I have, I get about 0.08 VDC from PT when off and at least 2.8 VDC when PT is on. ESP32 switches at 1.7V (per someone who measured) Guaranteed values are 0.825V low and 2.475V high. So I can connect to a normal GPIO, and read as a binary sensor. Makes it much easier to deal with in Home Assistant and Node Red.
I had started by trying to feed to PT output voltage into an Analog to Digital Converter (ADC), then would read the value, and use that to determine if the LED is on or not. The ESP32 has a bunch of ADC inputs, but I was only able to use four of them with esphome. Others I tried to use are shared with the Wi-Fi radio, so they didn't work. So I stopped this because I wanted to read more then 4 LEDs.
The ESP32 reads the outputs as binary sensors and sends the data via MQTT. I use Home Assistant(HA) and Node Red (NR) to receive the data. HA displays if the LEDs are on or off. NR sends me a text if there is something I need to know about.
So LED -> Photo transistor -> ESP32 -> MQTT -> HA & NR
Parts
TEPT5600 photo transistor Mouser link Datasheet. Vishay App note. Cheaper unit price if you buy 10
ESP32 I used a mini D1 form factor ESP32.
56k resistor exact value is not critical, and you might need to tweak this value
Cat 6 ethernet cable Used to wire between photo transistors and ESP32 board. Was able to use 17 feet of wire between the esp and photo transistors, so you can go pretty far.
Protoboards for mounting
usb power plug and usb cable used to power the ESP32
wire and heat shrink
ESPHome yaml file info for binary sensor:
binary_sensor:
- platform: gpio
pin:
number: 32
name: "SWG Power LED gpio"
- platform: gpio
pin:
number: 35
name: "SWG Generating LED gpio"
- platform: gpio
pin:
number: 33
name: "SWG Flow LED gpio"
- platform: gpio
pin:
number: 34
name: "SWG Check LED gpio"
- platform: gpio
pin:
number: 18
name: "SWG Low Salt LED gpio"
- platform: gpio
pin:
number: 19
name: "SWG Hi Salt LED gpio"
MQTT Topics
Naming the ESP in ESPHome
esphome:
name: esphome_swgled
Then it broadcasts over MQTT with topic:
esphome_swgled/binary_sensor/swg_power_led_gpio/state
esphome_swgled/binary_sensor/swg_generating_led_gpio/state
esphome_swgled/binary_sensor/swg_flow_led_gpio/state
esphome_swgled/binary_sensor/swg_check_led_gpio/state
esphome_swgled/binary_sensor/swg_low_salt_led_gpio/state
esphome_swgled/binary_sensor/swg_hi_salt_led_gpio/state
ADC Info
I tried using ADC first, but figured out it was easier to make them binary sensors, and I only get 4 working ADCs so stopped using this.
ESPHome yaml file info for ADC input
- platform: adc
pin: 32
name: "SWG Power LED"
update_interval: 60s
attenuation: auto
- platform: adc
pin: 35
name: "SWG Generating LED"
update_interval: 60s
attenuation: auto
- platform: adc
pin: 33
name: "SWG Flow LED"
update_interval: 60s
attenuation: auto
- platform: adc
pin: 34
name: "SWG Check LED"
update_interval: 60s
attenuation: auto
MQTT Topics for ADC
Naming the ESP in ESPHome
esphome:
name: esphome_swgled
Then it broadcasts over MQTT with topic:
esphome_swgled/sensor/swg_power_led/state
Other MQTT topics are
esphome_swgled/sensor/swg_generating_led/state
esphome_swgled/sensor/swg_flow_led/state
esphome_swgled/sensor/swg_check_led/state
Photo transistor connections
tept5600 collector on notch side, emitter on round side
tept5600-collector to 3.3V
tept5600-emitter to GPIO
56 k resistor GPIO to ground
Larger resistor makes the circuit more sensitive to light. Started with 10k but not sensitive enough so increased to 56k. Anything around that should be fine, its what I picked from my junk drawer.
You can use a DVM to measure the voltage at the emitter. It should change with changes in lighting.
I used a 56k resistor instead of the 10k resistor shown here.
I used CAT6 ethernet cable to connect from the ESP32 to the photo transistors.
Mounted 4 photo transistors and four 56k resistors on a little perf board from my junk drawer.
Heatshrink on photo transistors leads so they don't short.
ADC (More info)
Only 4 ADCs are available with ESPHome to use with my ESP32
ADC1_CH4 (GPIO 32) far left
ADC1_CH7 (GPIO 35) far right
ADC1_CH5 (GPIO 33) far right
ADC1_CH6 (GPIO 34) far right
Mounted ESP32 on a larger perf board. I installed pins on the ESP32 board. Used a Mini D1 form factor board.
For the inner row, they didn't include with board so I found pins and sockets in junk drawer. I have ones from ESP8266 boards that don't include last two pins, so inner rows are 2 pins shorter. I don't need those pins for this, and it makes it obvious how to plug in board.
Installed sockets on the perfboard so the ESP32 can plug into it, then connected other end of CAT6 cable to it.
Also connect power supply 5V to 5VP pin, and ground. Can use a USB power adapter.
All solid color wires are tied to ground. Better noise immunity if 1/2 of every pair is ground.
I have a separate yellow white wire to connect 3.3V from the ESP32 board to the photo transistor board
When I changed from using ADC's to binary sensors, I kept most of the wiring the same because I could so it was easier.
GPIO 32 - blue striped Power LED
GPIO 35 - green striped Generating LED
GPIO 33 - orange striped Flow LED
GPIO 34 - brown striped Check LED
Added these too:
GPIO 18 - blue striped Low Salt LED
GPIO 19 - green striped High Salt LED
rear side of mini d1 ESP32 board
For power, I used a short micro usb cable to a usb power plug.
Home Assistant
Added to binary sensor include yaml file to convert value to true or false
Used 1.5 because its middle of 3 and 0.
binary_sensor:
- platform: template
sensors:
SWG_Gen_On:
friendly_name: "SWG Generating On"
value_template: >
{% set gen = states('sensor.swg_generate_led')|float %}
{{ (gen > 1.5) }}
Install pics
Photo transistors are just the right size to fit in LED holes. You can still see the LEDs through the PTs.
Troubleshooting
8/23
Not sure if its related, but a couple days after Tropical Storm Hilary, power led sensor said always on, so need to figure out what is wrong.
Started at ESP32 end,
ESP32 wiring
GPIO 32 - blue striped Power LED
GPIO 35 - green striped Generating LED
GPIO 33 - orange striped Flow LED
GPIO 34 - brown striped Check LED
I have a separate yellow white wire to connect 3.3V from the ESP32 board to the photo transistor board
GPIO 18 - blue striped Low Salt LED
GPIO 19 - green striped High Salt LED