At this point your electronics should be connected (PowerSwitch Tail, Feather and Thermometer) and your feather should have CircuitPython firmware and thermometer drivers installed.
1. GETTING STARTED
Plug your Feather into your computer with a Micro USB cable. The Feather will show up on your computer as a USB mass storage device called "CIRCUITPY". Open a new text file in the editor of your choice (I use Atom) and save it to the CIRCUITPY drive as code.txt
. To see the output of your Feather while coding you will need to connect a serial read–eval–print loop (REPL). If you are not sure how to open the serial REPL follow this guide. Once you have your code.txt file saved to your Feather (CIRCUITPY drive) and the serial REPL open you are ready to code!
2. CONNECT THE THERMOMETER:
In your code.txt
file use the code below to import the thermometer and supporting libraries. This will also import the I2C protocol from busio and rename the thermometer output (here we used t). Save the code to your Feather and check to see that the temperature is printed in your serial REPL.
# Load the libraries we need. import adafruit_mcp9808 from board import * import busio import digitalio import time # Do one reading and print it out. with busio.I2C(SCL, SDA) as i2c: t = adafruit_mcp9808.MCP9808(i2c) print("temperature in celsius:", t.temperature)
3. CONNECT TO THE LED AND THE POWERSWITCH TAIL
For this section you do not need to have the PowerSwitch Tail plugged into the wall, the microcontroller will power its LED. This code labels your indicator LED at pin D13 and switches it to output. Next, it connects the PowerSwitch Tail to the Feather through pin A5. If you used a different pin to connect to the PowerSwitch Tail, you will need to change the code here (edit A5).
led = digitalio.DigitalInOut(D13) led.switch_to_output() power = digitalio.DigitalInOut(A5) power.switch_to_output()
4. WRITE THE CODE FOR THE THERMOSTAT
I wanted to use as little power as possible to make my heater smart so I am using a pretty simple thermostat. First, the while loop creates an infinite loop for the microcontroller. Inside the loop, the temperature sampling is delayed to every 5 minutes with a sleep function. Since I am dealing with a large vat of tea, the temperature shouldn't fluctuate very much. To test your sensor you may want to change the time.sleep()
function to something shorter or comment it out. As is, the print statement will output the temperature to the serial REPL every 5 minutes. The print statement is helpful during setup but can be commented out when you save the code to your board. The thermostat is a simple if/elif statement which turns the power on if the temperature falls below 24.5C and turns the power off if the temperature is over 26.5C. This works for me but if you are looking for something more exact you can may consider a PID function:
while True: #setting 5 minutes delay for temp sampling time.sleep(300) print (t.temperature) if t.temperature < 24.5: power.value = True led.value = True elif t.temperature > 26.5: power.value = False led.value = False
Here is all of the code put together:
# Load the libraries we need. import adafruit_mcp9808 from board import * import busio import digitalio import time # Do one reading and print it out. with busio.I2C(SCL, SDA) as i2c: t = adafruit_mcp9808.MCP9808(i2c) print("temperature in celsius:", t.temperature) led = digitalio.DigitalInOut(D13) led.switch_to_output() power = digitalio.DigitalInOut(A5) power.switch_to_output() while True: #setting 5 minutes delay for temp sampling time.sleep(300) print (t.temperature) if t.temperature < 24.5: power.value = True led.value = True elif t.temperature > 26.5: power.value = False led.value = False
Text editor powered by tinymce.