Recently, I have been learning about Raspberry Pi. Just following tutorials felt empty, so I thought about making a small device to solidify what I’ve learned.
Coincidentally, some friends from Guangdong in the group have been envious of Chengdu’s climate, saying they still can’t live without short sleeves and air conditioning, which forced me to do something for their unfortunate situation.
So after careful consideration, I decided to write a tutorial on how to build a temperature-controlled fan driven by Raspberry Pi for her, hoping she can gain knowledge while enjoying some coolness. (I know it’s just a small fan? Please don’t scold me; I just happen to be a bit short on funds, but my heart for my friends remains unchanged), it’s definitely beginner-friendly (after all, I’m a beginner too, right?), so feel free to take my recommendation.
1. Hardware Preparation
Note: All the products mentioned below can be purchased from the DFRobot official mall mini-program! Input the product SKU
to search quickly.
-
Raspberry Pi 3 Model B with installed system X1 (
DFR0567-1
) or Raspberry Pi 4 Model B (4GB) X1 (DFR0619
) -
Raspberry Pi 4B/3B+ IO Expansion Board X1 (
DFR0566
) (Note: It must have IO ports) -
Servo X1 (
SER0011
) -
DC Motor Fan X1 (
DFR0411
) (one piece solves the motor fan issue, not bad) -
BME680 Environmental Sensor X1 (
SEN0248
) (can measure many things, but I only used temperature this time) -
Data Cable X1 (choose between Android cable or type C cable based on Raspberry Pi’s power interface) and a 5V/3A USB power adapter, it’s recommended to prepare an adapter instead of powering directly from the host) (
FIT0351-OE
) -
Network Cable X1 (the Raspberry Pi should be connected to the same router as the PC)

2. Software Preparation
The remote connection software I used is MobaXterm.
3. Specific Operations
1. Hardware Connection (as shown)
Insert the expansion board onto the Raspberry Pi, then connect the DC motor fan to port 5
, the servo to port 22
, and the environmental sensor to the IIC
port.


Of course, you can choose other pins, but please ensure that the motor and servo are connected to GPIO ports; you can check the specific pins in the figure below.

Just ensure the network cable and power cable are connected correctly. (Note: It’s best to use an adapter for the power cable plugged into the socket, do not power directly from the host, as the main unit’s current is too low to ensure smooth operation of the motor and servo.)
2. Enable Pins and Install I2C Libraries and Tools
Before using the I2C pins of the Raspberry Pi, we need to enable the I2C pins. Input the following code and follow the animation to complete.
sudo raspi-config

When selecting, use the up and down keys on the right side of the keyboard, and finally exit by pressing the Esc
key in the upper left corner.
Next, we need to install the I2C library and tools:
sudo apt-get install i2c-tools

3. Download the Library Files for the BME680 Environmental Sensor
Note, it’s best to operate in the left desktop folder to avoid problems, such as lack of permission to delete or add files. (If this problem has already occurred, you can continue to operate in the desktop folder as described below.)
1) Download locally and drag into Raspberry Pi, as shown.
git clone https://github.com/DFRobot/DFRobot_BME680

2) Download directly on Raspberry Pi, please wait a moment, it takes some time.
sudo git clone https://github.com/DFRobot/DFRobot_BME680

4. Find the Data Reading File in the BME680 Library Files
Follow the order to enter the folder DFRobot_BME680
->Python
->RaspberryPi
->examples
, and you will see a python file named read_all_data.py
.
Click the path to copy it, and then input cd /home/pi/Desktop/DFRobot_BME680/Python/RaspberryPi/examples/
As shown in the animation:

Input the following code to run this file to read all the data from the environmental sensor, as shown.
python read_all_data.py

Press ctrl+c
to exit the running state.
5. Modify the Code
Based on this code, I added the code for the servo and motor fan.
You can do it this way: create a new text file on the PC desktop, rename it to auto_fan.py
, as shown:

Then open it and paste the code below, save it, and drag it into the list as shown.
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)

Tip: If a dialog box pops up during the editing and saving process, choose the third option.

6. Run the Program
python auto_fan.py
The displayed result is as shown:

Press ctrl+c
to exit the program.
The video effect is as follows:
Click to watch👆
– END –
Hardware Arsenal
Click to learn more👆
If you have any thoughts or corrections regarding the article, please feel free toleave a message!
If you have extra energy, you can click
Read the original text to continue learning in the community!
Previous Reviews
What is Raspberry Pi? Can it be eaten??
【Raspberry Pi Basic Series Tutorial】1. Configuring and Initializing Raspberry Pi
【Raspberry Pi Basic Series Tutorial】2. Remote Connection to Raspberry Pi
【Raspberry Pi Basic Series Tutorial】3. Learning Linux Commands
【Raspberry Pi Basic Series Tutorial】4. Lighting Up an LED
【Raspberry Pi Basic Series Tutorial】5. Ultrasonic Sensor Distance Measurement
【Raspberry Pi Basic Series Tutorial】6. Button Control to Shut Down Raspberry Pi
Click to read👆