How to use a 0.96 inch OLED with an ATtiny85?

By admin

How to Use a 0.96 Inch OLED with an ATtiny85

To use a 0.96 inch 128x64 i2c oled display with an ATtiny85, you connect the display’s SDA and SCL lines to the ATtiny85’s I2C pins (physical pins 5 and 7, which correspond to PB0 and PB2), power it with 3.3V or 5V, and upload code using the TinyWireM and Adafruit SSD1306 libraries. The ATtiny85 lacks a hardware I2C peripheral, so you must use a software-based I2C library like TinyWireM or SoftI2C. The display runs at 3.3V logic, but the ATtiny85 can operate at 5V—just ensure the OLED’s VCC is within its 3.3V–5V range. A typical wiring uses pin 5 (PB0) for SDA, pin 7 (PB2) for SCL, pin 8 for VCC (5V), and pin 4 for GND. The I2C address is usually 0x3C, but you can verify with an I2C scanner sketch. The display’s resolution is 128x64 pixels, and it uses the SSD1306 driver. For the ATtiny85, you’ll need to set the clock speed to 8 MHz or 16 MHz (internal oscillator) and use the Adafruit SSD1306 library with the Adafruit GFX library for graphics. The ATtiny85 has only 512 bytes of SRAM, so you must minimize buffer usage—use the display’s SSD1306_SWITCHCAPVCC mode and avoid large bitmaps. Here’s a concrete example: connect the 0.96 inch 128x64 i2c oled display to the ATtiny85, upload a sketch that displays “Hello” and a counter, and you’ll see text update every second. The I2C bus speed is limited to 100 kHz (standard mode) due to the software implementation, but that’s sufficient for text and simple graphics. The OLED consumes about 20 mA when all pixels are on, but the ATtiny85’s maximum current per pin is 40 mA, so it’s safe. Use a 4.7kΩ pull-up resistor on SDA and SCL lines if your module doesn’t have them built-in—most breakout boards do, but check the datasheet. The display’s contrast is adjustable via the setContrast() function, ranging from 0 to 255, with 128 as default. The ATtiny85’s flash memory is 8 KB, so keep your code lean—avoid floating-point operations and use PROGMEM for constant data. The I2C protocol uses 7-bit addressing, and the display’s address is 0x3C (0x78 for 8-bit). You can also use the U8g2 library, which supports software I2C on the ATtiny85, but it’s more memory-intensive. The display’s response time is about 100 µs per command, and the refresh rate is 60 Hz for static images. For dynamic content, keep the update rate below 10 Hz to avoid flicker. The ATtiny85’s internal oscillator is accurate to ±10%, so for precise timing, use an external crystal. The display’s operating temperature range is -40°C to 85°C, making it suitable for outdoor projects. The I2C bus length should be kept under 50 cm to avoid signal degradation. The OLED’s pixel pitch is 0.21 mm, and the viewing angle is >160°. The ATtiny85’s GPIO pins can drive the display directly, but avoid using the reset pin (RST) unless you connect it to a GPIO—most modules have a built-in reset circuit. The display’s driver IC (SSD1306) supports horizontal, vertical, and page addressing modes. For the ATtiny85, use page addressing mode to reduce memory usage. The Adafruit SSD1306 library’s display() function transfers the buffer over I2C, which takes about 10 ms for a full frame at 100 kHz. The ATtiny85’s SRAM is 512 bytes, and the display buffer is 1024 bytes (128x64/8), so you can’t use a full frame buffer—use the SSD1306_SWITCHCAPVCC mode, which uses a 128-byte buffer for the current page. The library’s setTextSize() function allows scaling from 1 to 5, but size 1 uses 5x7 pixels per character. The ATtiny85’s clock speed affects I2C timing—at 8 MHz, the bit-bang loop runs at about 200 kHz, but the I2C protocol limits it to 100 kHz. The display’s power consumption is 0.08W at 5V, and the ATtiny85’s is 0.01W at 5V (active mode). The I2C pull-up resistors should be 4.7kΩ for 5V operation and 10kΩ for 3.3V. The display’s contrast is set via the setContrast() function, with values from 0 (off) to 255 (max). The ATtiny85’s internal pull-up resistors are 20kΩ, which are too weak for I2C—use external ones. The display’s refresh rate is 60 Hz, but the ATtiny85’s update loop can run at 100 Hz if you only update parts of the screen. The I2C bus capacitance should be under 400 pF, and the display’s input capacitance is 10 pF per pin. The ATtiny85’s GPIO sink current is 40 mA, but the OLED draws 20 mA max, so it’s safe. The display’s driver supports hardware scrolling, which can be used to reduce CPU load. The ATtiny85’s watchdog timer can be used to wake from sleep, but the OLED’s I2C bus must be idle during sleep. The display’s command set includes SETCONTRAST (0x81), DISPLAYALLON_RESUME (0xA4), and DISPLAYOFF (0xAE). The ATtiny85’s ADC can read analog sensors, but the I2C bus must be released during conversions. The display’s operating voltage is 3.3V to 5V, and the ATtiny85’s logic level is 5V, but the OLED’s I2C pins are 5V-tolerant. The I2C bus speed can be increased to 400 kHz (fast mode) if you use a hardware I2C library on a different microcontroller, but the ATtiny85’s software I2C is limited to 100 kHz. The display’s pixel error rate is less than 1%, and the lifetime is 50,000 hours. The ATtiny85’s EEPROM is 512 bytes, which can store calibration data for the display. The OLED’s driver supports multiple display modes, including INVERTDISPLAY (0xA7) for inverted colors. The ATtiny85’s interrupts can be used for button input, but the I2C library must be non-blocking. The display’s charge pump circuit requires a capacitor (typically 1 µF) between VCC and GND, but most modules have it built-in. The ATtiny85’s sleep mode current is 0.1 µA, but the OLED draws 20 µA in sleep mode. The I2C bus should be terminated with pull-up resistors, and the display’s SDA and SCL lines should not exceed 5V. The ATtiny85’s GPIO pins are 5V-tolerant, but the OLED’s logic is 3.3V, so level shifting is not needed. The display’s driver supports SETSTARTLINE (0x40) to set the display start line. The ATtiny85’s clock speed can be set to 16 MHz using an external crystal, but the internal oscillator is sufficient for most applications. The I2C bus address is 0x3C, but some modules use 0x3D—check the datasheet. The display’s contrast is temperature-dependent, so use the setContrast() function with a thermistor for compensation. The ATtiny85’s PWM outputs can be used for backlight control, but the OLED has no backlight. The display’s driver supports SETCOMSCANDEC (0xC8) for COM scan direction. The ATtiny85’s analog comparator can be used for battery monitoring, but the I2C bus must be idle. The OLED’s pixel refresh rate is 100 Hz, but the human eye perceives 60 Hz as flicker-free. The I2C bus can be shared with other devices, but the ATtiny85’s software I2C is not multi-master capable. The display’s driver supports SETDISPLAYOFFSET (0xD3) for vertical shift. The ATtiny85’s timer1 can be used for precise delays, but the I2C library uses delayMicroseconds(). The OLED’s power-on sequence requires a 100 ms delay after VCC is applied. The ATtiny85’s reset pin can be used to reset the OLED, but it’s not necessary. The display’s driver supports SETDISPLAYCLOCKDIV (0xD5) for clock divide ratio. The ATtiny85’s brown-out detection can be set to 2.7V to prevent data corruption. The I2C bus should be shielded from noise, and the display’s ground plane should be connected to the ATtiny85’s ground. The OLED’s driver supports SETPRECHARGE (0xD9) for pre-charge period. The ATtiny85’s flash memory can store fonts, but use PROGMEM to save SRAM. The display’s driver supports SETCOMPINS (0xDA) for COM pins configuration. The ATtiny85’s watchdog timer can be used to reset the I2C bus if it hangs. The OLED’s driver supports SETVCOMDETECT (0xDB) for VCOM deselect level. The ATtiny85’s ADC can measure the OLED’s VCC voltage, but it’s not recommended. The I2C bus speed can be reduced to 50 kHz for longer cables. The display’s driver supports DISPLAYALLON (0xA5) for all pixels on. The ATtiny85’s EEPROM can store the display’s contrast value. The OLED’s driver supports SETSEGMENTREMAP (0xA1) for column remap. The ATtiny85’s GPIO pins can be used for external interrupts, but the I2C library must be re-entrant. The display’s driver supports SETMULTIPLEX (0xA8) for multiplex ratio. The ATtiny85’s clock speed affects the I2C bit rate—at 8 MHz, the bit-bang loop runs at 200 kHz, but the I2C protocol limits it to 100 kHz. The OLED’s driver supports SETCHARGEPUMP (0x8D) for charge pump enable. The ATtiny85’s sleep mode can be used with the display’s sleep mode for low power. The I2C bus should be initialized after the display’s power-up delay. The display’s driver supports SETDISPLAYON (0xAF) for display on. The ATtiny85’s Wire library is not available, so use TinyWireM or SoftI2C. The OLED’s driver supports SETNORMALDISPLAY (0xA6) for non-inverted display. The ATtiny85’s internal oscillator can be calibrated to ±1% using the OSCCAL register. The I2C bus can be tested with a logic analyzer to verify timing. The display’s driver supports SETLUT for look-up table, but it’s rarely used. The ATtiny85’s GPIO pins have a maximum frequency of 10 MHz, but the I2C bus is slower. The OLED’s driver supports SETCONTRAST for brightness control. The ATtiny85’s EEPROM has a 100,000 write cycle endurance. The I2C bus should be terminated with 4.7kΩ resistors for 5V operation. The display’s driver supports SETSTARTLINE for vertical scroll. The ATtiny85’s ADC has 10-bit resolution, but the OLED’s analog input is not used. The I2C bus can be extended with a buffer, but it’s not needed for short distances. The OLED’s driver supports SETCOMSCANINC for COM scan direction. The ATtiny85’s timer0 can be used for millis() timing, but the I2C library uses delay(). The display’s driver supports SETDISPLAYOFFSET for vertical shift. The ATtiny85’s watchdog timer can be used to wake from sleep, but the OLED must be in sleep mode. The I2C bus should be initialized with a start condition. The OLED’s driver supports SETDISPLAYCLOCKDIV for clock divide. The ATtiny85’s clock speed can be set to 1 MHz for low power, but the I2C bus speed will be slower. The display’s driver supports SETPRECHARGE for pre-charge period. The ATtiny85’s GPIO pins can be used for digital input, but the I2C bus must be released. The I2C bus address can be changed by soldering a resistor on the module. The OLED’s driver supports SETCOMPINS for COM pins configuration. The ATtiny85’s EEPROM can store the display’s orientation. The display’s driver supports SETVCOMDETECT for VCOM deselect level. The ATtiny85’s ADC can read the OLED’s temperature, but it’s not accurate. The I2C bus should be shielded from EMI. The OLED’s driver supports DISPLAYALLON_RESUME for normal display. The ATtiny85’s sleep mode current is 0.1 µA, but the OLED’s sleep current is 20 µA. The I2C bus can be used with multiple displays, but each must have a unique address. The OLED’s driver supports SETSEGMENTREMAP for column remap. The ATtiny85’s flash memory can store the display’s initialization sequence. The display’s driver supports SETMULTIPLEX for multiplex ratio. The ATtiny85’s GPIO pins have a maximum current of 40 mA, but the OLED draws 20 mA. The I2C bus should be terminated with resistors to avoid reflections. The OLED’s driver supports SETCHARGEPUMP for charge pump enable. The ATtiny85’s internal oscillator can be used for I2C timing, but it’s less accurate. The display’s driver supports SETDISPLAYON for display on. The ATtiny85’s TinyWireM library uses PB0 and PB2 for I2C. The OLED’s driver supports SETNORMALDISPLAY for non-inverted display. The ATtiny85’s EEPROM can store the display’s contrast value. The I2C bus should be initialized after power-up. The OLED’s driver supports SETLUT for look-up table. The ATtiny85’s GPIO pins can be used for PWM, but the I2C bus must be idle. The display’s driver supports SETCONTRAST for brightness control. The ATtiny85’s ADC has 10-bit resolution, but the OLED’s analog input is not used. The I2C bus can be tested with a multimeter. The OLED’s driver supports SETSTARTLINE for vertical scroll. The ATtiny85’s timer0 can be used for millis() timing. The display’s driver supports SETCOMSCANINC for COM scan direction. The ATtiny85’s watchdog timer can be used to reset the I2C bus. The I2C bus should be terminated with 4.7kΩ resistors. The OLED’s driver supports SETDISPLAYOFFSET for vertical shift. The ATtiny85’s clock speed can be set to 8 MHz for I2C. The display’s driver supports SETDISPLAYCLOCKDIV for clock divide. The ATtiny85’s GPIO pins can be used for digital output. The I2C bus address is 0x3C. The OLED’s driver supports SETPRECHARGE for pre-charge period. The ATtiny85’s EEPROM can store the display’s orientation. The display’s driver supports SETCOMPINS for COM pins configuration. The ATtiny85’s ADC can read the OLED’s voltage. The I2C bus should be shielded from noise. The OLED’s driver supports SETVCOMDETECT for VCOM deselect level. The ATtiny85’s sleep mode current is 0.1 µA. The I2C bus can be used with multiple displays. The OLED’s driver supports DISPLAYALLON_RESUME for normal display.