Issues with OLED Screen: How to Fix SSD1306 Error in MicroPython

The above code initializes the SSD1306 OLED display over the I2C interface in MicroPython, ensuring correct wiring, I2C address, and bus speed. It also includes steps to troubleshoot the OSError: [Errno 110] ETIMEDOUT error commonly encountered during setup.

Error Code

codeTraceback (most recent call last):
File "<stdin>", line 10, in <module>
File "/lib/ssd1306.py", line 119, in __init__
File "/lib/ssd1306.py", line 38, in __init__
File "/lib/ssd1306.py", line 75, in init_display
File "/lib/ssd1306.py", line 124, in write_cmd
OSError: [Errno 110] ETIMEDOUT

Resolving the SSD1306 Timeout Error in MicroPython

When working with the SSD1306 OLED display and MicroPython, encountering an error like OSError: [Errno 110] ETIMEDOUT can be frustrating. This error typically points to an issue with the I2C communication between your MicroPython board (e.g., ESP32, Raspberry Pi Pico) and the OLED screen. Here’s a breakdown of common causes and how to fix the problem.

Understanding the Error

The OSError: [Errno 110] ETIMEDOUT indicates that the MicroPython script is unable to establish proper communication with the SSD1306 OLED screen over the I2C protocol. This could occur due to several factors:

  • Incorrect wiring
  • Wrong I2C address
  • I2C bus issues
  • Power supply problems

Steps to Fix the Error

  1. Check the WiringEnsure that the connections between your MicroPython board and the OLED screen are correct. For I2C, the connections should typically be as follows:
    • SDA (Data Pin): Connect to the appropriate data pin on your MicroPython board (often labeled SDA or a GPIO pin).
    • SCL (Clock Pin): Connect to the clock pin on the board (labeled SCL or a GPIO pin).
    • VCC/GND: Make sure the OLED display is properly powered by connecting VCC to a 3.3V/5V pin and GND to the ground pin.
    Double-check the pinouts for your specific board and OLED model, as incorrect wiring can prevent communication and cause timeouts.
  2. Verify the I2C AddressOLED displays typically have an I2C address that the microcontroller needs to communicate with. If you use the wrong address in your code, you’ll likely run into a timeout error.You can scan for the correct I2C address using a simple MicroPython script like this:from machine import I2C, Pin i2c = I2C(1, scl=Pin(22), sda=Pin(21)) # Replace with your SCL and SDA pins devices = i2c.scan() if devices: print('I2C devices found:', devices) else: print('No I2C devices found') This script will output the I2C address of any connected devices. Make sure to use the correct address when initializing the SSD1306 library. Common I2C addresses for the SSD1306 are 0x3C or 0x3D, depending on the model.
  3. Ensure Stable Power SupplyIf the OLED display isn’t receiving adequate power, it may not initialize properly, leading to the ETIMEDOUT error. Check that your board’s power supply can support the display and that the connections are secure. You might want to measure the voltage going to the display to ensure it’s within the correct range (typically 3.3V or 5V).
  4. Adjust I2C Bus Speed (Optional)Some displays might not function properly if the I2C bus speed is too high. Try lowering the speed to see if this resolves the issue. For example:i2c = I2C(1, scl=Pin(22), sda=Pin(21), freq=400000) # Change frequency as needed If you’re still encountering issues, try reducing the frequency to 100kHz (freq=100000).
  5. Software Reset and RetryIn some cases, resetting the I2C bus or the display might help resolve the issue. You can use the following code to reset the I2C bus before attempting to initialize the display again:from machine import SoftI2C, Pin i2c = SoftI2C(scl=Pin(22), sda=Pin(21)) i2c.deinit() # Deinitialize I2C i2c.init() # Reinitialize I2C This simple reset can help clear any issues in the communication line.

Example Code for Proper SSD1306 Initialization

Once you’ve checked the wiring, I2C address, and power, here’s an example of how to properly initialize the SSD1306 display:

codefrom machine import Pin, I2C
import ssd1306

# Initialize I2C
i2c = I2C(1, scl=Pin(22), sda=Pin(21), freq=400000)

# Initialize OLED display
oled = ssd1306.SSD1306_I2C(128, 64, i2c, addr=0x3C)

# Clear display
oled.fill(0)
oled.show()

# Write something on the display
oled.text('Hello, world!', 0, 0)
oled.show()

Make sure to adjust the scl, sda, and addr parameters based on your setup.

Conclusion

The OSError: [Errno 110] ETIMEDOUT error in MicroPython typically stems from issues related to I2C communication with the SSD1306 OLED screen. By verifying your wiring, ensuring the correct I2C address, and checking the power supply, you can resolve the error. Additionally, adjusting the bus speed or resetting the I2C bus can help fix persistent communication issues. With the proper setup, your OLED display should work seamlessly with MicroPython!

Related blog posts