ESP-02S & optocoupler

Hi all,

I am very new in this ESP/Tasmota-World and actually working on my first little project: converting a solar LED spot with PIR to become smart. As soon as the LED gets ON the little ESP shall send a message to HA so that the system knows it is on (and can initiate some action).

The Hardware works fine: I use a stepdown converter for the battery-power to 3.3V, an optopcoupler (3-5V) to read out if the Spot is ON and last but not least the tasmotized ESP-02S. All components are working.

The optocoupler-module has three pins at the “output”-side which are VCC, GND and OUT. If the “input”-side is ON, the OUT-Pin shows power, if the input-side is OFF Power the OUT-Pin shows 0V. I connected this OUT-Pin to GPIO14 on the ESP.

My issue: which component is to be assigned to the GPIO in Tasmota? I tried several options (Button, Switch etc.) but this results in nothing in best case, the crash of the ESP in the worst case!

Please have a friendly look at the circuit diagram. I can’t see an error in the hardware. So it must be a Tasmota issue.

You would be way better off asking on the Tasmota forums. Although some HA users do use Tasmota, the majority that program their own use ESPHome.

Okay,
throughout the day I found out that the realization with Tasmota will overburden my simple mind …

I have already tasmotised a Tuya Power Plug and this was totally easy with Tasmota. But this project is to complicated for me.

So I tried ESPHome fairly successfully. I was able to migrate from Tasmot to ESPHome and I’m wondering that there is no Web-UI. But YAML-Scripting and Installing on the chip works fine.

I was also able to define the sensor:

#Sensordefinition
binary_sensor:
  - platform: gpio
    name: "Status Spot"
    pin:
      number: GPIO14
      inverted: true
      mode:
        input: true
        pullup: true

After that the sensor was implemented inside HA, but the status is always OFF. As written in my opener:

  • if the “input”-side is ON, the OUT-Pin shows certain Voltage,
  • if the “input”-side is OFF, the OUT-Pin shows 0,00V

(Multimeter-measurement!).

So there might be just a little issue with the definition in the .yaml-file. I already tried “device_class: power” but this was not accepted by the installer.

Any idea how to fix it?

There is, however it’s a bit more memory hungry than the Tasmota one.

Post your full yaml (redacting any passwords) and we will have a look.

esphome:
  name: solarspot1
  friendly_name: Livarno SolarSpot(1)

esp8266:
  board: esp01_1m

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "1234567890#0987654321"

ota:
  - platform: esphome
    password: "top_secret"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Solarspot1 Fallback Hotspot"
    password: "extremely_secret"

captive_portal:
  
#Sensordefinition
binary_sensor:
  - platform: gpio
    name: "Status Spot"
    pin:
      number: GPIO14
      inverted: true
      mode:
        input: true
        pullup: true

That’s all …

Ah - I think I see what you are trying to achieve, you are actually trying to read the voltage on the optto-coupler output?

For that you need an ADC sensor - on an ESP8266 you have to use GPIO17. Also it only reads from 0V to 1.0V.

That’s exactly - not!
The ESP-02S has no GPIO17. And I don’t need the real voltage. I just need to know if there is any voltage or not. So in my opinion this should just be a binary sensor with “YES” or “NO”.

Ok - got confused. So your issue is that the binary sensor is not turning on when you expect it to? For starters, you don’t need that second optocoupler. You can simply use a voltage divider to connect the output of the first device.

Also - why inverted: true? That would mean that the input is considered off when voltage is present and on when zero volts.

The hardware is already installed …

1 = Optocoupler
2 = StepDown Converter
3 = ESP-02S

As you can see in the circuit diagram the optocoupler runs with 3.3V - so the voltage on the OUT-Pin/GPIO is lower (I haven’t measured yet with 3.3V, but i will tomorrow). I actually assume that the voltage is just a bit too low to be recognized by the Chip. What do you think about that?

Sorry, but I have to go to bed now - it’s very late over here.

But thanks for your help. CU tomorrow.

I think - although I haven’t found it in an Espressif data sheet, that HIGH is at least 75% of VCC. So around 2.5V?

Not a common optocoupler, can you post a link or details about it?

Optocouplers:

https://de.aliexpress.com/item/1872072122.html?spm=a2g0o.productlist.main.43.5f78b2Nvb2Nv2x&algo_pvid=f2a2c659-4cbb-4a9d-8f8c-886844c3133f&algo_exp_id=f2a2c659-4cbb-4a9d-8f8c-886844c3133f-21&pdp_ext_f={"order"%3A"1"%2C"eval"%3A"1"}&pdp_npi=4%40dis!EUR!6.41!5.19!!!6.76!5.47!%40213ba74a17435870194376717e7242!12000027718541291!sea!DE!0!ABX&curPageLogUid=NnqUxi5xnu5B&utparam-url=scene%3Asearch|query_from%3A

I have measured 3.37V at OUT when the output is running on the primary circuit. This is battery power between 3.6V and 3.9V (depending on the load). Actually it was 3.8V.

When I put the output on the secondary circuit (stable 3.3V) I receive 3.16V at OUT.

So this should be enough for the ESP to recognize it.

BUT: I did the measurement on a model with identical devices. There might be an issue on the original devices. So I unfortunately have to deconstruct my masterpiece … but not now. I will do this in the evening.


You should have pretty close to VCC on output. For sure it’s recognized.
Output is obviously inverted.

I once did something similar. I brutally replaced the LED light by Esp01, leaving original li-ion battery, charging circuit, MCU and PIR untouched.
When motion was detected it powered Esp (instead of LED), which sent MQTT and went to deep sleep.

You are using pullup and inverted, so the result will always be off.

I couldn’t find this in the docs - just copied from a similar project. Please don’t let me live in darkness …

I’ve never used the esp01_1m, so I don’t know if it will work on that board.

      number: GPIO14
      inverted: true
      mode:
        input: true
        pulldown: true

Or

      number: GPIO14
      inverted: true
      mode:
        input: true
        pullup: false

You don’t need any pull-ups /downs on code, the module has it already. See the image I posted.

Okay, I have uncovered several issues:

  1. One of the three batteries was bad! She lost her load very quickly and so the voltage went down to under 3V - don’t ask me why the system was still working. I have been testing the batteries twice – except the bad one :scream:. Now I have a set of completely new batteries (NiMh) from an identical batch.

  2. With these freshly loaded batteries I have measured the installation inside the lamp (not a test setup). I receive 4.21V in the primary circuit and 3,28V in the secondary circuit. The optocoupler sends 3,28V on the OUT-Pin if the input is OFF and 2,76V if the input is ON. The LED circuit is 2,92V (but that’s enough to switch the optocoupler). The actual sensor definition is:

binary_sensor:
  - platform: gpio
    name: "Status Spot"
    pin:
      number: GPIO14
      inverted: true
      mode:
        input: true

The sensor status in HA is always OFF.
Is ∆V to small? (Or the voltage at ON to high?)

As far as I can see the internal resistor R2 is 10kΩ (I’m not an electronics engineer, i’m just a handyman) .

There is something seriously wrong here. Should be 0 when ON.
If you really have that 3-5V input optocoupler wired to Esp without additional components between them, you have some damaged devices.
Feel free to post a photo.

Why not, Esp is fine down to ~2.8 voltages, maybe even lower.

Fortunately, the devices were so cheap that I bought several of them. I will install another optocoupler and another converter tomorrow. Those from my test setup seemed to work correctly.

Thanks very much so far. As soon as it works I will post complete instructions an photos here. I’m sure that everybody needs such a spot!!! :laughing: