Need Help with DS18B20

Hi everyone I want to make a project that if temperature reaches below or above a certain level it switches on and off a certain relay the problem is that I have to use 3 sensors that means I have to collect data from 3 sensors (DS18B20) and 8 relays My problem is that I don't know how to read data from 3 of them and how to make an if statement for each one of them .


Thanks But Can I use in if statement ?

Yes but I think it more normal to use while for this.

    while (Serial2.available()) 
    char c =;
    readString += c;

Are you discussing the DS18B20 on a Mega or on an Uno? Study the attached ino for multiple DS18B20’s on a Nano.

don’t forget: the DS18B20’s are extremely handy because they are single bus sensors - they require only single pin on an Arduino - I have 10 of them woorking for me on one pin on a single Nano. They measure floor heating loop temperatures. Wiring scheme can be found at

Every sensor has its own ID. You can trace these IDs via DS18B2_address_finder (in attached zip)
Here is a four-sensor ino (data to pin 8. Don’t forget a 4.7 k pull up resistor - without them no data, or -127 centigrade reading).
Cheers (2.66 KB)

Thanks you I am using Mega And I want to trigger relays with it

Unless something else that you are not talking about gets into the act, it doesn't make the slightest difference whether you are using a Mega or a Uno. You just read the data into variables, and then go through each in turn, testing for whatever condition by "if" statements.