This guide assumes that you've gotten your Raspberry Pi up and running, and have CircuitPython installed.

Installing Fonts

This project uses a Microsoft TrueType Font. To install it, enter the following into the terminal on your Raspberry Pi:

sudo apt-get install ttf-mscorefonts-installer

Installing CircuitPython Libraries

Since CircuitPython is running on the Raspberry Pi - installing the libraries for this guide is quick and easy.

To install the library for the displayenter the following into the terminal

sudo pip3 install adafruit-circuitpython-ssd1306

You'll also need to install the CircuitPython library to read data from the Analog Devices ADT7410

sudo pip3 install adafruit-circuitpython-adt7410

The code for this project is below. Save it to the Raspberry Pi as adafruit_io_adt7410.py

# SPDX-FileCopyrightText: 2019 Brent Rubell for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""
'adafruit_io_adt7410.py'
==================================
Example of sending temperature
values to an Adafruit IO feed using
an ADT7410 breakout.

Dependencies:
    - Adafruit_Blinka
        (https://github.com/adafruit/Adafruit_Blinka)
    - Adafruit_CircuitPython_SSD1306
        (https://github.com/adafruit/Adafruit_CircuitPython_SSD1306)
    - Adafruit_CircuitPython_ADT7410
        (https://github.com/adafruit/Adafruit_CircuitPython_ADT7410)
"""
# Import standard python modules
import time

# import Adafruit SSD1306 OLED
from PIL import Image, ImageDraw, ImageFont
import adafruit_ssd1306

# import Adafruit IO REST client
from Adafruit_IO import Client

# import Adafruit CircuitPython adafruit_adt7410 library
import adafruit_adt7410

# import Adafruit Blinka
import board
import busio
import digitalio

# Delay between sensor reads, in seconds
DELAY_SECONDS = 30

# Set to your Adafruit IO key.
# Remember, your key is a secret,
# so make sure not to publish it when you publish this code!
ADAFRUIT_IO_KEY = 'ADAFRUIT_IO_KEY'

# Set to your Adafruit IO username.
# (go to https://accounts.adafruit.com to find your username)
ADAFRUIT_IO_USERNAME = 'ADAFRUIT_IO_USERNAME'

# Create an instance of the REST client
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

# Set up `temperature` feed
pi_temperature = aio.feeds('temperature')

# Set up OLED
i2c_bus = busio.I2C(board.SCL, board.SDA)
oled_reset = digitalio.DigitalInOut(board.D21)
disp = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c_bus, reset=oled_reset)
# Clear display.
disp.fill(0)
disp.show()

# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
width = disp.width
height = disp.height
image = Image.new('1', (width, height))
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
# `sudo apt-get install ttf-mscorefonts-installer` to get these fonts
font_big = ImageFont.truetype('/usr/share/fonts/truetype/msttcorefonts/Arial_Bold.ttf', 16)
font_small = ImageFont.truetype('/usr/share/fonts/truetype/msttcorefonts/Arial_Bold.ttf', 12)

adt = adafruit_adt7410.ADT7410(i2c_bus, address=0x48)
adt.high_resolution = True
time.sleep(0.25)  # wait for sensor to boot up and get first reading

while True:
    # clear screen
    draw.rectangle((0, 0, width, height), outline=0, fill=0)

    # Read the temperature sensor
    tempC = adt.temperature
    tempC = round(tempC, 2)

    # display the temperature on the OLED
    draw.text((0, 0), "Temp: %0.2F *C" % tempC, font=font_big, fill=255)

    # Send temperature to Adafruit IO
    print('Sending temperature {0} C to Adafruit IO'.format(tempC))
    draw.text((0, 16), "Sending...", font=font_small, fill=255)
    disp.image(image)
    disp.show()

    aio.send(pi_temperature.key, tempC)
    draw.text((0, 16), "Sending...Done!", font=font_small, fill=255)
    disp.image(image)
    disp.show()

    # Delay for DELAY_SECONDS seconds to avoid timeout from adafruit io
    time.sleep(DELAY_SECONDS)

Before running the code, you'll need to set the ADAFRUIT_IO_KEY to the Adafruit IO Key we saved earlier and set the ADAFRUIT_IO_USERNAME to your Adafruit IO Username.

When both of these variables are changed, save the file. You can run the program by entering the following in your terminal:

python adafruit_io_adt7410.py

and Press the Enter Key.

You should see the following print out in your terminal

Sending temperature 22.26 C to Adafruit IO
Sending temperature 22.27 C to Adafruit IO
Sending temperature 22.25 C to Adafruit IO
Sending temperature 22.27 C to Adafruit IO
Sending temperature 22.28 C to Adafruit IO

The display should display the temperature read from the ADT7410 along with the status of the data being sent to Adafruit IO.

Next, we're going to check that the data has been received by Adafruit IO. We can do this by visiting the Adafruit IO Monitor page. Every 30 seconds, the page will refresh with the latest values from the ADT7410.

 

If you want to change the delay, change the variable DELAY_SECONDS to the desired delay between sensor reads. 

Navigate to the dashboard you created earlier to view the temperature gauge we added. Every 30 seconds, it'll update and display a new value.

For more information about the ADT7410 CircuitPython library, check out the latest docs!

This guide was first published on Feb 11, 2019. It was last updated on Feb 11, 2019.

This page (Python Code) was last updated on Oct 02, 2023.

Text editor powered by tinymce.