X
Home > Blog > STEM for Arduino > HOW to Make a Remote Control Fan with KidsIOT

HOW to Make a Remote Control Fan with KidsIOT

By r October 18th, 2024 47 views
In this project, we utilize an IR receiver and an 130 motor to construct a remote control fan, whose startup and rotation speed are controlled by a infrared remote control.

Flow

4301

Assembly

line1

Required Components

43_00

line1

Step 1

43_01

line1

Step 2

43_02

line1

Step 3

43_03

line1

Step 4

43_04

line1

Step 5

43_05

line1

Step6

43_06

line1

Completed

43_07

line1

Wiring Diagram

4302

Test Code

Open KidsBlock and connect the board to computer. Choose File –> Load from your computer.

3111

Choose path D:\Code\2.Code_kidsIOT and open 4.3Remote control.sb3.

4303

Click 3209 to connect to port and then 2210.

Explanations

5top

Code Blocks

Blocks

Code block

Events

begin

Vari

variablename

Vari

setvariable

Vari

variable++

Vari

variable

IR

IR_pin

IR

IR_Received

IR

IR_read

IR

IR_Refresh

Operators

=

Operators

mod

Motor

Motor_analogWrite

Control

forever

Control

ifelse

Control

if

line5

Build blocks:

Click add to import libraries DC Motor for esp32 and ir remote.

4304

  1. Initialization

    Define four variables with initial values of 0: key1_press_num; key2_press_num; key3_press_num; key.

    The first three variables are used to store the pressing times of key 1, key 2 and key 3; the last one records the data received by IR receiver.

    Zero out INA and INB, and initialize the IR receiver pin IO13.

    4305

line3

  1. Main Code

    Loop: Press key 1, key 2 and key 3, the fan will rotates at different speed. Press again, the fan stops. These three speeds can have a switchover without stopping the fan.

    4306


    Build blocks:

     forever

    ② Add if, IR_Received, setvariable, IR_read and IR_Refresh as follows:

    4307

    When an infrared signal is received, assign the received value to the variable key and then refresh the received value.

    ③ Put if, =, variable, variable++ and setvariable as follows: It determines whether the key 1 is pressed.

    4308

    If key = 16738455 (key 1 is pressed), key1_press_num will adds one, and key2_press_num and key3_press_num will zero out.

    ④ Put ifelse, =, mod, variable and Motor_analogWrite as follows:

    4309

    These code blocks determine whether the remainder of key1_press_num divided by 2 equals 1.

    • remainder = 1: fan rotates.

    • remainder ≠ 1: fan stops.

    ⑤ Put the blocks in ④ into ③:

    4310

    For this whole, the fan will rotate at a high speed if key 1 is pressed; it stops when key 1 is pressed again.

    Analysis:

    Press key 1 once, key1_press_num(initial value is 0) adds one, and key2_press_num and key3_press_num zero out.

    Determine whether the remainder of key1_press_num divided by 2 equals 1: herein, 1 ÷ 2 = 0…1, so the condition is met, and the fan rotates at high speed.

    Press key 1 again, key1_press_num adds one. Now it equals 2, and the other two values is 0.

    Determine whether the remainder of key1_press_num divided by 2 equals 1: herein, 2 ÷ 2 =1…0, so the condition is not satisfied, and the fan stops.

    ⑥ Duplicate the “if” block in ⑤ twice.

    4311

    As follows:

    4312

    The working principles are the same. These two copies are used to control key 2 and key 3. For key 2, the fan will rotates at a medium speed; For key 3, it is at low speed.

    ⑦ Drag blocks of ⑥ into ②, as follows:

    4313

    When infrared signals are received, determine whether they are the decimal codes of key 1, key 2 and key 3.

    Then, determine the number of pressing time is odd or even. Odd: the fan rotates at corresponding speed; Even: fan stops.

    6top

    Q :Why zero out the other two variables?

    These three keys work according to the same principle. When the pressing times is an odd number, the fan rotates; otherwise, the fan stops.

    If we do not zero out them, we cannot make sure every time these three speeds can have a switchover without stopping the fan. This is because we have no idea whether the value of the other two is always even, so the fan may change speed or stop when we press another key. We zero out them when we press one key, therefore the fan can immediately change its speed without stopping when we press another key.

    A : This step ensures that, after we press one key, the values of the other two are always even, so that these three speeds can have a switchover without stopping the fan.

    6bottom

5bottom

Test Result
4top
After uploading code, we can control the startup and speed of the fan via the remote control.
Key 1: high speed
Key 2: medium speed
Key 3: low speed
These three speeds can have a switchover without stopping the fan. Press the same key again and the fan will stop.
4303
4bottom
HOW to Make an Automatic Fan with KidsIOT
Previous
HOW to Make an Automatic 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