In response to requests to update the HP03 library to work with boards like the Mega, here are the latest files. This version uses the Arduino tone function to produce the external clock needed by HP03. You can read more about the HP03 pressure sensor module in this old thread: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1223819446/0
Here is the test sketch:
// HP03 example sketch
// prints the temperature, presssure and altitude to the serial port
// also shows the maximum deviation in feet from the average altitude
// Created 17 July 2009
// Updated Jun 1 2011 to support Mega (tone funtion used instead of FrequencyTimer2)
#include <HP03.h>
#include <Wire.h>
// Standard boards (168 and 328 chips) use digital pin 11 for the clock, analog pins 2 and 3 for control
// Mega uses digital pin 10 for clock and 18 & 19 for control (I2C are pins 20 & 21)
const int numReadings = 12; // the number of readings to smooth
// with HP03 conversion rate of 85ms, 12 readings takes one second
long count = 0; // the number of readings taken
long minA = 9999999; // used to show how much the altitude deviates
long maxA = 0;
long deviation;
void setup()
{
Serial.begin(9600); // start serial for output
Serial.flush();
if(HP03.begin() == false)
Serial.println("Error getting HP03 calibration, check sensor connection");
}
void loop()
{
if(HP03.update() == false)
Serial.println("Error getting HP03 data, check sensor connection");
else
{
count++;
showReadings();
}
}
void showReadings()
{
Serial.print("Temp ");
Serial.print(HP03.Temperature/10.0);
Serial.print(" Pressure ");
Serial.print(HP03.Pressure/100.0);
HP03.distanceUnits = METERS;
Serial.print(" Altitude: Meters = ");
Serial.print(HP03.getAltitude(HP03.Pressure)/ 10);
HP03.distanceUnits = FEET;
Serial.print(", Feet = ");
long feet = HP03.getAltitude(HP03.Pressure);
Serial.print(feet);
feet = smooth(feet);
if(count >= numReadings)
{
Serial.print(" smoothed = ");
Serial.print(feet);
if(feet < minA) minA = feet;
if(feet > maxA) maxA = feet;
Serial.print(", deviation = ");
Serial.print( (maxA-minA) /2 );
}
Serial.println();
int val = map(HP03.Pressure /10,9750, 10250, 0,255);
analogWrite(9,val );
Serial.println( val);
}
long smooth(long value)
{
static int index = 0;
static long readings[numReadings] ;
static boolean isPrimed = false; // set to true when at least numReadings have been received
static long total = 0;
total = total - readings[index]; // remove the oldest value
readings[index] = value; // store the new value
total= total + readings[index]; // add the reading to the total:
index = index + 1; // advance to the next position in the array:
// if we're at the end of the array...
if (index >= numReadings) // check if this is the end of the array
{
index = 0; // and wrap to the beginning if so
isPrimed = true; // enough readings received for average
}
if(isPrimed)
return total / numReadings;
else
return value; // return the raw value if not enough samples received;
}
HP03.h header file to go into the library directory
/*
HP03.h - Arduino library support for HP03 Pressure sensor
Copyright (c)2008 Michael Margolis All right reserved
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Version: 1.0 - July14 2008
Version: 1.1 - May31 2011 - updated to use tone to work with mega board
*/
#ifndef HP03_H
#define HP03_H
typedef unsigned char boolean;
/*
define some constants for errors based on the return values
of the functions in utility/twi.c
*/
#define WIRE_OK 0
#define WIRE_ERR_NO_BUFFER_SPACE 1
#define WIRE_ERR_BAD_ADDRESS 2
#define WIRE_ERR_NOT_IN_TRNSMISSION_MODE 2
#define WIRE_ERR_DATA_NO_ACK 3
#define WIRE_ERR_BUS 4
typedef enum distanceUnits_t {METERS, FEET};
class HP03class // shell class for HP03 glcd code
{
private:
// methods to send and receive data via I2C
int i2cTransfer(unsigned char Address, const unsigned char *TxBuf, int TxBytes, unsigned char *RxBuf, int RxBytes);
int i2cSendCommand(unsigned char Address, unsigned char Command);
int i2cMeasure(unsigned char SubType);
int i2cResult(unsigned int *Result);
public:
HP03class();
float Temperature;
float Pressure;
long Altitude;
unsigned int rawPressure; // D1 - raw values are only for test purposes
unsigned int rawTemperature; // D2
distanceUnits_t distanceUnits; // METERS or FEET
boolean begin( void );
boolean getCalibrationData();
#ifdef PRINT_CALIBRATION_DATA
void HPO3class::printCalibrationData();
#endif
boolean update();
boolean readRawData();
void calculatePressureAndTemperature();
long getAltitude(long pressure); //pressure is 100 times what it should be
};
extern HP03class HP03;
#endif
Tested with Uno and Mega boards using Arduino 0022
Note that the Uno uses digital pin 11 for the external clock and analog pins 2 and 3 for control (I2C uses analog 4 and 5)
The Mega uses digital pin 10 for the clock and digital pins 18 and 19 for control (I2C uses digital pins 20 and 21)