SparkFun Forums 

Where electronics enthusiasts find answers.

Tips and questions relating to the GPS modules from SFE
By zonk
#153161
Hello there,

I am building a gps-data-logger which records temperature & humidity.
I am using the RHT03 Temperature & Humidity Sensor (5V / https://www.sparkfun.com/products/10167) and the 66 Channel LS20031 GPS 10Hz Receiver (3.3V / https://www.sparkfun.com/products/8975).

The RHT03 sensor output is corrupted when both components are powered, lots of check sum errors and ACK time outs (i am using this library https://github.com/ringerc/Arduino-DHT2 ... 224af1cf7d) and no proper values. When i unplug the gps receiver the RHT03-readings suddenly turn good, wenn I power the gps on again the readings turn bad again. I wired the GPS module as descripted here (http://www.sparkfun.com/tutorials/176). The RHT03 runs on 5V, GPS Module runs on 3.3V, both share GND.

Any suggestions how to run the GPS-Module and the RHT03 simultaneously and get good readings from the RHT03?

I work with Arduino 1.0.2 (on osx 10.6.8) and an Arduino Uno SMD.
Thanks in advance for any suggestions & answers

cheers, simon

same post on arduino forum -> http://arduino.cc/forum/index.php/topic,137244.0.html
By WethaGuy
#153166
Easy fix.

Those both use Serial connections. You can only use one device at a time on a Serial connnection. So either you have them take turns using the connection or just use separate connections. The easiest solution is using SoftwareSerial to make the connection for one of those and Serial for the other. See the SoftwareSerial on Arduino.cc for more info.
By D9W
#154812
I understood it had to do with the CLI() function and it's partner sei() and timing the RHT03 needs that stops some important internal clocks.
cli(); //------------------------------------ disable global interrupts {see DHT.cpp}
sei(); //------------------------------------------------ enable interrupts
I cant find it right now but those two functions were explained in the Arduino Cookbook Second Edition.
If I understood things right these two functions also will give I2C an headache too, if they are used when a I2C call is being processed.

The way it was explained to me was you don't want to read both at the same time. I know. I was scratching my head at that one too. There is an example in Arduino Cook Book 2nd Edition where it showed how to talk to one serial object at a time. (Great bed time story- just hard to remember where locate the information)

DHTtester.pde >>
Code: Select all
// DHTtester.pde FILE
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

#include "DHT.h"
#define DHTPIN 2 // what pin we're connected to

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

// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

DHT dht(DHTPIN, DHTTYPE);

void setup() {
    Serial.begin(9600); 
    Serial.println("DHTxx test!");
dht.begin();
}
void loop() {
// 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 = dht.readHumidity();
float t = dht.readTemperature();

// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h)) {
        Serial.println("Failed to read from DHT");
    } else {
        Serial.print("Humidity: "); 
        Serial.print(h);
        Serial.print(" %\t");
        Serial.print("Temperature: "); 
        Serial.print(t);
        Serial.println(" *C");
        }
}
DHT.h >>
Code: Select all
//HDT.h File 

#ifndef DHT_H 
#define DHT_H 
#if ARDUINO >= 100 
 #include "Arduino.h" 
#else 
 #include "WProgram.h" 
#endif 

/* DHT library  

MIT license 
written by Adafruit Industries 
*/ 

// how many timing transitions we need to keep track of. 2 * number bits + extra 
#define MAXTIMINGS 85 

#define DHT11 11 
#define DHT22 22 
#define DHT21 21 
#define AM2301 21 

class DHT { 
 private: 
  uint8_t data[6]; 
  uint8_t _pin, _type, _count; 
  boolean read(void); 
  unsigned long _lastreadtime; 
  boolean firstreading; 

 public: 
  DHT(uint8_t pin, uint8_t type, uint8_t count=6); 
  void begin(void); 
  float readTemperature(bool S=false); 
  float convertCtoF(float); 
  float readHumidity(void); 

}; 
#endif 

DHT.cpp >>
Code: Select all
/* DHT.cpp library 

MIT license 
written by Adafruit Industries 
*/ 

#include "DHT.h" 

DHT::DHT(uint8_t pin, uint8_t type, uint8_t count) { 
_pin = pin; 
_type = type; 
_count = count; 
firstreading = true; 
} 

void DHT::begin(void) { 
// set up the pins! 
pinMode(_pin, INPUT); 
digitalWrite(_pin, HIGH); 
_lastreadtime = 0; 
} 

//boolean S == Scale. True == Farenheit; False == Celcius 
float DHT::readTemperature(bool S) { 
float f; 

if (read()) { 
switch (_type) { 
case DHT11: 
f = data[2]; 
if(S) 
f = convertCtoF(f); 

return f; 
case DHT22: 
case DHT21: 
f = data[2] & 0x7F; 
f *= 256; 
f += data[3]; 
f /= 10; 
if (data[2] & 0x80) 
f *= -1; 
if(S) 
f = convertCtoF(f); 

return f; 
} 
} 
Serial.print("Read fail"); 
return NAN; 
} 

float DHT::convertCtoF(float c) { 
return c * 9 / 5 + 32; 
} 

float DHT::readHumidity(void) { 
float f; 
if (read()) { 
switch (_type) { 
case DHT11: 
f = data[0]; 
return f; 
case DHT22: 
case DHT21: 
f = data[0]; 
f *= 256; 
f += data[1]; 
f /= 10; 
return f; 
} 
} 
Serial.print("Read fail"); 
return NAN; 
} 


boolean DHT::read(void) { 
uint8_t laststate = HIGH; 
uint8_t counter = 0; 
uint8_t j = 0, i; 
unsigned long currenttime; 

// pull the pin high and wait 250 milliseconds 
digitalWrite(_pin, HIGH); 
delay(250); 

currenttime = millis(); 
if (currenttime < _lastreadtime) { 
// ie there was a rollover 
_lastreadtime = 0; 
} 
if (!firstreading && ((currenttime - _lastreadtime) < 2000)) { 
return true; // return last correct measurement 
//delay(2000 - (currenttime - _lastreadtime)); 
} 
firstreading = false; 
/* 
Serial.print("Currtime: "); Serial.print(currenttime); 
Serial.print(" Lasttime: "); Serial.print(_lastreadtime); 
*/ 
_lastreadtime = millis(); 

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

// now pull it low for ~20 milliseconds 
pinMode(_pin, OUTPUT); 
digitalWrite(_pin, LOW); 
delay(20); 
cli(); //------------------------------------ disable global interrupts 
digitalWrite(_pin, HIGH); 
delayMicroseconds(40); 
pinMode(_pin, INPUT); 

// read in timings 
for ( i=0; i< MAXTIMINGS; i++) { 
counter = 0; 
while (digitalRead(_pin) == laststate) { 
counter++; 
delayMicroseconds(1); 
if (counter == 255) { 
break; 
} 
} 
laststate = digitalRead(_pin); 

if (counter == 255) break; 

// ignore first 3 transitions 
if ((i >= 4) && (i%2 == 0)) { 
// shove each bit into the storage bytes 
data[j/8] <<= 1; 
if (counter > _count) 
data[j/8] |= 1; 
j++; 
} 

} 

sei(); //------------------------------------------------ enable interrupts 

/* 
Serial.println(j, DEC); 
Serial.print(data[0], HEX); Serial.print(", "); 
Serial.print(data[1], HEX); Serial.print(", "); 
Serial.print(data[2], HEX); Serial.print(", "); 
Serial.print(data[3], HEX); Serial.print(", "); 
Serial.print(data[4], HEX); Serial.print(" =? "); 
Serial.println(data[0] + data[1] + data[2] + data[3], HEX); 
*/ 

// check we read 40 bits and that the checksum matches 
if ((j >= 40) && 
(data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) ) { 
return true; 
} 


return false; 

}