I made a sketch for the SHT11 and dump the reading on a 16x2 LCD, and it works fine.
However I would also like to enable the possibility to read the same values from the USB/serial. But as soon as I add the Serial.begin() to the setup rutine the LCD refuse to initialize.
Am I overlooking some timing or what could be wrong?
#include <LCD4Bit.h>
LCD4Bit lcd = LCD4Bit(2); //create a 2-line display.
#include <stdlib.h>
#include <math.h>
/*
* SHT11 to USB (Serial)
* by Anders S. Nielsen
*
* An example of using the Arduino board to receive data from the
* Sensirion SHT11 humidity and temperature sensor.
* The Arduino board relays the data from the sensor to the USB port (serial port).
* Based on code by http://dmgaming.com/
*
*/
// Pin assignments
int shtClk=3; // SHT11 Clock pin is connected to this pin
int shtData=2; // SHT11 Data pin is connected to this pin
int boardLED=13; // A LED is connected to the pin (on Diecimila) and used to signal user
int baudrate=9600; // speed used for serial communication
// Misc variables
long logTime;
int adcCount;
int ioByte;
int ackBit;
unsigned int retVal; // Return value from SHT11
int dly;
long msTimer;
uint8_t bitmask;
double Temp; // changed from float to double
double Humi; // changed from float to double
double dewpoint;
// Calculation constants based on SHT11 data sheet
int d1 = -40; // from SHT11 data sheet @ 5V Vdd and Celcius operation
float d2 = 0.01; // from SHT11 data sheet @ 14bit and Celcius operation
int c1 = -4; // from SHT11 data sheet @ 12bit operation
float c2 = 0.0405; // from SHT11 data sheet @ 12bit operation
float c3 = -2.8E-6; // from SHT11 data sheet @ 12bit operation
// Function that writes a byte to the SHT11
void SHT_Write_Byte(void)
{
pinMode(shtData, OUTPUT);
shiftOut(shtData, shtClk, MSBFIRST, ioByte);
pinMode(shtData, INPUT);
digitalWrite(shtData, LOW);
digitalWrite(shtClk, LOW);
digitalWrite(shtClk, HIGH);
ackBit = digitalRead(shtData);
digitalWrite(shtClk, LOW);
}
int shiftIn()
{
int cwt;
cwt=0;
bitmask=128;
while (bitmask >= 1)
{
digitalWrite(shtClk, HIGH);
cwt = cwt + bitmask * digitalRead(shtData);
digitalWrite(shtClk, LOW);
bitmask=bitmask/2;
}
return(cwt);
}
// Function that reads a byte from the SHT11
void SHT_Read_Byte(void)
{
ioByte = shiftIn();
digitalWrite(shtData, ackBit);
pinMode(shtData, OUTPUT);
digitalWrite(shtClk, HIGH);
digitalWrite(shtClk, LOW);
pinMode(shtData, INPUT);
digitalWrite(shtData, LOW);
}
// Function that reset the SHT11
void SHT_Connection_Reset(void)
{
shiftOut(shtData, shtClk, LSBFIRST, 255);
shiftOut(shtData, shtClk, LSBFIRST, 255);
}
void SHT_Soft_Reset(void)
{
SHT_Connection_Reset();
ioByte = 30;
ackBit = 1;
SHT_Write_Byte();
delay(15);
}
void SHT_Wait(void)
{
delay(5);
dly = 0;
while (dly < 600)
{
if (digitalRead(shtData) == 0) dly=2600;
delay(1);
dly=dly+1;
}
}
void SHT_Start(void)
{
digitalWrite(shtData, HIGH);
pinMode(shtData, OUTPUT);
digitalWrite(shtClk, HIGH);
digitalWrite(shtData, LOW);
digitalWrite(shtClk, LOW);
digitalWrite(shtClk, HIGH);
digitalWrite(shtData, HIGH);
digitalWrite(shtClk, LOW);
}
// Function that reads data from the sensor
void SHT_Measure(int vSvc)
{
SHT_Soft_Reset();
SHT_Start();
ioByte = vSvc;
SHT_Write_Byte();
SHT_Wait();
ackBit = 0;
SHT_Read_Byte();
int msby;
msby = ioByte;
ackBit = 1;
SHT_Read_Byte();
retVal = msby;
retVal = retVal * 0x100;
retVal = retVal + ioByte;
if (retVal <= 0) retVal = 1;
}
// This function can get the current status of the sensor
int SHT_Get_Status(void)
{
SHT_Soft_Reset();
SHT_Start();
ioByte = 7;
SHT_Write_Byte();
SHT_Wait();
ackBit = 1;
SHT_Read_Byte();
return(ioByte);
}
// Heater function that can be used for sensor calibration
void SHT_Heater(void)
{
SHT_Soft_Reset();
SHT_Start();
ioByte = 6;
SHT_Write_Byte();
ioByte = 4;
SHT_Write_Byte();
ackBit = 1;
SHT_Read_Byte();
delay(500);
SHT_Soft_Reset();
SHT_Start();
ioByte = 6;
SHT_Write_Byte();
ioByte = 0;
SHT_Write_Byte();
ackBit = 1;
SHT_Read_Byte();
}
//--------------------------------------------------------------------
double calc_dewpoint( double h,double t){
//--------------------------------------------------------------------
// calculates dew point
// input: humidity [%RH], temperature [[ch65533]C]
// output: dew point [[ch65533]C]
float k,dew_point; // local variables
k = (log10(h)-2)/0.4343 + (17.62*t)/(243.12+t);
dew_point = 243.12*k/(17.62-k);
return(dew_point);
}
void printInt(long n){
byte buf[10]; // prints up to 10 digits
byte i=0;
if(n==0)
lcd.print('0');
else{
if(n < 0){
lcd.print('-');
n = -n;
}
while(n>0 && i <= 10){
buf[i++] = n % 10; // n % base
n /= 10; // n/= base
}
for(; i >0; i--)
lcd.print((char) (buf[i-1] < 10 ? '0' + buf[i-1] : 'A' + buf[i-1] - 10));
}
}
void printDouble( double val, byte precision){
// prints val with number of decimal places determine by precision
// precision is a number from 0 to 6 indicating the desired decimial places
// example: printDouble( 3.1415, 2); // prints 3.14 (two decimal places)
printInt( (long)val); //prints the int part
if( precision > 0) {
lcd.print('.'); // print the decimal point
unsigned long frac;
unsigned long mult = 1;
byte padding = precision -1;
while(precision--)
mult *=10;
if(val >= 0)
frac = (val - int(val)) * mult;
else
frac = (int(val)- val ) * mult;
unsigned long frac1 = frac;
while( frac1 /= 10 )
padding--;
while( padding--)
lcd.print('0');
printInt(frac) ;
}
}
void setup()
{
//setup serial port
Serial.begin(baudrate); // open serial
// Initialize the LCD
lcd.init();
lcd.clear();
//digitalWrite(boardLED, LOW);
//delay(1000);
lcd.cursorTo(1,0);
delay(50);
// Initialize the SHT11
lcd.printIn("Initialize SHT11");
pinMode(shtClk, OUTPUT); // Set pin as output
digitalWrite(shtClk, HIGH); // Set pin high
pinMode(shtData, OUTPUT); // Set pin as output
pinMode(boardLED, OUTPUT); // Set pin as output
// Initialize the sensor
lcd.printIn("Resetting SHT11");
SHT_Connection_Reset(); // Reset the SHT11 sensor
lcd.cursorTo(2,0);
lcd.printIn("Reset done");
digitalWrite(boardLED, HIGH); // Light the pin13 LED
//delay(1000); // Wait for 1000 miliseconds
lcd.cursorTo(2,0); // Move cursor to first space on second line
lcd.printIn("Ready to start!");
delay(1000); // Wait for 1000 miliseconds
digitalWrite(boardLED, LOW); // Turn pin13 LED off (we are ready)
lcd.clear(); // Remove everything visible on the display
lcd.cursorTo(1,0);
lcd.printIn(" HumiTemp v0.2a");
delay(3000);
}
void loop()
{
msTimer=millis(); // load number of milliseconds since the Arduino board began running the current program into msTimer
if (msTimer <= logTime) // if msTimer is lower than the the last time we checked the run time then we reset the logTime.
{
logTime = 0;
}
if (msTimer > (logTime + 1000)) // if program has been running in more that 5 sec then we fetch new readings from the sensor
{
logTime = millis(); // recored current program run time
digitalWrite(boardLED, HIGH); // Light the pin13 LED
SHT_Measure(3); // Get SHT11 temperature
digitalWrite(boardLED, LOW); // Turn pin13 LED off
Temp = d1+d2*retVal; // Temperature conversion according to SHT11 data sheet
lcd.cursorTo(1,0); // Move LCD cursor to first space on first line
lcd.printIn("T:"); // Preload LCD with text
printDouble(Temp,1); // display temp in ascii on LCD
lcd.print(223); // print degree character (223d = 11011111b = CFh)
lcd.printIn("C "); // will be printed right after temperature (no need to calculate position)
digitalWrite(boardLED, HIGH); // Light the pin13 LED
SHT_Measure(5); // Get SHT11 humidity
digitalWrite(boardLED, LOW); // Turn pin13 LED off
Humi = c1+c2*retVal+c3*(retVal*retVal); // Humidity conversion according to SHT11 data sheet
lcd.printIn("H:"); // Preload LCD with text
printDouble(Humi,1); // display humi in ascii on LCD
lcd.printIn("%RH "); // will be printed right after humidity (no need to calculate position)
dewpoint = calc_dewpoint(Temp,Humi);
lcd.cursorTo(2,0); // Move LCD cursor to first space on second line
lcd.printIn("Dp:"); //
printDouble(dewpoint,1);
lcd.print(223); // print degree character (223d = 11011111b = CFh)
lcd.printIn("C "); // will be printed right after temperature (no need to calculate position)
}
}
thanks in advance.