Hello All,
I'm working on a project that has 11 points of measurement for humidity and temperature. With that in mind, I bought a Mega 2560 to have the correct amount of pins. Along with the board, I got 11 si7021 Humidity and Temperature sensors ( SparkFun Humidity and Temperature Sensor Breakout - Si7021 - SEN-13763 - SparkFun Electronics ). These sensors use an i2c communication protocol.
At first, I could get no code working at all, and I believe that was due to the code being used for just the Uno. I eventually found one that could compile and give me accurate readings. The original is down below:
// Distributed with a free-will license.
// Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
// SI7021
// This code is designed to work with the SI7021_I2CS I2C Mini Module available from ControlEverything.com.
// https://www.controleverything.com/content/Humidity?sku=SI7021_I2CS#tabs-0-product_tabset-2
#include
// SI7021 I2C address is 0x40(64)
#define Addr 0x40
void setup()
{
// Initialise I2C communication as MASTER
Wire.begin();
// Initialise serial communication, set baud rate = 9600
Serial.begin(9600);
// Start I2C transmission
Wire.beginTransmission(Addr);
// Stop I2C transmission
Wire.endTransmission();
delay(300);
}
void loop()
{
unsigned int data[2];
// Start I2C transmission
Wire.beginTransmission(Addr);
// Send humidity measurement command, NO HOLD MASTER
Wire.write(0xF5);
// Stop I2C transmission
Wire.endTransmission();
delay(500);
// Request 2 bytes of data
Wire.requestFrom(Addr, 2);
// Read 2 bytes of data
// humidity msb, humidity lsb
if(Wire.available() == 2)
{
data[0] = Wire.read();
data[1] = Wire.read();
}
// Convert the data
float humidity = ((data[0] * 256.0) + data[1]);
humidity = ((125 * humidity) / 65536.0) - 6;
// Start I2C transmission
Wire.beginTransmission(Addr);
// Send temperature measurement command, NO HOLD MASTER
Wire.write(0xF3);
// Stop I2C transmission
Wire.endTransmission();
delay(500);
// Request 2 bytes of data
Wire.requestFrom(Addr, 2);
// Read 2 bytes of data
// temp msb, temp lsb
if(Wire.available() == 2)
{
data[0] = Wire.read();
data[1] = Wire.read();
}
// Convert the data
float temp = ((data[0] * 256.0) + data[1]);
float cTemp = ((175.72 * temp) / 65536.0) - 46.85;
float fTemp = cTemp * 1.8 + 32;
// Output data to serial monitor
Serial.print("Relative humidity : ");
Serial.print(humidity);
Serial.println(" % RH");
Serial.print("Temperature in Celsius : ");
Serial.print(cTemp);
Serial.println(" C");
Serial.print("Temperature in Fahrenheit : ");
Serial.print(fTemp);
Serial.println(" F");
delay(500);
}
Being that this just works with the SDA and SCL pins that are hardwired into the board and being called by the Wire.h file extension, I can't use this for multiple sensors. So I've been trying to find an extension that allows me to set multiple digital lines for more I2C comms. I've used SoftI2CMaster, but I'm not sure how to send the information for serial to read. My attempt is down below, along with the error messages that I am receiving:
// Distributed with a free-will license.
// Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
// SI7021
// This code is designed to work with the SI7021_I2CS I2C Mini Module available from ControlEverything.com.
// https://www.controleverything.com/content/Humidity?sku=SI7021_I2CS#tabs-0-product_tabset-2
// Including the ports before the program
#define SDA_PORT PORTA
#define SDA_PIN 22
#define SCL_PORT PORTA
#define SCL_PIN 23
#define I2C_TIMEOUT 100
#include <SoftI2CMaster.h> //including a test I2C program to implement multiple sensors
void setup()
{
// Initialise I2C communication as MASTER
i2c_read(true);
// Initialise serial communication, set baud rate = 9600
Serial.begin(9600);
// Start I2C transmission
Wire.beginTransmission(var1);
// Stop I2C transmission
Wire.endTransmission();
delay(500);
}
void loop()
{
unsigned int data[2];
// Start I2C transmission
Wire.beginTransmission(var1);
// Send humidity measurement command, NO HOLD MASTER
Wire.write(var1);
// Stop I2C transmission
Wire.endTransmission();
delay(500);
// Request 2 bytes of data
Wire.requestFrom(var1, 2);
// Read 2 bytes of data
// humidity msb, humidity lsb
if(Wire.available() == 2)
{
data[0] = Wire.read();
data[1] = Wire.read();
}
// Convert the data
float humidity = ((data[0] * 256.0) + data[1]);
humidity = ((125 * humidity) / 65536.0) - 6;
// Start I2C transmission
Wire.beginTransmission();
// Send temperature measurement command, NO HOLD MASTER
Wire.write(0xFFFF);
// Stop I2C transmission
Wire.endTransmission();
delay(500);
// Request 2 bytes of data
Wire.requestFrom(var1, 2);
// Read 2 bytes of data
// temp msb, temp lsb
if(Wire.available() == 2)
{
data[0] = Wire.read();
data[1] = Wire.read();
}
// Convert the data
float temp = ((data[0] * 256.0) + data[1]);
float cTemp = ((175.72 * temp) / 65536.0) - 46.85;
float fTemp = cTemp * 1.8 + 32;
// Output data to serial monitor
Serial.print("Relative humidity : ");
Serial.print(humidity);
Serial.println(" % RH");
Serial.print("Temperature in Celsius : ");
Serial.print(cTemp);
Serial.println(" C");
Serial.print("Temperature in Fahrenheit : ");
Serial.print(fTemp);
Serial.println(" F");
delay(1000);
}
Arduino: 1.8.1 (Linux), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"
/home/ken/Arduino_Projects/Temp_Humid_Sensor/DocB_i2c_SI7021/DocB_i2c_SI7021.ino/DocB_i2c_SI7021.ino.ino: In function 'void setup()':
DocB_i2c_SI7021.ino:26: error: 'Wire' was not declared in this scope
Wire.beginTransmission(var1);
^
DocB_i2c_SI7021.ino:26: error: 'var1' was not declared in this scope
Wire.beginTransmission(var1);
^
/home/ken/Arduino_Projects/Temp_Humid_Sensor/DocB_i2c_SI7021/DocB_i2c_SI7021.ino/DocB_i2c_SI7021.ino.ino: In function 'void loop()':
DocB_i2c_SI7021.ino:37: error: 'Wire' was not declared in this scope
Wire.beginTransmission(var1);
^
DocB_i2c_SI7021.ino:37: error: 'var1' was not declared in this scope
Wire.beginTransmission(var1);
^
exit status 1
'Wire' was not declared in this scope
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
Any guidance or help with this would be greatly appreciated. Thank you!