Arduino&Basic Stamp II

Hey, I'm new to using Arduino and I've run out of variable space on my Basic Stamp II. I want to transfer the Sensirion SHT11 Temperature/Humidity Sensor Module to my Arduino board and have it set for a threshold Temp/Hum and when it reaches the threshold have it send a message to my Basic Stamp board. I've found some codes online but I haven't been able to get them to work, and I'm very very inept. Thanks.

Well, I'm not very knowledgeable about Basic Stamps, but do you happen to know the communication bus system for the Basic Stamp (i.e. SPI, I2C, etc)?

Here is the code I’ve used:

File: sketchbook/libraries/SHT1x/SHT1x.h

/**
* SHT1x Library
*
* Based on previous work by:
* Jonathan Oxer <jon@oxer.com.au> / <www.practicalarduino.com>
* Maurice Ribble: <www.glacialwanderer.com/hobbyrobotics/?p=5>
* Wayne Prasek: <ragingreality.blogspot.com/2008/01/ardunio-and-sht15.html>
*
* Manages communication with SHT1x series (SHT10, SHT11, SHT15)
* temperature / humidity sensors from Sensirion (www.sensirion.com).
* 
* Rev 1.0 - Jan 2010
* 
*/
#ifndef SHT1x_h
#define SHT1x_h
 
#include "WProgram.h"


class SHT1x
{
      public:
            float TempC;
            float RH;


            SHT1x(const byte dataPin, const byte clockPin);
            void readSensor();
            void resetSensor();
            void resetComm();
            byte readStatus();
            void setHeater(boolean mode);
            float DewPt();
            float RH2(float T2);
            void debug();

      private:
            byte _dataPin;
            byte _clockPin;


            void readTemperature();
            void readHumidity();
            void sendTXStart();
            void sendCommandSHT(const byte _command);
            void waitForResultSHT();
            int getData16SHT();
            void skipCrcSHT();
};
 
#endif

File: sketchbook/libraries/SHT1x/SHT1x.cpp

/**
* SHT1x Library
* 
* Based on previous work by:
* Jonathan Oxer <jon@oxer.com.au> / <www.practicalarduino.com>
* Maurice Ribble: <www.glacialwanderer.com/hobbyrobotics/?p=5>
* Wayne Prasek: <ragingreality.blogspot.com/2008/01/ardunio-and-sht15.html>
*
* Manages communication with SHT1x series (SHT10, SHT11, SHT15)
* temperature / humidity sensors from Sensirion (www.sensirion.com).
* 
* Rev 1.0 - Jan 2010
* 
*/
#include "WProgram.h"
#include "SHT1x.h"
 
SHT1x::SHT1x(const byte dataPin, const byte clockPin)
{
      _dataPin = dataPin;
      _clockPin = clockPin;
}

 
/* ================ Public methods ================ */


/*
 * Fetch values from the sensor 
 */
void SHT1x::readSensor()
{
      // Must read temperature first because it is needed by readHumdity
      // for temp compensation
      readTemperature();
      readHumidity();

}

/*
 * Send a soft reset to the SHT1x
 */ 
void SHT1x::resetSensor()
{
      // Command to send to the SHT1x to soft reset device
      const byte _gResetCmd = 0x1E;      // 00011110

      // Fetch the value from the sensor
      sendTXStart();
      sendCommandSHT(_gResetCmd);
      delay(11);

}


/*
 * Reset comm line only; does not reset status register
 */
void SHT1x::resetComm()
{
      // Send at least 9 "1" bits; This will send 16 bits.
      pinMode(_dataPin,OUTPUT);
      shiftOut(_dataPin, _clockPin, MSBFIRST, 0xFF);
      shiftOut(_dataPin, _clockPin, MSBFIRST, 0xFF);
      pinMode(_dataPin,INPUT);
}

/*
 * Read the status register byte
 */
byte SHT1x::readStatus()
{
      
      int _reg;
      
      // Command to send to the SHT1x to request status register
      const byte _gReadStatusCmd = 0x07;            // 00000111

      // Fetch the value from the sensor
      sendTXStart();
      sendCommandSHT(_gReadStatusCmd);
      _reg = getData16SHT();

      // Send final ACK
      pinMode(_dataPin,OUTPUT);
      digitalWrite(_dataPin,LOW);
      digitalWrite(_clockPin,HIGH);
      digitalWrite(_clockPin,LOW);
      pinMode(_dataPin,INPUT); 

      // Status is in upper 8 bits, CRC is in lower 8 bits
      // Discard CRC & return only lower byte
      _reg >>= 8;
      
      return(_reg);
      
      }

/*
 * Set the device heater on/off
 */
void SHT1x::setHeater(boolean mode)
{
      
      byte _reg;
      
      // Status reg bit 2 = heater mode
      // Note: this also sets measurement mode to hi-resolution (default)
      _reg = (mode) ? 0x04 : 0x00;            // 00000100
      
      // Command to send to the SHT1x to set status register
      const byte _gWriteStatusCmd = 0x06;      // 00000110

      // Write status register
      sendTXStart();
      sendCommandSHT(_gWriteStatusCmd);
      sendCommandSHT(_reg);

}

/*
 * Calculate the Dew Point
 * Formula from Sensirion App Note: Introduction to Humidity
 * Revision 2.0, July 2009
 */
float SHT1x::DewPt()
{
      float _H, _td;
      
      _H = log(RH/100.0) + (17.62 * TempC)/(243.12+TempC);
      _td = 243.12*_H/(17.62-_H);
      
      return(_td);
}

/*
 * Calculate Relative Humidity at Temp T2 (DegC)
 * given sensor's current RH & Temp.
 * Use for determining actual RH when sensor is heated by calc'ing
 * to un-heated temp.
 * Formula from Sensirion App Note: Humidity at a Glance
 * Revision 1.0, August 2008
 */
float SHT1x::RH2(float T2)
{
      // 17.62 * 243.12 = 4283.7744
      return( RH*exp(4283.7744*(TempC-T2) / ((243.12+TempC)*(243.12+T2)) ) );
      
}

/* ================ Private methods ================ */

/**
* Reads current temperature-corrected relative humidity
*/
void SHT1x::readHumidity()
{
      int _rawRH;
      float _RHlin;
      
      // Command to send to the SHT1x to request humidity
      const byte _gHumidCmd = 0x05;      // 00000101

      // Fetch the value from the sensor
      sendTXStart();
      sendCommandSHT(_gHumidCmd);
      waitForResultSHT();
      _rawRH = getData16SHT();
      skipCrcSHT();

      // Apply linear conversion to raw value
      // RH_linear = C1 + C2*RH_raw + C3*(RH_raw)^2
      // Conversion coefficients from SHT15 datasheet (V4 device, 12 bit mode)
      // C1 = -2.0468 (V3 device: -4.0)
      // C2 = 0.0367  (V3 device: 0.0405)
      // C3 = -0.0000015955  (V3 device: -0.0000028)
      _RHlin = -2.0468 + (_rawRH * (0.0367 + (_rawRH * -0.0000015955))); // V4
      // _RHlin = -4.0 + (_rawRH * (0.0405 + (_rawRH * -0.0000028))); // V3


      // Correct humidity value for current temperature
      // RH_true = (TempC - 25.0)*(T1 + T2*RH_raw) + RH_linear
      // Conversion coefficients from SHT15 datasheet (12 bit mode)
      // V3 and V4 device coefficients are identical
      // T1 = 0.01
      // T2 = 0.00008
      RH = (TempC - 25.0) * (0.01 + (0.00008 * _rawRH)) + _RHlin;

}
 
 
 
/**
* Reads the current temperature value
*/
void SHT1x::readTemperature()
{
      int rawTemp;

      // Command to send to the SHT1x to request Temperature
      const byte _gTempCmd = 0x03;      // 00000011

      sendTXStart();
      sendCommandSHT(_gTempCmd);
      waitForResultSHT();
      rawTemp = getData16SHT();
      skipCrcSHT();

      // Convert raw value to DegC
      // TempC = D1 + D2*Temp_raw
      // Conversion coefficients from SHT15 datasheet (V4 device, 14 bit mode @ 5 VDC)
      // D1 = -40.1;  V3 device: D1 = -40.0
      // D2 = 0.01  (1/100)
      TempC = (rawTemp / 100.0) - 40.1;
}

void SHT1x::sendTXStart()
{
      // Transmission Start
      pinMode(_dataPin, OUTPUT);
      pinMode(_clockPin, OUTPUT);
      digitalWrite(_dataPin, HIGH);
      digitalWrite(_clockPin, HIGH);
      digitalWrite(_dataPin, LOW);
      digitalWrite(_clockPin, LOW);
      digitalWrite(_clockPin, HIGH);
      digitalWrite(_dataPin, HIGH);
      digitalWrite(_clockPin, LOW);

}


void SHT1x::sendCommandSHT(const byte _command)
{
      boolean ack; 

      pinMode(_dataPin, OUTPUT);

      // The command (3 msb are address and must be 000, and last 5 bits are command)
      shiftOut(_dataPin, _clockPin, MSBFIRST, _command);

      // Verify we get the correct ack
      // Set pin mode to INPUT *IMMEDIATELY* after shiftout is done.  The SHT1x
      // starts pulling the data line low (ACK bit) on the 8th falling edge
      // of the clock, so we must stop driving the data line quickly.
      pinMode(_dataPin, INPUT);

      digitalWrite(_clockPin, HIGH);  // 9th rising edge -- read ACK bit

      ack = digitalRead(_dataPin);
      if (ack != LOW) {
            //Serial.println("Ack Error 0");
      }
      digitalWrite(_clockPin, LOW); // 9th falling edge -- end of ACK bit
      
      ack = digitalRead(_dataPin);
      if (ack != HIGH) {
            //Serial.println("Ack Error 1");
      }
}
 

void SHT1x::waitForResultSHT()
{
      byte i;
      boolean ack;

      pinMode(_dataPin, INPUT);

      for(i = 0; i < 100; i++)
      {
            delay(10);
            ack = digitalRead(_dataPin);

            if (ack == LOW) {
                  break;
            }
      }

      if (ack == HIGH) {
            //Serial.println("Ack Error 2"); // Can't do serial stuff here, need another way of reporting errors
      }
}
 
/**
*/
int SHT1x::getData16SHT()
{
      int val;
      byte i;


      pinMode(_dataPin, INPUT);
      pinMode(_clockPin, OUTPUT);

      digitalWrite(_clockPin,LOW);

      for(i = 0, val = 0; i < 17; i++) {
            if(i != 8) {
            // read bits 0..7 and 9..16 (data bits)
                  digitalWrite(_clockPin,HIGH);
                  val <<= 1; // shift existing bits one position towards msb
                  val |= digitalRead(_dataPin); // read input bit into lsb
                  digitalWrite(_clockPin,LOW);
            }
            else {
            // send ACK bit (bit 8) between bytes
                  pinMode(_dataPin,OUTPUT);
                  digitalWrite(_dataPin,LOW);
                  digitalWrite(_clockPin,HIGH);
                  digitalWrite(_clockPin,LOW);
                  pinMode(_dataPin,INPUT); 
            }
      }

      return val;
}
 
/**
*/
void SHT1x::skipCrcSHT()
{
      // Skip acknowledge to end trans (no CRC)
      pinMode(_dataPin, OUTPUT);
      pinMode(_clockPin, OUTPUT);

      digitalWrite(_dataPin, HIGH);
      digitalWrite(_clockPin, HIGH);
      digitalWrite(_clockPin, LOW);
}

File: sketchbook/libraries/SHT1x/examples/ReadSHT1x/ReadSHT1x.pde

/**
* ReadSHT1xValues
*
* Read temperature and humidity values from an SHT1x-series (SHT10,
* SHT11, SHT15) sensor.
*
*
* Based on previous work by:
* Jonathan Oxer <jon@oxer.com.au>
* www.practicalarduino.com
*/
 
#include <SHT1x.h>
 
// Specify data and clock connections and instantiate SHT1x object
#define dataPin 9
#define clockPin 8
SHT1x sht1x(dataPin, clockPin);
 
void setup()
{
      Serial.begin(9600); // Open serial connection to report values to host
      Serial.println("Starting up");

      sht1x.resetSensor();
}
 
void loop()
{

      float Dpt;

      sht1x.readSensor();
      Dpt = sht1x.DewPt();

      Serial.print("Temprature (C/F):");
      Serial.print(sht1x.TempC);
      Serial.print(" / ");
      Serial.println( (1.8*sht1x.TempC + 32) );
      Serial.print("Humidity (%):");
      Serial.println(sht1x.RH); 
      Serial.print("Dew Pt: ");
      Serial.println(Dpt);
      Serial.println("---------------------------------");

      delay(60000);
}

cel1990:

communication bus system for the Basic Stamp

Indeed you're not very knowledgeable about BS. They suck. They cost more than an Arduino and do much much less. Unless you get the highest-end model (BS2p) they don't do any sort of comm protocol other than the standard serial port, and a 16bit shift-in/shift-out command (I suppose that qualifies as SPI).

The high-end model also has 1-wire and I2C and some built-in LCD functions.

Used one once.... never going back.

It's pretty crude, but at....

http://sheepdogsoftware.co.uk/sc4smka.htm

.. there's an essay about connecting a Basic Stamp and an Arduino. My advice: Give up on your project as described! But if you want to persist, the things I did above may help.

Here is the code I've used:

Thanks! I'll give the codes a try and see if I can get them to run. And you're right about the BS2 sucking. It sucks big time, which is why I'm having these problems.

Would you also happen to know a good reference, or online book for beginner Arduino Environment programming?

Give up on your project as described! But if you want to persist, the things I did above may help.

The project is an assignment so I don't have the option of giving up, and the BS2 and Arduino boards are all I have to work with. I've got to run two servos, a ping sensor, the temp/hum sensor, and a transceiver. The Basic Stamp keeps running out of variable space so I've got to add on another Microcontroller. I only have a propeller demo board or my Arduino. Neither of which I really know how to work with.

The Arduino webiste has quite a lot of useful information.

http://arduino.cc/

Check out the Getting Started, Learning, Reference, and Playground links at the top of the page. Also the Arduino language is C/C++ with a few functions added, so any reference on C/C++ will help.

Alright thanks. Also, whenever I try and compile the coding you gave me I get an error message: 19: error: SHT1x.h: No such file or directory.

You may have the files in the wrong directory. I put the file paths just above each code listing. You may need to create some of the directories.

In your sketchbook folder, create a libraries folder. In that create SHT1x. Save the first code listing as SHT1x.h in that folder, and the second listing as SHT1x.cpp.

The third code listing can be pasted into a new sketch.

Not related to the PIC problem, but since I just saw this post and I've just updated the SHT1x library a few minutes ago it seemed relevant. I don't know where that version of the library shown in the original post came from (it looks like there are changes that have been made since my previous release, but no additional author name added in the comments) so I'm curious to know where you got it. If there are fixes that I should incorporate I'd love to know about it.

I've just updated the original version of the SHT1x library with a fix for the temperature correction in the humidity calculation. It's here:

http://github.com/practicalarduino/SHT1x

Cheers :)

Jon

Freetronics: www.freetronics.com Practical Arduino: www.practicalarduino.com

It was some changes that I made mostly to add a few more of the device's functions and to use the updated RH coefficients. I had intended it mostly for my own use so didn't add new author information.