X
Home > Blog > STEM for Arduino > HOW to Make a Variable Speed Fan with Kidspico

HOW to Make a Variable Speed Fan with Kidspico

By r October 29th, 2024 47 views
In this experiment, we build a small fan with variable rotation speed: 40%, 60%, 80% and 100%. The required modules are a five-channel AD button module (speed control) and an 130 motor.

Flow

4501

Assembly

line1

Required Components

45_00

line1

Step 1

45_01

line1

Step 2

45_02

line1

Step 3

45_03

line1

Step 4

45_04

line1

Step 5

45_05

line1

Step 6

45_06

line1

Step 7

45_07

line1

Completed

45_08

line1

Wiring Diagram

4502

Test Code

Open 4.5Gearshift mode.py and click 1407.

'''
 * Filename    : Gearshift mode
 * Thonny      : Thonny 4.1.4
 * Auther      : http//www.keyestudio.com
'''
from machine import Pin,PWM,ADC
import time

key = ADC(26)

# set INA and INB to PWM pins
pwm_INA = PWM(Pin(14)) 
pwm_INB = PWM(Pin(15)) 
 
# set PWM frequency. The frequency depends on the motor application.
pwm_INA.freq(500)
pwm_INB.freq(500)
 
# Set the maximum and minimum rotation speed
max_duty = 65535
min_duty = 0
 
# Linear interpolation is used to calculate duty values for different speeds
def calc_duty(speed):
    speed = int(speed * (max_duty - min_duty) / 100 + min_duty)
    return speed
 
# change the rotation speed
def set_speed_INA(speed):
    duty = calc_duty(speed)
    pwm_INA.duty_u16(duty)

# change the rotation speed
def set_speed_INB(speed):
    duty = calc_duty(speed)
    pwm_INB.duty_u16(duty)
    
while True:
    key_value = key.read_u16()

    if key_value < 10000:   # no key  is pressed
        pass

    elif key_value < 14000: # SW5 is pressed
        set_speed_INA(0)
        set_speed_INB(40)

    elif key_value < 27000: # SW4 is pressed
        set_speed_INA(0)
        set_speed_INB(60)

    elif key_value < 40000: # SW3 is pressed
        set_speed_INA(0)
        set_speed_INB(80)

    elif key_value < 53000: # SW2 is pressed
        set_speed_INA(0)
        set_speed_INB(100)

    else:                   # SW1 is pressed                 
        set_speed_INA(0)
        set_speed_INB(0)
    time.sleep(0.1)

Explanations

5top

Conceive:

The code structure of program is similar to that of Chapter 3.8, so please refer to that section for detailed explanations.

  1. Initialization: Set pins of AD button and 130 motor.

  2. Loop:

    ① Press button 5 to adjust the speed to 40.

    ② Press button 4 to adjust the speed to 60.

    ③ Press button 3 to adjust the speed to 80.

    ④ Press button 2 to adjust the speed to 100.

    ⑤ Press button 1 to stop the fan.

5bottom

Test Result
4top
After uploading code, press button 5 - 2 orderly, and the fan speed will be adjusted to 40, 60, 80,100 accordingly. When you press button 1, the fan turns off.
4503
4bottom
HOW to Make a Manual Control Fan with Kidspico
Previous
HOW to Make a Manual Control Fan with Kidspico
Read More
HOW to Make a Track Alarm with KidsIOT
Next
HOW to Make a Track Alarm with KidsIOT
Read More
Message Us