How many device can i connect when using OneWire?

I am using 2 devices.

  1. MAX30102 for Heartbeat
  2. DS18B20 for Temperature

I tried the example given by SparkFun Library (example 5) and DS18B20 library GitHub - matmunk/DS18B20: Arduino library for the Maxim Integrated DS18B20 1-Wire temperature sensor..

When i try to use the code for temperature, that is DS18B20, the heartbeat values are not displayed. Can someone help me in displaying both heartbeat and also temperature values?

#include /* I added this code */ #include #include "MAX30105.h"

#include "heartRate.h"

MAX30105 particleSensor;

const byte RATE_SIZE = 4; //Increase this for more averaging. 4 is good.
byte rates[RATE_SIZE]; //Array of heart rates
byte rateSpot = 0;
long lastBeat = 0; //Time at which the last beat occurred

DS18B20 ds(8); /* I added this code */

float beatsPerMinute;
int beatAvg;

void setup()
{
Serial.begin(115200);
Serial.println("Initializing...");

// Initialize sensor
if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
{
Serial.println("MAX30105 was not found. Please check wiring/power. ");
while (1);
}
Serial.println("Place your index finger on the sensor with steady pressure.");

particleSensor.setup(); //Configure sensor with default settings
particleSensor.setPulseAmplitudeRed(0x0A); //Turn Red LED to low to indicate sensor is running
particleSensor.setPulseAmplitudeGreen(0); //Turn off Green LED
}

void loop()
{
long irValue = particleSensor.getIR();

if (checkForBeat(irValue) == true)
{
//We sensed a beat!
long delta = millis() - lastBeat;
lastBeat = millis();

beatsPerMinute = 60 / (delta / 1000.0);

if (beatsPerMinute < 255 && beatsPerMinute > 20)
{
  rates[rateSpot++] = (byte)beatsPerMinute; //Store this reading in the array
  rateSpot %= RATE_SIZE; //Wrap variable

  //Take average of readings
  beatAvg = 0;
  for (byte x = 0 ; x < RATE_SIZE ; x++)
    beatAvg += rates[x];
  beatAvg /= RATE_SIZE;
}

}

Serial.print("IR=");
Serial.print(irValue);
Serial.print(", BPM=");
Serial.print(beatsPerMinute);
Serial.print(", Avg BPM=");
Serial.println(beatAvg);

Serial.print(ds.getTempC()); /*I added this code */

if (irValue < 50000)
Serial.print(" No finger?");

Serial.println();
delay(20);
//
}

1. Post the sketch that works only for Hearbeat Sensor -- do not include codes of DS18B20 Sensor.

2. Post the sketch that works only for DS18B20 Sensor -- do not include codes of Hearbeat Sensor.

3. Now, carefully merge the above two sketches so that both work.

4. If possible, please acquire the address of the Heartbeat Sensor and post the value.

Thank you for replying.
for Temperature,

#include <DS18B20.h>

DS18B20 ds(2);

void setup() {
  Serial.begin(9600);
}

void loop() {
      Serial.println(ds.getTempC());
  }

for Heartbeat,

#include <Wire.h>
#include "MAX30105.h"

#include "heartRate.h"

MAX30105 particleSensor;

const byte RATE_SIZE = 4; //Increase this for more averaging. 4 is good.
byte rates[RATE_SIZE]; //Array of heart rates
byte rateSpot = 0;
long lastBeat = 0; //Time at which the last beat occurred

float beatsPerMinute;
int beatAvg;

void setup()
{
  Serial.begin(115200);
  Serial.println("Initializing...");

  // Initialize sensor
  if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
  {
    Serial.println("MAX30105 was not found. Please check wiring/power. ");
    while (1);
  }
  Serial.println("Place your index finger on the sensor with steady pressure.");

  particleSensor.setup(); //Configure sensor with default settings
  particleSensor.setPulseAmplitudeRed(0x0A); //Turn Red LED to low to indicate sensor is running
  particleSensor.setPulseAmplitudeGreen(0); //Turn off Green LED
}

void loop()
{
  long irValue = particleSensor.getIR();

  if (checkForBeat(irValue) == true)
  {
    //We sensed a beat!
    long delta = millis() - lastBeat;
    lastBeat = millis();

    beatsPerMinute = 60 / (delta / 1000.0);

    if (beatsPerMinute < 255 && beatsPerMinute > 20)
    {
      rates[rateSpot++] = (byte)beatsPerMinute; //Store this reading in the array
      rateSpot %= RATE_SIZE; //Wrap variable

      //Take average of readings
      beatAvg = 0;
      for (byte x = 0 ; x < RATE_SIZE ; x++)
        beatAvg += rates[x];
      beatAvg /= RATE_SIZE;
    }
  }

  Serial.print("IR=");
  Serial.print(irValue);
  Serial.print(", BPM=");
  Serial.print(beatsPerMinute);
  Serial.print(", Avg BPM=");
  Serial.println(beatAvg);
  Serial.print(ds.getTempC());

  if (irValue < 50000)
    Serial.print(" No finger?");

  Serial.println();
    //
}

Temperature,

For heartbeat,

When i comment out the temperature code heartbeat code works correctly.
When i comment out the heartbeatcode, the temperature code works perfectly.
If i keep both. Only temperature code works.

You need to study the Analog Devices* 1-WireTM documentation.

1-WireTM can support very large numbers of devices - but it requires more careful driver design than the single microcontroller port pin commonly found in Arduino projects.

There's been a number of discussions on this recently; eg,


* Analog Devices acquired Maxim, who acquired Dallas Semiconductor - the originators of 1-WireTM

Try the following (untested) sketch:

#include <Wire.h>
#include<OneWire.h>
#include "MAX30105.h"
#include "heartRate.h"

MAX30105 particleSensor;
OneWire ds(2);
byte dsAddr[8];
byte dsData[9];

const byte RATE_SIZE = 4; //Increase this for more averaging. 4 is good.
byte rates[RATE_SIZE]; //Array of heart rates
byte rateSpot = 0;
unsigned long lastBeat = 0; //Time at which the last beat occurred

float beatsPerMinute;
int beatAvg;

void setup()
{
  Serial.begin(115200);
  Serial.println("Initializing...");
  ds.reset();
  ds.search(dsAddr); //get the address of DS18B20

  // Initialize sensor
  if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
  {
    Serial.println("MAX30105 was not found. Please check wiring/power. ");
    while (1);
  }
  Serial.println("Place your index finger on the sensor with steady pressure.");

  particleSensor.setup(); //Configure sensor with default settings
  particleSensor.setPulseAmplitudeRed(0x0A); //Turn Red LED to low to indicate sensor is running
  particleSensor.setPulseAmplitudeGreen(0); //Turn off Green LED
}

void loop()
{
  long irValue = particleSensor.getIR();
  if (checkForBeat(irValue) == true)
  {
    //We sensed a beat!
    unsigned long delta = millis() - lastBeat;
    lastBeat = millis();

    beatsPerMinute = 60 / (delta / 1000.0);

    if (beatsPerMinute < 255 && beatsPerMinute > 20)
    {
      rates[rateSpot++] = (byte)beatsPerMinute; //Store this reading in the array
      rateSpot %= RATE_SIZE; //Wrap variable

      //Take average of readings
      beatAvg = 0;
      for (byte x = 0 ; x < RATE_SIZE ; x++)
      {
        beatAvg += rates[x];
      }
      beatAvg /= RATE_SIZE;
    }
  }

  Serial.print("IR=");
  Serial.print(irValue);
  if (irValue < 50000)
  {
    Serial.println(" No finger?");
  }
  else
  {
    Serial.print(", BPM=");
    Serial.print(beatsPerMinute);
    Serial.print(", Avg BPM=");
    Serial.println(beatAvg);
  }
  ds.reset();       //bring 1-Wire into idle state
  ds.select(dsAddr); //slect with DS-1 with address addr1
  ds.write(0x44);    //conversion command
  delay(750);   //data ready withinh DS18B20-1 or poll status word
  //---------------------------
  ds.reset();
  ds.select(dsAddr);  //selectimg the desired DS18B20-1
  ds.write(0xBE);    //Function command to read Scratchpad Memory (9Byte)
  ds.read_bytes(dsData, 9); //data comes from DS and are saved into buffer data[8]
  //---------------------------------
  int16_t raw = (dsData[1] << 8) | dsData[0]; //---data[0] and data[1] contains temperature data : 12-bit resolution-----
  float celsius = (float)raw / 16.0;  //12-bit resolution
  Serial.print("Temp from Sensor: ");
  Serial.println(celsius, 2);
}

You only have one device, the DS18B20 that is connected by 1-Wire.
The MAX30102 is communicating by I2C.

I'm not sure whether this is the cause of your problem, but the DS18B20 takes more than 600ms to do its temperature conversion. It is likely that during this time the MAX30102 misses heartbeats.

Thank you for replying.
I am getting the below output,
image

1. Are you getting this output running the sketch of post #5?

2. Does the output make sense to you?

1 Like

Yes. The output is for the sketch #5. The BPM and Avg BPM are showing wrong values. Avg BPM always shows 0.

reading the temperatures of DS18B20-sensors can be done in a nonblocking way.
You will need to read how this works.

Me personal I don't use the DS18B20.h-library.
I don't know if this library supports non-blocking reading the temperatures.

With googling I found this library

which has an example-code that demonstrates the non-blocking reading

You have to call function

sensorDs18b20.update();

again and again and again (whích is done through the looping of loop)
executing the function
sensorDs18b20.update() is finished very quick.

Another important thing to know is Callback-function means:

the callback-function is called from inside sensorDs18b20.update()
every time a temperature-conversion inside the DS18B20-sensors has finished.
This means

sensorDs18b20.update();

is called over and over again
but only in case a temperature-conversion has really finished
the function handleIntervalElapsed() gets executed.

best regards Stefan

1 Like

The good thing is, I am able to see the temperature value as well as heartbeat value.

1. Now, at least both sensors are working together -- do you agree? You can adjust the sketch of post #5 so that you get expected responses from oxymeter sensor.

2. Can you post the output of the oxymeter sensor when it was running alone (without DS18B20 sensor) ?

3. By the by, what is the purpose of running DS18B20 temperature sensor along with oxymeter sensor?

1 Like

Yes, it is working. I commented out the delay part ( delay(750); //data ready within DS18B20-1 or poll status word).

The Output for the beats are not that accurate.

I am using DS18B20 temperature sensor to display temperature.of human body.

After the convert command, you msut allow 750 ms dealy for conversion. Check the DS18B20 data sheets.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.