X
Home > Blog > STEM for Arduino > HOW to Make A Fan with Temperature Detection Function by Kidspico

HOW to Make A Fan with Temperature Detection Function by Kidspico

By r October 23rd, 2024 46 views
The fan will monitor the operating temperature. When it is overheat, the fan will start to rotate automatically.
This temperature detection device adopts a DS18B20 temperature sensor and an 130 motor.

Flow

4101

Assembly

line1

Required Components

41_00

line1

Step 1

41_01

line1

Step 2

41_02

line1

Step 3

41_03

line1

Step 4

41_04

line1

Step 5

41_05

line1

Step 6

41_06

line1

Step 7

41_07

line1

Completed

41_08

line1

Wiring Diagram

4102

Test Code

Open 4.1Temperature detection.py and click 1407.

'''
 * Filename    : Temperature detection
 * Thonny      : Thonny 4.1.4
 * Auther      : http//www.keyestudio.com
'''
from machine import Pin
import machine
import onewire
import ds18x20
import time

# motor pins
INA = Pin(14, Pin.OUT) #INA corresponds to IN+
INB = Pin(15, Pin.OUT) #INB corresponds to IN- 

# connect DS18B20 to IO22
ds18b20_pin = machine.Pin(22)
ds18b20_sensor = ds18x20.DS18X20(onewire.OneWire(ds18b20_pin))
 
roms = ds18b20_sensor.scan()

temp = 0

while True:
    # delay 1s to wait for the stablization of DS18B20 sensor
    time.sleep(1)
    if roms:
        rom = roms[0]
        res = ds18b20_sensor.convert_temp()
        time.sleep(0.1)
        
        temp = ds18b20_sensor.read_temp(rom)
        print(temp)
    else:
        print("No DS18B20 sensor found!")
    if temp > 30:
        INA.value(0)
        INB.value(1)
    else:
        INA.value(0)
        INB.value(0)       

Explanations

5top

Conceive:

  1. Initialization: set the pins of DS18B20 temperature sensor and 130 motor.

  2. Loop:

    DS18B20 sensor reads the temperature value and outputs it.

    Determine whether the temperature value is greater than 30 (this threshold is adjustable according to needs).

    • temperature > 30: the fan rotates to cool down.

    • temperature <= 30: the fan does not work.

5bottom

Test Result
4top
NOTE: Please connect to a power supply externally to avoid uploading failure due to a power shortage.
After uploading code, the fan will rotate for cooling down when the temperature value exceeds 30°.
You can raise the temperature by pinching the DS18B20 sensor with your finger or lighting a lighter near the sensor.
WARNING: Please use lighters with the guidance of an adult!
4103
4bottom
HOW to Make a Variable Speed Fan with KidsIOT
Previous
HOW to Make a Variable Speed Fan with KidsIOT
Read More
HOW to Make a Track Alarm with KidsIOT
Next
HOW to Make a Track Alarm with KidsIOT
Read More
Message Us