Tuesday, September 10, 2013

DHT-11 Temperature and Humidity Sensor

This is the fourth tutorial in our 37 in 1 Sensor Kit series. We have addressed this module and it's family in several previous posts. It's a low end temperature and humidity sensor accurate enough for residential weather and environmental monitoring. It's very easy to use, and hookup info and code samples for both the DHT-11 (included in the sensor kit) and it's bigger brother the DHT-21/22 can be found at

http://arduinotronics.blogspot.com/2013/01/temperature-and-humidity-redux.html

http://www.instructables.com/id/The-Embedded-Arduino-building-an-Arduino-into-a-p/

http://www.instructables.com/id/Temperature-and-Humidity-on-a-Graphical-LCD/



Sensor Kit (or get the DHT-11 by itself)
Male to Female Jumper Kit

Monday, September 9, 2013

Using a Rotary Encoder with the Arduino

This is the third tutorial in our 37 in 1 Sensor Kit series. Normal volume controls and potentiometers have stops at each end of the scale. A rotary encoder is a bit different. There are no stops, it's continuous 360 degree motion, and sends a signal to the controller for each "step" (you can feel the notches or detents). This works well for dialing in a temperature, speed, volume, or even making menu choices. This sketch will output a digit for each stop, so you only need to make that digit value (2,3,4, etc) equal something in your sketch. For instance, set 140 on the encoder to keep a water heater at the correct temperature, or dial in 3600 to set a motor speed. Pushing down on the shaft is a reset button that resets the encoder value to zero. Rotating the shaft clockwise increments the index by one per step, rotating counter clockwise decrements the index. There are 20 steps in one full revolution, and this sketch can count over 65000 steps before starting from zero again. Because this sketch uses interrupts instead of a loop, it responds quickly and there's no missed pulses.

Sensor Kit (or get the rotary encoder by itself)
Male to Female Jumper Kit



/* interrupt routine for Rotary Encoders


   The average rotary encoder has three pins, seen from front: A C B
   Clockwise rotation A(on)->B(on)->A(off)->B(off)
   CounterCW rotation B(on)->A(on)->B(off)->A(off)

   and may be a push switch with another two pins, pulled low at pin 8 in this case
  

*/

// usually the rotary encoders three pins have the ground pin in the middle
enum PinAssignments {
  encoderPinA = 2,   // right (labeled DT on our decoder, yellow wire)
  encoderPinB = 3,   // left (labeled CLK on our decoder, green wire)
  clearButton = 8    // switch (labeled SW on our decoder, orange wire)
// connect the +5v and gnd appropriately
};

volatile unsigned int encoderPos = 0;  // a counter for the dial
unsigned int lastReportedPos = 1;   // change management
static boolean rotating=false;      // debounce management

// interrupt service routine vars
boolean A_set = false;              
boolean B_set = false;


void setup() {

  pinMode(encoderPinA, INPUT_PULLUP); // new method of enabling pullups
  pinMode(encoderPinB, INPUT_PULLUP); 
  pinMode(clearButton, INPUT_PULLUP);
 // turn on pullup resistors (old method)
  //digitalWrite(encoderPinA, HIGH);
 // digitalWrite(encoderPinB, HIGH);
 // digitalWrite(clearButton, HIGH);

// encoder pin on interrupt 0 (pin 2)
  attachInterrupt(0, doEncoderA, CHANGE);
// encoder pin on interrupt 1 (pin 3)
  attachInterrupt(1, doEncoderB, CHANGE);

  Serial.begin(9600);  // output
}

// main loop, work is done by interrupt service routines, this one only prints stuff
void loop() { 
  rotating = true;  // reset the debouncer

  if (lastReportedPos != encoderPos) {
    Serial.print("Index:");
    Serial.println(encoderPos, DEC);
    lastReportedPos = encoderPos;
  }
  if (digitalRead(clearButton) == LOW )  {
    encoderPos = 0;
  }
}

// Interrupt on A changing state
void doEncoderA(){
  // debounce
  if ( rotating ) delay (1);  // wait a little until the bouncing is done

  // Test transition, did things really change? 
  if( digitalRead(encoderPinA) != A_set ) {  // debounce once more
    A_set = !A_set;

    // adjust counter + if A leads B
    if ( A_set && !B_set ) 
      encoderPos += 1;

    rotating = false;  // no more debouncing until loop() hits again
  }
}

// Interrupt on B changing state, same as A above
void doEncoderB(){
  if ( rotating ) delay (1);
  if( digitalRead(encoderPinB) != B_set ) {
    B_set = !B_set;
    //  adjust counter - 1 if B leads A
    if( B_set && !A_set ) 
      encoderPos -= 1;

    rotating = false;
  }
}

Sunday, September 8, 2013

Building a IR Fire Sensor

This second sensor project uses the IR Fire Sensor. It has two outputs, 1 digital, 1 analog. I'm using the digital output (sensitivity set with onboard trimmer pot) to set the Pin 13 LED, to show flicker. I'm dumping the raw analog output to the serial monitor. Increased IR levels decrease the values output to the serial monitor.

Sensor Kit
Male to Female Jumper Kit






const int analogPin =  A0;      // the number of the analog pin
const int digitalPin = 7;     // the number of the digital pin
const int ledPin =  13;      // the number of the LED pin
int aVal = 0;
int dVal = 0;

void setup() {

  Serial.begin(9600);          //  setup serial

  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);    
  // initialize the digital pin as an input:
  pinMode(digitalPin, INPUT);  
}

void loop(){


  aVal = analogRead(analogPin);    // read the input pin
  Serial.println(aVal);             // analog value

  dVal = digitalRead(digitalPin);   // read the input pin
  digitalWrite(ledPin, dVal);    // sets the LED to the digital sensor's value
}

Interfacing a Joystick to an Arduino

We recently received a kit of sensors, and have started documenting how to use them. The sensor we are working with today is a joystick. This is a 2-axis unit with a push button on the shaft. There are two analog outputs, and one digital for the switch. At rest, the unit reads 512x512, with a 1 on the switch. The joystick can be pushed in all directions, with 0x1023, 0x512, 0x0, 512x0, 1023x512, 1023x1023, and 512x1023 at the extremes. Make sure you have your serial monitor speed set correctly. Please suggest practical projects based on this sensor, and we may build it!


Sensor Kit
Male to Female Jumper Kit


// Arduino pin numbers
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output

void setup() {
  pinMode(SW_pin, INPUT);
  digitalWrite(SW_pin, HIGH);
  Serial.begin(115200);
}

void loop() {
  Serial.print(digitalRead(SW_pin));
  Serial.print(" @ ");
  Serial.print(analogRead(X_pin));
  Serial.print("x");
  Serial.println(analogRead(Y_pin));
  delay(10);
}