18F4550 with humidity sensor

Discussion in 'Embedded Systems and Microcontrollers' started by puddled, Jun 5, 2010.

  1. puddled

    Thread Starter New Member

    Jun 5, 2010

    I am trying to interface a humidity sensor to an 18F4550 and display the result on an LCD.

    The sensor in question is a DHT-11 or AM2303 made by aosong Datasheet

    The sensor outputs a digital signal on 1 wire, when requested by the mcu.

    I have the mcu communicating with the sensor, but am having trouble getting the results read, and then decoded to display on the lcd.

    Basically, the sensor outputs the signal as 5x8 bit words, consisting of a humidity and temperature readings and also a checksum.

    The signal is made up of varying high / low levels, with the lows indicating next bit is ready, and the length of the high indicating a 1 or 0.

    I am using mikro c for coding.

    Has anyone worked with one of these before, or any any pointers.

  2. tkrudresh

    New Member

    Jun 9, 2010
    Do you able to read the data from the sensor? or display problem. if it is display problem check the LCD driver.

    if the read fails. i think its just timing issue. do u wait for the exact time for low signal before the actual data. find the LOW signal delay from the data sheet..

    if you provide more information i will try to help..

  3. Vaughanabe13

    Active Member

    May 4, 2009
    First of all, this sensor really sucks, to be totally honest. It has an entirely too-complicated data transmission system. A better (and much easier to program) sensor would have at least two lines, data and clock. But if you are absolutely set on using this sensor, here is how you should do it:

    In hardware, attach the data line of the sensor to an interrupt-on-change pin of the PIC. For example, INT0. Then configure the interrupt to trigger on a negative edge, but do not enable interrupts yet. Put a weak pullup resistor on the data line to keep it at VCC, or use the internal pullups in software.

    In software, you are going to need very precise timing. This means you need a fast and accurate crystal/resonator, and you need to do the calculations for precise delays, which will be used next.

    Now to transmit data, do this:
    1. Configure the INT0 pin as OUTPUT and pull the line low for >80us to start transmission.
    2. Configure the INT0 pin as INPUT and the pullup resistor will bring it back to VCC. Wait for a negative edge, indicating the sensor is ready, and then wait for a positive signal again (NOT using interrupts yet). For example, you could write:
    Code ( (Unknown Language)):
    1. while(PORTBbits.RB0);
    2. while(!PORTBbits.RB0);
    where PORTBbits.RB0 is the DATA/INT0 pin.
    After this you know the sensor is ready to transmit data. Now you need to enable the interrupts on your INT0 pin and let the interrupt service routine do the rest of the work. At a falling edge your ISR will take over, indicating the first data bit is transmitting. Inside the ISR, you must program in a precise delay of >50us. I would make it 60us to be safe. As soon as this delay is over, read the state of the DATA pin into your 8-bit variables. The way you want to do this is up to you. Make sure you are not overwriting any previous data. I would probably use the shift operator and addition to do this. i.e.
    Code ( (Unknown Language)):
    1. unsigned char firstByte, secondByte = 0b00000000;
    2. unsigned char count = 0;
    3. void ISR(void) {
    4.      //clear interrupt flag here
    5.      //Delay 60us here
    6.      if (count < 8) {
    7.           if (PORTBbits.RB0) firstByte+= 1; //Makes the LSB of firstByte == 1 or 0
    8.           firstByte = firstByte << 1; //Shifts the byte left by 1.
    9.      }
    10.      else if (count >= 8 && count < 16) {
    11.           if (PORTBbits.RB0) secondtByte+= 1;  //repeat for all variables
    12.           secondByte = secondByte << 1;
    13.      }
    14.      //etc. etc.
    15.      else if (//count is at the last bit) {
    16.           //Make sure checksum is valid here. If valid, disable interrupts,
    17.           //indicating the end of data transmission
    18.      }
    19.      count++;
    20. }
    3. Now you have your sensor data and you can process it as you wish.