r/learnpython • u/oklopo • 1d ago
Encoder issue in Micropython/Microcontroller
I'm building my first project, starting with the Wokwi ESP32 simulator. My program was working until I tried adding a rotary encoder. I keep getting errors. Here's the code and error message. The ssd1306.py file is standard. I've tried changing things over to SoftI2C but was still having issues.
I took the encoder code from here.
Error:
Warning: I2C(-1, ...) is deprecated, use SoftI2C(...) instead
Traceback (most recent call last):
File "main.py", line 27, in <module>
File "ssd1306.py", line 116, in __init__
File "ssd1306.py", line 36, in __init__
File "ssd1306.py", line 61, in init_display
File "ssd1306.py", line 121, in write_cmd
OSError: [Errno 19] ENODEV
from machine import Pin, I2C
import ssd1306
import network
import urequests
import time
import random
# ESP32 Pin assignment
i2c = I2C(scl=Pin(22), sda=Pin(21))
button_pin = Pin(27, Pin.IN, Pin.PULL_UP)
direction_pin = Pin(26, Pin.IN, Pin.PULL_UP)
step_pin = Pin(25, Pin.IN, Pin.PULL_UP)
# I2C variables
ID = 0
SDA = Pin(0)
SCL = Pin(1)
i2c = I2C(scl=SCL, sda=SDA)
# for tracking the direction and button state
previous_value = True
button_down = False
#oled details
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
3
u/socal_nerdtastic 1d ago edited 1d ago
Why are you defining
i2ctwice? The rotary encoder isn't I2C is it? So just remove thei2cdefinition that does not match the reality of where you have the display connected. As a protip: always try to use the native I2C pins for I2C, the ones that are labeled as SDA and SCL on the ESP32 pinout diagram.Show us your schematic please.
2
u/SigridFischer42 23h ago
You have two i2c = I2C(...) lines with different pins, which can confuse the hardware, pick one set of pins (usually 21 for SDA and 22 for SCL on ESP32) and delete the other.