Esp8266 with Buttons and rotary encoder

I need to learn some stuff about wiring esp8266’s.
I have a hard time finding exact wiring diagrams for this case.

I want to build a controller with 5 buttons and a rotary encoder.

Hardware: Esp8266 d1 mini
gpio expander pcf8574
Rotary encoder hw040
Arcade buttons momentary push 5x

First i wired without any resistors at all:
3v through all buttons and into ports on the expander 1-5.
nothing worked. i noticed that i hade 3v on the expander pins when they where disconnected from the buttons.

i2c:
  sda: GPIO4
  scl: GPIO5
  scan: true
  id: bus_a

pcf8574:
  - id: 'pcf8574_hub'
    address: 0x20
    pcf8575: false

substitutions:
  device_name: volume_rotary_encoder
  friendly_name: Volume rotary encoder
# Button 1 = pcf8574_hub pin: 1
# Button 2 = pcf8574_hub pin: 2
# Button 3 = pcf8574_hub pin: 3
# Button 4 = pcf8574_hub pin: 4
# Button 5 = pcf8574_hub pin: 5

binary_sensor:
  # VOLUME KNOB BUTTON ----------------------
  - platform: gpio
    pin:
      number: GPIO15
      #mode: INPUT_PULLUP
      inverted: false
    name: "${friendly_name} Volume Switch Button"
    on_press:
      - homeassistant.service:
          service: media_player.play_pause
          data:
            entity_id: media_player.xxx
  # Button 1 ----------------------
  - platform: gpio
    pin:
      pcf8574: pcf8574_hub
      # Use pin number 1
      number: 1
      mode: INPUT
      inverted: false
    name: "${friendly_name} Button 1"
    on_press:
      - logger.log: "Button 1"
      - homeassistant.service:
          service: media_player.play_pause
          data:
            entity_id: media_player.xxx
  # Button 2 ----------------------

  # Button 3 ----------------------

  # Button 4 ----------------------

  # Button 5 ----------------------

# Rotary Encoder ----------------------
sensor:
  - platform: rotary_encoder
    name: "${friendly_name} Rotary Encoder"
    pin_b:
      number: GPIO12
      mode: INPUT
    pin_a:
      number: GPIO14
      mode: INPUT
    on_clockwise:
      - homeassistant.service:
          service: media_player.volume_set
          data_template:
            entity_id: media_player.xxx
            volume_level: "{{ state_attr('media_player.xxx', 'volume_level') + 0.02 }}"
      - logger.log: "Turned Clockwise"
    on_anticlockwise:
      - homeassistant.service:
          service: media_player.volume_set
          data_template:
            entity_id: media_player.xxx
            volume_level: "{{ state_attr('media_player.xxx', 'volume_level') - 0.02 }}"
      - logger.log: "Turned Anti Clockwise"
      

After this I tried to learn how to connect the things with resistors (pull-ups).
And maby change the buttons to GND instead of 3v.

I made this new wiring diagram. Do you think this can work?

No. You have connected the pull-up resistors to the wrong side of the buttons. They should connect to the side of the button that joins to your pcf8574. That way the inputs are normally high until the button is pressed then they are shorted to ground.

Also you will need:

inverted: true

for each button input.

The other way to do it is to put the resistors in the same place (on the pcf8574 side of the button) but have the other ends of the resistors connected to ground. Then have the buttons connect to 3.3V instead of ground. That way you won’t need inverted: true. There’s not really any major advantage of doing it one way or the other.

Thank you!

I have made a revised edition if you have the time to look again.

And I read that i could need a capasitor to de-couple something. Can you explain what that does?

Yep, that looks good.

1 Like

I will try this and come back.

Here is the final circuit. It all works fine now. Thank you @tom_l

and here is the code:

i2c:
  sda: GPIO4
  scl: GPIO5
  scan: true
  id: bus_a

pcf8574:
  - id: 'pcf8574_hub'
    address: 0x20
    pcf8575: false


# Button 1 = pcf8574_hub pin: 1
# Button 2 = pcf8574_hub pin: 2
# Button 3 = pcf8574_hub pin: 3
# Button 4 = pcf8574_hub pin: 4
# Button 5 = pcf8574_hub pin: 5
# rotary encoder button = pcf8574_hub pin: 6
binary_sensor:

  # Button 1 ----------------------
  - platform: gpio
    pin:
      pcf8574: pcf8574_hub
      # Use pin number 1
      number: 1
      mode: INPUT
      inverted: true
    name: "Button 1"
    on_press:
      - logger.log: "Button 1"
      - homeassistant.service:
          service: media_player.play_pause
          data:
            entity_id: media_player.xxx
  # Button 2 ----------------------
  - platform: gpio
    pin:
      pcf8574: pcf8574_hub
      # Use pin number 2
      number: 2
      mode: INPUT
      inverted: true
    name: "Button 2"
    on_press:
      - logger.log: "Button 2"
      - homeassistant.service:
          service: media_player.play_pause
          data:
            entity_id: media_player.xxx
  # Button 3 ----------------------
  - platform: gpio
    pin:
      pcf8574: pcf8574_hub
      # Use pin number 3
      number: 3
      mode: INPUT
      inverted: true
    name: "Button 3"
    on_press:
      - logger.log: "Button 3"
      - homeassistant.service:
          service: media_player.play_pause
          data:
            entity_id: media_player.xxx
  # Button 4 ----------------------
  - platform: gpio
    pin:
      pcf8574: pcf8574_hub
      # Use pin number 4
      number: 4
      mode: INPUT
      inverted: true
    name: "Button 4"
    on_press:
      - logger.log: "Button 4"
      - homeassistant.service:
          service: media_player.play_pause
          data:
            entity_id: media_player.xxx
  # Button 5 ----------------------
  - platform: gpio
    pin:
      pcf8574: pcf8574_hub
      # Use pin number 5
      number: 5
      mode: INPUT
      inverted: true
    name: "Button 5"
    on_press:
      - logger.log: "Button 5"
      - homeassistant.service:
          service: media_player.play_pause
          data:
            entity_id: media_player.xxx
  # rotary encoder button -------------------
  - platform: gpio
    pin:
      pcf8574: pcf8574_hub
      # Use pin number 6
      number: 6
      mode: INPUT
      inverted: true
    name: "rotary encoder button"
    on_press:
      - logger.log: "rotary encoder button"
      - homeassistant.service:
          service: media_player.play_pause
          data:
            entity_id: media_player.xxx
# Rotary Encoder ----------------------
sensor:
  - platform: rotary_encoder
    name: "Rotary Encoder"
    pin_b:
      number: GPIO12
      mode: INPUT
    pin_a:
      number: GPIO14
      mode: INPUT
    on_clockwise:
      - homeassistant.service:
          service: media_player.volume_set
          data_template:
            entity_id: media_player.xxx
            volume_level: "{{ state_attr('media_player.xxx', 'volume_level') + 0.02 }}"
      - logger.log: "Turned Clockwise"
    on_anticlockwise:
      - homeassistant.service:
          service: media_player.volume_set
          data_template:
            entity_id: media_player.xxx
            volume_level: "{{ state_attr('media_player.xxx', 'volume_level') - 0.02 }}"
      - logger.log: "Turned Anti Clockwise"
      
1 Like

There isn’t an “exact” wiring diagram to follow and the reason why is, there are multiple ways to wire and use a button/switch and whichever way you choose, yyou need to make sure you configure your gpio sl that it knows what kind of Digital signal it will be watching for.

Rewarding the use of pullup/pull dowb etc is very important to understand and this concept will be used everywhere when your making circuits so, i got you something to read and i highly recommend doing that.

https://learn.sparkfun.com/tutorials/button-and-switch-basics

1 Like

Thank you!

No problem, just dont be late paying my bill when it comes in the mail!! ; )