Monday, January 7, 2013

Temperature and Humidity Redux

A long while back (in a galaxy very far away), we built a temp and humidity monitor with the SHT-21 chip. It's a very accurate, albeit very expensive, solution. What if good enough, is good enough? If you don't need scientific resolution, the DHT-11 is a very inexpensive solution. Today we built a temp and humidity monitor with this module. I started off with a DHT-11 breakout board from geeetech. I used this module because it has the pull up resistor and a conditioning capacitor onboard. I connected S (signal) to pin 2, - to Gnd, and + to 5vdc.
I downloaded the library from github and followed the tutorial from adafruit. I changed the sketch slightly, as it outputs the temperature in C, and I want to see it in F, so I changed two lines from:

Serial.print(t);
    Serial.println(" *C");

to:

Serial.print(t*1.8+32);
    Serial.println(" *F"); 

Here is the final result:


// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
// Fahrenheit conversion added by Steve Spence, http://arduinotronics.blogspot.com

#include "DHT.h"

#define DHTPIN 2     // what pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11 
//#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Connect pin + (middle) of the sensor to +5V
// Connect pin S  (on the right) of the sensor to whatever your DHTPIN is
// Connect pin - (on the left) of the sensor to GROUND

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600); 
  Serial.println("DHTxx test!");

  dht.begin();
}

void loop() {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(t) || isnan(h)) {
    Serial.println("Failed to read from DHT");
  } else {
    Serial.print("Humidity: "); 
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperature: "); 
    Serial.print(t*1.8+32);
    Serial.println(" *F");
  }
}

Working with the SainSmart 5v Relay Board

Today we are working with our SainSmart 5v Relay Board. This is a simple and inexpensive 4 port SPDT relay board (there are boards with more or less relays)  that takes a digital signal (LOW) from the Arduino, through an optoisolator, which triggers a transistor, pulling in the relay. The relay contacts are rated for 10 amps at 120/240vac, and 10 amps at 30vdc or less. 

IMPORTANT!

The contact pins are not numbered, and are reversed if you go by what the schematic appears to be saying. Facing the screw terminals, with the board face up (solder side down), the screw terminals are as follows (from left to right):

K4
1 - Normally Open
2 - Common
3 - Normally Closed

K3
1 - Normally Open
2 - Common
3 - Normally Closed

K2
1 - Normally Open
2 - Common
3 - Normally Closed

K1
1 - Normally Open
2 - Common
3 - Normally Closed

When you send a logic low, that turns on the LED, and energizes the coil. However, a disconnected input will drop out the LED and the coil, as will a logic HIGH. We will send a logic HIGH in setup to ensure the relays are disabled on boot.



There is a 6 pin male header, so you will need a cable or female pins to slide over the header to connect it to your Arduino. Pin 1 connects to Arduino GND, Pins 2-5 to Digital output pins, and Pin 6 to Arduino 5v. A red LED for each relay lights when active (LOW).



int relayPin1 = 7;                 // IN1 connected to digital pin 7
int relayPin2 = 8;                 // IN2 connected to digital pin 8
int relayPin3 = 9;                 // IN3 connected to digital pin 9
int relayPin4 = 10;                // IN4 connected to digital pin 10

void setup()
{
  pinMode(relayPin1, OUTPUT);      // sets the digital pin as output
  pinMode(relayPin2, OUTPUT);      // sets the digital pin as output
  pinMode(relayPin3, OUTPUT);      // sets the digital pin as output
  pinMode(relayPin4, OUTPUT);      // sets the digital pin as output
  digitalWrite(relayPin1, HIGH);        // Prevents relays from starting up engaged
  digitalWrite(relayPin2, HIGH);        // Prevents relays from starting up engaged
  digitalWrite(relayPin3, HIGH);        // Prevents relays from starting up engaged
  digitalWrite(relayPin4, HIGH);        // Prevents relays from starting up engaged
}

void loop()
{
  digitalWrite(relayPin1, LOW);   // energizes the relay and lights the LED
  digitalWrite(relayPin2, LOW);   // energizes the relay and lights the LED
  digitalWrite(relayPin3, LOW);   // energizes the relay and lights the LED
  digitalWrite(relayPin4, LOW);   // energizes the relay and lights the LED
  delay(1000);                  // waits for a second
  digitalWrite(relayPin1, HIGH);    // de-energizes the relay and LED is off
  digitalWrite(relayPin2, HIGH);    // de-energizes the relay and LED is off
  digitalWrite(relayPin3, HIGH);    // de-energizes the relay and LED is off
  digitalWrite(relayPin4, HIGH);    // de-energizes the relay and LED is off
  delay(1000);                  // waits for a second
}

Sunday, January 6, 2013

Build the $15 Arduino

Since I don't want to dedicate a $22 Arduino UNO for every project (for space reasons, not price), I've started embedding a Atmel 328 chip into my projects. You can build your own Arduino UNO compatible for approximately $15 (buy parts in bulk, and make several), depending on the options you want to include. This DIY Arduino will be the foundation for a series of books we are writing based on the Arduino and general electronics.

The bare minimum needed to build is the following:

Qty. (1) ATMEGA328P (with Arduino Bootloader) $2.95
Qty. (1) 28 pin socket (optional, but highly recommended)
Qty. (2) 22pf ceramic capacitors
Qty. (2) 100nf (.1 uf) ceramic capacitors
Qty. (1) 16 mHz crystal $2.57
Qty. (1) 6 pin male header (for FTDI Programming Cable, see parts link above)

Optional:
Qty. (1) 150 ohm 1/4w resistor &
Qty. (1) Green LED (pin 13 indicator)

Qty. (1) PCB Momentary Tactile Push Button Switch (reset)

Power supply:
Qty. (1) LM7805 5v voltage regulator
Qty. (2) 10uf electrolytic caps
Qty. (1) Green LED
Qty. (1) 150 Ohm 1/4w resistor

Full instructions on assembly are located at http://thetransistor.com/projects/arduino/