Adafruit_Learning_System_Gu.../Jupyter_USB/PCT2075.ipynb
2019-12-24 09:58:28 -05:00

155 lines
4.2 KiB
Text

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Adafruit Blinka + PCT2075\n",
"<img src=\"https://cdn-shop.adafruit.com/1200x900/4369-04.jpg\" alt=\"PCT2075\" height= \"500\" width=\"500\"/>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# Python Software Package Installation\n",
"import sys\n",
"!{sys.executable} -m pip install adafruit-blinka adafruit-circuitpython-msa301 hidapi ipympl adafruit-circuitpython-pct2075\n",
"\n",
"# Set an Environment Variable so Adafruit Blinka knows we're using the MCP2221\n",
"import os\n",
"os.environ[\"BLINKA_MCP2221\"] = \"1\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Verify the cell below returns the temperature before proceeding to the graph cell."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import board\n",
"import busio\n",
"import adafruit_pct2075\n",
"i2c = busio.I2C(board.SCL, board.SDA)\n",
"\n",
"pct = adafruit_pct2075.PCT2075(i2c)\n",
"print('Temperature: {}*C'.format(pct.temperature))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This cell will graph the temperature values over time"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib notebook\n",
"from datetime import datetime\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib.animation as animation\n",
"from collections import deque\n",
"\n",
"# How many sensor samples we want to store\n",
"HISTORY_SIZE = 100\n",
"\n",
"# Graph update interval (in seconds)\n",
"INTERVAL = 0.5\n",
"\n",
"# Maximum Temperature (in degrees C)\n",
"MAX_TEMP = 30\n",
"\n",
"# Minimum Temperature (in degrees C)\n",
"MIN_TEMP = 10\n",
"\n",
"# Global x-axis array\n",
"x_time = deque(maxlen=HISTORY_SIZE)\n",
"\n",
"# Temperature data\n",
"temp_data = deque(maxlen=HISTORY_SIZE)\n",
"\n",
"# Create new plot\n",
"fig, ax = plt.subplots()\n",
"\n",
"# Global title\n",
"fig.suptitle(\"PCT2075 Temperature\", fontsize=14)\n",
"\n",
"def animate(i):\n",
" # Read the temperature sensor and add the value to the temp_data array\n",
" temp_data.append(pct.temperature)\n",
" print(temp_data)\n",
" \n",
" # Grab the datetime, auto-range based on length of accel_x array\n",
" x_time.append(datetime.now().strftime('%M:%S'))\n",
" print(x_time)\n",
"\n",
" # Clear axis prior to plotting\n",
" ax.cla()\n",
" \n",
" # Constrain the Y-axis\n",
" plt.ylim(top=50,bottom=0)\n",
"\n",
" # Y-Axis label\n",
" plt.ylabel('Temperature\\n(c)')\n",
" \n",
" # Rotate and align the x-axis tick labels, add a grid\n",
" fig.autofmt_xdate()\n",
" ax.grid(True, linestyle=':', linewidth=0.5)\n",
"\n",
" # Add temperature plot to graph\n",
" plt.plot(x_time, temp_data)\n",
" \n",
" # Add a horizontal minimum line across the X-axis\n",
" plt.axhline(y=MAX_TEMP, color='r', linestyle=':', label='Max. Temperature')\n",
"\n",
" # Add a horizontal maximum line across the X-axis\n",
" plt.axhline(y=MIN_TEMP, color='b', linestyle=':', label='Min. Temperature')\n",
" \n",
" # Add a legend to the graph\n",
" ax.legend()\n",
" \n",
" # Pause the plot for INTERVAL seconds \n",
" plt.pause(INTERVAL)\n",
"\n",
"ani = animation.FuncAnimation(fig, animate)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}