Sunday, September 14, 2014

ARDUINO Humidity & Temperature Measurement Using DHT11 Sensor

In this project we are going to interface the arduino uno with DHT11 digital humidity and temperature sensor.
About DHT11 humidity and temperature sensor:
The DHT11 sensor comes in a single row 4-pin package and operates from 3.5 to 5.5V power supply. It can measure temperature from 0-50 °C with an accuracy of ±2°C and relative humidity ranging from 20-95% with an accuracy of  ±5%. The sensor provides fully calibrated digital outputs for the two measurements. It has got its own proprietary 1-wire protocol, and therefore, the communication between the sensor and a microcontroller is not possible through a direct interface with any of its peripherals. The protocol must be implemented in the firmware of the MCU with precise timing required by the sensor.

The following timing diagrams describe the data transfer protocol between a MCU and the DHT11 sensor. The MCU initiates data transmission by issuing a “Start” signal. The MCU pin must be configured as output for this purpose. The MCU first pulls the data line low for at least 18 ms and then pulls it high for next 20-40 us before it releases it. Next, the sensor responds to the MCU “Start“  signal by pulling the line low for 80 us followed by a logic high signal that also lasts for 80 us. Remember that the MCU pin must be configured to input after finishing the “Start“ signal. Once detecting the response signal from the sensor, the MCU should be ready to receive data from the sensor. The sensor then sends 40 bits (5 bytes) of data continuously in the data line. Note that while transmitting bytes, the sensor sends the most significant bit first.

Data consists of decimal and integral parts. A complete data transmission is 40bit, and the sensor sends higher data bit first.
Data format: 8bit integral RH data + 8bit decimal RH data + 8bit integral T data + 8bit decimal T data + 8bit check sum. If the data transmission is right, the check-sum should be the last 8bit of "8bit integral RH data + 8bit decimal RH data + 8bit integral T data + 8bit decimal T data".
The DHT11 is a digital sensor so it sends 1's and 0's, but it is very important to know how it sends the digital data. The figure below shows how the sensor sends its information:

The Circuit:
The figure below shows the wiring of our circuit using arduino uno board, DHT11 sensor and 16X2 LCD display:


 #include <LiquidCrystal.h>
 LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
 byte deg[7] = {           //to display ° character
 unsigned char a, b,i,rh1,rh2,t1,t2,sum;
 void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  lcd.createChar(0, deg);
 int startsignal(){
  pinMode(8, OUTPUT);
  pinMode(8, INPUT);}
 int checkresponse(){
  a = 0;delayMicroseconds(40);
     a = 1;}
 int readdata(){
   while(!digitalRead(8));  //Wait pin8 goes high
   else { bitSet(i,7-b); 
       while(digitalRead(8));//Wait pin8 goes low
 void loop() {
  startsignal();    //send the start signal to the sensor
  checkresponse();  //check if the sensor sends the response signal
  if (a==1){
   rh1 = i;
   rh2 = i;
   t2 = i;
   sum = i;
   if (sum == rh1+rh2+t1+t2){      //Checksum
     //Display Temperature  
     lcd.setCursor(0, 0); lcd.print("Temp    =   .0 C");
     lcd.setCursor(10, 0); lcd.print(t1);
     lcd.setCursor(14, 0);lcd.write(byte(0));
     //Display Humidity:
     lcd.setCursor(0, 1); lcd.print("Humidity=   .0%");
     lcd.setCursor(10, 1); lcd.print(rh1);
   else{        //If the checksum is incorrect
     lcd.setCursor(1, 0); lcd.print("Checksum error");}  
   else {       //If the sensor does not respond
    lcd.setCursor(2, 0); lcd.print("no response");
    lcd.setCursor(0, 1); lcd.print("from the sensor");

Tuesday, September 2, 2014

LED blink without delay, arduino timer interrupt

Blink Without Delay:

Sometimes you need to do two things at once. For example you might want to blink an LED (or some other time-sensitive function) while reading a button press or other input. In this case, you can't use delay(), or you'd stop everything else the program while the LED blinked. The program might miss the button press if it happens during the delay(). This sketch demonstrates how to blink the LED without using delay(). It keeps track of the last time the Arduino turned the LED on or off. Then, each time through loop(), it checks if a long enough interval has passed. If it has, it toggles the LED on or off.


We can make this LED blink without delay using tow different codes: 
Code 1:
This code below uses the millis() function, a command that returns the number of milliseconds since the Arduino board started running its current program, to blink an LED.

long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(13, OUTPUT);      

void loop()
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;  
    // toggle the state of pin 13:
    digitalWrite(13, !digitalRead(13));

Code 2:
Resetting the millis() function is not easy and this function stay running and until it reaches the limit value of a 4 byte unsigned number (about 49 continuous days)  then it returns to 0 where our function can not affect anymore 
This code uses timer1 interrupt every 1000 ms to make our led blink.

  int timer1_value;
   void setup()
  pinMode(13, OUTPUT);

  // initialize timer1 
  noInterrupts();           // disable all interrupts
  TCCR1A = 0;
  TCCR1B = 0;

  // Set timer1_value to the correct value for our interrupt interval
  timer1_value = 3036;   // preload timer 65536-16MHz/(256*1Hz)
  TCNT1 = timer1_value;   // preload timer
  TCCR1B |= (1 << CS12);    // 256 prescaler 
  TIMSK1 |= (1 << TOIE1);   // enable timer overflow interrupt
  interrupts();             // enable all interrupts

 ISR(TIMER1_OVF_vect)        // interrupt service routine 
  TCNT1 = timer1_value;   // preload timer
  digitalWrite(13, !digitalRead(13));

  void loop()
  // write your program...

Arduino Timer/Counter

A timer/counter is a piece of hardware built in the Arduino ATmega microcontroller. It is like a clock, and can be used to measure time events.
The timer can be programmed by some special registers. You can configure the pre-scaler for the timer, or the mode of operation and many other things.
The Arduino board is based on the Atmel AVR ATmega168 or the ATmega328 microcontroller. These chips are pin compatible and only differ in the size of internal memory. Both have 3 timers, called Timer0, Timer1 and Timer2. Timer0 and Timer2 are 8bit timer, where Timer1 is a 16bit timer.

 Timer0 is a 8bit timer.
 In the Arduino world Timer0 is been used for the timer functions, like delay(), millis() and micros().
 If you change Timer0 registers, this may influence the Arduino timer function. So you should know what 
 you are doing.
 Timer1 is a 16bit timer.
 In the Arduino world the Servo library uses Timer1 on Arduino Uno (Timer5 on Arduino Mega).
 Timer2 is a 8bit timer like Timer0.
 In the Arduino work the tone() function uses Timer2.
Arduino uno timer/counter1 registers and time frequency:
  • TCCRx - Timer/Counter Control Register. The pre-scaler can be configured here. 
  • TCNTx - Timer/Counter Register. The actual timer value is stored here. 
  • Timer/Counter Interrupt Mask Register. To enable/disable timer interrupts.