r/learnpython • • 7d 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 Upvotes

4 comments sorted by

View all comments

Show parent comment

1

u/Sehzat_Davoyan 4d ago

aren't pins 0 and 1 usually tied up with boot and serial stuff on those boards anyway?

1

u/socal_nerdtastic 4d ago

Generally not, no. There's usually an enable / reboot pin, but there's no logic to what pin number it corresponds to, it's different for every board. And usually you can use repurpose it for other things too if you don't want the enable / reboot function, although that involves some extra thought and careful connections. Every MCU (eg the ESP32) has many breakout boards that use it, and it's up the board designer what pin to route where, or whether to route them at all.