18F4550 with humidity sensor

Thread Starter

puddled

Joined Jun 5, 2010
1
Hi,

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.

Thanks
 

tkrudresh

Joined Jun 9, 2010
1
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..

Regards
Rudresh
 

Vaughanabe13

Joined May 4, 2009
102
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:
Rich (BB code):
while(PORTBbits.RB0);
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.
Rich (BB code):
unsigned char firstByte, secondByte = 0b00000000;
unsigned char count = 0;
void ISR(void) {
     //clear interrupt flag here
     //Delay 60us here
     if (count < 8) {
          if (PORTBbits.RB0) firstByte+= 1; //Makes the LSB of firstByte == 1 or 0
          firstByte = firstByte << 1; //Shifts the byte left by 1.
     }
     else if (count >= 8 && count < 16) {
          if (PORTBbits.RB0) secondtByte+= 1;  //repeat for all variables
          secondByte = secondByte << 1;
     }
     //etc. etc.
     else if (//count is at the last bit) {
          //Make sure checksum is valid here. If valid, disable interrupts,
          //indicating the end of data transmission
     }
     count++;
}
3. Now you have your sensor data and you can process it as you wish.
 
Top