0

I am new to low-level programming, and attempting to connect a DHT22 sensor onto my Adafruit M0 Lora for temperature readings. So far I only retrieve NaNs.

The connections I have set up are identical with this sketch, aside from using pin 13 as opposed to pin 2 for sensor input/output. I am aware of the sketch being made for a different feather board, although the logic should still remain the same from what I can understand.

I am making use of Adafruit's DHT library

#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>

// pin connected to DH22 data line
#define DATA_PIN 13
DHT_Unified dht(DATA_PIN, DHT22);

void setup() {

  // start the serial connection
  Serial.begin(9600);

  // wait for serial monitor to open
  while(! Serial);

  // initialize dht22
  dht.begin();

  // connect to io.adafruit.com
  Serial.print("Connecting to Adafruit IO");

  // we are connected
  Serial.println();

}

void loop() {

  sensors_event_t event;
  dht.temperature().getEvent(&event);

  float celsius = event.temperature;
  float fahrenheit = (celsius * 1.8) + 32;

  Serial.print("celsius: ");
  Serial.print(celsius);
  Serial.println("C");

  Serial.print("fahrenheit: ");
  Serial.print(fahrenheit);
  Serial.println("F");

  // save fahrenheit (or celsius) to Adafruit IO

  dht.humidity().getEvent(&event);

  Serial.print("humidity: ");
  Serial.print(event.relative_humidity);
  Serial.println("%");
  delay(5000);
}

Would anyone be able to help point of what I am doing incorrectly? I tried at other bauds than 9600, as well as changing the programmable pin. Any help at all would be greatly appreciated.

gre_gor
  • 6,669
  • 9
  • 47
  • 52
Hakon
  • 1
  • 1

1 Answers1

1

I don't think it's a code problem. Pin 13 is special. Choose a different pin.

Specifically:

NOTE: Digital pin 13 is harder to use as a digital input than the other digital pins because it has an LED and resistor attached to it that's soldered to the board on most boards. If you enable its internal 20k pull-up resistor, it will hang at around 1.7V instead of the expected 5V because the onboard LED and series resistor pull the voltage level down, meaning it always returns LOW. If you must use pin 13 as a digital input, set its pinMode() to INPUT and use an external pull down resistor.

From Arduino documentation.

Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175