hello, im trying to connect my hih8121 sensor to the arduino with this code and im getting very wierd characters in the serial monitor like squares , arrows etc...
Can someone help me out?
`/* includes */
#include <Wire.h>
/* prototypes */
void delay_minutes(unsigned int);
/* defines /
#define BYTES2READ (4)
#define HIH8121addr (0x27) / slave I2c address /
#define EPOCH (1) / time between reads (mins) */
void setup()
{
Serial.begin(19200);
Wire.begin();
}
void loop()
{
byte HIHdata[4]; // data read from the sensor
byte HIHstatus; // sensor status bits
unsigned int RHraw, Tempraw; // raw RH (%) and temp (c) values
float RH, TempC; // converted RH (%) and Temp (C)
byte dummydata = 0xff; // dummy data for kick write
unsigned int sample = 1; // sample counter
delay(3000); // wait while opening monitor screen
Serial.println(" HIH8121 Sensor Test");
Serial.println();
// the following delay is just a reminder that on power up you want to
// wait before talking to the sensor so you can't put it into command mode
delay(50); // wait past the command window
/* print header /
Serial.println("Sample RH Temp (C)");
/ read sensor and display result loop*/
while (1) {
/* kick the sensor */
Wire.beginTransmission(HIH8121addr);
Wire.write(dummydata);
Wire.endTransmission(); // send it
delay(50); // 50 ms delay for measurement (~36.65 ms)
// get the data
Wire.requestFrom(HIH8121addr, BYTES2READ); // get 4 bytes of data
HIHdata[0] = Wire.read();
HIHdata[1] = Wire.read();
HIHdata[2] = Wire.read();
HIHdata[3] = Wire.read();
HIHstatus = HIHdata[0] & 0b11000000; // get status bits
if (HIHstatus != 0) {
Serial.println("Status Bits Error!");
}
HIHdata[0] = HIHdata[0] & 0b00111111; //mask status
Tempraw = (HIHdata[2] * 256) + HIHdata[3];
Tempraw = Tempraw / 4; //shift temperature
RHraw = (HIHdata[0] * 256) + HIHdata[1];
RH = ((float)RHraw / 16382.0) * 100.0;
TempC = (((float)Tempraw / 16382.0) * 165.0) - 40;
Serial.print(sample);
Serial.print(" ");
Serial.print(RH);
Serial.print(" ");
Serial.print(TempC);
Serial.println();
delay_minutes(EPOCH);
sample++;
}
}
void delay_minutes(unsigned int mins) {
unsigned int count;
for (count = 0; count < mins; count++) {
delay(1000); // delay 60 seconds
//delay(1000); // use this if you want seconds epoch
}
}
void print_float(float f, int num_digits)
{
int f_int;
int pows_of_ten[4] = {1, 10, 100, 1000};
int multiplier, whole, fract, d, n;
multiplier = pows_of_ten[num_digits];
if (f < 0.0)
{
f = -f;
Serial.print("-");
}
whole = (int) f;
fract = (int) (multiplier * (f - (float)whole));
Serial.print(whole);
Serial.print(".");
for (n=num_digits-1; n>=0; n--) // print each digit with no leading zero suppression
{
d = fract / pows_of_ten[n];
Serial.print(d);
fract = fract % pows_of_ten[n];
}
} `