Good day everyone!
I am a mechanical engineering student using the Arduino for my project (Very low skill in programming)
For my project, I am using Arduino Uno to run an analog thermocouple, accelerometer(I2C-ADXL345), humidity sensor(DHT22) and LCD (I2C-LCD05). I need help on setting up the I2C aspect. I have wired all the SCL/SCA wires together. But nothing seems to happen when i run the program, not even on the serial monitor.
Q1. The lazy method?
If i am not wrong, there is two I2C ports? Is it possible if i use and set 1 for acclerometer and 1 for the LCD??
Q2. The standard procedure?
When running individual components they seems fine, the problems seems to be the two I2C components? I believe i need to do something for them to link up?
Basically, I just need them to run all together so any other method is fine. Appreciate any help! Thank you!
Here is my codes:
/*-----( Import needed libraries )-----*/
#include <LCD03.h>
#include <Wire.h>
#include "DHT.h"
#define DEVICE (0x53) //ADXL345 device address
#define TO_READ (6) //num of bytes we are going to read each time (two bytes for each axis)
#define THRESH_ACT (0x24)
#define THRESH_INACT (0x25)
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
LCD03 lcd; // Create new LCD03 instance
byte buff[TO_READ] ; //6 bytes buffer for saving data read from the device
char str[512]; //string buffer to transform data before sending it to the serial port
// Declare Variable
float voltage; //Analog input voltage from AD8495 thermocouple
float temp; //Temperature variable thermocoupple
float tem[10]; //Temperature variable thermocoupple
float x, y, z; //Acceleration variable
float x1, y1, z1; //Acceleration variable
int tpin = A0; //thermocouple to A0
void thermocouple() {
int voltage = analogRead(tpin); //Read voltage coming from AD8495
float vout = (voltage * 5)/ 1023.0; //Calculate vout
float temp = (vout - 1.25) / 0.005 ; //Convert to temperature
Serial.print("Thermocouple : "); Serial.print("temp"); Serial.print((char)186); Serial.print(" C");
lcd.print("Thermocouple: ");lcd.print(temp);lcd.print("C");
}
void writeTo(int device, byte address, byte val) {
Wire.beginTransmission(device); //start transmission to device
Wire.write(address); // send register address
Wire.write(val); // send value to write
Wire.endTransmission(); //end transmission
}
void readFrom(int device, byte address, int num, byte buff[])
{
Wire.beginTransmission(device); //start transmission to device
Wire.write(address); //sends address to read from
Wire.endTransmission(); //end transmission
Wire.beginTransmission(device); //start transmission to device (initiate again)
Wire.requestFrom(device, num); // request 6 bytes from device
int i = 0;
while(Wire.available()) //device may send less than requested (abnormal)
{
buff[i] = Wire.read(); // receive a byte
i++;
}
Wire.endTransmission(); //end transmission
}
void accelerometer()
{
int accelerometer = 0x32; //first axis-acceleration-data register on the ADXL345
readFrom(DEVICE, accelerometer, TO_READ, buff); //read the acceleration data from the ADXL345
//each axis reading comes in 10 bit resolution, ie 2 bytes. Least Significat Byte first!!
//thus we are converting both bytes in to one int
x = (((int)buff[1]) << 8) | buff[0];
y = (((int)buff[3])<< 8) | buff[2];
z = (((int)buff[5]) << 8) | buff[4];
x1 = x/256;
y1 = y/256;
z1 = z/256;
Serial.print("X: ");Serial.print(x1); Serial.println(" LSB/mg");
Serial.print("Y: ");Serial.print(y1); Serial.println(" LSB/mg");
Serial.print("Z: ");Serial.print(z1); Serial.println(" LSB/mg");
lcd.print("VibrationZ:");lcd.print(x1);lcd.print("mm/s2");
}
void humidity() {
float h = dht.readHumidity();
float t = dht.readTemperature(); // Read temperature as Celsius (the default)
if (isnan(h) || isnan(t)) { // Check if any reads failed and exit early (to try again).
lcd.println("Failed to read from DHT sensor!");
return;
}
float hic = dht.computeHeatIndex(t, h); // Compute heat index in Celsius
lcd.print("Humidity: ");lcd.print(h);lcd.print("% ");
lcd.print("System_Temp:");lcd.print(t);lcd.print("C ");
//lcd.print("Heat index: ");lcd.print(hic);lcd.print("*C "); // What is heat index
Serial.print("Humidity: ");Serial.print(h);Serial.println(" % ");
Serial.print("Temperature: ");Serial.print(t);Serial.println(" ");
//Serial.print("Heat index: ");Serial.print(hic);Serial.print("*C ");
}
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
dht.begin(); //start humidity sensor
lcd.begin(20, 4); //start lcd
lcd.backlight(); // Turn on the backlight
Serial.println("The SenMon");
delay(2000); // Wait a few seconds between measurements.
writeTo(DEVICE, 0x2D, 0); //Turning on the ADXL345
writeTo(DEVICE, 0x2D, 16); //Turning on the ADXL345
writeTo(DEVICE, 0x2D, 8); //Turning on the ADXL345
}
void loop()
{
accelerometer();
thermocouple();
humidity();
// Serial.write(10);
delay(1000); //It appears that delay is needed in order not to clog the port
}