This little post was triggered by reading Owen Duffy's post titled "Arduino thermometer using DS18B20 and OLED display". Even though I'm quite comfortable writing C or C++, I've been enjoying the clean MicroPython environment. These days I've pretty much switched from classic Atmel Arduinos to RP2040 based boards.
I implemented this little thermometer in just a few minutes with two easy to find and install libraries. Here's how it looks:
And here is the code:
import time
from machine import Pin, SoftI2C
import ssd1306
from micropython_mcp9808 import mcp9808
i2c = SoftI2C(sda=Pin(6), scl=Pin(7), freq=100_000)
display = ssd1306.SSD1306_I2C(128, 32, i2c)
mcp = mcp9808.MCP9808(i2c) # , address=0x18)
time.sleep(0.5)
while True:
temp = mcp.temperature
#print(temp)
time.sleep(5.0)
display.fill(0)
display.text(f"{mcp.temperature:.2f}C", 30, 15, 1) # x,y,colour
display.show()
The imported modules are mcp9808 for the i2c temperature board and ssd1306 for the OLED display.
I use Thonny for editing, uploading and debugging.
One mystery is that the hardware I2C isn't working for me on the RP2040 board but SoftI2C works fine.
When getting started I scan for I2C devices like this:
i2c = SoftI2C(sda=Pin(6), scl=Pin(7), freq=100_000)
devices = i2c.scan()
print("devices: ")
for device in devices:
print("0x{0:02x}".format(device))
Obviously adjust for the pins you've used. The RP2040 is a wonderfully powerful chip. The board I used here is from Seeed studio.
Cheers to Owen for all his great insights and sharing.