Hi phantomtypist
I had a Humidity Sensor - HIH 4030 and decided to try it out for my research lab. Unfortunately, i faced with an error on this ( 'HumiditySensor.HIH4030._sensor' is never assigned to, and will always have its default value null ) I did change some of the code into SecretLabs. The red font is the error i am facing. Could you tell me what is the problem.
public class HIH4030 : IDisposable
{
[color=#ff0000;] private SecretLabs.NETMF.Hardware.AnalogInput _sensor;[/color]
private bool _disposed;
protected virtual void Dispose(bool disposing)
{
// Check if Dipose has already been called.
if (!_disposed)
{
if (disposing)
{
_sensor.Dispose();
_disposed = true;
}
}
}
~HIH4030()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this); // tell the GC not to finalize.
}
public HIH4030(Cpu.Pin analogPin)
{
SecretLabs.NETMF.Hardware.AnalogInput _sensor = new SecretLabs.NETMF.Hardware.AnalogInput(analogPin);
_sensor.SetRange(0, 3300);
}
/// <summary>
/// Calculates the relative humidity with temperature compensation.
/// </summary>
/// <param name="temp">Current temperature in Celsius.</param>
/// <returns>Returns the relative humidity as a percentage.</returns>
public double RelativeHumidity(float temp)
{
// Get the humidity sensor reading.
int sensorReading = _sensor.Read();
// Calculate the humidity sensor voltage.
double sensorVoltage = (((double)sensorReading / 3300) * 3.3);
// Define the voltage the sensor returns at 0% humidity.
double zeroVoltage = 0.528;
/* It has been observed that some sensors are consistently
* inaccurate and off by a certain voltage than what they
* should be. Use this variable to compensate for what the
* voltage should be if needed. */
double calibrationVoltage = 0.00;
/* Determine the maxium voltage of the sensor with
temperature compensation. */
double maxVoltage = (2.1582 - (0.004426 * temp));
/* Determine the temperature compensated relative humidity
as a percentage. */
double rh = ((sensorVoltage + calibrationVoltage - zeroVoltage) / maxVoltage) * 100;
return rh;
}
Thanks!
This is code for the Honeywell HIH-4030 humidity sensor (SparkFun SKU: SEN-09569).
Please note that the sensor is supposed to be used with 5v input, but it works just fine with 3.3v input because the output is linear.
The code below is tailored to 3.3v input.
/// <summary> /// Honeywell HIH-4030 humidity sensor (SEN-09569). /// Please note that the sensor is supposed to be used with 5v input, /// but it works just fine with 3.3v input because the output is linear. /// The code below is tailored to 3.3v input. /// </summary> public class HIH4030 : IDisposable { private AnalogInput _sensor; private bool _disposed; protected virtual void Dispose(bool disposing) { // Check if Dipose has already been called. if (!_disposed) { if (disposing) { _sensor.Dispose(); _disposed = true; } } } ~HIH4030() { Dispose(false); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); // tell the GC not to finalize. } public HIH4030(Cpu.Pin analogPin) { _sensor = new AnalogInput(analogPin); _sensor.SetRange(0, 3300); } /// <summary> /// Calculates the relative humidity with temperature compensation. /// </summary> /// <param name="temp">Current temperature in Celsius.</param> /// <returns>Returns the relative humidity as a percentage.</returns> public double RelativeHumidity(float temp) { // Get the humidity sensor reading. int sensorReading = _sensor.Read(); // Calculate the humidity sensor voltage. double sensorVoltage = (((double)sensorReading / 3300) * 3.3); // Define the voltage the sensor returns at 0% humidity. double zeroVoltage = 0.528; /* It has been observed that some sensors are consistently * inaccurate and off by a certain voltage than what they * should be. Use this variable to compensate for what the * voltage should be if needed. */ double calibrationVoltage = 0.00; /* Determine the maxium voltage of the sensor with temperature compensation. */ double maxVoltage = (2.1582 - (0.004426 * temp)); /* Determine the temperature compensated relative humidity as a percentage. */ double rh = ((sensorVoltage + calibrationVoltage - zeroVoltage) / maxVoltage) * 100; return rh; } }