I'm pretty fresh to the Raspberry Pi scene and decided my first project is to design a smart humidifier for a Cigar Humidor.

I looked online at some projects other people have used, but it involved a full size humidifier that was plugged into a smart outlet to control a room humidifier. My project has obvious size constraints being that both the sensor and humidifier must be small enough to fit in a humidor with cigars, and obviously going too big will result in loss of usable space which kind of defeats the purpose.

Due to the nature of a humidor having moisture inside I'd like to keep the Raspberry Pi unit external, so only the Humidity sensor & Ultrasonic humidifier will be inside.

For the Humidity sensor I'm going with the AM2302 Sensor as it is already setup for mounting, the fact that the polling is limited to 2 seconds isn't that huge of a deal as it is measuring Relative Humidity (or RH) and only needs to be checked within a 5-10 target drop in RH% (Target humidity for cigars are between 60-80% RH, but this is preferential to the smoker). The plan is to have the sensor poll the RH inside the humidor to the Raspberry Pi outside of the Humidor, if the result is above the specified threshold nothing will occur. If the result is below the specified threshold this will result in the Ultrasonic Humidifier to be powered on, thus increasing the amount of humidity in the humidor.

I found a guide easily enough through Adafruit for wiring the sensor so that it will poll to the Raspberry Pi, but that's where I'm getting stumped! I know there is limited control for the Raspberry Pi's USB ports, in fact the only thing I seem to find is an "All or Nothing" response where it would disable not only the 4 USB ports but also the Ethernet Port which would prevent my Pi from communicating with the outside world.

So my question is, how would I go about controlling power to a USB powered device?

  • Transistor and relay?
    – anonymoose
    Apr 18, 2017 at 22:13
  • 1
    Humidifier Humidor
    – Atticakes
    Apr 18, 2017 at 22:18
  • 1
    @anonymoose I'm really wet behind the ears, but I looked up USB Relay Modules and hopefully i'm understanding this concept correctly. Would something like this suffice? If i'm understanding this correctly, I could plug this USB Powered relay into a powered USB hub that is connected to my Raspberry Pi, and send commands to it via Python to turn on/off a device? I'd have to splice the USB cable on the humidifier as i'm sure it only draws power from the USB cable, correct?
    – Atticakes
    Apr 18, 2017 at 22:26
  • 1&2: Probably. I was actually thinking about a circuit like this hooked up to a Pi's GPIO pin. 3: Yes, you would have to open the cable up. The page I linked to shows line power connected to the relay's contacts, but you would open up the cable, cut one wire (black or red) and connect the cut ends to the relay contacts. You might also want to cut up a USB extension cable (make sure it's heavy gauge) in case you need to replace or reuse the humidifier.
    – anonymoose
    Apr 18, 2017 at 22:50
  • @anonymoose I linked it above since I don't have enough prestige to have more than 2 links in my posts. Humidifier just in case you missed it. It's pretty vague in the description, but from what I gather it only uses the USB port for power and there is no on/off switch so I assume the instant the device is plugged in it is on.
    – Atticakes
    Apr 18, 2017 at 22:53

2 Answers 2


You should have a look at https://www.yepkit.com/products/ykush switchable usb hubs. I use these to power cycle multiple usb devices on a daily basis. They are really easy to use via command line and/or custom scripts. Hope this helps.

  • That's a great option! It ups the price of my project a bit: 29.94 Euro equals 32.09 US Dollar This brings my total cost of the project to ~$70 USD, but is MUCH easier to implement! I'm going with this option! Thank you so much!
    – Atticakes
    Apr 20, 2017 at 21:46

I won't get in to all the details of USB power management, but in many cases it's done via co-operation between a host and a device both speaking USB protocol. For example, a USB 1.1 or 2.0 device that's plugged into a hub or host is expected never to draw more than 100 mA until it's sent messages upstream to confirm that it is or is not allowed to draw up to 500 mA, and only draw more than 100 mA after it's received a confirmation from upstream that it can do so.

There are also standard and non-standard communication methods using things like varying resistances on the data lines (rather than USB protocol) for dedicated chargers that allow devices from certain manufacturers to safely draw more than 500 mA from supplies following standard or non-standard specifications for this. Many hosts and hubs do not know about or support these standards, however; they're really aimed at charge-only ports on USB chargers that plug into wall outlets.

Many devices that work on USB power, however, especially cheap ones, don't speak USB protocol at all but simply (and in violation of the standards) draw as much power as they need from the 5V lines, sometimes even more than the 500 mA that's the maximum allowed under the spec. To be safe, especially since it probably includes a resistive heater, you should assume that your humidifier is one of these, and you probably don't want to have it drawing power from the Pi's USB ports unless you've confirmed that it won't draw so much that it will affect the operation of your Pi.

So what I would do here is build an in-line USB "switch" that takes a USB connection into the upstream side to supply power (from a USB power supply or whatever), has the device connected to the downstream side, and has a separate input to determine whether the upstream and downstream sides should be connected or not.

Control of the device is probably most easily done with a GPIO pin or two. Ideally you want to ensure that power is connected before the data lines are connected (the USB connectors do this by making the power and ground connectors extend further out than the data connectors so that they contact first[1]). You could do this with two separate control signals or you could tweak the device to delay the switching of the data lines. (Or you could not pass the data lines through, if you're sure of the behaviour of your particular device, but that seems more work to me.)

The switching itself could be done with relays or MOSFETs; exactly how to do that would be a good subject for a question on the EE StackExchange.

Summary: don't trust devices like this to be nice about their power draw; do a careful analysis before you plug one directly into your Pi. Use a separate in-line switch to switch on and off the connection to your device and have at least the option to plug it into a separate USB power supply.

  • Clarification: I don't plan on connecting anything directly into the raspberry pi other than a powered USB hub. The humidifier will be plugged into said powered USB hub, so power draw isn't a huge issue. I plan on using a USB Powered relay module which will interrupt the power of the humidifier, which allows me to control it via python commands. I also decided to look at a different vendor who provided more details on the humidifier: Voltage: DC 5v Power: 2w.
    – Atticakes
    Apr 19, 2017 at 0:29
  • Yes, that sounds like a good approach. If the humidifier shorts or something like that, the worst it should be able to do is take out the hub. There are a ton of options for how you actually turn the thing on and off once you understand the basics of what's going on with USB.
    – cjs
    Apr 19, 2017 at 7:06

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.