Skip to content

Instantly share code, notes, and snippets.

@elliotwoods elliotwoods/oled.py

Created Nov 18, 2019
Embed
What would you like to do?
code for managing ssd1306 OLED on ESP32 with micropython on a TTGO OLED board
from machine import Pin, I2C
import ssd1306
import time
import esp32
def farenheit_to_celcius(farenheit_value):
return (farenheit_value - 32) * 5 / 9
class Oled:
def __init__(self, reset = 16, scl = 15, sda = 4):
self.reset_pin = Pin(reset, Pin.OUT)
self.scl_pin = Pin(scl)
self.sda_pin = Pin(sda)
self.reset()
self.show_temperature()
def reset(self):
self.reset_pin.off()
time.sleep_ms(50)
self.reset_pin.on()
self.i2c = I2C(-1, scl=self.scl_pin, sda=self.sda_pin)
self.oled = ssd1306.SSD1306_I2C(128, 64, self.i2c)
def display(self, messages):
y = 10
self.oled.fill(0)
for message in messages:
self.oled.text(message, 0, y)
y += 10
self.oled.show()
print(messages)
def show_temperature(self):
temperature_f = esp32.raw_temperature()
temperature_c = farenheit_to_celcius(temperature_f)
messages = []
messages.append("Temperature:")
messages.append("{0:.1f}C".format(temperature_c))
self.display(messages)
screen = Oled()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.