Go Down

Topic: Connecting Multiple I2C Sensors (Read 3510 times) previous topic - next topic

kage_diver

Feb 20, 2013, 04:38 am Last Edit: Feb 20, 2013, 04:47 am by kage_diver Reason: 1
Hello All,

I'm kinda new to the Arduino world.  I have an Arduino Uno R3.  I have kinda figured out the I2C communication thing.  My current problem is how do I connect multiple I2c sensors to the one controller?  

I have a RTC module, and a BMP085.  Do I need to set my uno as the master, and the devices as slaves? Any help would be great!!

Thank you All

Code: [Select]

// REV 1.1.0
// 02-19-2013

// Date and time functions using just software, based on millis() & timer

#include <Wire.h>
#include <RTClib.h>
#include <SimpleTimer.h>
#include <Adafruit_BMP085.h>

const int temp = 0;
const int LED = 13;
const float wind = 1;
RTC_Millis RTC;
Adafruit_BMP085 bmp;


void setup ()
{
 
   Serial.begin(9600);
   pinMode(LED,OUTPUT);
   RTC.begin(DateTime(__DATE__, __TIME__)); //sets the RTC to the date & time this sketch was compiled
   bmp.begin();
   
}

void loop ()
{
   /*  noInterrupts();
       if(millis() % 1200000 == 0)
        RTC.adjust(DateTime(__DATE__, __TIME__));
     interrupts();
 */
 float volt, degC, MPH;
 int room, deadband, setpoint, degF, dispHour, dispDOW, F, Pressure;
 
 MPH = getVoltage(wind);
 
 volt = getVoltage(temp);
 degC = (volt - .5) * 100.0;
 degF = degC * (9.0/5.0) + 32.0;
 
 room = 72;
 deadband = 3;
 setpoint = room - deadband;
 
 F = bmp.readTemperature() * (9.0/5.0) + 32.0;
 
 Pressure = bmp.readPressure() * 0.000295;
 
 
   DateTime now = RTC.now();
     dispHour = now.hour();
     dispDOW = now.dayOfWeek();
         
     // print readings every 5 min.
if (millis() % 6000 == 0)
{
    // RTD Day Of the Week
   if(dispDOW == 0)
     Serial.print("SUN: ");
   if(dispDOW == 1)
     Serial.print("MON: ");
   if(dispDOW == 2)
     Serial.print("TUE: ");
   if(dispDOW == 3)
     Serial.print("WED: ");
   if(dispDOW == 4)
     Serial.print("THU: ");
   if(dispDOW == 5)
     Serial.print("FRI: ");
   if(dispDOW == 6)
     Serial.print("SAT: ");
     
     // RTD Date format D/M/TYYY    
   Serial.print(now.month());
   Serial.print('/');
   Serial.print(now.day());
   Serial.print('/');
   Serial.print(now.year() - 2000);
   Serial.print(' ');
 
    // RTC Time in 12 hour format with AM/PM
   Serial.print((dispHour > 12) ? dispHour - 12 : ((dispHour == 12) ? 12 : dispHour));
   Serial.print(":");
   if (now.minute() < 10)
        Serial.print("0");
   Serial.print(now.minute());
   Serial.print(':');
   if (now.second() < 10)
        Serial.print ("0");
   Serial.print(now.second());
   Serial.print((dispHour<12) ? " AM" : " PM");
   Serial.println();
   
             // Temp Display in Deg F
     Serial.print("  Temp: "); // Temp will be in a format of
     Serial.println(degF);         // ##.#
     
            // BMP Temp Display in Deg F
         Serial.print("    BMP_Temp: ");
         Serial.println(F);    
         
           // Pressure Displayed in inHG
           Serial.print("    ATM Pressure: ");
           Serial.println(Pressure);
           
           // Wind Speed
             Serial.print(  "MPH: ");
             Serial.println(MPH);
     
}
  delay(100);   // next step try to use the RTC to take readings every 5 min.

 
 
   
   if (degF <= setpoint)
 {
   digitalWrite(LED,HIGH);
 }
   if (degF > room)
 {
   digitalWrite(LED,LOW);
 }
 
}

float getVoltage(int pin1)
{
 return (analogRead(pin1) * 0.004882814);
}

float getVoltage(float pin2)
{
  return (analogRead(pin2));
}



Above is my current code for the weather station I am working on.  No advice is bad advice!!


PaulS

Quote
My current problem is how do I connect multiple I2c sensors to the one controller?

Each I2C device has a separate address. The libraries you are using seem to know the correct addresses to talk to.

Erdin

I like you choice for the Adafruit BMP085 library. I use it since a few days and it is a good library.
The Arduino uses the Wire library for I2C.
The Wire library sets the Arduino by default in Master mode.
http://arduino.cc/en/Reference/Wire
But the libraries for the RTC and BMP085 use the Wire library, and so they use the Arduino as Master.
You don't have to worry about master or slave or whatever.

The RTC and BMP085 are always in slave mode.

You could use a i2c scanner to detect the connected slaves, http://playground.arduino.cc/Main/I2cScanner

kage_diver


The libraries you are using seem to know the correct addresses to talk to.


Everything seemed to upload properly.  Does this mean it is good to go?

If so...

Is there a reason why my Serial.print isnt working?

johnwasser


Is there a reason why my Serial.print isnt working?


Which Serial.print()? In what way is it 'not working'?
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

kage_diver

The problem I am having is what I upload the program, and start my serial monitor nothing comes up... nothing "prints".  Is it something with my if statement that drives the print process?

PaulS

Code: [Select]
if (millis() % 6000 == 0)
{

This is not the way to do something every 5 minutes. Your calculator needs new batteries if you think that 6000 milliseconds equals 5 minutes.

Expecting the value of millis to be exactly divisible by 6000 is pretty unreasonable. All your Serial.print()s hide behind this unreasonable assumption.

kage_diver


Code: [Select]
if (millis() % 6000 == 0)
{



Expecting the value of millis to be exactly divisible by 6000 is pretty unreasonable. All your Serial.print()s hide behind this unreasonable assumption.


That is exactly what it was.  How would you recommend taking a reading from my sensors every 5 min with out using a delay?

Erdin

At least sent a message in the setup() function, something like "hello" or something like this:
Code: [Select]

void setup ()
{
 Serial.begin(9600);
 Serial.println("Running code : " __DATE__ ", " __TIME__ );

 ...

PaulS

Quote
How would you recommend taking a reading from my sensors every 5 min with out using a delay?

Record when you last sent one. If now minus then is greater than 300000UL, send again, and update the time last sent.

Altogether now: Look at the blink without delay example.

kage_diver

That worked out perfectly!  Thanks for the advice!

Go Up