Project 18:Small Fan

  1. Introduction

In the hot summer, we need an electric fan to cool us down, so in this project, we will use a Raspberry Pi Pico to control 130 motor module and small blade to make a small fan.

  1. Component Knowledge

Raspberry Pi Pico*1 Raspberry Pi Pico Expansion Board*1
L293D Chip*1 DC Motor*1 Breadboard*1 Fan*1 Jumper Wore USB Cable*1
  1. Component Knowledge:

L293D Chip:

L293D is a direct current drive IC, which can be used to drive DC motor or stepper motor in some robot projects.

It has a total of 16 pins and can drive two-channel DC motors at the same time.

Its Input voltage range is 4.5 V ~ 36 V, the output current of per channel is MAX 600mA, which can drive inductive loads. What’s more, its input end can be directly connected and controlled by the single-chip microcomputer.

When driving a small DC motor, the control of two-channel motors and the forward and reverse rotation can be realized by changing the high and low level of the input terminal. There are many motor drive boards using L293D chips on the market, of course, we can also use it via simply connecting.

L293D Pin out:

No Name Description
1 Enable1,2 Enable pin Input 1(2)and Input 2(7)
2 Input1 Directly input pin 1via digital circuit
3 Output1 Connected to one end of motor1
4 GND Grounded(0V)
5 GND Grounded(0V)
6 Output2 Connected to one end of motor1
7 Input2 Directly output pin 2 via digital circuit
8 Vcc2 (Vss) Connected to voltage pin of motor(4.5V-36V)
9 Enable3,4 Enable pin 3(10)and 4(15)
10 Input3 Input3 pin, controlled by digital circuit
11 Output3 Connected to one end of motor2
12 GND Grounded(0V)
13 GND Grounded(0V)
14 Output4 Connected to one end of motor2
15 Input4 Input4 pin, controlled by digital circuit
16 Vcc1(Vss) Connect + 5V to enable IC function
  1. Circuit diagram and wiring diagram:

  1. Test Code

The code used in this tutorial is saved in the file …\Python_Codes. You can move the code to anywhere,for example,we can save the Python_Codes file in the Disk(D), the route is <span “color: rgb(0, 209, 0);”>D:\Python_Codes.

Open“Thonny”, click“This computer”→“D:”→“Python_Codes”→“Project 18:Small Fan”. And double left-click the“Project_18_ Small_Fan.py”.

from machine import Pin
import time
#Pin of each section of L293D and set as output
IN1_Pin = 17
IN2_Pin = 16
ENA_Pin = 18 #Control the speed of the motor
# speed:speed,0-100
# direction: Rotation direction,1 is clockwise,0 stops,-1 counterclockwise
# speed_pin:Pin number that controls the start and stop of the motor
def motorRun(speed, direction, speed_pin, clockwise_pin, anti_clockwise_pin):
if speed > 100: speed=100
if speed < 0: speed=0
in1 = machine.Pin(anti_clockwise_pin, machine.Pin.OUT)
in2 = machine.Pin(clockwise_pin, machine.Pin.OUT)
pwm = machine.PWM(machine.Pin(speed_pin))
pwm.freq(50)
pwm.duty_u16(int(speed/100*65535))
if direction < 0:
in2.value(0)
in1.value(1)
if direction == 0:
in2.value(0)
in1.value(0)
if direction > 0:
in2.value(1)
in1.value(0)
while True:
motorRun(100, 1, ENA_Pin, IN2_Pin, IN1_Pin)
time.sleep(5)
motorRun(100, 0, ENA_Pin, IN2_Pin, IN1_Pin)
time.sleep(2)
motorRun(80, -1, ENA_Pin, IN2_Pin, IN1_Pin)
time.sleep(5)
motorRun(100, 0, ENA_Pin, IN2_Pin, IN1_Pin)
time.sleep(2)
  1. Test Result

Ensure that the Raspberry Pi Pico is connected to the computer,click“Stop/Restart backend”.

Click“Run current script”, the code starts executing, we will see that The small fan turns counterclockwise for 5 seconds and stops for 2 seconds, and then turns clockwise for 5 seconds and stops for 2 seconds. Repeat this rule for 5 times and then the small fan stops. Press“Ctrl+C”or click“Stop/Restart backend”to exit the program.