ESPHome: coin collector as what (binary_sensor, sensor, pulse_counter, ...)?

Hi everybody,

I have a CH-926 coin collector which I am trying to integrate to my Home Assistant via ESPHome.

Does anybody here have experience with this kind of device and can help me integrate it in Home Assistant?

The device itself has

  • 12V
  • GND
  • COIN
  • COUNT

pins. I have read you have to connect ground to both your 12V power supply’s ground and your arduino (or in this case, ESP8266). I only tested this with the arduino, and when I do, the device will not power on. So I did in later tests not connect GND to anything other than the 12V power supply.

I have tried these three different ways to integrate it via ESPHome so far

 sensor:
   - platform: adc
     pin: A0
     name: "Coin Collector"
     update_interval: 1s
# OR
 sensor:
   - platform: pulse_counter
     pin: D5
     name: "Pulse Counter"
# OR
binary_sensor:
  - platform: gpio
    pin:
      number: D5
      mode: INPUT_PULLUP
    name: "Input Pullup"
    filters:
      - delayed_on: 2s
      - delayed_off: 2s

I have tried both COIN and COUNT for this, as I wasn’t sure which to use. Neither of these will provide reliable output. Sometimes, it will automatically switch between ON and OFF when I am not doing anything at all, other times it will not switch when I actually insert a coin.

I have also tried putting different resistors in between the ouput (COIN or COUNT) and A0/D5. This will sometimes produce less output, at other times no output at all.

Unfortunately, there isn’t much documentation that I can work with. I have found this sample arduino code, which will work on an Arduino MEGA 2560, well, at least to some extend. It does actually count inserted coins, though it will not reliably identify which coin had been inserted (though this might be configurable when setting up the coin collector).

I read that you can somehow run “generic” arduino code in ESPHome, but I have not the slightest clue on how to do this. Does anybody here and would help me out?

I would at least have this as a binary_sensor entity reporting when any coin has been inserted. If possible, it’d be nice to report which coin had been inserted. So far, I trained the machine to identify three different types of coin (50c, 1€, 2€). This is all just for fun, so the any coin option would already be great, but if possible, I’d like to report the value just for completeness’ sake.

Thank you for your ideas :slight_smile:

1 Like

Update

After forgetting about this project, I gave it another shot today.

What works / needs to be done?

  • [x] grab impulse when (trained!) coin has been inserted into coin slot [2022-03-04]
  • [ ] identify what value coin has been inserted
  • [ ] count total value in ESPHome (or Home Assistant)
  • [ ] reset total value

Hardware

  • nodeMCU v2
  • nodeMCU Breakout board (for 12V power supply for both nodeMCU and CH-926 coin collector)

Code

esphome:
  name: testcoinslot
  friendly_name: 00/Test/Coinslot

esp8266:
  board: nodemcuv2


# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "asdasdasdsad"

ota:
  password: "asdasdasdsad"

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

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

captive_portal:

binary_sensor:
  - platform: gpio
    pin: D4
    name: "Counter"

Steps

ESPHome

  • upload code
  • add device to Home Assistant

You will now have something like this

(please ignore the Coin entity; it does not exist any longer and was from a previous test)

CH-926

So far, I have only trained one single coin (€ 1).

Connect

  • nodeMCU v2 > nodeMCU breakout board
  • CH-926 DC12V > nodeMCU breakout board V1
  • CH-926 GND > nodeMCU breakout board GND
  • CH-926 COUNTER > nodeMCU breakout board D4

TODO

see above;

Counting and resetting the value will be easy once identifying the coin value has been done.

According to the guide I linked above, you can set (train?) and receive a different number of pulses per coin value. So can we utilize pulse counter sensor for this?

When I tested this, I had a constant of 69 pulses (nice) when nothing was going on. That was the Count entity from above that had been deleted in the meantime. While writing this, I realize that perhaps this constant pulse output was caused by using NC state (set on the CH-926) instead of NO. I will try again and report back later.

I added this

sensor:
  - platform: pulse_counter
    pin: D7
    name: "Pulse Counter"
    update_interval: 1s

This would work at first. When nothing happened, it would display 0,00 pulses/min. When I inserted my trained coin, it would switch to a value between 59 and 61 and reset right away.

This is already not great: when inserting the coin just at the wrong time, the pulse would not be counted (because it would reset in that very moment you’d insert it). The binary_sensor, which would trigger whenever a coin was inserted still works.

I then changed to update_interval: 2s. Still, not working correctly. The first time I inserted a coin, it would count 29,9 pulses/min; then any additional coin would not be counted at all. It would stay at 0,00 pulses/min.

Is there a better way to catch the pulses? In this case, we don’t want pulses per minute at all. We want to get the pulses that happen whenever a coin is inserted, then a very quick reset (because people would probably insert coins quite quickly and not wait x seconds before inserting the next coin), then get the next coin’s pulses, then reset, etc.


Update

I have tried pulse_counter, pulse_meter, and pulse_width; no success. It seems like they will register some value once, then not change at all.

Then I changed the D7 pin to

  - platform: gpio
    pin:
      number: D7
    name: "Pulse"

Hoping that we could just see how many times this pin would toggle its status. This does not work: it stays off at all times.