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+

Pico 2 W Kit

You can also buy them separately from the links below.

SN

COMPONENT

QUANTITY

LINK

1

Getting to Know Pico 2 W

1

2

Micro USB Cable

1

3

Breadboard

1

BUY

4

Jumper Wires

Several

BUY

5

TA6586 - Motor Driver Chip

1

6

DC Motor

1

BUY

7

Li-po Charger Module

1

8

18650 Battery

1

Schematic

sch_motor

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.

wiring_motor

Code

Note

  • Open the 3.5_small_fan.py from pico-2w-kit-main/micropython or 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

  1. Initialize the Pins:

    motor1A and motor2A are 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)
    
  2. Define Functions:

    • rotate_clockwise(): Sets motor1A high and motor2A low to rotate the motor clockwise.

    • rotate_counterclockwise(): Sets motor1A low and motor2A high to rotate counterclockwise.

    • stop_motor(): Sets both motor1A and motor2A low to stop the motor.

  3. 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.

    wiring_run_reset

  • 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).