DIY - Raspberry PI + DHT11 Sensor + SainSmart 2 Channel Relay

Discussion in 'Do It Yourself' started by Twerkle, Oct 10, 2016.


    Twerkle Well-Known Member

    Hey everyone! I saw a post awhile back about a curing box using a sensor and fan. Getting bud to a good RH before curing is always my weakest link so I decided to start a fun little project. Tweaking the tutorial a bit you could use the relay for turning on/off anything basically.

    Warning: I'm new to both hardware and programming. So the code and tutorial might have some flaws, and things I might saw might be completely wrong. LOL. Don't be afraid to send input my way on ways to improve it.

    Things you need (some you might already have):

    • Raspberry Pi - There are many different models. Depending on what else you want to do with it you might want a better one. For this a Rasp Pi 2 will work. You want 1GHtz processor because the sensor sends the signal extremely fast. A slow CPU will cause lots of bad data reads from the sensor
    • DHT11 / DHT22 - I used DHT11 originally but I plan on switching to DHT22 once its fully operational. DHT11 is +/- 5%RH where as DHT22 is 2 +/-%RH. It wasn't avail at my local store unfortunately. Make sure you get one on a board! They have build in resistors and such, much easier.
    • Female to Female Jumpers - Self explanatory.
    • SainSmart 2-Channel 5v Relay - I couldn't find one channel for much cheaper so I figure could always use it for something else in the future. It can handle AC250V @10A ; DC30V @ 10A, for this project we only need 12v. If you want to run 120v they make stronger relays, just make sure that its 3.3v or 5v switch so the pins on the Rasp Pi can control it.
    • 12v Adapter - Check you wire pile first before buying. Any 12v adapter should do, mine is 12v 2A but 1A should work too.
    • 12v Computer Fan - I had this one laying around the house, if you have a spare 12v fan that will work just fine. Some have stronger motors than others, only real difference.
    • 5v Micro USB Adapter - This powers the Pi. You probably have many laying around the house. Just make sure its 5v and at least 1Amp. A good one will be 2 Amps, a shitty one will be 500mA.
    • 8GB Class 10 SD Card - You might have one of these laying around, 4GB will work but 8GB is best. Class 10 is important, faster read/write speed. You will also need an SD adapter if your computer doesn't have an SD slot.
    • USB Wifi Dongle - Not necessary but I have it push the temp and humidity to a chart I can access on my home network.
    Setting Up The Raspberry Pi:

    I'm going to quickly cover the steps you need to do before we start working with the sensors and relay. Some of the concepts might be new to people who haven't used linux before but there are MANY tutorials that will walk you step by by through everything. Google will handle the rest of your questions.

    • Download and Install Raspbain - This is the Pi's linux based OS. How you install it to the SD card depends on your own Operating System. You do not just copy the download file to the SD. :)
    • Find and Set Static IP - Hook up a monitor (via HDMI, your TV will work) to the Pi, as well as a usb keyboard and mouse. If you do not have an HDMI, USB mouse/keyboard, you can find this all out from another computer with a little more work. Info is in the first link regardless.
    • Make sure Python is installed -
      python -V
    • Install WirePi - If you have any issues you might need to run as 'sudo', or admin when running WirePi.
    • [OPTIONAL] Install Apache - This allows you to use Plotly to map a chart to a web server you can access from any computer.
    • [OPTIONAL] Install - This maps the HUMIDITY and TEMP to a chart.
    Download My Repo:
    • Download the files from GitHub. You can use the command prompt to download the repo straight to the Pi or download it to you computer move it to the Pi.

    Assemble The Sensor: USE THIS CHART. Pin number refers to pin number and not name.
    • Using the female to female jumpers connect the (+) pin on the sensor to pin 1 (3.3v pin).
    • Connect the ground (-) pin to pin 6 (ground).
    • Connect the data pin (S) to pin 7 (GPIO 4).
    Assemble the Relay:
    • Using female to female jumpers connect the power (vcc) pin to pin 2 (5v pin).
    • Connect GND to pin 9 (GND).
    • Connect IN1 to pin 11 (GPIO 17)
    Wire the Relay:
    • Cut the 12v Adapter end and separate the two wires.
    • Cut the end of the fan wires and remove some of the shielding. Most fans come in 3 wire and 4 wire. 2 are pos/neg, the other 2 are pulse width or speed control. For this we will not be working with those two.
    • It is easy to tell positive and negative on the 12v adapter but fans are more difficult. I just wired one fan wire to power of the adapter and then touched each remaining fan wire to the negative wire on the adapter. Fans are not bidirectional and will only run when wired correctly. I'm sure this way is dangerous/stupid, lol so at your own risk.
    • Once you know which wires you need insert the negative 12v wire into the center terminal of the RELAY. To do this unscrew the terminal a but and then slide the exposed part of the wire in and screw tight. Similar to wiring a speaker.
    • This relay has an ALWAY ON and ALWAYS OFF terminal. In this we are only using ALWAYS OFF, but you can add to the ALWAYS ON depending on your project. It is easy to tell by looking at the board which on is which. Insert the exposed part of the negative fan wire into the ALWAYS OFF terminal, screw tighten.
    • Wire the power wire from the fan to the power wire from the 12v adapter. Add wire nut or electrical tape.
    Running the code:
    • Plug pi in and power up.
    • Connect via terminal on another computer. Using the static IP we assigned earlier:
      ssh [email protected]
    • Default Rasp Pi password is 'raspberry'
    • In the terminal navigate to the folder containing the github files.
    • If you are using the DHT11 you should be able to run the code right away.
      sudo python
      If everything is set up correctly the script will run, the fan will power up for 3 seconds and then off to show it is running correctly. This might take a few seconds.
    • Any issues you might have to run the 'make' command on dht_old.c. Navigate to the folder with dht_old.c and enter:
      cc -Wall dht_old.c -o dht -lwiringPi
      Then run the python script command from above.
    Currently the humidity is set to 62%, you and edit this by running
    Change the number in line 55 to whatever you want.
            if  humd > 62:
    Let me know if anyone has any questions. I'll post pictures of it as I test thinks out and update accordingly.

    If anyone has any suggestion fork the repo or post here. Thanks!
    CaptainCAVEMAN and AAjax like this.

    Twerkle Well-Known Member

    --Saved for pictures--

    Attached Files:


    kenobi Well-Known Member

    Dude, nice... I will give it a shot. let me know how yours works out

    Twerkle Well-Known Member

    I put the sensor in a tupperware container with a wet paper towel and put it in the sun. Fan kicked on and turn off when I took it out so I know it works. Now im trying to figure out the best way to make a drying chamber.

    kenobi Well-Known Member

    I have added all the items to my wish list lol!!the humidity sensor is currently out of stock

    Twerkle Well-Known Member

    DHT11 is what I used but the DHT22 is more accurate if they have those. Should work just the same.

    qballizhere Well-Known Member


    Twerkle Well-Known Member

Share This Page