Oak Development Technologies
  • IcyBlue
  • Oak Blog
  • Prototyping
  • Livestream
  • Open Source Projects
  • Contact
Tutorials, Announcements, and More from...

Oak Development 
​Technologies

Connect with us on Facebook, Twitter & Instagram!

(Tutorial) Slow Tech Temperature Sensor And Indication using the CP Sapling

11/23/2020

0 Comments

 
Picture
With our new CP Sapling, there is a world of possibilities that you can explore with the SAMD21 microcontroller. And thanks to the hard work of Adafruit, there is a ton of great examples that can be used to explore microcontroller functionality and make a few cool projects to impress your family this holiday season!

As always before we get started, if you have never used a CircuitPython board like the QT PY, be sure to check out Adafruit's learning site to quickly get started with your new board. --> Adafruit Learning Site: Getting Started With CircuitPython

​What you'll need:

To get started, you'll need the following parts:
  1. CircuitPython compatible development board (CP Sapling used in this tutorial: CP Sapling m0 Development Board)
  2. MicroUSB data cable
  3. Mu Editor (Get that HERE)

Getting Everything Wired Up

The great thing about this tutorial is there is nothing to wire up. We are using the internal temperature sensor on the SAMD21E18A microcontroller!

The Code:

The code for this example is pretty simple. When we look in the CircuitPython Getting Started Documentation, we can utilize the internal microcontroller temperature sensor through a simple command! This command returns the value of the temperature in Celsius, though if you are in the United States and not comfortable with metric, we're including the conversion for Fahrenheit as well in this example. 

First we will simply display the temperature in the serial window in the MU Editor.
# print microcontroller cpu temperature 
import microcontroller # import the microcontroller lib
import time

# infinite loop to keep checking the temperature.
while True:
    # get the current temperature
    current_temp = microcontroller.cpu.temperature 
    # print out the temperature in celsius
    print("Current Temp(Celsius): ",current_temp) 
    # print converted temperature in fahrenheit
    print("Current Temp(Farhenheit):", current_temp * (9/5) + 32) 
    time.sleep(1) # sleep for 1 second



temperature outputs of samd21e18a microcontroller
This code allows us to print the temperature every second which you can see in the MU Serial Monitor. What we are really wanting to do is make a simple temperature check device that can tell us if a temperature is over a certain value. Adding a few if/else statements allows us to do just that. For reference, 37C is roughly 98.6F or the average human body temperature. Some people run hotter than 98.6 and some people run a little cooler. 

We will turn the on board neopixel red if we detect a temperature hotter than or equal to 24C or about 76F, otherwise keep the neopixel green. This is because it might take a while to get the temperature up to a temperature above 37C if you're not sick.

# check the temperature with MCU temp
# output temperature with neopixel color
import microcontroller # import the microcontroller lib
import board # import board so we can use it for the neopixel
import time #import time so we can sleep
import neopixel #import neopixel to use the neopixel

# configure the onboard neopixel
led = neopixel.NeoPixel(board.NEOPIXEL, 1)
# set the brightness of the neopixel
led.brightness = 0.3
# infinite loop to keep checking the temperature.
while True:
    current_temp = microcontroller.cpu.temperature # get the current temperature
    if (current_temp >= 24):
        # turn the neopixel red because the temperature is >= 38C
        led[0] = (255, 0, 0)
    else:
        # otherwise keep the neopixel green
        led[0] = (0, 255, 0)
    print("Current Temp(Celsius): ",current_temp) # print out the temperature in celsius
    print("Current Temp(Farhenheit):", current_temp * (9/5) + 32) # print converted temperature in fahrenheit
    time.sleep(1) # sleep for 1 second


CP Sapling with red neopixel color illuminated for temperatures higher than 24 degrees celsius
CP Sapling microcontroller showing a green led color for temperatures less than 24 degrees celsius
We can observe that as the microcontroller is in the open air, as long as the temperature is below 24C we can see the green light coming from the neopixel. Once the temperature is equal to or crosses 24C, the neopixel is then changed to red.

Final Thoughts:

Using the internal temperature of the CPU is a fun way to measure temperature with SAMD21 microcontrollers. These projects can be expanded using some breakout modules like the temperature and humidity sensors sold by Adafruit! These could be used as a way to experiment with thermometers, and provide a fun project to understand the temperature fluctuations inside and outside your home and even experiment taking surface body temperatures. 
0 Comments



Leave a Reply.

    Authors

    Seth is embedded software engineer and open source hardware developer. 

    Archives

    May 2021
    April 2021
    January 2021
    November 2020
    October 2020
    September 2020

    Categories

    All
    New Products
    Product Spotlight
    Tutorials

    RSS Feed

Powered by Create your own unique website with customizable templates.
  • IcyBlue
  • Oak Blog
  • Prototyping
  • Livestream
  • Open Source Projects
  • Contact