X
Home > Blog > STEM for Arduino > HOW to Make an Automatic Fan with Kidspico

HOW to Make an Automatic Fan with Kidspico

By r October 24th, 2024 46 views
We design to trigger the fan to automatically work when something approaches to it. This trigger mode integrates an ultrasonic sensor and an 130 motor.

Flow

4201

Assembly

line1

Required Components

42_00

line1

Step 1

42_01

line1

Step 2

42_02

line1

Step 3

42_03

line1

Step 4

42_04

line1

Step 5

42_05

line1

Step 6

42_06

line1

Step 7

42_07

line1

Completed

42_08

line1

Wiring Diagram

4202

Test Code

Open 4.2Trigger mode.py and click 1407.

'''
 * Filename    : Trigger mode
 * Thonny      : Thonny 4.1.4
 * Auther      : http//www.keyestudio.com
'''
from machine import Pin
import time

Trig = Pin(21, Pin.OUT) 
Echo = Pin(27, Pin.IN)
INA = Pin(14, Pin.OUT) 
INB = Pin(15, Pin.OUT) 

distance = 0 # set initial distance to 0
soundVelocity = 340 #Set the speed of sound.

def getDistance():
    # maintain Trig pin at high level for 10us to enable the ultrasonic sensor
    Trig.value(1)
    time.sleep_us(10)
    Trig.value(0)
    #Start counting, record the initial time when ultrasonic waves starts to transmit
    while Echo.value() == 0:
        Start = time.ticks_us()
    #record the time when the ultrasonic echo is received
    while Echo.value() == 1:
        Stop = time.ticks_us()
    #total time = the receiving time - the initial time
    Time = time.ticks_diff(Stop,Start)
    # Calculat the distance according to the formula: distance(m)
    # distance value devide by 100: distance(cm)
    distance =  Time * soundVelocity //2 // 10000
    #return the distance value in cm
    return distance

while True:
    Distance = getDistance()
    print('Distance: ', Distance, 'cm')
    time.sleep_ms(500)
    if Distance < 10:
        INA.value(0)
        INB.value(1)
        time.sleep(1)
    else:
        INA.value(0)
        INB.value(0)
        time.sleep(1)

Explanations

5top

Conceive:

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

  2. Loop:

    ① Print the detected distance value every 500ms.

    ② Determine whether the value is less than 10 (this threshold is adjustable according to needs).

    • distance < 10: the fan starts to rotates.

    • distance >= 10: the fan does not work.

5bottom

Test Result
4top
After uploading code, get close to the ultrasonic sensor. When the ultrasonic sensor detects things around itself with a distance less than 10, the fan will rotates. If the distance equals or is greater than 10, the fan will stop rotating.
4203
4bottom
HOW to Make A Fan with Temperature Detection Function by Kidspico
Previous
HOW to Make A Fan with Temperature Detection Function by Kidspico
Read More
HOW to Make a Track Alarm with KidsIOT
Next
HOW to Make a Track Alarm with KidsIOT
Read More
Message Us