gokul/pcb-thermal-camera
A 38.4×91.8 mm two-layer thermal-camera PCB integrating an RP2040-Zero MCU, MLX90640 infrared sensor, 2.8-inch TFT display, decoupling capacitors, pull-up/current-limiting resistors, and power/fault LEDs.
- Version
- 0.2.0
- License
- unset
- Stars
- 0
Use with the tscircuit CLI
tsci is the tscircuit CLI. Install it with npm install -g tscircuit.
tsci clone gokul/pcb-thermal-camera # download the package files into ./pcb-thermal-camera
tsci add gokul/pcb-thermal-camera # add it to your project as @tsci/gokul.pcb-thermal-cameraIn a cloned or new (tsci init) project: tsci dev starts a local preview, tsci build outputs Circuit JSON, and tsci push publishes to tscircuit.com.
firmware/code.py
"""USB thermal camera for the Waveshare RP2040-Zero, CircuitPython 10.x."""
import array
import math
import time
import board
import busio
import digitalio
import displayio
import terminalio
from fourwire import FourWire
from adafruit_mlx90640 import MLX90640, RefreshRate
from adafruit_st7789 import ST7789
def heat_color(index):
# Purple through red and yellow to white.
stops = ((24, 8, 48), (103, 20, 105), (220, 51, 40), (255, 176, 34), (255, 255, 224))
segment = min(index // 16, 3)
mix = (index - 16 * segment) / (15 if segment == 3 else 16)
a, b = stops[segment], stops[segment + 1]
r, g, blue = (round(a[i] + (b[i] - a[i]) * mix) for i in range(3))
return (r << 16) | (g << 8) | blue
def paint_frame(frame, image):
valid = [temperature for temperature in frame if math.isfinite(temperature)]
if not valid:
raise ValueError("No valid temperature pixels")
low, high = min(valid), max(valid)
span = max(5.0, high - low)
for i, temperature in enumerate(frame):
image[i % 32, i // 32] = (
1 + min(63, max(0, int((temperature - low) * 63 / span)))
if math.isfinite(temperature) else 0
)
return low, high
def main():
fault = digitalio.DigitalInOut(board.GP8)
fault.switch_to_output(value=True)
displayio.release_displays()
spi = busio.SPI(board.GP14, MOSI=board.GP15)
bus = FourWire(spi, command=board.GP12, chip_select=board.GP13,
reset=board.GP11, baudrate=12000000)
display = ST7789(bus, width=240, height=320, rowstart=0, rotation=180,
auto_refresh=False)
group = displayio.Group()
display.root_group = group
image = displayio.Bitmap(32, 24, 65)
colors = displayio.Palette(65)
colors[0] = 0
for i in range(64):
colors[i + 1] = heat_color(i)
enlarged = displayio.Group(scale=7, x=8, y=60)
enlarged.append(displayio.TileGrid(image, pixel_shader=colors))
group.append(enlarged)
font = terminalio.FONT
gw, gh = font.get_bounding_box()[:2]
ink = displayio.Palette(2)
ink[0], ink[1] = 0x000000, 0xFFFFFF
width = 224 // gw
status = displayio.TileGrid(font.bitmap, pixel_shader=ink, width=width,
height=1, tile_width=gw, tile_height=gh, x=8, y=247)
group.append(status)
def show(message):
for col in range(width):
glyph = font.get_glyph(ord(message[col] if col < len(message) else " "))
status[col] = glyph.tile_index
display.refresh(minimum_frames_per_second=0)
show("Starting sensor")
i2c = busio.I2C(board.GP5, board.GP4, frequency=400000)
frame = array.array("f", [0.0] * 768)
sensor = None
while True:
try:
if sensor is None:
sensor = MLX90640(i2c)
sensor.refresh_rate = RefreshRate.REFRESH_2_HZ
sensor.getFrame(frame)
low, high = paint_frame(frame, image)
show("MIN %.1f C MAX %.1f C" % (low, high))
fault.value = False
except (OSError, RuntimeError, ValueError) as error:
fault.value = True
image.fill(0)
show("Sensor fault - retrying")
print(error)
sensor = None
time.sleep(1)
if __name__ == "__main__":
main()