Pages: [1]   Go Down
Author Topic: Connecting Multiple I2C Sensors  (Read 3170 times)
0 Members and 1 Guest are viewing this topic.
Wisconsin, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
// 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!!

« Last Edit: February 19, 2013, 10:47:56 pm by kage_diver » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Edison Member
*
Karma: 58
Posts: 2078
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Wisconsin, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8975
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Which Serial.print()? In what way is it 'not working'?
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Wisconsin, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

Wisconsin, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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?
Logged

Offline Offline
Edison Member
*
Karma: 58
Posts: 2078
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

At least sent a message in the setup() function, something like "hello" or something like this:
Code:
void setup ()
{
  Serial.begin(9600);
  Serial.println("Running code : " __DATE__ ", " __TIME__ );

  ...
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Wisconsin, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That worked out perfectly!  Thanks for the advice!
Logged

Pages: [1]   Go Up
Jump to: