This walkthrough will demonstrate how you can use the EdgePi to automatically turn on a fan when room temperature gets too high.
Your EdgePi will be connected to a thermocouple and a DC fan. The thermocouple will continuously monitor the room temperature. The EdgePi will turn on a DC fan when the thermocouple detects a temperature that is higher than a predetermined threshold. Once the room temperature dips a few degrees below the threshold temperature, the fan will turn off.
Ensure the polarity is correct when attaching the thermocouple. Please refer to the labelling on the EdgePi.
All the code required for this project can be found on the project's GitHub repository.
$ sudo apt-get install python3-venv
$ python -m venv venv
$ source venv/bin/activate
$ python3 -m pip install edgepi-python-sdk
Let's first test a continuous temperature reading program. Refer to the test_temp_reads.py file on the GitHub repository under sandbox
.
import time
from edgepi.tc.edgepi_tc import EdgePiTC
from edgepi.tc.tc_constants import ConvMode
# initialize thermocouple
edgepi_tc = EdgePiTC()
# set thermocouple to measure temperature continuously
edgepi_tc.set_config(conversion_mode=ConvMode.AUTO)
# set thermocouple to measure temperature continuously
edgepi_tc.set_config(conversion_mode=ConvMode.AUTO)
MAX_TEMP = 28
_,room_temp = edgepi_tc.read_temperatures()
# sample temperature readings until room temp exceeds max temp
while room_temp < MAX_TEMP:
time.sleep(1) # wait 1 second between samples
_,room_temp = edgepi_tc.read_temperatures()
print(room_temp)
print("TOO HOT! Time to cool down!")
# stop continuous measurements once you're done sampling
edgepi_tc.set_config(conversion_mode=ConvMode.SINGLE)
Congrats! You now understand the first main concept of the program
There are two solutions to power the fan. The first demonstration will use analogue output using the DAC module. The second will utilize digital output and the DOUT module.
If you are using a low powered fan (this guide uses a 12V fan), this solution will work fine. It is preferrable to use digital out as you will be able to output higher voltages. For the sake of expirementation, consider following this solution. Refer to the test_dac_fan.py file on the GitHub repository under sandbox
.
from edgepi.dac.dac_constants import DACChannel as Ch
from edgepi.dac.edgepi_dac import EdgePiDAC
# initialize DAC
edgepi_dac = EdgePiDAC()
# setting DAC range 0-10V
edgepi_dac.set_dac_gain(True)
# write voltage value of 10 V to analog out pin number 4
edgepi_dac.write_voltage(Ch.AOUT4, 10)
# read state of DAC output 4
code, voltage, gain = edgepi_dac.get_state(Ch.AOUT4, True, True, True)
print(code,voltage,gain)
The port on the EdgePi is different from the output channel number. In our case, we used port 5 and A/D output pin number 4. Ensure you read the labelling on the EdgePi correctly.
edgepi_dac.reset()
at the end of your code.
.reset()
can be overkill as it resets all the analogue output pins
Refer to the test_dout_fan.py file on the GitHub repository under sandbox
.
from edgepi.gpio.gpio_constants import GpioPins
from edgepi.digital_output.edgepi_digital_output import EdgePiDigitalOutput
# initialize DOUT
edgepi_dout = EdgePiDigitalOutput()
# set GpioPin 4 to OUT direction
edgepi_dout.digital_output_direction(GpioPins.DOUT4, False)
# set Gpiopin 4 to High/on
edgepi_dout.digital_output_state(GpioPins.DOUT4, True)
False
and rerun your python file.Congrats! You now understand the second main concept of the program.
Whether you used DAC or DOUT to power your fan, the final solution should look similar. Refer to the dac_solution.py and dout_solution.py files on GitHub respectively.
import time
from edgepi.gpio.gpio_constants import GpioPins
from edgepi.digital_output.edgepi_digital_output import EdgePiDigitalOutput
from edgepi.tc.edgepi_tc import EdgePiTC
from edgepi.tc.tc_constants import ConvMode
# Initialize new Thermocouple and DOUT
edgepi_tc = EdgePiTC()
edgepi_dout = EdgePiDigitalOutput()
# set thermocouple to measure temperature continuously
edgepi_tc.set_config(conversion_mode=ConvMode.AUTO)
# set max temp condition
MAX_TEMP = 28
# set hysteresis for turning off fan
FINISH_TEMP = MAX_TEMP - 4
# Check if fan is off/on
FAN_STATE = False
RUN = True
while RUN:
# wait 1 second between samples
time.sleep(1)
# read cold-junction and linearized thermocouple temperatures
_, room_temp = edgepi_tc.read_temperatures()
# print room temperature
print(f"{room_temp} degrees Celsius")
# turn on fan if room temp exceeds max temp and fan hasn't already been turned on
if room_temp > MAX_TEMP and not FAN_STATE:
print("TOO HOT! Time to cool down!")
# setting GpioPin 4 to output direction
edgepi_dout.digital_output_direction(GpioPins.DOUT4, False)
# setting GpioPin 4 to High/On
edgepi_dout.digital_output_state(GpioPins.DOUT4, True)
# turn fan state on
FAN_STATE = True
if room_temp < FINISH_TEMP and FAN_STATE:
print("Finished Cooling!")
# setting GpioPin 4 to Low/Off
edgepi_dout.digital_output_state(GpioPins.DOUT4, False)
# turn fan state off
FAN_STATE = False
Congrats! You should have a fully functional automatic fan system!
Congrats on completing this simple EdgePi project! You should now understand the basics of connecting hardware to your EdgePi and interacting with it using the SDK.