Do anyone knows if there is a conflict using 1wire and 4wire communications at the same time? I have a program that tries to use an SD card to write on a sensor value taken from the dht22 that works with the 1 wire protocol. I haven’t conflicts between the sd pins an the sensor pin. But after the “SD.begin” the sensor does not answer to arduino (dht22 start condition 1 not met). Without SD begin everything is ok.
This is the code:
#include "dht22.h"
#include <SD.h>
dht22 mioTwigTempHum(A2);
void setup()
{
Serial.begin(9600);
//inizializzo la parte della scheda SD SETUP->SD
Serial.println("Initializing SD card...");
// disable w5100 SPI while setting up SD
pinMode(10,OUTPUT);
digitalWrite(10, HIGH);//per arduino mega http://arduino.cc/forum/index.php/topic,46976.0.html
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(53, OUTPUT);
digitalWrite(53, HIGH);
if (!SD.begin(4)) {
Serial.println("non potra' salvare su SD!");
}
}
void loop()
{
mioTwigTempHum.aggiornaValori();
Serial.println(mioTwigTempHum.umidita );
Serial.println(mioTwigTempHum.temperatura );
delay(1000); // wait for one second
}
and this is the sensor library
.H
#ifndef dht22_h
#define dht22_h
class dht22
{
private:
int analogInArduino; //pin arduino dove e' connesso il sensore
unsigned char read_dht22_dat();
public:
dht22(int analogInArduino); //costruttore
~dht22( ); //distruttore
bool aggiornaValori(); //aggiorna le variabili umidita e temperatura
float umidita;
float temperatura;
};
#endif
.CPP
/******************************************************************************
* Includes
******************************************************************************/
#include "dht22.h"
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif //Arduino includes
/******************************************************************************
* Definitions
******************************************************************************/
/******************************************************************************
* Constructors
******************************************************************************/
dht22::dht22(int analogInArduino)
{
this->analogInArduino=analogInArduino;
pinMode(analogInArduino,OUTPUT);
}
dht22::~dht22( )
{
}
/******************************************************************************
* User API
******************************************************************************/
bool dht22::aggiornaValori()
{
byte dht22_dat[5];
byte i;
// start condition
// 1. pull-down i/o pin from 18ms
digitalWrite(analogInArduino,LOW);
delay(18);
digitalWrite(analogInArduino,HIGH);
delayMicroseconds(40);
pinMode(analogInArduino,INPUT);
if(digitalRead(analogInArduino)){
Serial.println("dht22 start condition 1 not met");
return(false);
}
delayMicroseconds(80);
if(!digitalRead(analogInArduino)){
Serial.println("dht22 start condition 2 not met");
return(false);
}
delayMicroseconds(80);
// now ready for data reception
for (i=0; i<5; i++)
dht22_dat[i] = read_dht22_dat();
pinMode(analogInArduino,OUTPUT);
digitalWrite(analogInArduino,HIGH);
byte dht22_check_sum = dht22_dat[0]+dht22_dat[1]+dht22_dat[2]+dht22_dat[3];
// check check_sum
if(dht22_dat[4]!= dht22_check_sum)
{
Serial.println("DHT22 checksum error");
return(false);
}
umidita=((float)(dht22_dat[0]*256+dht22_dat[1]))/10;
temperatura=((float)(dht22_dat[2]*256+dht22_dat[3]))/10;
return(true);
}
unsigned char dht22::read_dht22_dat()
{
byte i = 0;
byte result=0;
for(i=0; i< 8; i++){
while(!digitalRead(analogInArduino)); // wait for 50us
delayMicroseconds(30);
if(digitalRead(analogInArduino))
result |=(1<<(7-i));
while(digitalRead(analogInArduino)); // wait '1' finish
}
return result;
}
I have tested this on the arduino 2560 and the duemilanove. For the sd I have used the ethernet shield and the wireless sd shiled.
Do you have any idea of the reason of the conflicts considering that pins are not in conflicts?
Thanks in advance