Controlling computer fan with transistor and basic code-not working

Made the circuit attached only with no diode and a 2N3904 transistor. Using the example code "Fade" as well as a few of my own but the multiple fans I've tried all refuse to spin.

Circuit works at 5 V instead of 12 with LED's...5 volts wont turn the fan anyway (not without a push)

Tried upping the voltage and Amp on my power supply...no luck

Tried rotating transistor so Emitter was collector and collector was emitter...fan turned constantly at full speed, unsure why...

Fans all work when connected to regular power supply...they are all the 3 wire variety wit red+black, then yellow for measuring RPM (i think?)

Anyway need help

image01.gif

I've kept at it but no success. I forgot about coding for now and focused on the circuit. I just used a optocoupler and a 9V battery. Whether or not the optocoupler has 5V coming from the arduino the fan wont spin...it spins beautifully when when connected to the battery.

Will be checking the thread frequently, please help

No, no, no... go back to the first circuit!

Hi,
A picture of your project will help so we can see if your layout is okay.
Almost sounds like you have got transistor fitted the wrong way.

Tom.... :slight_smile:

Picture attached is my circuit. The black and red wires exiting the bottom right connect the fan.

Remade the circuit, it sort of works...when I run the code with the maximum pin output (1023) the fan spins at full speed...cool.

I want to vary the fan speed with PWM...but at different values the fan doesn't spin.

int fan = 3;

void setup() {
  Serial.begin(9600);
pinMode(fan, OUTPUT);
}

void loop() {

analogWrite(fan, 1023);
delay(3000);
analogWrite(fan, 0);
delay(3000);
analogWrite(fan, 512);
delay(3000);
analogWrite(fan, 0);
delay(3000);
}

I know this isn't a coding forum but hopefully it's simple enough nobody minds. I'll check somewhere else if otherwise.

tldr; need to run at half speed now, either changing the circuit or changing the code.

How big is the fan. 120mm? Could draw ~200mA. Bit much for a 2N3904.
Saturating the transistor for 200mA requires 10mA base current.
The opto and the resistor will limit base current to only 1mA...
If you don't saturate the transistor, it will get hot.
Can't you share grounds, dump the opto, and drive the base directly from a PWM pin.

analogWrite(fan, 1023); // Looks like an Uno clone. 100% PWM is 255, not 1023
Leo..

Hi,

  • If you jumper pins 4 and 5 of the opto together, does the fan run?
  • What is the voltage across the two 220R resistors when the fan is supposed to be running?

Here is a useful trick, put an LED in series with the opto LED, change the series resistor by recalculating your LED current for 10mA, the extra external LED will act as an fan on/off indicator and not consume any extra current.
(In this case be good to help circuit debugging)

Fans all work when connected to regular power supply...they are all the 3 wire variety wit red+black, then yellow for measuring RPM

You have BLDC fans they do not like running at half or 3/4 rated voltage, they have built in speed control circuity, to keep fan speed constant, varying the supply voltage to change speed is just a unstable/unpredictable response from the fan control circuit.
Use digitalWrite instead of analogWrite and it will work properly.

Tom.... :slight_smile:

Wawa:
Can't you share grounds, dump the opto, and drive the base directly from a PWM pin.

analogWrite(fan, 1023); // Looks like an Uno clone. 100% PWM is 255, not 1023
Leo..

Unsure of fan size, bigger then 40mm, probably about 65mm, not 120 though I desire to control those too.

I'll try just using Pin9 for the base...It wasn't working earlier, I'll try again when I get back to the workshop tomorrow.

TomGeorge:
Hi,

  • If you jumper pins 4 and 5 of the opto together, does the fan run?

Use digitalWrite instead of analogWrite and it will work properly.

Tom.... :slight_smile:

[/quote]

So connect 12V to 4&5 (essentially feeding the 12V power through the 10K to the transistor base)...I recall it working.

So digitalWrite instead of Analog, can I still preform PWM funtions with digitalWrite?
Only way I can think of it is

digitalWrite(fan, HIGH)
delay(20)
digitalWrite(fan, LOW)
delay(20)

Chill_Polins:
I'll try just using Pin9 for the base...It wasn't working earlier, I'll try again when I get back to the workshop tomorrow.

You might already have blown the transistor with all your experiments.
Never connect a transistor backward.
Always use a current limiting resistor between PWM pin and base.
Calculate base current for ~1/20 of the collector current. 470ohm is a good start.

So +fan to +12volt supply, -fan to collector. Add a diode across the fan.
-12volt supply (ground) to emitter and Arduino ground.
PWM pin via ~470ohm resistor to base.

analogWrite(fan, 255); // sets PWM to 100%
analogWrite(fan, 128); // sets PWM and fan power (not speed) to 50%
Leo..

Sorry, I got busy and forgot about this project.

I played and searched around some more. This picture and thread helped a lot for anyone coming back to the topic. I don't quite understand everything 100% but they explain in the thread here Is this circuit okay? Arduino using MOSFET IRF540N as a switch for a motor - General Electronics - Arduino Forum

I used an IRF520 and it works fine.

I see that the 5V gate signal and the negative from the 12V supply need to share groundsFOR SOME REASON..wanted to separate circuit with optocoupler but that would require a second 5V supply independent of the Arduino...not interested.

Thanks to all that helped, I appreciate it.

Hi,
The reason that the two supplies, 5V for the gate and 12V for the fan, need to have their gnd/neg connected together is because the MOSFET is controlled by a voltage between the gate and source, the fan current needs to go through the source to drain circuit.
So both voltages need to have their gnd or neg connected to the source.

Tom... :slight_smile:

interesting, in my first post I attached a picture of a circuit with an octocoupler. However it gave no details on where to connect pin 6 of the IC...is this connected to the 12V source I'm controlling.

Chill_Polins:
interesting, in my first post I attached a picture of a circuit with an octocoupler. However it gave no details on where to connect pin 6 of the IC...is this connected to the 12V source I'm controlling.

It's typically not connected to anything. The most basic use would be to use it to tweak the output of the optocoupler by giving it a weak pullup or down.

interesting, in my first post I attached a picture of a circuit with an octocoupler. However it gave no details on where to connect pin 6 of the IC...is this connected to the 12V source I'm controlling.

The only reason your original circuit didn't work is that both the optocoupler and the fan transistor are not being driven adequately.

Here's the datasheets for the components:
https://www.fairchildsemi.com/datasheets/BD/BD135.pdf

Although the transfer ratio for the CNY75 is quite low, it increases with more current driven through the led. Calculating the required resistor for 20mA, we get (5-1.25)/0.020 = 187.5Ω Also, the 10K base resistor for the BD135 is far to high.

A simple solution would be to use 220Ω for the optocoupler and 470Ω for the transistor.

The range of control for analogWrite (PWM) is 0-255.

When testing the fan, if you slowly increase the duty cycle, you may find that the fan will start at 40% then speed up properly as the duty cycle reaches 100%.

If you if you slowly decrease the duty cycle, you may find that the fan will slow down normally and stop at 10%.

To get the extra control range and start the fan from a stopped condition, just give a 200ms boost of 255 (full power) just before entering your first required value.

dlloyd:
Also, the 10K base resistor for the BD135 is far to high.

A simple solution would be to use 220Ω for the optocoupler and 470Ω for the transistor.

Yes, I noticed that - expecting a transistor to switch with a 10k base resistor seems
commonplace for some reason - it isn't going to work. Perhaps people confuse darlingtons
with single BJTs.