Automate a basic humidifier with an Arduino, relay and simple sensors

Arduino, PowerSwitch Tail 2, humidifierMechatronics is described as the intersection between mechanical, electrical and computer engineering, and it covers just about anything that uses electronic signals to act on mechanical systems. It’s what I went to school for, and it’s where my hobby interests lie. The Arduino is a fantastic example of how people with limited experience can jump right into mechatronics projects. I’ve been tinkering with them for a few years, and decided to put the components I had to good use by taking a timid step to home automation.

Indoor humidity can affect the comfort level of a home pretty dramatically. Since warm air can hold far more moisture than cold air, the humidity inside during the winter falls dramatically. This drop can cause an increase in static electricity, sickness and dry, itchy skin. Too dry and a home can just feel downright uncomfortable. Too much humidity when the outside temperature is low can also cause condensation on the windows and mould growth. Some humidifiers have a built in control system to maintain the proper level of humidity, but what if yours doesn’t?

By combining an Arduino with a basic humidity sensor and relay, you can trigger your simple humidifier to only come on when needed.

Bill of Materials

With these pieces combined together, you can find the indoor temperature and humidity, and adjust the humidifier’s set point manually to achieve the proper moisture level.


Complete breadboardHere are some links about connecting the different components that make up the system.

Additionally I have a few bonus status LEDs for displaying when the correct humidity has been reached (although you’ll be able to tell from whether the humidifier is on or off). The great part about the Arduino community is that most of the major components have libraries dedicated to their functions, so you can spend less time figuring out how the hardware works and more time thinking of projects.

Control System

The control system is the set of governing equations and parameters that determine what behaviour the system’s output should have. In this case, it takes in the current level of moisture in the air and tells the relay to switch the humidifier on or off. For that reason, it’s a type of control system called “closed-loop”, because turning the humidifier on will increase the sensor value and trigger it off.

There are many other different types of control systems but we’re fortunate that the main type in use here is called an “on-off controller” for its inability to adjust output values. Like a furnace, the humidifier can be either on or off, with the time in each state determining the magnitude of change. Compared to a PID controller, which uses error correction to maintain a system, the on-off controller is far simpler.

The complete source code is available to download below, but I’ll go through some of the important parts here.

Temperature and humidity outputEven in a relatively simple system such as this, it’s important to follow proper coding patterns. This means using the Model-View-Controller paradigm to retrieve sensor data and output to the screen and relay. In even simpler terms, it means storing the inputs as variables and refreshing the screen rapidly with those variables. For that, it’s a good idea to use something like a Timer class to handle running specific methods at an interval without blocking the main program.

With the button, I’m able to toggle between two states: normal and setting. Doing that is easy with enumerations.

Each time the button is clicked, the state of systemMode is changed, and the display is updated accordingly.

Toggling display modesThe ModeSetting value is where a new setpoint for the control system is entered. When TARGET is displayed on screen, the encoder LED flashes (again with another timer) and turning the encoder updates the value.

While the above links show a clear way of entering the new encoder value, here’s my function.

You only want to update the target value if the current mode is Setting and the new encoder value is different from the previous.

Whenever the system mode returns to normal, the controller checks if the current value is where it needs to be and updates the relay accordingly. However, it’s not as simple as turning the relay on when humidity is below the target and turning it off when it goes above. If that was the case, there would be far too much cycling. That means as the error approaches 0 (humidity reaches the target), the actual value could repeatedly go across the threshold and cause the humidifier to cycle on and off quickly. I’ve set the refresh rate to be 1 minute, but even that would be annoying if it cycled that quickly. To fix the problem, we need to introduce a control system property of hysteresis, which in simplified terms means making the humidifier stay on until it passes the target by a few percent, and not turning on until it’s a few points below the target. This way it cannot rapidly switch between the on and off states. In code, that might look something like this.

Here, the variable correct is the value of whether the system is currently at the desired level, and hysteresis is the percent of padding (2% here).

What this block is essentially saying, then, is “if the humidity is currently above the desired value, don’t turn on again until the humidity is below target – 2” or “if the humidity is currently below the desired value, stay on until the humidity rises to target + 2”. In this way, the system won’t turn on and off in quick succession.

Connect the PowerSwitch relay pins to ground and an Arduino 5V output, and set the state of the pin according to correct. Then sit back and listen to the satisfying click of the relay as computers do your job for you.

Controller and humidifier together

What’s next?

Since this current version requires you to set the target point on the humidifier manually, it’d be nice to make that automatic, too. But how? The problem is that indoor humidity is linked to outside temperature, as I mentioned above, so that means you need to find out the local temperature and adjust the humidity accordingly. That can be done any number of ways, like adding a WiFi or ethernet shield to the Arduino.

This is something I’m planning on doing, but I’m going a slightly different route so that I can build on it for more advanced home automation. By building an Xbee network, I can make nodes that act as sensors or outputs and connect them all to a central computer. Since I already have Ubuntu server, that part is taken care of. I can log data to the server and have it request local weather and tell this Arduino whether the humidifier should be on or off.

But the fun wouldn’t even need to stop there. By designing the network right, suddenly you can add other components to do things like turn lights or appliances on or off or alert you when a window is left open at night. Where will you take it?

Tagged with: , , , ,
16 comments on “Automate a basic humidifier with an Arduino, relay and simple sensors
  1. noone says:

    Great dude, I’m working on something like this, but with a controlled water source for harvest, any headsup would be appreciated. Cheers!.

  2. Kurt says:

    What version of Arduino are you compiling on?
    Can you give a link where you got your libraries from?


  3. Kurt says:

    I think you have something messed up in your void updateMode

    void updateMode() {
    if ( == CLICK_SINGLECLICKED && != lastButtonState) {

    Click_Singleclicked is not declared!

  4. Wes says:

    @Kurt I believe I’m using the latest Arduino IDE, probably 1.0.5 and the CLICK_SINGLECLICKED constant is from the button library. I’ll check that it is in the clickbutton library and confirm it with you.

  5. Kurt says:


    I found my problems and it had to do with similar libraries in my library folder.
    Once I moved them into a temp folder a lot of my compiling errors went away!
    Strange thing is that IDE version 1.03 is lots more forgiving and compiles code that 1.05 would not!

  6. Kurt says:

    BTW, Can you do a quick wire up diagram……
    It will help others who come across your project!

  7. Kurt says:

    Ok , now getting CLICK_SINGLECLICKED not declared in this scope
    Also had error on
    I think the library has updated to not
    I get a clickbutton has no member click error!
    Sorry just want to get this going!

  8. Kurt says:

    Just to let everybody know, the clickbutton library was revamped and in order to get this sketch to work you’ve got some re-writing to do!
    Have a Happy…..

  9. Ken Masengill says:

    Great Job on the humidity project. I would like to build one but I am not good enough yet to be modifing librarys. Are the required libraries available and correct now for this project or is modification still required?

  10. Ken Masengill says:

    I would like to build this and was wondering if the library and sketch match up and work now, as I am a novice and not yet up to the task of correcting and fixing at this level. Please reply on the blog. Much appreciated, gerat work!

  11. Base says:

    so from my understanding it’s possible to build this without the encoder but i’d have the humidifier turning on and off very frequently right?

  12. Wes says:

    The encoder is a means of adjusting the setpoint for when the humidifier toggles on and off, so without it, you’d have to set those values in the code once before connecting with Arduino.

  13. Chris says:

    Looks like a great project. I don’t know if you changed your wordpress theme, but I can’t seem to find the attachment containing the complete code.

  14. Bert says:

    Hi Wes,
    It seems I cannot download the code anymore?
    can you give me a link or send the code?


  15. Bert says:

    Hello Wes,

    is it possible to get the source?
    The link seems not to work anymore..


  16. Tbird says:

    Looks like a cool project! šŸ™‚ I am also wondering where to download the source code? Anyone that can help me out?

Leave a Reply

Your email address will not be published. Required fields are marked *