D1 mini and rotary encoder

I’m hoping to link up a rotary encoder, small OLED and servo to a D1 mini. Whilst I’ve used servos and the OLED before, I’ve not done anything with the rotary encoder and seem to be having issues getting that to work at all.

I accidentally got the encoder version without breadboard which seems to have very few tutorials online but so far I’ve soldered the ground pins together and then to the ground pin on the D1 mini. I’ve then connected the 2 pins from the 3 pin side to D1 and D2 on the mini. I’m excluding the final pin for the push button for now just to keep things basic.

Codewise, ESPHome makes it very easy so I only have the below at the moment but I’m not getting any logs when I turn the dial. I have a feeling it’s potentially due to the pins I’ve chosen on the D1 mini (I noticed the breakout version has Vcc but I don’t?) but if it’s not obvious I’ll just buy the breakout board version too!

sensor:
  - platform: rotary_encoder
    name: "Rotary Encoder"
    pin_a: D1
    pin_b: D2
    on_clockwise:
      - logger.log: "Turned Clockwise"
    on_anticlockwise:
      - logger.log: "Turned Anticlockwise"

Any ideas?

Not sure what you mean. Do you have a link to the device?

Sorry, I had so much in front of me when I typed this… I meant to say “version without breakout board” NOT “breadboard”… So the one on the right here…

As far as I can tell (and I am by no means gospel on this) the breakout board has pullup resistors, hence the need for VCC.

So it may be that you need to use pullup: true to enable the internal pullups on the esp. There is an example in the rotary encoder docs Rotary Encoder Sensor — ESPHome

I’ll have to do some reading up on that tomorrow. I’m not overly familiar with the pull up/down stuff but that might be the case. I’m guessing I will need to wire up the 3.3v in some format then, even without the breakout board…

No, just try your config like this

sensor:
  - platform: rotary_encoder
    name: "Rotary Encoder"
    pin_a: 
      number: D1
      inverted: true
      mode:
       input: true
       pullup: true
    pin_b: 
      number: D2
      inverted: true
      mode:
        input: true
        pullup: true
    on_clockwise:
      - logger.log: "Turned Clockwise"
    on_anticlockwise:
      - logger.log: "Turned Anticlockwise"
3 Likes

And it works! That easy eh, thanks! Saves me having to buy some of the other type.

1 Like

Hey,
I used the exact same module from the picture (KY040):

Mine reports reverse values by default for the button and for the steps too.

With this you can:
1: Invert the pins if the sensor reports false by default
2: Use the button of the encoder to reset it and as a momentary button for other actions.

  # Rotary Encoder:
  - platform: rotary_encoder
    id: rot_enc
    name: "Rotary Encoder"
    pin_a: #CLK Pin
      number: YOURGPIOHERE
      inverted: False #If the module reports reverse values, invert ONE of the pins.
    pin_b:  #DT Pin
      number: YOURGPIOHERE
      inverted: False #If the module reports reverse values, invert ONE of the pins.
    pin_reset: #SW pin
      number: YOURGPIOHERE
      inverted: False
      allow_other_uses: True  #This is needed to share pin betwen functions: https://esphome.io/guides/configuration-types#config-pin-schema
    unit_of_measurement: "Steps"
    on_clockwise:
      - logger.log: 
          level: INFO
          tag: "Rotary Encoder"
          format: "Turned Clockwise"
    on_anticlockwise:
    - logger.log: 
        level: INFO
        tag: "Rotary Encoder"
        format: "Turned CounterClockwise"
...
binary_sensor:
  # Rotary button:
  - platform: gpio
    pin:
      number: YOURGPIOHERE
      inverted: True
      allow_other_uses: True  #This is needed to share pin betwen functions: https://esphome.io/guides/configuration-types#config-pin-schema
    id: rot_bttn
    name: "Rotary Enc Button"
    on_state: 
      then:
        - logger.log:
            level: INFO
            tag: "Rotary Encoder Button"
            format: "Rotary Encoder Button Pressed!"

Question about the module for those who have it:
Notice on the bottom, you are missing a resistor too by default after SW.
The SW pin connects to the encoder but it has a trace to the next pads which through the already installed middle resistor, joins the VCC track.
IRL mine has the bottom two vertical resistors bridging DT and CLK to VCC.
Some pictures and replicas of the module online show 10k printing around the pads.
(The vertical pads after SW pin on the top.)
ky040
Wondering if we can achieve extra functionality by adding one? Thoughts? (Ex.: if SW is A0 then we get a momentary ADC measurement on it too if that could be useful in any case, etc…no?) or is it just for the pulll up/down only as mentioned here: