/ Jupyter_USB / PCT2075.ipynb
PCT2075.ipynb
  1  {
  2   "cells": [
  3    {
  4     "cell_type": "markdown",
  5     "metadata": {},
  6     "source": [
  7      "# Adafruit Blinka + PCT2075\n",
  8      "<img src=\"https://cdn-shop.adafruit.com/1200x900/4369-04.jpg\" alt=\"PCT2075\" height= \"500\" width=\"500\"/>"
  9     ]
 10    },
 11    {
 12     "cell_type": "code",
 13     "execution_count": null,
 14     "metadata": {
 15      "scrolled": true
 16     },
 17     "outputs": [],
 18     "source": [
 19      "# Python Software Package Installation\n",
 20      "import sys\n",
 21      "!{sys.executable} -m pip install adafruit-blinka adafruit-circuitpython-msa301 hidapi ipympl adafruit-circuitpython-pct2075\n",
 22      "\n",
 23      "# Set an Environment Variable so Adafruit Blinka knows we're using the MCP2221\n",
 24      "import os\n",
 25      "os.environ[\"BLINKA_MCP2221\"] = \"1\""
 26     ]
 27    },
 28    {
 29     "cell_type": "markdown",
 30     "metadata": {},
 31     "source": [
 32      "Verify the cell below returns the temperature before proceeding to the graph cell."
 33     ]
 34    },
 35    {
 36     "cell_type": "code",
 37     "execution_count": null,
 38     "metadata": {},
 39     "outputs": [],
 40     "source": [
 41      "import board\n",
 42      "import busio\n",
 43      "import adafruit_pct2075\n",
 44      "i2c = busio.I2C(board.SCL, board.SDA)\n",
 45      "\n",
 46      "pct = adafruit_pct2075.PCT2075(i2c)\n",
 47      "print('Temperature: {}*C'.format(pct.temperature))"
 48     ]
 49    },
 50    {
 51     "cell_type": "markdown",
 52     "metadata": {},
 53     "source": [
 54      "This cell will graph the temperature values over time"
 55     ]
 56    },
 57    {
 58     "cell_type": "code",
 59     "execution_count": null,
 60     "metadata": {},
 61     "outputs": [],
 62     "source": [
 63      "%matplotlib notebook\n",
 64      "from datetime import datetime\n",
 65      "import matplotlib.pyplot as plt\n",
 66      "import matplotlib.animation as animation\n",
 67      "from collections import deque\n",
 68      "\n",
 69      "# How many sensor samples we want to store\n",
 70      "HISTORY_SIZE = 100\n",
 71      "\n",
 72      "# Graph update interval (in seconds)\n",
 73      "INTERVAL = 0.5\n",
 74      "\n",
 75      "# Maximum Temperature (in degrees C)\n",
 76      "MAX_TEMP = 30\n",
 77      "\n",
 78      "# Minimum Temperature (in degrees C)\n",
 79      "MIN_TEMP = 10\n",
 80      "\n",
 81      "# Global x-axis array\n",
 82      "x_time = deque(maxlen=HISTORY_SIZE)\n",
 83      "\n",
 84      "# Temperature data\n",
 85      "temp_data = deque(maxlen=HISTORY_SIZE)\n",
 86      "\n",
 87      "# Create new plot\n",
 88      "fig, ax = plt.subplots()\n",
 89      "\n",
 90      "# Global title\n",
 91      "fig.suptitle(\"PCT2075 Temperature\", fontsize=14)\n",
 92      "\n",
 93      "def animate(i):\n",
 94      "    # Read the temperature sensor and add the value to the temp_data array\n",
 95      "    temp_data.append(pct.temperature)\n",
 96      "    print(temp_data)\n",
 97      "    \n",
 98      "    # Grab the datetime, auto-range based on length of accel_x array\n",
 99      "    x_time.append(datetime.now().strftime('%M:%S'))\n",
100      "    print(x_time)\n",
101      "\n",
102      "    # Clear axis prior to plotting\n",
103      "    ax.cla()\n",
104      "    \n",
105      "    # Constrain the Y-axis\n",
106      "    plt.ylim(top=50,bottom=0)\n",
107      "\n",
108      "    # Y-Axis label\n",
109      "    plt.ylabel('Temperature\\n(c)')\n",
110      "    \n",
111      "    # Rotate and align the x-axis tick labels, add a grid\n",
112      "    fig.autofmt_xdate()\n",
113      "    ax.grid(True, linestyle=':', linewidth=0.5)\n",
114      "\n",
115      "    # Add temperature plot to graph\n",
116      "    plt.plot(x_time, temp_data)\n",
117      "    \n",
118      "    # Add a horizontal minimum line across the X-axis\n",
119      "    plt.axhline(y=MAX_TEMP, color='r', linestyle=':', label='Max. Temperature')\n",
120      "\n",
121      "    # Add a horizontal maximum line across the X-axis\n",
122      "    plt.axhline(y=MIN_TEMP, color='b', linestyle=':', label='Min. Temperature')\n",
123      "    \n",
124      "    # Add a legend to the graph\n",
125      "    ax.legend()\n",
126      "    \n",
127      "    # Pause the plot for INTERVAL seconds \n",
128      "    plt.pause(INTERVAL)\n",
129      "\n",
130      "ani = animation.FuncAnimation(fig, animate)"
131     ]
132    }
133   ],
134   "metadata": {
135    "kernelspec": {
136     "display_name": "Python 3",
137     "language": "python",
138     "name": "python3"
139    },
140    "language_info": {
141     "codemirror_mode": {
142      "name": "ipython",
143      "version": 3
144     },
145     "file_extension": ".py",
146     "mimetype": "text/x-python",
147     "name": "python",
148     "nbconvert_exporter": "python",
149     "pygments_lexer": "ipython3",
150     "version": "3.7.4"
151    }
152   },
153   "nbformat": 4,
154   "nbformat_minor": 2
155  }