Using Adafruit library for the DHT for Atmega328p

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

I wrote a simple code with Arduino IDE and I programmed my Atmega328p with it but it doesn't work.

Below codes work with Arduino UNO.

 

#include <avr/io.h>
#include "DHT.h"  
#define DHTTYPE DHT11
#define DHTPIN 2 

uint8_t t;
uint8_t h;
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  dht.begin();
  
  pinMode(9, OUTPUT);

}

void loop() {
  
  h = dht.readHumidity();
  t = dht.readTemperature();
  
    if(h > 0){
  digitalWrite(9, HIGH);
    }
  delay(100);
}

 

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

sajjadrb wrote:
it doesn't work

You're going to need to give more detail than that!

 

  • What were you expecting it to do?
  • What did it actually do?
  • What testing/debugging/investigation have you done to find the problem?

 

 

codes work with Arduino UNO.

So look at the differences between "your Atmega328p" and the UNO.

 

  • Is it on a PCB, or a breadboard, or what?
  • How did you load the code into "your Atmega328p" ?
  • Can you run any other code - in particular, a simple LED blink - on "your Atmega328p

 

Post a schematic of the setup of "your Atmega328p", plus a good, clear, in-focus photo (or photos) - Instructions here: https://www.avrfreaks.net/comment...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

sajjadrb wrote:
it doesn't work

You're going to need to give more detail than that!

 

  • What were you expecting it to do?
  • What did it actually do?
  • What testing/debugging/investigation have you done to find the problem?

 

codes work with Arduino UNO.

 

It has to turn on a LED according to humidity.

I tested Atmega328p and Arduino UNO simultaneously, Arduino read the humidity but atmega didn't.

 

awneil wrote:

So look at the differences between "your Atmega328p" and the UNO.

 

  • Is it on a PCB, or a breadboard, or what?
  • How did you load the code into "your Atmega328p" ?
  • Can you run any other code - in particular, a simple LED blink - on "your Atmega328p

 

Post a schematic of the setup of "your Atmega328p", plus a good, clear, in-focus photo (or photos) - Instructions here: https://www.avrfreaks.net/comment...

 

A new Atmega328p on breadboard.

I've changed one of boards in boards.txt of Arduino IDE to make it compatible with my Atmega328p  and I loaded code with Arduino IDE.

yeah, I've run different codes with this method including a simple LED blink and even more complex code that interfaces with ENC28J60.

 

Schematic

 

breadboard

Attachment(s): 

Last Edited: Mon. Jan 1, 2018 - 05:02 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You didn't follow the instructions on how to post the photo & schematic, did you?

 

Put them in the post so that we can see them:

 

 

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Mon. Jan 1, 2018 - 04:36 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

You didn't follow the instructions on how to post the photo & schematic, did you?

 

Sorry, I did. 

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

Well, you have edited you post and done it now - but you didn't do it originally.

 

Other questions remain unanswered:

 

  • How did you load the code into "your Atmega328p" ?
  • Can you run any other code - in particular, a simple LED blink - on "your Atmega328p"  <-- ie, specifically on this particular Atmega328p on this particular breadboard.

 

You say it works on a UNO - so instrument the code to see where, exactly, it is failing.

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

My guess is you’ve not set the fuses correctly and the mega328 is running at 1MHz. As Andy suggests - blinking an led would be a good test.

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

To me it looks like half of the chip is not wired up and I'm not going to say anything about caps.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

As js wrote : you need two decoupling capacitors.

As adafruit wrote  https://www.adafruit.com/product/385

it " requires careful timing to grab data. "

Thus, I am surprised you hav no Xal to warrant an accurate timing and you do not define the frequency (a naked 328 does not oscillate at the same frequency than an Arduino....) . From https://github.com/adafruit/DHT-... , line 150-151 : they disable interruopts because the pulse duration is time critical.

 

Thus, you should at least verify a LED blinks at the rythm you want (and hope the internal oscillator is accurate enough , as estimations you can do are not perfect).... I fear a Xal (and two extra 22pF condensators) would be safer....

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

The problem may be code, or it may be hardware.  Verify that the hardware is working first.

 

  The Mega328 on the plug-in breadboard doesn't have the power/ground pins connected on the left side (pins 7 and 8).  There is no crystal or external clock, so if this mega328 has been fused to operate on an Arduino UNO circuit board, then it is getting no clock. 

 

  Try loading the Arduino blink sketch and connecting an LED to D13 (pin 19, Port B5).  If there is no blink, then there is no 16MHz external clock coming into the mega328 [as there would be if the mega328 were mounted on the UNO circuit board].

 

  I recommend getting some of the $3 Arduino Nano boards with the CH340G USB/serial IC that are sold on BangGood.com and eBay Arduino section.  They are very stable and can be used as a simple, minimal AVR platform through Arduino.  Forgo all the other AVR devices.  If you can't do your application with an Arduino Nano, then move up to a 32-bit CPU.  If your application is too small to justify the cost of a $3 Nano platform, then add features and intutive user-interface devices (like a TFT touchscreen) until you have something useful, reliable, and commercial.