Coding for two DS18B20 temperature sensors

Hello everyone,

So I've just started a project for the first time looking to measure various water quality parameters with an Arduino. I'm taking it step by step and just successfully coded my first sketch that actively measured temperature and had a speaker go off if it went out of bounds of parameters I set. Now, I'm interested in adding an additional DS18B20 temperature sensor, but am now running into problems.

Here's my code below:

#include <DallasTemperature.h>
#include <OneWire.h>

int digitalspeak = 8;   //Pin Digital Speaker is connected to
int temp_sensor1 = 5;   //Pin DS18B20 Sensor 1 is connected to
int temp_sensor2 = 6;   //Pin DS18B20 Sensor 2 is connected to

float temperature = 0;   //Variable to store the temperature in

int lowerLimit = 15;  //define the lower threshold for temperature
int higherLimit = 30;  //define the higher threshold for temperature

OneWire oneWirePin1(temp_sensor1);
OneWire oneWirePin2(temp_sensor2);

DallasTemperature sensors(&oneWirePin1);
DallasTemperature sensors(&oneWirePin2);

void setup(void) {
  // put your setup code here, to run once:

  //Setup the speaker to act as outputs
  pinMode (digitalspeak, OUTPUT);


void loop() {
  // put your main code here, to run repeatedly:

  Serial.print("Requesting Temperatures from sensors: ");

  temperature = sensors.getTempCByIndex(0);

  digitalWrite(digitalspeak, LOW);

  Serial.print("Temperature is ");

  //Setup the speaker to act as output

  if(temperature <= lowerLimit){
    Serial.println(", Digital speaker is Activated");
  else if(temperature >= higherLimit){
    Serial.println(", Digital speaker is Activated");

I'm starting to get error messages when introducing "DallasTemperature sensors(&oneWirePin2);". What am I doing wrong?

What am I doing wrong?

You are not posting the entire text of the error message(s). There is a copy errors button in the lower right of the IDE window. Copy the errors and paste into a post in code tags.

Sorry - here are the error messages:

Arduino: 1.8.9 (Windows 7), Board: "Arduino/Genuino Mega or Mega 2560, ATmega1280"

sketch_jun15a:18:26: error: redefinition of 'DallasTemperature sensors'

 DallasTemperature sensors(&oneWirePin2);


C:\Users\Tyler.Tyler-PC\Desktop\sketch_jun15a\sketch_jun15a.ino:17:19: note: 'DallasTemperature sensors' previously declared here

 DallasTemperature sensors(&oneWirePin1);


exit status 1
redefinition of 'DallasTemperature sensors'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

This seems ridiculous. You appear to be trying to use a one wire device with proper one wire code on two pins. I bet you don't have good reason for using two pins anyway.

Try for two sensors on one pin. Note that there are two programmes, the first to get the sensor addresses, and the second to use them.

In the unlikely event that you really do need each sensor on its own pin, try this duino/ar3ne1tt2.htm
About the only reason for using this is that you are running out of memory faster than you are running out of pins, but I don't think you save much..