1

I am trying to speed control a generic 12V 2 pin computer case fan (80mm x 80mm x 15mm).

I have purchased an Adafruit TB6612 1.2A DC/Stepper Motor Driver Breakout Board and a Raspberry Pi Zero W.

I have connected the fan, 12V source and TB6612 as this image, but I did not connect up PWMB, AIN2, BIN2 and STBY.

Using the code below, I can turn the fan on (source):

#!/usr/bin/env python

# Import required modules
import time
import RPi.GPIO as GPIO

# Declare the GPIO settings
GPIO.setmode(GPIO.BOARD)

# set up GPIO pins
GPIO.setup(7, GPIO.OUT) # Connected to PWMA
GPIO.setup(11, GPIO.OUT) # Connected to AIN2
GPIO.setup(12, GPIO.OUT) # Connected to AIN1
GPIO.setup(13, GPIO.OUT) # Connected to STBY

# Drive the motor clockwise
GPIO.output(12, GPIO.HIGH) # Set AIN1
GPIO.output(11, GPIO.LOW) # Set AIN2

# Set the motor speed
GPIO.output(7, GPIO.HIGH) # Set PWMA

# Disable STBY (standby)
GPIO.output(13, GPIO.HIGH)

# Wait 5 seconds
time.sleep(5)

# Drive the motor counterclockwise
GPIO.output(12, GPIO.LOW) # Set AIN1
GPIO.output(11, GPIO.HIGH) # Set AIN2

# Set the motor speed
GPIO.output(7, GPIO.HIGH) # Set PWMA

# Disable STBY (standby)
GPIO.output(13, GPIO.HIGH)

# Wait 5 seconds
time.sleep(5)

# Reset all the GPIO pins by setting them to LOW
GPIO.output(12, GPIO.LOW) # Set AIN1
GPIO.output(11, GPIO.LOW) # Set AIN2
GPIO.output(7, GPIO.LOW) # Set PWMA
GPIO.output(13, GPIO.LOW) # Set STBY

Problem

The above code only shows a 'HIGH' for PWMA

How to control the speed of the fan using the TB6612?

1
  • since this is a Q&A site, I took the liberty to add a question to your post ... also removed text that is not related to the problem ... the edit will be visible if it is accepted
    – jsotola
    Jul 10, 2021 at 20:49

2 Answers 2

0

You need to send PWM pulses to PWMA. At the moment you are setting it high (full on) or low (off).

You need to find the documentation for RPi.GPIO to see how to send PWM.

0

Using the GPIO library you can set a pin to PWM mode using the pwm = GPIO.PWM(channel, frequency), starting it by calling pwm.start(), and controlling the speed by calling pwm.ChangeDutyCycle(dc). frequency is how quickly it should pulse voltage in Hz and dc is the duty cycle or percentage (0-100) of time on. This is described in the library documentation on PWM. For reference, PWM stands for pulse width modulation, the pulse width being analogous to the period of the frequency.

Modifying your code:

#!/usr/bin/env python

# Import required modules
import time
import RPi.GPIO as GPIO

# Declare the GPIO settings
GPIO.setmode(GPIO.BOARD)

# set up GPIO pins
GPIO.setup(11, GPIO.OUT) # Connected to AIN2
GPIO.setup(12, GPIO.OUT) # Connected to AIN1
GPIO.setup(13, GPIO.OUT) # Connected to STBY

# Configure the PWM, but don't start it spinning
pwm = GPIO.PWM(7, 1000)
pwm.start(0)

# Drive the motor clockwise
GPIO.output(12, GPIO.HIGH) # Set AIN1
GPIO.output(11, GPIO.LOW) # Set AIN2

# Set the motor speed to 75%
pwm.ChangeDutyCycle(75)

# Disable STBY (standby)
GPIO.output(13, GPIO.HIGH)

# Wait 5 seconds
time.sleep(5)

# Drive the motor counterclockwise
GPIO.output(12, GPIO.LOW) # Set AIN1
GPIO.output(11, GPIO.HIGH) # Set AIN2

# Set the motor speed to 25%
pwm.ChangeDutyCycle(25)

# Disable STBY (standby)
GPIO.output(13, GPIO.HIGH)

# Wait 5 seconds
time.sleep(5)

# Reset all the GPIO pins by setting them to LOW
GPIO.output(12, GPIO.LOW) # Set AIN1
GPIO.output(11, GPIO.LOW) # Set AIN2
pwm.stop() # Turn off the PWM
GPIO.output(13, GPIO.LOW) # Set STBY

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.