Monitoring is good, but controlling is even better!

I recently posted an instructable for building a temperature and humidity monitor, you can check it out here:

Raspberry Pi IoT: Temperature and humidity monitor

So, I decided I needed a robotic solution, to open the window of my bathroom when humidity was high, and even activate a ventilator when humidity went over the top.

I present you, dear reader, an instructable to build your own robotic humidity controller! What's more, you'll be able to set up your humidity controller with the cloud and see what it's doing from everywhere on the globe, a true Internet of Things machine!

Step 1: Bill of Materials

First a disclaimer: It is taken as a given that you know what you're doing. This instructable serves as insipiration rather than a fool-proof guide to building this prototype. Don't turn on me if you set your home on fire or get an electric shock trying to do so!

Below is a list of what I used to build the humidity controller prototype. You can modify the list to your needs and to the components you already have available.

  • Raspbery Pi A+. I chose the A+ model for its lower than low energy consumption, since it's used only for a single purpose.
  • Wifi dongle. If you need the cloud connectivity. Also, the RPi is set-up as a headless system

    (i.e. no screen): You really need a way to communicate with it, which is per terminal (SSH connection).

  • microSD with Raspbian installed, SSH connections enabled, GPIO enabled.
  • A temperature and humidity sensor module. I have used the AM2302 module, which is based on the DHT22 sensor, with all needed resistors pre-soldered. You could also use a plain DHT22 or a (less accurate) DHT11 sensor, but then you would have to do some soldering yourself.

  • NEMA 14 bipolar stepper motor.
  • L293D based bipolar stepper motor driver. If you use a unipolar motor, the a ULN2003 darlington-array based driver may be better suited.
  • 2x pc cooler fans.
  • MOSFET based current switch.
  • A double-output USB charger, at least 2x2A output. One port is used for powering the Raspberry Pi and the other for powering the stepper motor and the fans. If you find out that the USB charger does not provide enough juice for your stepper / fans, you can use a laptop charger. Some of them come with an additional USB charger port, which you can use to drive your RPi. If you use two different power supplies, make sure that you have one common ground in all your electrical connections.
  • Cables.
    • You can use breadboard connectors for connecting the circuits, motors and fans together.
    • A USB-A male to USB Micro-B male for connecting the RPi to the USB charger.
    • A USB cable with a USB-A male end, if you intend to power the motor and the fans with the USB charger.
  • A M5 metric threaded rod. The length depends on the maximum opening of the window.
  • A M5 metric hex nut.

If you use a larger and more powerful stepper (e.g. a NEMA 17 or a more powerful NEMA 14) you may want to use a larger diameter threaded rod and nut. You will also have to adjust the 3D printed parts accordingly.

  • 2x hinges with approx. 4cm width.
  • Screws.
  • Duct tape.
  • Some 3D printed parts, for which I provide the .stl files here. You can, however, design you own, and/or also carve them from plywood / use some other material. Improvise!
    The 3D printed parts are:
    • Frame mount (stl model base)
    • window mount (you can use the stl model named connector)
    • hex nut casing (stl model hex nut)
    • hex nut casing cup (stl model hex nut cap)
    • connector between window mount and hex nut casing assembly (print again stl model named connector)

Step 2: 3D Printed Parts

You can print the parts from the .stl (stereolithography) files provided here.

The 3D printed parts are:

  • Frame mount (stl model base)
  • Window mount (you can use the stl model named connector)
  • Hex nut casing (stl model hex nut)
  • Hex nut casing cup (stl model hex nut cap)
  • Connector between window mount and hex nut casing assembly (print again stl model named connector)

Remember to print the part humidity_controller_connector_ver2_ascii.stl twice, since it's used both as a connector and as the window mount.

If you don't have a 3D printer, don't worry so much: As you can see the parts don't have any extravagant shape, so you can make them from pretty flat and rectangular pieces of wood with some basic carpentry skills.

Step 3: Stepper Motor Driver

There are two big families of stepper motors: Unipolar and bipolar steppers. Bipolar steppers give supposedly more bang (torque) for the buck (weight). What is important to know is that these two different types also require two different type of circuits to drive them.

The simplest way to drive a unipolar motor is by using a darlington transistor array, typically a ULN2003A chip, while a bipolar motor is best driven by an H-bridge, the most popular of which (among amateurs like us) is the L293D chip.

You can either buy some ready made stepper driver boards based on the right circuit for your stepper, or you can just buy the chip and solder your own board. I used a bipolar motor for the humidity controller and because I happened to have some L293D chips lying around (and no board), I made my own. You can use my fritzing design if you are adventurous enough:

I designed it for a 6-pin NEMA17 TEAC bipolar stepper motor I dismantled from a 5.25". However, I used it later on the smaller form 4-pin NEMA14 bipolar, by connecting only the pins 1,2,3,4 and leaving the last two 12V pins unconnected.

Step 4: MOSFET Relay for the Ventilator

I first tried to switch the ventilator on and off using a classical electromechanical relay. There was, however, a problem: The one I had in hand (a SANYOU SRB-S-112DM2) had a 12V coil voltage and was not responding to the USB power supply's 5V. So, I opted for a MOSFET relay (IRLU8721PBF) self - designed and made board. Again, you can see the schematic here and reproduce it (maybe improve it, too :-) or you can find some ready made option that suits your needs.

Step 5: Wire Everything Together

It is a good idea to make sure all your circuits work as expected, before mounting everything on your window.

Electrical connections

First of all, you have to give some juice to your circuits!

The Raspberry Pi does not need any special treatment;

USB 1.x/2.0 standard pinout Pin Name Wire color Description 1 VBUS Red (or orange) +5 V 2 D− White (or gold) Data− 3 D+ Green Data+ 4 GND Black (or blue) Ground

Source: Wikipedia

To connect the MOSFET relay and the stepper motor driver to the USB power supply:

  1. Take a USB cable you won't miss after chopping it to pieces.
  2. Chope the USB cable to pieces :-) You need the USB-A male end with enough cable.
  3. If you look at the loose end, you'll see that it consists of four insulated wires Red, White, Green, Black. We need the red (+5V) and black (Ground). Strip them and
  4. connect them (in parallel) with the two boards, the MOSFET relay and the stepper driver.

GPIO connections

The tricky part in this step is to identify the pinout of the version of your Raspberry Pi. There are actually two different pinout numbering schemes for the Raspberry Pi GPIO: The one was the numbering scheme provided by the creators of the Raspberry, and the other is based on the Broadcom BCM2835 chip pinout. For more info, you can refer to the followin forum post: GPIO Header Pinout Clarification

My Raspberry Pi is an A+ V1.1 and I found out the hard way that the pinout coincides with that of the schematic above (Source: Raspberry Pi Spy), this being the BCM numbering scheme. You will have to identify the GPIO pins before proceeding to connect the sensor and the screen.

When you are sure about your Pi's pinout:
Connect the temperature and humidity sensor

  • Connect the sensor's ground to a ground pin (e.g. pin 6, 9, 14, 20, 30, 34 or 39)
  • Connect the sensor's VCC pin to a 3V3 power pin (e.g. 1 or 17)
  • Connect the sensor's data pin to GPIO4. In my board this was pin number 7. You can of course choose another GPIO pin, and you will have to change the respective change in your code.

if you are using some sensor other than AM2302, you can refer to Adafruit's tutorials on how to connect your sensor to your Pi. Check the following link:


Connect the stepper motor driver

As I've already implied, depending on the motor you are using, your motor may have from 4 to 6 pins. You control usually the 4 of them from 4 dedicated pins (although there are designs where you only need two pins) and the rest (0 -2) pins are connected to ground.

Step 6: Create a Cloud Account

I've chosen Ubidots (http://ubidots.com/), which is a nice small company (I guess) with a nice interface for IoT applications. It also happens to support a lot of different platforms, with python being the one interesting for this project.

After you create your account, add a new data source (your humidity controller!) with four variables, one for the temperature measurements, one for the humidity measurements, one for the window status (open/closed) and another for the ventilator fan status (running/stopped).

You'll get an ApiClient code for your new data source, and a variable ID for each of your variables. Note them down, because we're going to need them later.

Step 7: Software Makes the World Go Around!

Login (remotely) to your Raspberry

Supposing you've logged in as user pi and you've named your Raspberry humidity-controller, you'll see in your terminal something like this:

pi@humidity-controller ~ $

Firstly, create a folder, where the humidity-controller software will be stored:

mkdir humidity-controller

...and change to the new folder:

cd humidity-controller

The command line prompt will change to

pi@humidity-controller ~/humidity-controller $

It's now time to fetch the humidity-controller sources:

git clone https://bitbucket.org/techprolet/humidity-controller.git

Ubidots settings

You'll have to edit the Ubidots variables' data. Using your favorite editor, open the ubidots_api.ini script.


nano ubidots_api.ini

The ubidots api initialization file contains the following lines:






Replace the xxxxxx with the variables you got from your Ubidots registration.

Humidity & temperature sensor settings

If you haven't used an AM2302 sensor module, you can modify the settings_sensor.py to reflect your hardware.

Adjust the following line:

sensor = sensor_args["2302"]

You can also set a different connection pin (other than 4) by changing the pin setting:

pin = 4

MOSFET relay settings

You can set a different pin for the MOSFET relay that controls the ventilator fan at relay.py:

RelayPin = 21

Stepper motor settings

Edit the stepper motor variables in stepper.py:

If you have used pins other than the BCM 9, 25, 26,20, change the following lines accordingly:

# be sure you are setting pins accordingly


StepPins = [9, 25, 26, 20]

For my bipolar stepper, the recommended sequence was:

- + + -

- + - +

+ - - +

+ - + -

If you use a unipolar stepper or the manufacturer's specs differ, you'll have to change the stepper sequence lines:

#Bipolar stepper sequence

PinCount = 4

Seq = [3,2,1,0]

Seq[0] = [0,1,1,0]

Seq[1] = [0,1,0,1]

Seq[2] = [1,0,0,1]

Seq[3] = [1,0,1,0]

Finally, you can set when the humidity levels at which the humidity takes action by opening or closing the window and by running or stopping the ventilator fan. You can also set how many steps the stepper has to take to open or close the window.

In humidity_controller.py, the main program, check the following lines:

ThresholdWindowOpen = 62.0

ThresholdWindowClose = 58.0

ThresholdFanStart = 72.0

ThresholdFanStop = 68.0

WindowSteps = 15000


You can now run the humidity-controller by typing the following:

sudo python humidity-controller.py

You can also make Raspberry execute the script everytime it boots (so that you don't have to manually run it everytime you accidentally pulled the plug...)

Firstly, you have to make the humidity_controller.sh script executable:

chmod 755 humidity_controller.sh

Then create a logs folder, where the execution logs are going to be stored:

mkdir logs

Then run crontab:

sudo crontab -e

and enter the following line

@reboot sh /home/pi/humidity-controller/humidity_controller.sh >/home/pi/humidity-controller/logs/cronlog 2>&1

From now on, when the Raspberry boots, the script should start automatically.

Test that everything work as expected. When you are happy with the result, you can proceed to the last step.

Step 8: Mount It!

Put the hex nut in the 3D printed part as shown on the photo. The screw the part named

humidity_controller_hex_nut_cap_ascii on the hex nut assembly, so that the hex nut is blocked inside. Check that the threaded rod moves freely when screwing and unscrewing.

Mount the fans on the base with two screws for each fan. Mount the base on the window frame and secure the stepper motor on it with some carpentry binders and duct tape (again: improvise!).

Mount one of the connectors on the window. Connect it with the other connector using a hinge and then use a second hinge to connect to the hex nut assembly.

You're done. Take a shower to test your accomplishment!

<p>Nice bathroom</p>
<p>Yep, I like it, too ;-)</p>
<p>Very nice and through Instructable. I voted for it in the Raspberry Pi category.</p>
<p>Thank you very much!</p>
<p>Nice idea :-) But you should probably mount this only if your window is out of reach of a ladder!</p>
<p>Well, it's meant for windows with a tilt mechanism - it doesn't make them neither more nor less secure ;-)</p>
<p>Not really. Windows have a closing mechanism. If that is open (like in your case) a burglar wouldn't even need a tool to open it. A hard push would suffice. </p>
<p>You bet! A hard push opens a lot of doors (and windows)</p>

About This Instructable



More by techprolet:RPi IoT: Cloud Logging Robotic Humidity Controller Raspberry Pi IoT: Temperature and humidity monitor Intel IoT Edison Google Calendar reminder 
Add instructable to: