Background:
Recently, I’ve been exploring the functions of Raspberry Pi. As a newbie to Pi, I tried reading abundant tutorials related to Raspberry Pi to help me learn, but afterwards I found it boring to just study its functions on paper. Well, true knowledge comes from practices, why not make something with Raspberry Pi. Since fall is coming, and it’s not too hot these days, I don’t need to use AC at my home, but I still want to have some cool breeze blowing on me when I am sitting at the desk to read a book. So this idea comes to me: make a temperature-controlled fan with Raspberry Pi. Without further ado, here we go!
Preparation:
Hardware List
1. Raspberry Pi 3 Model B+ (Or you can use Raspberry Pi 4)
2. IO Expansion HAT for Raspberry Pi 4B/3B+
3. Goteck Micro Metal Gear Servo (2.5kg)
4. Gravity: 130 DC Motor Module
5. Gravity: I2C BME680 Environmental Sensor
6. USB cable (micro-USB for Pi 3, USB type -C for Pi4)
7. 5V/3A USB Adaptor (Recommend not to power from PC USB port directly)
8. Ethernet Cable
Software
I used the software MobaXterm in this project.
Operation
Step 1: Hardware Connection
Plug the expansion board to Raspberry Pi, connect DC fan to P5, servo to P22, environment sensor to IIC.
Of course, you can choose to use other ports, but please make sure that the motor fan and servo are connected to the GPIO ports. Refer to the following for detailed PIN information.
Connect the Ethernet cable and power supply cable to the correct position.
Note: it is not recommended to power the project from the PC’s USB port since its current is not large enough to ensure the normal operation of this project.
Step 2: Enable Pin and install IIC library and tool
We have enable IIC pin of Raspberry Pi before using it. Input the following codes and complete the operations below:
sudo raspi-config
Use the up and down key on the keyboard to select, and press ESC to exit.
Then, install IIC library and tool.
sudo apt-get install i2c-tools
Step 3: Download the BME680 Sensor. library file
You’d better do this step under the left desktop folder of Raspberry Pi, otherwise, it may cause problems like no permission to delete the added file.
There are two ways to download the file to Raspberry Pi.
1)Download to your local computer, and then drag it to Raspberry Pi desktop folder.
2)Directly download into Raspberry Pi. It may take times, please be patient.
sudo git clone https://github.com/DFRobot/DFRobot_BME680
Step 4: Find the read data file in BME680 library file.
Enter DFRobot_BME680->Python->RaspberryPi->examples, then we can see a Python file named as read_all_data.py.
Copy the path and paste, and input as shown below:
cd /home/pi/Desktop/DFRobot_BME680/Python/RaspberryPi/examples/
Type in the following code to read all the data of the BME680 sensor .
python read_all_data.py
Press Ctrl+C to exit the running state.
Step 5: Revise code
We add the servo and motor fan based this program. So yo can first create a text file in your PC desktop, name it as auto_fan.py. As shown below:
Then copy the following into it, save and drag it into the List.
[/align][align=left]import sys
sys.path.append('../')
from DFRobot_BME680 import DFRobot_BME680
import time
import RPi.GPIO as GPIO
import signal
import atexit
atexit.register(GPIO.cleanup)
servopin = 22
motorpin=5
GPIO.setmode(GPIO.BCM)
GPIO.setup(servopin,GPIO.OUT)
GPIO.setup(motorpin,GPIO.OUT)
angle=GPIO.PWM(servopin,50)
angle.start(0)
sensor = DFRobot_BME680()
sensor.set_humidity_oversample(sensor.OS_2X) #Oversampling value: OS_NONE, OS_1X, OS_2X, OS_4X, OS_8X, OS_16X
sensor.set_pressure_oversample(sensor.OS_4X) #Oversampling value: OS_NONE, OS_1X, OS_2X, OS_4X, OS_8X, OS_16X
sensor.set_temperature_oversample(sensor.OS_8X) #Oversampling value: OS_NONE, OS_1X, OS_2X, OS_4X, OS_8X, OS_16X
sensor.set_filter(sensor.FILTER_SIZE_3) #increasing resolution but reducing bandwidth
sensor.set_gas_status(sensor.ENABLE_GAS_MEAS) #1 for enable and 0 for disable
sensor.set_gas_heater_temperature(320) #value:target temperature in degrees celsius, between 200 ~ 400
sensor.set_gas_heater_duration(150) #value:target duration in milliseconds, between 1 and 4032
sensor.select_gas_heater_profile(0) #value:current gas sensor conversion profile: 0 to 9
print("\n\nPolling:")
while True:
if sensor.get_sensor_data():
if sensor.data.temperature>25.00:
GPIO.output(motorpin,GPIO.HIGH)
for dc in range(0,181,1):
angle.ChangeDutyCycle(float(dc)/18+2.5)
time.sleep(0.1)
for dc in range(180,-1,-1):
angle.ChangeDutyCycle(float(dc)/18+2.5)
time.sleep(0.1)
else:
GPIO.output(motorpin,GPIO.LOW)
angle.stop()
print("temperature: {0:.2f} C, pressure: {1:.2f} hPa, humidity: {2:.2f} %RH".format(sensor.data.temperature, sensor.data.pressure, sensor.data.humidity))
time.sleep(1)[/align][align=left]
Tip: If the display box as shown below pop up during the editing or saving process, select the third option.
Step 6: Run the program
Python auto_fan.py
The result is as below:
Ctrl+C to exit the program.