I want to connect 24 DS18B20 sensors to arduino by onewire library in the same digital pin .
Can anyone post an example code ?
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It only takes a minute to sign up.Sign up to join this community
Use the OneWire and the DallasTemperature libraries. They are both in the Arduino IDE in the Library Manager.
The DallasTemperature library has the example "Multiple". You need to make an array of DeviceAddress.
To tell them apart, you have to label each temperature sensor with its device address (a unique identifier inside each DS18B20). Therefor you have to connect them one-by-one and write down the device address.
Start with one sensor, and if that works, try to make arrays.
For 24 temperature sensors, you need a lot of wires. That could make it less reliable. You can use two or three or more pins as well. Then you have to create more than one "OneWire" bus in the sketch.
There are many examples for multiple DS18B20 sensors. For example this one is with an array: Arduino forum: "multiple temp sensors ds18b20".