Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
š Ready to explore and create with us? Click [here] and join today!
3.5 Controlling a Small Fan (DC Motor)ļ
In this lesson, weāll learn how to control a DC motor (like a small fan) using the Raspberry Pi Pico 2 W and an TA6586 motor driver. The TA6586 allows us to control the direction of the motor rotationāboth clockwise and counterclockwise. Since the DC motor requires a relatively large current, for safety reasons, here we use a power module to supply power to the motor.
Required Components
In this project, we need the following components.
Itās definitely convenient to buy a whole kit, hereās the link:
Name |
ITEMS IN THIS KIT |
LINK |
|---|---|---|
Pico 2 W Starter Kit |
450+ |
You can also buy them separately from the links below.
SN |
COMPONENT |
QUANTITY |
LINK |
|---|---|---|---|
1 |
1 |
||
2 |
Micro USB Cable |
1 |
|
3 |
1 |
||
4 |
Several |
||
5 |
1 |
||
6 |
1 |
||
7 |
1 |
||
8 |
18650 Battery |
1 |
Schematic

Wiring
Note
Since DC motors require a high current, we use a Li-po Charger module to power the motor here for safety reasons.
Make sure your Li-po Charger Module is connected as shown in the diagram. Otherwise, a short circuit will likely damage your battery and circuitry.
Code
Note
Open the
3.5_small_fan.pyfrompico-2w-kit-main/micropythonor copy the code into Thonny, then click āRunā or press F5.Ensure the correct interpreter is selected: MicroPython (Raspberry Pi Pico).COMxx.
import machine
import utime
motor1A = machine.Pin(14, machine.Pin.OUT)
motor2A = machine.Pin(15, machine.Pin.OUT)
def clockwise():
motor1A.high()
motor2A.low()
def anticlockwise():
motor1A.low()
motor2A.high()
def stopMotor():
motor1A.low()
motor2A.low()
while True:
clockwise()
utime.sleep(1)
stopMotor()
utime.sleep(1)
anticlockwise()
utime.sleep(1)
stopMotor()
utime.sleep(1)
Once the program is running, the motor will rotate back and forth in a regular pattern.
Understanding the Code
Initialize the Pins:
motor1Aandmotor2Aare connected to GP14 and GP15, controlling the direction of the motor.motor1A = machine.Pin(14, machine.Pin.OUT) motor2A = machine.Pin(15, machine.Pin.OUT)
Define Functions:
rotate_clockwise(): Setsmotor1Ahigh andmotor2Alow to rotate the motor clockwise.rotate_counterclockwise(): Setsmotor1Alow andmotor2Ahigh to rotate counterclockwise.stop_motor(): Sets bothmotor1Aandmotor2Alow to stop the motor.
Main Loop:
The motor rotates clockwise, stops, rotates counterclockwise, and stops again, each for one second, repeatedly.
while True: clockwise() utime.sleep(1) stopMotor() utime.sleep(1) anticlockwise() utime.sleep(1) stopMotor() utime.sleep(1)
Troubleshooting Tips
Motor Keeps Spinning After Stopping the Script:
If the motor continues to run after stopping the program, you may need to reset the Pico. Use a wire or a button to momentarily connect the RUN pin to GND, which resets the Pico.

Pico Disconnects or Becomes Unresponsive:
The motor may draw too much current, causing voltage fluctuations. Ensure youāre using a separate power supply for the motor and that all grounds are connected.
Conclusion
In this lesson, youāve learned how to control a DC motor using the TA6586 motor driver and the Raspberry Pi Pico 2 W. You can now control the motorās direction and create projects like a small fan or a motorized device.
Next Steps
Speed Control: Try using PWM (Pulse Width Modulation) to control the speed of the motor by connecting the EN1 pin to a PWM-capable GPIO pin.
Control Multiple Motors: Use the other channels of the TA6586 to control additional motors.
Sensor Integration: Incorporate sensors to control the motor based on input (e.g., temperature, light).
