Hi I got a HR202 sensor module with a LM393 comparator board, I am planning to use it for humidity data log in my room. I got a code that was meant for hr202 sensor but when I program it it seem the humidity is stuck at 66% for most of the time. Is that suppose to happen? If not is there a way to solve this problem?
Bellow is the code
// HR202 Humidity Sensor Arduino Code
const int sensorPin = A0; // Analog pin connected to HR202 sensor
void setup() {
Serial.begin(9600); // Initialize serial communication
}
void loop() {
int sensorValue = analogRead(sensorPin); // Read analog value from the sensor
float humidity = calculateHumidity(sensorValue); // Convert sensor value to humidity percentage
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println("%");
delay(2000); // Delay between readings (adjust as needed)
}
// Function to calculate humidity based on sensor value
float calculateHumidity(int sensorValue) {
// Conversion formula specific to HR202 sensor
float humidity = (float)sensorValue * 100 / 1024;
return humidity;
}
What is the purpose of the comparator on that board?
Commonly, it would be used to give you a binary (yes/no) indication of whether the humidity is above a certain value or not...
The LM393 is integrated into the sensor module and there was only a digital output no analog output so i assume that means the sensor module is like a switch?
I still don't know what you need to get, but as @awneil said, it seems to me that this comparator will give you just on/off based on a threshold defined by the trimmer, not the humidity percentage value.
Said that, I have just three words: throw it away.
IMHO that HR202 is a horrible sensor, almost useless and unreliable. Please remember you don't read humidity value, but RH, Relative Humidity, as it's "relative" to temperature. So I suggest you to get a DHT11 instead: it costs a few cents more than HR202 but it is a more commonly used and reliable "Digital Temperature and Humidity Sensor", and it has a specific library to manage the read values.
PS: If you need wider range and precision there are the DHT22/AM2302, not as cheap as DHT11 but we aren't talking about many bucks...
Exactly: so your analogRead() is only ever going to return one of two values - you are never going to get a continuous humidity reading from this board.
You either need to "hack" the board to get access to the analogue value, or get a different board which does give a continuous humidity reading.