DHT11 C program arduino

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello.

 

So I'm currently on a school project on my Embedded class where we get to pick our self a thing to do.

 

I decided to make a small DHT11 sensor that just takes up the temperature and humidity.

 

I'm using this site and code ( cloud.timeedit.net/nackademin/web/1/ri135507g06547Q0g5QY5153Z16YX0Q3Q61Y560yZ0.html ) as a base for my code.

 

However, the problem is that there isn't coming out anything from the sensors. When I go in to the dev screen, all I see is empty rows (linux terminal command).

 

I've tried to turn it from Rasperry Pi to Arduino style code for C. I'm not allowed to use the Arduino IDE and must use C. This is the code I currently got:

     mail.c (ignore uint8_t ReciveData(). I'm not using it)

#include <avr/io.h>
#include <util/delay.h>
#include "serial.h"
#include "led.h"
#define MAXTIMINGS    85
int dht11_dat [5] = {0, 0, 0, 0, 0};
uint8_t c = 0;

// http://www.uugear.com/portfolio/dht11-humidity-temperature-sensor-module/
/*uint8_t Receive_data()            
{    
    // Sätter den till en output
    DDRC |= (1 << PORTC5);
    // Sättern den till en input utan att ändra nägra av bittarna
    DDRC &= ~(1 << PORTC5);
    for (int i = 0; i < 8; i++)
    {
        while((PIND & (1<<PORTC5)) == 0);  
        _delay_us(30);
        if(PIND & (1<<PORTC5))
        c = (c<<1)|(0x01);    
        else            
        c = (c<<1);
        while(PIND & (1<<PORTC5));
    }
    return c;*/
}
uint8_t ReadDHT11Data() {
    uint8_t counter = 0;
    uint8_t laststate = (1 << PORTC5);
    uint8_t j = 0, i;
    float f;

    dht11_dat[0] = dht11_dat[1] = dht11_dat[2] = dht11_dat[3] = dht11_dat[4] = 0;

    
    DDRC |= (1 << PORTC5);
    
    DDRC &= ~(1 << PORTC5);
    
    

    for (i = 0; i < MAXTIMINGS; i++)
    {
        counter = 0;
        while (DDRC & 0b10000000)
        {
            counter++;
            _delay_ms(1000);
            if (counter == 255)
            {
                break;
            }
        }
        laststate = (PORTC5 & 0b10000000);

        if (counter == 255)
            break;

        if ((1 >= 4) && (i % 2 == 0)) {
            dht11_dat[j / 8] <<= 1;
            if (counter > 16) 
                dht11_dat[j / 8] |= 1;
            j++;
        }

    }

    if ((j >= 40) &&
        (dht11_dat[4] == ( (dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) & 0xFF) )){
            f = dht11_dat[2] * 9. / 5. + 32;
            printf("Humidity = %d %d %% \n Temperature = %d %d *C (%.1f *F)\n", 
            dht11_dat[0], dht11_dat[1], dht11_dat[2], dht11_dat[3], f); 
        } else {
            printf("Data not found. Retrying.\n");
        }
}


void main (void) {
    LEDInit();

    printf("Getting Tempretaure.\n");

    while (1) {
        ReadDHT11Data();
        _delay_ms(1000);
    }
}

     led.c

 

#include <avr/io.h>
#include "led.h"

void LEDInit() {
	DDRD = (0 << DDD5);

	
}

I'd like all the help to understand why this isn't working as it should.

 

Thank you.

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Receive_data has a better chance of working methinks. The second function seems to rely on loop time which is going to vary depending on what device and how it is clocked. In fact, i can’t see how it could ever work in its current form. anything that does i/o on a Raspberry pi is unlikely to work on an AVR.

One thing to note is that you must supply the cpu speed either via a #define or a -D on the command line in order for the delays to be properly calculated for your cpu.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm guessing a whole class has been given this DH11 assignment as there's been other threads about DH11 in the last few days. You may want to search those out so you can take a look at your classmate's approach ;-)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ravoos wrote:
I'm not allowed to use the Arduino IDE and must use C.

There is nothing wrong with using Arduino h/w and example code to test your h/w to see if it all works, and see and learn from others how they have solved the problem, or organized their code.....

Then write your own code in pure C to satisfy your class assignment.   As a professional, I'll use a quick arduino sketch found on the "net" to test out a new sensor or a new technique that I've heard about and want to learn more.

 

Jim

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

Last Edited: Thu. Feb 14, 2019 - 01:40 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:
Then write your own code in pure C to satisfy your class assignment. 
What's more all the Arduino source is open - admittedly it is C++ not C - but often you can study the execution sequence and at least borrow the "algorithm" for your own code.