skyales
Posts: 12
Joined: Wed Jan 16, 2019 10:18 pm

Control Fan by switching USB port on and off

Mon Jan 21, 2019 11:47 pm

Hello guys,
I am a total beginner to begin with :D
But I want to power 4 fans with one or two USB ports.
The fans are DC5V. I will attach a picture of them. I cut a USB cable and I will use the red and black cable to power the fans.
I have a PIR sensor that starts a .sh Script when motion is detected. In this Script I want to write a command which turns on a specific USB port and the USB port powers the fans.

Has someone an idea which command I could use?
Screenshot_20190122-004605~2.png
Screenshot_20190122-004605~2.png (123.36 KiB) Viewed 1446 times

User avatar
Imperf3kt
Posts: 2904
Joined: Tue Jun 20, 2017 12:16 am
Location: Australia

Re: Control Fan by switching USB port on and off

Tue Jan 22, 2019 12:45 am

Will this be running headlessly via ethernet?
55:55:44:44:4C
52:4C:52:42:41

skyales
Posts: 12
Joined: Wed Jan 16, 2019 10:18 pm

Re: Control Fan by switching USB port on and off

Tue Jan 22, 2019 1:00 am

Imperf3kt wrote:
Tue Jan 22, 2019 12:45 am
Will this be running headlessly via ethernet?
No I'm using the Pi for a magic mirror. Everytime the PIR detects motion, the screen turns on and I want the fans to turn on too. The mirror is a closed system, and the screen heats up fast. That's why I want to cool it down only when the screen turns on (Motion detected).

hippy
Posts: 6104
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Control Fan by switching USB port on and off

Tue Jan 22, 2019 1:06 am

skyales wrote:
Mon Jan 21, 2019 11:47 pm
In this Script I want to write a command which turns on a specific USB port and the USB port powers the fans.
It cannot be done this way. The Pi does not have individual USB power control; it's all or nothing, all four ports powered or none.

You need GPIO lines to switch relays or a FET to turn power to the fans on and off or a USB controlled relay or switch.

skyales
Posts: 12
Joined: Wed Jan 16, 2019 10:18 pm

Re: Control Fan by switching USB port on and off

Tue Jan 22, 2019 1:29 am

hippy wrote:
Tue Jan 22, 2019 1:06 am
skyales wrote:
Mon Jan 21, 2019 11:47 pm
In this Script I want to write a command which turns on a specific USB port and the USB port powers the fans.
It cannot be done this way. The Pi does not have individual USB power control; it's all or nothing, all four ports powered or none.

You need GPIO lines to switch relays or a FET to turn power to the fans on and off or a USB controlled relay or switch.
Ok thanks! Good to know.
What would be the easiest way? Can I control all of the suggestions with the sh script? The USB relay looks easy, or is there a lot of programming necessary?

User avatar
Imperf3kt
Posts: 2904
Joined: Tue Jun 20, 2017 12:16 am
Location: Australia

Re: Control Fan by switching USB port on and off

Tue Jan 22, 2019 1:58 am

The easiest way, is to find out the current your device uses, select a suitable bipolar transistor capable of delivering that current or more, and wire it up to a single GPIO with a resistor, ground and a voltage source (if the screen is 5v, you can use pin 2 or 4)

If the GPIO is high, the transistor gate opens, and current flows to the screen, turning it on. If it is low, the gate closes and the screen goes off.

But personally, I think your focus should lie elsewhere.
Why is the screen overheating?
55:55:44:44:4C
52:4C:52:42:41

jbudd
Posts: 1020
Joined: Mon Dec 16, 2013 10:23 am

Re: Control Fan by switching USB port on and off

Tue Jan 22, 2019 6:02 am

hippy wrote:
Tue Jan 22, 2019 1:06 am
It cannot be done this way. The Pi does not have individual USB power control; it's all or nothing, all four ports powered or none.
I seem to recall reading an article about reducing power consumption which suggested that on the 3B+ at least, you can disable the Ethernet and just two of the 4 USB sockets

skyales
Posts: 12
Joined: Wed Jan 16, 2019 10:18 pm

Re: Control Fan by switching USB port on and off

Tue Jan 22, 2019 1:29 pm

@Imperf3kt
I only want to control the 4 fans by running the sh script that turns the HDMI off( screen standby).
Can you tell me how to connect which part?
What parts do I need?

@jbudd
I have a 3 B+. So it is possible with USB?
Do you remember how?

hippy
Posts: 6104
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Control Fan by switching USB port on and off

Tue Jan 22, 2019 1:33 pm

jbudd wrote:
Tue Jan 22, 2019 6:02 am
hippy wrote:
Tue Jan 22, 2019 1:06 am
It cannot be done this way. The Pi does not have individual USB power control; it's all or nothing, all four ports powered or none.
I seem to recall reading an article about reducing power consumption which suggested that on the 3B+ at least, you can disable the Ethernet and just two of the 4 USB sockets
Probably the same article I remember reading but don't recall where. But I do recall the article I read seemed to be confusing 'disabling ports' with 'turning off power to individual ports'.

Added: Possibly this, but I'm sure it was somewhere else. Pretty much claims the same thing, that it does work, contradicted by it apparently not being possible that it would work -

https://www.raspberrypi.org/forums/view ... p?t=172313

If we had schematics which at least showed how the USB ports were wired we could simply look to see where the USB port power rails went, whether all were joined, were fed from a single power controlling chip, or separately. Instead one would have to reverse engineer the circuit or continuity check pins to see how it is, or wait for someone from the Foundation to provide a definitive answer.

My understanding is all USB port power rails are commoned and fed from a single power controlling chip ( when not fed direct from 5V ), but I may be wrong, and I cannot cite a source for having come to that understanding.

Added: Probably the above link and similar.

jbudd
Posts: 1020
Joined: Mon Dec 16, 2013 10:23 am

Re: Control Fan by switching USB port on and off

Tue Jan 22, 2019 1:58 pm

I was just going to post that link too. But that's not the one I remember. It is discussing the Pi3B.

However it triggers a vague memory that there were two discrete USB drivers in /sys/bus/drivers and thus a way to switch two USB ports or two plus ethernet. Sadly my 3B+ is no more so I can't check.

I cannot find the post I originally read but it was discussing ways to minimise power consumption on batteries. It did have figures suggesting it worked.
But I might have imagined all that!

I'm sure one of the moderators must know if it's possible?


I posted the link below.
But I'm guessing that disabling the ports will not cut off the voltage available at the port
Last edited by jbudd on Tue Jan 22, 2019 2:18 pm, edited 1 time in total.


skyales
Posts: 12
Joined: Wed Jan 16, 2019 10:18 pm

Re: Control Fan by switching USB port on and off

Tue Jan 22, 2019 2:53 pm

So it is possible? :D That would be perfect, but how do I control it via sh script? I have an existing sh script and I wand to add the needed commands.

hippy
Posts: 6104
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Control Fan by switching USB port on and off

Tue Jan 22, 2019 4:00 pm

skyales wrote:
Tue Jan 22, 2019 2:53 pm
So it is possible? :D
No it is not possible.

Disabling individual or groups of USB ports reduces power consumption within the USB/LAN chip but that doesn't disconnect power delivered to the individual USB ports.

skyales
Posts: 12
Joined: Wed Jan 16, 2019 10:18 pm

Re: Control Fan by switching USB port on and off

Tue Jan 22, 2019 4:53 pm

hippy wrote:
Tue Jan 22, 2019 4:00 pm
skyales wrote:
Tue Jan 22, 2019 2:53 pm
So it is possible? :D
No it is not possible.

Disabling individual or groups of USB ports reduces power consumption within the USB/LAN chip but that doesn't disconnect power delivered to the individual USB ports.
Oh I'm sorry for misunderstanding it..
I think I'll try to use the gpio's.
One fan has a current of 0,1A and DC5V.
I want to run 4 of them.
What specific parts do I need?

Sorry for my non existing knowledge... But can you tell me what I need to build the command in an sh Script?

User avatar
Imperf3kt
Posts: 2904
Joined: Tue Jun 20, 2017 12:16 am
Location: Australia

Re: Control Fan by switching USB port on and off

Tue Jan 22, 2019 9:16 pm

https://en.wikipedia.org/wiki/2N2907

This can handle 0.6A of current, enough for four 0.1A fans.
Then read this.
http://www.learningaboutelectronics.com ... ircuit.php
55:55:44:44:4C
52:4C:52:42:41

skyales
Posts: 12
Joined: Wed Jan 16, 2019 10:18 pm

Re: Control Fan by switching USB port on and off

Wed Jan 23, 2019 2:26 pm

Imperf3kt wrote:
Tue Jan 22, 2019 9:16 pm
https://en.wikipedia.org/wiki/2N2907

This can handle 0.6A of current, enough for four 0.1A fans.
Then read this.
http://www.learningaboutelectronics.com ... ircuit.php
Thank you very much! That helped me a lot!
I attached the circuit. In some blogs I read that some transistors need base resistors. I didn't figure out what resistor I need.
I think the formula is Rb=Vbe/IB
Vbe should be 5v
IB =?

Type Designator: 2N2907
SMD Transistor Code: 2B
Material of Transistor: Si
Polarity: PNP
Maximum Collector Power Dissipation(Pc): 0.4 W
Maximum Collector-Base Voltage |Vcb|: 60V
Maximum Collector-Emitter Voltage |Vce|: 40 V
Maximum Emitter-Base Voltage |Veb|: 5 V
Maximum Collector Current |Ic max|: 0.6 A
Max. Operating Junction Temperature (Tj): 200 °C
Transition Frequency (ft): 200 MHz
Collector Capacitance (Cc): 8 pF
Forward Current Transfer Ratio (hFE), MIN: 35
Noise Figure, dB: -
Package: TO18

Screenshot_20190123-152529~2.png
Screenshot_20190123-152529~2.png (204.46 KiB) Viewed 1183 times

User avatar
Mortimer
Posts: 923
Joined: Sun Jun 10, 2012 3:57 pm

Re: Control Fan by switching USB port on and off

Wed Jan 23, 2019 2:54 pm

You'll need snubbers on those motors, to 'snub' the back EMF from the motors when they are turned off. Otherwise you'll get voltage spikes going back into the 5V supply rail.

It might also be preferable for the 5V supply to come directly from the PSU, rather than via the RPi.
--------------
The purpose of a little toe is to ensure you keep your furniture in the right place.

skyales
Posts: 12
Joined: Wed Jan 16, 2019 10:18 pm

Re: Control Fan by switching USB port on and off

Wed Jan 23, 2019 3:34 pm

Mortimer wrote:
Wed Jan 23, 2019 2:54 pm
You'll need snubbers on those motors, to 'snub' the back EMF from the motors when they are turned off. Otherwise you'll get voltage spikes going back into the 5V supply rail.

It might also be preferable for the 5V supply to come directly from the PSU, rather than via the RPi.
Thank you that's a good idea!
Would 1 diode for the 4 motors be enough?
Which one would you consider for this circuit?

drgeoff
Posts: 9901
Joined: Wed Jan 25, 2012 6:39 pm

Re: Control Fan by switching USB port on and off

Wed Jan 23, 2019 5:25 pm

skyales wrote:
Wed Jan 23, 2019 2:26 pm
In some blogs I read that some transistors need base resistors. I didn't figure out what resistor I need.
I think the formula is Rb=Vbe/IB
Vbe should be 5v
IB =?

Type Designator: 2N2907
SMD Transistor Code: 2B
Material of Transistor: Si
Polarity: PNP
Maximum Collector Power Dissipation(Pc): 0.4 W
Maximum Collector-Base Voltage |Vcb|: 60V
Maximum Collector-Emitter Voltage |Vce|: 40 V
Maximum Emitter-Base Voltage |Veb|: 5 V
Maximum Collector Current |Ic max|: 0.6 A
Max. Operating Junction Temperature (Tj): 200 °C
Transition Frequency (ft): 200 MHz
Collector Capacitance (Cc): 8 pF
Forward Current Transfer Ratio (hFE), MIN: 35
1. All bipolar transistors need a base resistor.

2. The base current needs to be at least the current through the fans divided by the minimum current gain (thats hfe) of the transistor at that collector current. That's 400mA divided by say 20 which comes to 20mA. That exceeds the 16 mA recommended maximum for a GPIO. Try 15 mA. The voltage across the resistor is the GPIO high output voltage minus the base to emitter voltage. The GPIO will not be able to sustain 3.3 volts when sourcing 15 mA. Estimate 3 volts. Use 0.7 for Vbe. So 2.3 volts and 15 mA gives 150 ohm as nearest commonly available resistor.

skyales
Posts: 12
Joined: Wed Jan 16, 2019 10:18 pm

Re: Control Fan by switching USB port on and off

Thu Jan 24, 2019 11:07 am

drgeoff wrote:
Wed Jan 23, 2019 5:25 pm
skyales wrote:
Wed Jan 23, 2019 2:26 pm
1. All bipolar transistors need a base resistor.

2. The base current needs to be at least the current through the fans divided by the minimum current gain (thats hfe) of the transistor at that collector current. That's 400mA divided by say 20 which comes to 20mA. That exceeds the 16 mA recommended maximum for a GPIO. Try 15 mA. The voltage across the resistor is the GPIO high output voltage minus the base to emitter voltage. The GPIO will not be able to sustain 3.3 volts when sourcing 15 mA. Estimate 3 volts. Use 0.7 for Vbe. So 2.3 volts and 15 mA gives 150 ohm as nearest commonly available resistor.
Thank you very much for your detailed answer!
In this case he is 35, but you used 20, because the base current has to be min. Collector current/base current. So 20 gives a higher base current right?
I understand that 15mA is the max we should use from a gpio. Why does a gpio not sustain 3,3v with 15 mA?
I understand we use 2,3 V because we estimated 3V - Vbe(0,7V).
The only thing that remains are the snub diodes. What specs do I have to look for?
Thank you for your help! You are helping me very much :)

PS: I just realized the 2n2907 transistor is a pnp transistor. Wouldn't it be better if it was a npn transistor? Like in my picture?

User avatar
Imperf3kt
Posts: 2904
Joined: Tue Jun 20, 2017 12:16 am
Location: Australia

Re: Control Fan by switching USB port on and off

Thu Jan 24, 2019 7:41 pm

You could use the 2N2222, but I forget what it's rated current is. 0.8A?

I use one to control a single fan myself. The transistor is connected to GPIO 13 in order to leverage use of PWM.
55:55:44:44:4C
52:4C:52:42:41

skyales
Posts: 12
Joined: Wed Jan 16, 2019 10:18 pm

Re: Control Fan by switching USB port on and off

Thu Jan 24, 2019 8:12 pm

Imperf3kt wrote:
Thu Jan 24, 2019 7:41 pm
You could use the 2N2222, but I forget what it's rated current is. 0.8A?

I use one to control a single fan myself. The transistor is connected to GPIO 13 in order to leverage use of PWM.
1 Fan is 0,1A and I have 4 of them. As a base current resistor 150 ohm. To protect the Rpi I use a 1A diode in parallel to the fans but inverted direction.
but I think I'll use Mosfet. It's safer.
I use the Mosfet because the GPIOs of the Rpi are not so loadable. MOSFETs are better for a low logic level. IRF7401 that's the one I will use. :)

User avatar
Burngate
Posts: 6063
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: Control Fan by switching USB port on and off

Fri Jan 25, 2019 10:19 am

Imperf3kt wrote:
Thu Jan 24, 2019 7:41 pm
You could use the 2N2222, but I forget what it's rated current is. 0.8A?
600mA
https://www.onsemi.com/pub/Collateral/P2N2222A-D.PDF

IRF7401:
https://www.mouser.co.uk/datasheet/2/19 ... 227042.pdf

Return to “General discussion”