Arduino Intro: Humidity Fan Sensor

To start with the context of my project, my apartment bathroom has a fan that is very weak and does little besides make noise. As we all love our good hot showers, sometimes the bathroom becomes too humid, which encourages mold/mildew growth. I own a small portable fan, but I don’t like having it on the entire time while showering because it’s cold when I’m done and step out. Therefore I decided to try making a switch that would turn the fan on conditionally based on the humidity of the bathroom. Here’s the storyboard for my idea: (apologies for the blurry text!)

The following is a list of all the materials/components I used to create my idea: a Servo motor (output), a DHT11 temperature and humidity sensor (input), my Arduino Uno and wires, and my roommates breadboard, which was larger than the one included in the kit. The logic flow was simple: the sensor repeatedly reads the temp/humidity of the room. If the readings are above a threshold, the sensor sends a signal to the Arduino to fire up the Servo motor and rotate the arm, which when taped onto the fan switch the it on. If the fan is off and the readings fall below the threshold again, then another signal is sent to the arm to rotate in the opposite direction to switch the fan back off. I had to look up a couple of articles to see how to set up the hardware, but luckily it was pretty straightforward. Both devices just needed one pin to ground, one to power (5V), and one to serial pin. There wasn’t anything particularly complicated about this, so fortunately I didn’t run into any problems here. I then started coding, and I imported the libraries and declared my global variables/macros as such: 

The coding part was pretty simple and the logic just as described above. I used a boolean flag to toggle on/off as the fan turns on/off, and a couple of if statements to determine if the fan switch needed to be flipped. If so, I used a for loop as we did in class to rotate the Servo arm. When I got the code compiled, I first checked to see if the humidity sensor was working properly and printed its readings to the console: 

For testing purposes I had to breathe on the sensor to change the humidity (sorry and I wiped it with Clorox afterwards.) But as the output shows, the sensor was changing accordingly. It was easy then to coordinate the Servo arm with the humidity changes to get the desired behavior. Here’s the final look of the prototype:

Although the prototype and basic mechanics were successful, unfortunately the creation of the actual project failed when it came to attaching the motor to the fan. The fan switch was actually slightly ajar and loose, which meant it required extra force to move. The little Servo motor arm was not strong enough to be able to switch it on alone. Perhaps a larger motor and stronger arm would be able to do the trick, but I currently don’t own one to try it out.

Overall though this project was fun, and as a CS major it was interesting to get acquainted with the hardware side of tech. I think I still prefer software, but I enjoyed familiarizing myself with the Arduino IDE and getting hands-on experience with wires, sensors, and ports. Debugging wasn’t anything new, but it was cool to see how the real-life wires and individual components interacted. As I was Googling for inspiration and ideas, I got to see how this little board can build complex projects, and that these basic components have potential to build a huge variety of things. 

Additionally, here’s my code for this project:

#include <Adafruit_Sensor.h>

#include <DHT.h>
#include <DHT_U.h>
#include <Servo.h>

Servo myservo; 
int pos = 0;
bool fan_on = false;

#define THRESHOLD 80
#define DHTPIN 2
#define DHTTYPE DHT11   

void setup() {
void loop() {
  float humidity = dht.readHumidity();
  float temp = dht.readTemperature(true);

  if (isnan(humidity) || isnan(temp)) {
    Serial.println("Failed to read from DHT sensor!");

  Serial.print("Humidity: ");
  Serial.print("% | ");
  Serial.print(" \xC2\xB0");
  Serial.print("F ");

  if (!fan_on && humidity >= THRESHOLD) {
    for (pos = 0; pos <= 180; pos += 1) {
    fan_on = true;

  else if (fan_on && humidity < THRESHOLD) {
    for (pos = 180; pos >= 0; pos -= 1) {
    fan_on = false;