Mkr 1010 and dht22 gives faulty reading on pin 3 or 4 it does fine for pin 2

Hi All, my first post, I am brand new to the world of Auduino and microcontrollers, I am trying to read from a dht22 using an arduino MKR 1010, and it worked fine for one sensor, when I try to add a second one to pin 3 or 4 it gives me faulty reading, I have try swapping the sensor to make sure the second is not faulty but same behaviour, if somebody could help me understand why when I use pin 2 everything works fine but if I try with pin 3 or 4 then I dont get reading or in same rare cases I get one with lower value I would appreciate it:
if I put it in pin3 I get

Publishing message
21.70
nan

Publishing message
21.70
10.80

Publishing message
21.70
nan

Publishing message
21.70
nan

in some rare cases I get in both

here is my code:

/*
  AWS IoT WiFi

  This sketch securely connects to an AWS IoT using MQTT over WiFi.
  It uses a private key stored in the ATECC508A and a public
  certificate for SSL/TLS authetication.

  It publishes a message every 5 seconds to arduino/outgoing
  topic and subscribes to messages on the arduino/incoming
  topic.

  The circuit:
  - Arduino MKR WiFi 1010 or MKR1000

  The following tutorial on Arduino Project Hub can be used
  to setup your AWS account and the MKR board:

  https://create.arduino.cc/projecthub/132016/securely-connecting-an-arduino-mkr-wifi-1010-to-aws-iot-core-a9f365

  This example code is in the public domain.
*/

#include <ArduinoBearSSL.h>
#include <ArduinoECCX08.h>
#include <ArduinoMqttClient.h>
#include <WiFiNINA.h> // change to #include <WiFi101.h> for MKR1000
#include "DHT.h"

#include "arduino_secrets.h"

/////// Enter your sensitive data in arduino_secrets.h
const char ssid[]        = SECRET_SSID;
const char pass[]        = SECRET_PASS;
const char broker[]      = SECRET_BROKER;
const char* certificate  = SECRET_CERTIFICATE;


WiFiClient    wifiClient;            // Used for the TCP socket connection
BearSSLClient sslClient(wifiClient); // Used for SSL/TLS connection, integrates with ECC508
MqttClient    mqttClient(sslClient);

unsigned long lastMillis = 0;

//assign pins for temperature and humidity sensors 1 and 2

#define DHTPIN 2     // Digital pin 2 connected to the DHT sensor1
#define DHTPIN2 3     // Digital pin 3 connected to the DHT sensor2
// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
// Pin 15 can work but DHT must be disconnected during program upload.

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 3 (on the right) of the sensor to GROUND (if your sensor has 3 pins)
// Connect pin 4 (on the right) of the sensor to GROUND and leave the pin 3 EMPTY (if your sensor has 4 pins)
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors.  This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
// create multiple instances of a DHT object.
DHT dht(DHTPIN, DHTTYPE);
DHT dht2(DHTPIN2, DHTTYPE);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = 0;
  // Read temperature as Celsius (the default)
  float t = 0;
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = 0;
  //sensor2 
  float h2 = 0;
  // Read temperature as Celsius (the default)
  float t2 = 0;
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f2 = 0;


// exist in previous code removed this - rafa start
/*
void setup() {
  Serial.begin(9600);
  Serial.println(F("DHTxx test!"));

  dht.begin();
}
*/
// exist in previous code will remove this rafa end

void setup() {
  dht.begin();
  Serial.begin(115200);
 
  while (!Serial);
  
  if (!ECCX08.begin()) {
    Serial.println("No ECCX08 present!");
    while (1);
  }

  // Set a callback to get the current time
  // used to validate the servers certificate
  ArduinoBearSSL.onGetTime(getTime);

  // Set the ECCX08 slot to use for the private key
  // and the accompanying public certificate for it
  sslClient.setEccSlot(0, certificate);

  // Optional, set the client id used for MQTT,
  // each device that is connected to the broker
  // must have a unique client id. The MQTTClient will generate
  // a client id for you based on the millis() value if not set
  //
  // mqttClient.setId("clientId");

  // Set the message callback, this function is
  // called when the MQTTClient receives a message
  mqttClient.onMessage(onMessageReceived);
}



void loop() {
  if (WiFi.status() != WL_CONNECTED) {
    connectWiFi();
  }

 //delay(2000);
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  f = dht.readTemperature(true);
  //sensor2
  h2 = dht2.readHumidity();
  // Read temperature as Celsius (the default)
  t2 = dht2.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  f2 = dht2.readTemperature(true);
  

  if (!mqttClient.connected()) {
    // MQTT client is disconnected, connect
    connectMQTT();
  }

  // poll for new MQTT messages and send keep alives
  mqttClient.poll();

  // publish a message roughly every 5 seconds.
  if (millis() - lastMillis > 5000) {
    lastMillis = millis();

    publishMessage();
  }
}

unsigned long getTime() {
  // get the current time from the WiFi module  
  return WiFi.getTime();
}

void connectWiFi() {
  Serial.print("Attempting to connect to SSID: ");
  Serial.print(ssid);
  Serial.print(" ");

  while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
    // failed, retry
    Serial.print(".");
    delay(5000);
  }
  Serial.println();

  Serial.println("You're connected to the network");
  Serial.println();
}

void connectMQTT() {
  Serial.print("Attempting to MQTT broker: ");
  Serial.print(broker);
  Serial.println(" ");

  while (!mqttClient.connect(broker, 8883)) {
    // failed, retry
    Serial.print(".");
    delay(5000);
  }
  Serial.println();

  Serial.println("You're connected to the MQTT broker");
  Serial.println();

  // subscribe to a topic
  mqttClient.subscribe("arduino/incoming");
}

void publishMessage() {
  Serial.println("Publishing message");

  // send message, the Print interface can be used to set the message contents
  mqttClient.beginMessage("arduino/outgoing");
  //mqttClient.print("hello ");
  
  mqttClient.print(F("Humidity Sensor1: "));
  mqttClient.print(h);
  mqttClient.print(F("%  Temperature Sensor1: "));
  mqttClient.print(t);
  Serial.println(t);
//sensor2 
  delay(1000);
  mqttClient.print(F("Humidity Sensor2: "));
  mqttClient.print(h2);
  mqttClient.print(F("%  Temperature Sensor2: "));
  mqttClient.print(t2);
  Serial.println(t2);
  mqttClient.endMessage();
}

void onMessageReceived(int messageSize) {
  // we received a message, print out the topic and contents
  Serial.print("Received a message with topic '");
  Serial.print(mqttClient.messageTopic());
  Serial.print("', length ");
  Serial.print(messageSize);
  Serial.println(" bytes:");

  // use the Stream interface to print the contents
  while (mqttClient.available()) {
    Serial.print((char)mqttClient.read());
  }
  Serial.println();

  Serial.println();
}

I had a wifi1010 with 2 DHT22 sensors. I used pins 4 &5 and they worked fine.
Looking over your code and pic everything looks to be in order except that you have them plugged into 5v which could fry your Wifi1010.
Even your comments say not to do that:

It may even be whats causing your issue.

Thank you Hutkikz!! I connected to VCC output which based on this post link and I moved to pin 4 and 5, with the same results, appreciate the help, glad to know I should not be using 5V with my MKR1010

1 Like

I figure it out I wasn't starting the second sensor it should have been like this

void setup() {
  dht.begin();
  dht2.begin();
  Serial.begin(115200);

instead of this:

void setup() {
  dht.begin();
  Serial.begin(115200);

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.