Tufty2040 - No display

Yes, I am very happy that everything is now working as it should.
This will now be a good starting point for me to learn MicroPython and get used to the RP2040.

1 Like

Im new to all this but could you share the weather.py info, I’m trying to do the same thing and no luck.

I don’t know if @alphanumeric created their own weather.py file. Otherwise, you can check Pimoroni’s Github page for tufty2040’s examples.

1 Like

This is my latest version, let me know what you think of it.

'''
Pimoroni Tufty 2040
BME280
'''
import utime

import picographics
from picographics import PicoGraphics, DISPLAY_TUFTY_2040, PEN_RGB332
display = PicoGraphics(display=DISPLAY_TUFTY_2040, rotate=180, pen_type=PEN_RGB332)
display.set_backlight(1.0)
display.set_font("bitmap8") 

from breakout_bme280 import BreakoutBME280
from pimoroni_i2c import PimoroniI2C
i2c = PimoroniI2C(sda=(4), scl=(5))
bme = BreakoutBME280(i2c)

from machine import ADC, Pin
vbat_adc = ADC(29)
vref_adc = ADC(28)
vref_en = Pin(27)
vref_en.init(Pin.OUT)
vref_en.value(0)
usb_power = Pin(24, Pin.IN)    

full_battery = 4.5
empty_battery = 2.8

from pimoroni import Button
button_a = Button(7, invert=False)
button_b = Button(8, invert=False)
button_c = Button(9, invert=False)
button_up = Button(22, invert=False)
button_down = Button(6, invert=False)

tempcolor = display.create_pen(0, 255, 0)  # this colour will get changed in a bit
humidcolor = display.create_pen(0, 255, 0)  # this colour will get changed in a bit
presscolor = display.create_pen(0, 255, 0)  # this colour will get changed in a bit
white = display.create_pen(255, 255, 255)
black = display.create_pen(0, 0, 0)
red = display.create_pen(255, 0, 0)
green = display.create_pen(0, 255, 0)
blue = display.create_pen(0, 0, 255)
yellow = display.create_pen(255, 255, 0)
orange = display.create_pen(255, 140, 0)
grey = display.create_pen(120, 120, 120)
violet = display.create_pen(255, 0, 255)

start_time = utime.time()

display.set_pen(black)
display.clear()

# converts the temperature into a description and pen colour
def describe_temperature(temperature):
    global tempcolor
    if temperature < 0:
        description = "Very Cold"
        tempcolor = blue
    elif 0 <= temperature < 12:
        description = "Cold"
        tempcolor = yellow
    elif 12 <= temperature < 17:
        description = "Cool"
        tempcolor = green
    elif 17 <= temperature < 22:
        description = "Warm"
        tempcolor = green      
    elif 22 <= temperature < 27:
        description = "Hot"
        tempcolor = orange
    elif temperature >= 27:
        description = "Very Hot"
        tempcolor = red
    else:
        description = ""
        tempcolor = black
    return description

# converts humidity into good/bad description and pen color
def describe_humidity(humidity):
    global humidcolor
    if humidity < 30:
        description = "Low - Dry"
        humidcolor = orange
    elif 30 <= humidity <= 60:
        description = "OK"
        humidcolor = green
    elif 60 < humidity < 80:
        description = "High"
        humidcolor = yellow
    elif humidity >= 80:
        description = "Very High"
        humidcolor = red        
    else:
        description = ""
        humidcolor = black        
    return description

# converts pressure into barometer-type description and pen color
def describe_pressure(pressure):
    global presscolor
    if pressure < 982:
        description = "Very Low"
        presscolor = red
    elif 982 <= pressure < 1004:
        description = "Low"
        presscolor = yellow
    elif 1004 <= pressure < 1026:
        description = ""
        presscolor = green
    elif 1026 <= pressure < 1048:
        description = "High"
        presscolor = blue
    elif pressure >= 1048:
        description = "Very High"
        presscolor = orange
    else:
        description = ""
        presscolor = black
    return description



def draw_graph(temp_value, press_value, humid_value):
    scaled_temp = int((temperature * 5) + 160)
    scaled_humid = int((humidity * (320 / 100)) - 10) 
    scaled_press = int(((pressuremb - 960) * 3) - 10)
    display.set_pen(black)
    display.clear()
    
    describe_temperature(temperature)
    display.set_pen(tempcolor)
    display.rectangle(0, 70, (scaled_temp), 19)
    display.circle((scaled_temp), 79, 9)
    display.text("{:0.0f}c".format(temperature), 0, 45, scale=3)
    display.text(describe_temperature(temperature), 150, 45, scale=3)
    display.set_pen(black)
    display.circle((scaled_temp), 79, 5) 
    
    describe_humidity(humidity)
    display.set_pen(humidcolor)
    display.rectangle(0, 125, (scaled_humid), 19)
    display.circle((scaled_humid), 134, 9)
    display.text("{:0.0f}%".format(humidity), 0, 100, scale=3)
    display.text(describe_humidity(humidity), 150, 100, scale=3)
    display.set_pen(black)
    display.circle((scaled_humid), 134, 5)
    
    describe_pressure(pressuremb)
    display.set_pen(presscolor)
    display.rectangle(0, 180, (scaled_press), 19)
    display.circle((scaled_press), 189, 9)
    display.text("{:0.0f}mb".format(pressuremb), 0, 155, scale=3)
    display.text(describe_pressure(pressuremb), 150, 155, scale=3)
    display.set_pen(black)
    display.circle((scaled_press), 189, 5)

    display.set_pen(white)
    display.text("Enviromental Tricorder", 5, 10, scale=3)
        
start_time = utime.time()
    
while True:

    time_elapsed = utime.time() - start_time
    
    temperature, pressure, humidity = bme.read()
    pressuremb = pressure / 100      
    draw_graph(temperature, pressure, humidity)
        
    vref_en.value(1)

    vdd = 1.24 * (65535 / vref_adc.read_u16())
    vbat = (
        (vbat_adc.read_u16() / 65535) * 3 * vdd
    )  # 3 in this is a gain, not rounding of 3.3V
    
    vref_en.value(0)
            
    percentage = 100 * ((vbat - empty_battery) / (full_battery - empty_battery))
    if percentage > 100:
        percentage = 100
    if percentage < 0:
        percentage = 0    

    if usb_power.value() == 1:
        display.set_pen(blue)
        display.text("USB Powered", 75, 210, scale=3)
    else:
        display.set_pen(green)
        display.text("Battery", 0, 210, scale=2)
        display.text("{:0.2f}v".format(vbat), 75, 210, scale=2)
        display.text("{:0.0f}%".format(percentage), 150, 210, scale=2)

    if button_up.is_pressed:
        display.set_backlight(1.0)
    elif button_down.is_pressed:
        display.set_backlight(0.5) 
            
    if time_elapsed < 1:
        temperature, pressure, humidity = bme.read()
        display.set_pen(black)
        display.clear()
        display.update()
        utime.sleep(1)
    else:
        display.update()
        utime.sleep(1)
        display.set_pen(black)
        display.clear()    
        utime.sleep(1)

1 Like