cpetrich / counterfeit_DS18B20

How to tell original from fake DS18B20 temperature sensors.
Apache License 2.0
595 stars 50 forks source link

How to get the relevant conversion time. #38

Closed mayjack0312 closed 2 weeks ago

mayjack0312 commented 3 weeks ago

Very good project, but I want to ask outside the line: How to modify the code to obtain the conversion time of each DS18B20 and print it out?

mayjack0312 commented 3 weeks ago

For example, I want to get the corresponding times in the table below one by one. image

lalo-uy commented 3 weeks ago

You send the start conversion cmd and time until the bus line goes low.

El El jue, 20 jun. 2024 a la(s) 13:08, LMY @.***> escribió:

For example, I want to get the corresponding times in the table below one by one. image.png (view on web) https://github.com/cpetrich/counterfeit_DS18B20/assets/102800660/b3e5861a-6aae-43c8-92b3-5083929306ff

— Reply to this email directly, view it on GitHub https://github.com/cpetrich/counterfeit_DS18B20/issues/38#issuecomment-2181061436, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACXBW4NB3LMEL2Z7HNY4DELZIL465AVCNFSM6AAAAABJUILIBWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOBRGA3DCNBTGY . You are receiving this because you are subscribed to this thread.Message ID: @.***>

mayjack0312 commented 3 weeks ago

You send the start conversion cmd and time until the bus line goes low. El El jue, 20 jun. 2024 a la(s) 13:08, LMY @.> escribió: For example, I want to get the corresponding times in the table below one by one. image.png (view on web) https://github.com/cpetrich/counterfeit_DS18B20/assets/102800660/b3e5861a-6aae-43c8-92b3-5083929306ff — Reply to this email directly, view it on GitHub <#38 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACXBW4NB3LMEL2Z7HNY4DELZIL465AVCNFSM6AAAAABJUILIBWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOBRGA3DCNBTGY . You are receiving this because you are subscribed to this thread.Message ID: @.>

Can you suggest a sample code? The code conversion times I wrote before were not quite correct. I almost cried from torture o(╥﹏╥)o

uzi18 commented 3 weeks ago

Use this function: https://github.com/milesburton/Arduino-Temperature-Control-Library/blob/65112b562fd37af68ed113c9a3925c09c4529e14/DallasTemperature.cpp#L411

mayjack0312 commented 3 weeks ago

Use this function: https://github.com/milesburton/Arduino-Temperature-Control-Library/blob/65112b562fd37af68ed113c9a3925c09c4529e14/DallasTemperature.cpp#L411

This is the code I wrote yesterday. Although I found that there are many potential bugs in the writing, I still don’t know how to modify it.

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

#define ONE_WIRE_BUS 2

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

DeviceAddress DS18B20_ID;

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

  unsigned long initStartTime = micros();

  sensors.begin();

  unsigned long initEndTime = micros();

  Serial.print("Initialization Time: ");
  Serial.print(initEndTime - initStartTime);
  Serial.println(" us");
}

void loop() {
  if (Serial.available() > 0) {
    String command = Serial.readStringUntil('\n');
    command.trim();

    if (command == "GET") {
      unsigned long waitStartTime = micros();

      delayMicroseconds(550);

      unsigned long waitEndTime = micros();

      unsigned long convStartTime = micros();

      sensors.requestTemperatures();

      unsigned long convEndTime = micros();

      unsigned long readCmdStartTime = micros();

      float temperature = sensors.getTempCByIndex(0);

      unsigned long readCmdEndTime = micros();

      unsigned long readRegStartTime = micros();

      Serial.print("Temperature: ");
      Serial.print(temperature);
      Serial.println(" C");

      unsigned long readRegEndTime = micros();

      Serial.print("Wait for Bus Release Time: ");
      Serial.print(waitEndTime - waitStartTime);
      Serial.println(" us");

      Serial.print("Trigger Conversion Time: ");
      Serial.print(convEndTime - convStartTime);
      Serial.println(" us");

      Serial.print("Send Read Command Time: ");
      Serial.print(readCmdEndTime - readCmdStartTime);
      Serial.println(" us");

      Serial.print("Read Register Value Time: ");
      Serial.print(readRegEndTime - readRegStartTime);
      Serial.println(" us");
    } else {
      Serial.println("Unknown command.");
    }
  }
}
lalo-uy commented 3 weeks ago

You don’t know how requestTemperatures() operates. Could just wait 1s to be sure. Get the onewire ds18b20 example, and after request the temp, instead delay(800) do a loop doing digitalRead() of the bus til is low.

El El jue, 20 jun. 2024 a la(s) 13:48, LMY @.***> escribió:

Use this function: https://github.com/milesburton/Arduino-Temperature-Control-Library/blob/65112b562fd37af68ed113c9a3925c09c4529e14/DallasTemperature.cpp#L411 https://github.com/milesburton/Arduino-Temperature-Control-Library/blob/65112b562fd37af68ed113c9a3925c09c4529e14/DallasTemperature.cpp#L411

This is the code I wrote yesterday. Although I found that there are many potential bugs in the writing, I still don’t know how to modify it.

include

include

define ONE_WIRE_BUS 2

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

DeviceAddress DS18B20_ID;

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

unsigned long initStartTime = micros();

sensors.begin();

unsigned long initEndTime = micros();

Serial.print("Initialization Time: "); Serial.print(initEndTime - initStartTime); Serial.println(" us"); }

void loop() { if (Serial.available() > 0) { String command = Serial.readStringUntil('\n'); command.trim();

if (command == "GET") {
  unsigned long waitStartTime = micros();

  delayMicroseconds(550);

  unsigned long waitEndTime = micros();

  unsigned long convStartTime = micros();

  sensors.requestTemperatures();

  unsigned long convEndTime = micros();

  unsigned long readCmdStartTime = micros();

  float temperature = sensors.getTempCByIndex(0);

  unsigned long readCmdEndTime = micros();

  unsigned long readRegStartTime = micros();

  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" C");

  unsigned long readRegEndTime = micros();

  Serial.print("Wait for Bus Release Time: ");
  Serial.print(waitEndTime - waitStartTime);
  Serial.println(" us");

  Serial.print("Trigger Conversion Time: ");
  Serial.print(convEndTime - convStartTime);
  Serial.println(" us");

  Serial.print("Send Read Command Time: ");
  Serial.print(readCmdEndTime - readCmdStartTime);
  Serial.println(" us");

  Serial.print("Read Register Value Time: ");
  Serial.print(readRegEndTime - readRegStartTime);
  Serial.println(" us");
} else {
  Serial.println("Unknown command.");
}

} }

— Reply to this email directly, view it on GitHub https://github.com/cpetrich/counterfeit_DS18B20/issues/38#issuecomment-2181129126, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACXBW4NPQF7MQEZUHIQNS6TZIMBUTAVCNFSM6AAAAABJUILIBWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOBRGEZDSMJSGY . You are receiving this because you commented.Message ID: @.***>

mayjack0312 commented 3 weeks ago

You don’t know how requestTemperatures() operates. Could just wait 1s to be sure. Get the onewire ds18b20 example, and after request the temp, instead delay(800) do a loop doing digitalRead() of the bus til is low. El El jue, 20 jun. 2024 a la(s) 13:48, LMY @.> escribió: Use this function: https://github.com/milesburton/Arduino-Temperature-Control-Library/blob/65112b562fd37af68ed113c9a3925c09c4529e14/DallasTemperature.cpp#L411 https://github.com/milesburton/Arduino-Temperature-Control-Library/blob/65112b562fd37af68ed113c9a3925c09c4529e14/DallasTemperature.cpp#L411 This is the code I wrote yesterday. Although I found that there are many potential bugs in the writing, I still don’t know how to modify it. #include #include #define ONE_WIRE_BUS 2 OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); DeviceAddress DS18B20_ID; void setup() { Serial.begin(9600); unsigned long initStartTime = micros(); sensors.begin(); unsigned long initEndTime = micros(); Serial.print("Initialization Time: "); Serial.print(initEndTime - initStartTime); Serial.println(" us"); } void loop() { if (Serial.available() > 0) { String command = Serial.readStringUntil('\n'); command.trim(); if (command == "GET") { unsigned long waitStartTime = micros(); delayMicroseconds(550); unsigned long waitEndTime = micros(); unsigned long convStartTime = micros(); sensors.requestTemperatures(); unsigned long convEndTime = micros(); unsigned long readCmdStartTime = micros(); float temperature = sensors.getTempCByIndex(0); unsigned long readCmdEndTime = micros(); unsigned long readRegStartTime = micros(); Serial.print("Temperature: "); Serial.print(temperature); Serial.println(" C"); unsigned long readRegEndTime = micros(); Serial.print("Wait for Bus Release Time: "); Serial.print(waitEndTime - waitStartTime); Serial.println(" us"); Serial.print("Trigger Conversion Time: "); Serial.print(convEndTime - convStartTime); Serial.println(" us"); Serial.print("Send Read Command Time: "); Serial.print(readCmdEndTime - readCmdStartTime); Serial.println(" us"); Serial.print("Read Register Value Time: "); Serial.print(readRegEndTime - readRegStartTime); Serial.println(" us"); } else { Serial.println("Unknown command."); } } } — Reply to this email directly, view it on GitHub <#38 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACXBW4NPQF7MQEZUHIQNS6TZIMBUTAVCNFSM6AAAAABJUILIBWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOBRGEZDSMJSGY . You are receiving this because you commented.Message ID: @.>

Please help me see if this code is written appropriately. Thank you.

sensors.requestTemperatures();
delay(800);

unsigned long startTime = millis();
while (digitalRead(pin_onewire) != LOW) {
}
unsigned long endTime = millis();

unsigned long duration = endTime - startTime;
Comm.print(F(" Low level detection time: "));
Comm.print(duration);
Comm.println(F(" ms"));
uzi18 commented 3 weeks ago

sensors.requestTemperatures();

unsigned long startTime = millis();
while (!sensors.isConversionComplete()) {
}
unsigned long endTime = millis();

unsigned long duration = endTime - startTime;
Comm.print(F(" Low level detection time: "));
Comm.print(duration);
Comm.println(F(" ms"));
mayjack0312 commented 3 weeks ago

Through a new attempt, I discovered a rather difficult problem. When trying to measure the temperature conversion time, a manual delay must be added between the initialization command and the conversion command. However, this leads to errors in judgment or inaccurate delays. I would like to seek help on how to solve this problem. I tried monitoring the high and low signals of the DQ line, but it seems that the pins of the Arduino Uno do not support this.

uzi18 commented 3 weeks ago

show complete code and point where is your problem ... please show also output from Serial Monitor

mayjack0312 commented 3 weeks ago

show complete code and point where is your problem ... please show also output from Serial Monitor

I tried several methods but none of them worked very well code.zip

09925bfd21576488a996a67faf8dc73 74dda048d1b38fa965d3c510829a701

image

b8709a1da2a8d43636a32ef024aaef8

The only time it succeeded was to display the time it took to read the temperature.

af5d057e0b11656245e20f1692b8d91
uzi18 commented 3 weeks ago

Please paste actual code to review

mayjack0312 commented 3 weeks ago

Please paste actual code to review

1 ``` #include #include #define ONE_WIRE_BUS 2 OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); DeviceAddress DS18B20_ID; void setup() { Serial.begin(9600); unsigned long initStartTime = micros(); sensors.begin(); unsigned long initEndTime = micros(); Serial.print("Initialization Time: "); Serial.print(initEndTime - initStartTime); Serial.println(" us"); } void loop() { if (Serial.available() > 0) { String command = Serial.readStringUntil('\n'); command.trim(); if (command == "GET") { // 刷新序列 unsigned long refreshStartTime = micros(); sensors.begin(); unsigned long refreshEndTime = micros(); unsigned long waitStartTime = micros(); delayMicroseconds(550); unsigned long waitEndTime = micros(); unsigned long convStartTime = micros(); sensors.requestTemperatures(); unsigned long convEndTime = micros(); unsigned long readCmdStartTime = micros(); float temperature = sensors.getTempCByIndex(0); unsigned long readCmdEndTime = micros(); unsigned long readRegStartTime = micros(); // 所有测量完成后再打印结果 unsigned long readRegEndTime = micros(); // 声明并赋值 readRegEndTime Serial.print("Temperature: "); Serial.print(temperature); Serial.println(" C"); Serial.print("Refresh Sequence Time: "); Serial.print(refreshEndTime - refreshStartTime); Serial.println(" us"); Serial.print("Wait for Bus Release Time: "); Serial.print(waitEndTime - waitStartTime); Serial.println(" us"); Serial.print("Trigger Conversion Time: "); Serial.print(convEndTime - convStartTime); Serial.println(" us"); Serial.print("Send Read Command Time: "); Serial.print(readCmdEndTime - readCmdStartTime); Serial.println(" us"); Serial.print("Read Register Value Time: "); Serial.print(readRegEndTime - readRegStartTime); Serial.println(" us"); } else { Serial.println("Unknown command."); } } } ```
2 ``` #include // OneWire DS18S20, DS18B20, DS1822 Temperature Example // // http://www.pjrc.com/teensy/td_libs_OneWire.html // // The DallasTemperature library can do all this work for you! // https://github.com/milesburton/Arduino-Temperature-Control-Library OneWire ds(10); // on pin 10 (a 4.7K resistor is necessary) void setup(void) { Serial.begin(9600); } void loop(void) { byte i; byte present = 0; byte type_s; byte data[9]; byte addr[8]; float celsius, fahrenheit; unsigned long initStartTime = micros(); // 记录初始化开始时间 if ( !ds.search(addr)) { Serial.println("No more addresses."); Serial.println(); ds.reset_search(); delay(250); return; } unsigned long initEndTime = micros(); // 记录初始化结束时间 Serial.print("ROM ="); for( i = 0; i < 8; i++) { Serial.write(' '); Serial.print(addr[i], HEX); } if (OneWire::crc8(addr, 7) != addr[7]) { Serial.println("CRC is not valid!"); return; } Serial.println(); // the first ROM byte indicates which chip switch (addr[0]) { case 0x10: Serial.println(" Chip = DS18S20"); // or old DS1820 type_s = 1; break; case 0x28: Serial.println(" Chip = DS18B20"); type_s = 0; break; case 0x22: Serial.println(" Chip = DS1822"); type_s = 0; break; default: Serial.println("Device is not a DS18x20 family device."); return; } unsigned long waitStartTime = micros(); // 记录等待DS18B20释放总线开始时间 ds.reset(); ds.select(addr); ds.write(0x44, 1); // start conversion, with parasite power on at the end delay(1000); // maybe 750ms is enough, maybe not // we might do a ds.depower() here, but the reset will take care of it. unsigned long waitEndTime = micros(); // 记录等待DS18B20释放总线结束时间 present = ds.reset(); ds.select(addr); ds.write(0xBE); // Read Scratchpad unsigned long readCmdStartTime = micros(); // 记录发送读数据命令开始时间 Serial.print(" Data = "); Serial.print(present, HEX); Serial.print(" "); for ( i = 0; i < 9; i++) { // we need 9 bytes data[i] = ds.read(); Serial.print(data[i], HEX); Serial.print(" "); } Serial.print(" CRC="); Serial.print(OneWire::crc8(data, 8), HEX); Serial.println(); unsigned long readCmdEndTime = micros(); // 记录发送读数据命令结束时间 // Convert the data to actual temperature // because the result is a 16 bit signed integer, it should // be stored to an "int16_t" type, which is always 16 bits // even when compiled on a 32 bit processor. int16_t raw = (data[1] << 8) | data[0]; if (type_s) { raw = raw << 3; // 9 bit resolution default if (data[7] == 0x10) { // "count remain" gives full 12 bit resolution raw = (raw & 0xFFF0) + 12 - data[6]; } } else { byte cfg = (data[4] & 0x60); // at lower res, the low bits are undefined, so let's zero them if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms //// default is 12 bit resolution, 750 ms conversion time } celsius = (float)raw / 16.0; fahrenheit = celsius * 1.8 + 32.0; unsigned long readRegEndTime = micros(); // 记录读取寄存器值结束时间 Serial.print(" Temperature = "); Serial.print(celsius); Serial.print(" Celsius, "); Serial.print(fahrenheit); Serial.println(" Fahrenheit"); // 打印各个时间 Serial.print("Initialization Time: "); Serial.print(initEndTime - initStartTime); Serial.println(" us"); Serial.print("Wait for Bus Release Time: "); Serial.print(waitEndTime - waitStartTime); Serial.println(" us"); Serial.print("Trigger Conversion Time: "); Serial.print(readCmdStartTime - waitEndTime); Serial.println(" us"); Serial.print("Send Read Command Time: "); Serial.print(readCmdEndTime - readCmdStartTime); Serial.println(" us"); Serial.print("Read Register Value Time: "); Serial.print(readRegEndTime - readCmdEndTime); Serial.println(" us"); } ```
3 ``` #include #include #define ONE_WIRE_BUS 2 OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); DeviceAddress DS18B20_ID; void setup() { Serial.begin(9600); while(!Serial); // wait until Serial device is ready. Serial.println(__FILE__); delay(100); // wait until output buffer of Serial is empty. unsigned long initStartTime = micros(); sensors.begin(); sensors.setResolution(12); // force an explicit resolution, 9..12 unsigned long initEndTime = micros(); Serial.print("Initialization Time: "); Serial.print(initEndTime - initStartTime); Serial.println(" us"); delay(100); // wait until output buffer of Serial is empty. } void loop() { if (Serial.available() > 0) { String command = Serial.readStringUntil('\n'); command.trim(); if (command == "GET") { delay(100); // wait for Serial output empty. // why measure this? unsigned long waitStartTime = micros(); delayMicroseconds(550); unsigned long waitEndTime = micros(); unsigned long convStartTime = micros(); sensors.requestTemperatures(); unsigned long convEndTime = micros(); unsigned long readCmdStartTime = micros(); float temperature = sensors.getTempCByIndex(0); unsigned long readCmdEndTime = micros(); // here you measure the time to put the strings // in the serial output buffer. not the print time. unsigned long readRegStartTime = micros(); Serial.print("Temperature: "); Serial.print(temperature); Serial.println(" C"); unsigned long readRegEndTime = micros(); Serial.print("Wait for Bus Release Time: "); Serial.print(waitEndTime - waitStartTime); Serial.println(" us"); Serial.print("Trigger Conversion Time: "); Serial.print(convEndTime - convStartTime); Serial.println(" us"); Serial.print("Send Read Command Time: "); Serial.print(readCmdEndTime - readCmdStartTime); Serial.println(" us"); Serial.print("Read Register Value Time: "); Serial.print(readRegEndTime - readRegStartTime); Serial.println(" us"); } else { Serial.println("Unknown command."); } } delay(1000); // wait for all output to be done } ```
4 ``` #include #define DS18B20_PIN 2 #define TIME_MONITOR_PIN 3 OneWire ds(DS18B20_PIN); typedef struct { uint8_t temp_lsb; uint8_t temp_msb; uint8_t th_reg_ub1; uint8_t tl_reg_ub2; uint8_t config_reg; uint8_t reserved; uint8_t user_byte3; uint8_t user_byte4; uint8_t crc; } DS18B20_SCRATCHPAD_S_TYPE; typedef struct { uint8_t negative_flag; float temperature; DS18B20_SCRATCHPAD_S_TYPE scratchpad; } DS18B20_STATUE_S_TYPE; DS18B20_STATUE_S_TYPE ds18b20 = { 0 }; void DS18B20_ResetSig() { ds.reset(); } uint8_t DS18B20_PresenceSig() { return ds.reset(); } void DS18B20_WriteBit(uint8_t bit) { if (bit) { ds.write(0x01, 1); } else { ds.write(0x00, 1); } } uint8_t DS18B20_ReadBit() { return ds.read_bit(); } void DS18B20_WriteByte(uint8_t data) { ds.write(data, 1); } uint8_t DS18B20_ReadByte() { return ds.read(); } void DS18B20_StartConversion() { DS18B20_ResetSig(); if (!DS18B20_PresenceSig()) { return; } DS18B20_WriteByte(0xCC); DS18B20_WriteByte(0x44); while (!DS18B20_ReadBit()); } void DS18B20_ReadScratchPad() { uint8_t i; uint8_t *addr; uint8_t max_read_byte = 9; DS18B20_ResetSig(); if (!DS18B20_PresenceSig()) { return; } DS18B20_WriteByte(0xCC); DS18B20_WriteByte(0xBE); for (i = 0; i < max_read_byte; i++) { addr = &ds18b20.scratchpad.temp_lsb + i; *addr = DS18B20_ReadByte(); } } void DS18B20_GetTemperature() { uint8_t shift_size = 0; uint8_t neg_flag = 0; uint8_t temp_msb = 0, temp_lsb = 0; uint16_t temp_reg = 0; float temperature = 0.0; DS18B20_StartConversion(); DS18B20_ReadScratchPad(); temp_msb = ds18b20.scratchpad.temp_msb; temp_lsb = ds18b20.scratchpad.temp_lsb; if (temp_msb > 7) { temp_msb = ~temp_msb; temp_lsb = ~temp_lsb; temp_reg = ((temp_msb << 8) | temp_lsb) + 1; temp_reg &= 0x07FF; neg_flag = 1; } else { temp_reg = (temp_msb << 8) | temp_lsb; temp_reg &= 0x7FF; } shift_size = (ds18b20.scratchpad.config_reg >> 5); temperature = (float)temp_reg / (2 << shift_size); ds18b20.negative_flag = neg_flag; ds18b20.temperature = temperature; } void DS18B20_Test() { DS18B20_GetTemperature(); delay(1000); } void setup() { // 初始化串口 Serial.begin(9600); pinMode(TIME_MONITOR_PIN, INPUT); } void loop() { DS18B20_Test(); Serial.print("Temperature: "); Serial.print(ds18b20.temperature); Serial.println(" C"); while (1); // 停止循环,等待reset } ```
lalo-uy commented 3 weeks ago

El El sáb, 22 jun. 2024 a la(s) 11:27, LMY @.***> escribió:

include

// OneWire DS18S20, DS18B20, DS1822 Temperature Example // // http://www.pjrc.com/teensy/td_libs_OneWire.html // // The DallasTemperature library can do all this work for you! // https://github.com/milesburton/Arduino-Temperature-Control-Library

OneWire ds(10); // on pin 10 (a 4.7K resistor is necessary)

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

void loop(void) { byte i; byte present = 0; byte type_s; byte data[9]; byte addr[8]; float celsius, fahrenheit;

unsigned long initStartTime = micros(); // 记录初始化开始时间

if ( !ds.search(addr)) { Serial.println("No more addresses."); Serial.println(); ds.reset_search(); delay(250); return; }

unsigned long initEndTime = micros(); // 记录初始化结束时间

Serial.print("ROM ="); for( i = 0; i < 8; i++) { Serial.write(' '); Serial.print(addr[i], HEX); }

if (OneWire::crc8(addr, 7) != addr[7]) { Serial.println("CRC is not valid!"); return; } Serial.println();

// the first ROM byte indicates which chip switch (addr[0]) { case 0x10: Serial.println(" Chip = DS18S20"); // or old DS1820 type_s = 1; break; case 0x28: Serial.println(" Chip = DS18B20"); type_s = 0; break; case 0x22: Serial.println(" Chip = DS1822"); type_s = 0; break; default: Serial.println("Device is not a DS18x20 family device."); return; }

unsigned long waitStartTime = micros(); // 记录等待DS18B20释放总线开始时间

ds.reset(); ds.select(addr); ds.write(0x44, 1); // start conversion, with parasite power on at the end

// delay(1000); // maybe 750ms is enough, maybe not

// Instead of delay while ( digital.Read(10) == 1 ) ;

// we might do a ds.depower() here, but the reset will take care of it.

unsigned long waitEndTime = micros(); // 记录等待DS18B20释放总线结束时间

present = ds.reset(); ds.select(addr); ds.write(0xBE); // Read Scratchpad

unsigned long readCmdStartTime = micros(); // 记录发送读数据命令开始时间

mayjack0312 commented 3 weeks ago

El El sáb, 22 jun. 2024 a la(s) 11:27, LMY @.***> escribió:

include // OneWire DS18S20, DS18B20, DS1822 Temperature Example // // http://www.pjrc.com/teensy/td_libs_OneWire.html // // The DallasTemperature library can do all this work for you! // https://github.com/milesburton/Arduino-Temperature-Control-Library OneWire ds(10); // on pin 10 (a 4.7K resistor is necessary) void setup(void) { Serial.begin(9600); } void loop(void) { byte i; byte present = 0; byte type_s; byte data[9]; byte addr[8]; float celsius, fahrenheit; unsigned long initStartTime = micros(); // 记录初始化开始时间 if ( !ds.search(addr)) { Serial.println("No more addresses."); Serial.println(); ds.reset_search(); delay(250); return; } unsigned long initEndTime = micros(); // 记录初始化结束时间 Serial.print("ROM ="); for( i = 0; i < 8; i++) { Serial.write(' '); Serial.print(addr[i], HEX); } if (OneWire::crc8(addr, 7) != addr[7]) { Serial.println("CRC is not valid!"); return; } Serial.println(); // the first ROM byte indicates which chip switch (addr[0]) { case 0x10: Serial.println(" Chip = DS18S20"); // or old DS1820 type_s = 1; break; case 0x28: Serial.println(" Chip = DS18B20"); type_s = 0; break; case 0x22: Serial.println(" Chip = DS1822"); type_s = 0; break; default: Serial.println("Device is not a DS18x20 family device."); return; } unsigned long waitStartTime = micros(); // 记录等待DS18B20释放总线开始时间 ds.reset(); ds.select(addr); ds.write(0x44, 1); // start conversion, with parasite power on at the end // delay(1000); // maybe 750ms is enough, maybe not // Instead of delay

while ( digital.Read(10) == 1 ) ; // we might do a ds.depower() here, but the reset will take care of it. unsigned long waitEndTime = micros(); // 记录等待DS18B20释放总线结束时间 present = ds.reset(); ds.select(addr); ds.write(0xBE); // Read Scratchpad unsigned long readCmdStartTime = micros(); // 记录发送读数据命令开始时间

I need a program to help me read the corresponding time in the table image

mayjack0312 commented 2 weeks ago

I solved the time issue, but a new problem has come up. I tried the two pieces of code below, but I feel like there's an issue with the execution logic in both.

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

// 定义DS18B20数据引脚连接到Arduino的数字引脚2
#define ONE_WIRE_BUS 2

// 设置OneWire实例以与任何OneWire设备通信(而不仅仅是DS18B20)
OneWire oneWire(ONE_WIRE_BUS);

// 将OneWire传递给Dallas Temperature库
DallasTemperature sensors(&oneWire);

void setup() {
  // 启动串行通信,用于调试和输出
  Serial.begin(9600);
  // 启动库
  sensors.begin();
}

void loop() {
  // 获取传感器的数量
  int deviceCount = sensors.getDeviceCount();

  // 遍历每个传感器进行测量
  for (int i = 0; i < deviceCount; i++) {
    // 记录开始时间
    unsigned long startTime = micros();

    // 请求温度转换
    sensors.requestTemperaturesByIndex(i);

    // 获取温度(以摄氏度为单位)
    float temperatureC = sensors.getTempCByIndex(i);

    // 记录结束时间
    unsigned long endTime = micros();

    // 计算读取温度所花费的时间(以微秒为单位)
    float duration = (endTime - startTime) / 1000.0;

    // 打印温度和读取时间到串行监视器
    Serial.print("传感器 ");
    Serial.print(i);
    Serial.print(" 温度: ");
    Serial.print(temperatureC);
    Serial.print(" °C, 读取时间: ");
    Serial.print(duration, 3);
    Serial.println(" 毫秒");

    // 延时1毫秒
    delay(1);
  }

  // 等待5秒钟
  delay(5000);
}

AND

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

// 定义DS18B20数据引脚连接到Arduino的数字引脚2
#define ONE_WIRE_BUS_1 2
#define ONE_WIRE_BUS_2 3

// 设置OneWire实例以与任何OneWire设备通信(而不仅仅是DS18B20)
OneWire oneWire1(ONE_WIRE_BUS_1);
OneWire oneWire2(ONE_WIRE_BUS_2);

// 将OneWire传递给Dallas Temperature库
DallasTemperature sensors1(&oneWire1);
DallasTemperature sensors2(&oneWire2);

void setup() {
  // 启动串行通信,用于调试和输出
  Serial.begin(9600);
  // 启动库
  sensors1.begin();
  sensors2.begin();
}

void loop() {
  // 获取传感器的数量
  int deviceCount1 = sensors1.getDeviceCount();
  int deviceCount2 = sensors2.getDeviceCount();

  // 遍历第一个IO口的传感器进行测量
  for (int i = 0; i < deviceCount1; i++) {
    // 记录开始时间
    unsigned long startTime = micros();

    // 请求温度转换
    sensors1.requestTemperaturesByIndex(i);

    // 获取温度(以摄氏度为单位)
    float temperatureC = sensors1.getTempCByIndex(i);

    // 记录结束时间
    unsigned long endTime = micros();

    // 计算读取温度所花费的时间(以微秒为单位)
    float duration = (endTime - startTime) / 1000.0;

    // 打印温度和读取时间到串行监视器
    Serial.print("IO口1 传感器 ");
    Serial.print(i);
    Serial.print(" 温度: ");
    Serial.print(temperatureC);
    Serial.print(" °C, 读取时间: ");
    Serial.print(duration, 3);
    Serial.println(" 毫秒");

    // 延时1毫秒
    delay(1);
  }

  // 遍历第二个IO口的传感器进行测量
  for (int i = 0; i < deviceCount2; i++) {
    // 记录开始时间
    unsigned long startTime = micros();

    // 请求温度转换
    sensors2.requestTemperaturesByIndex(i);

    // 获取温度(以摄氏度为单位)
    float temperatureC = sensors2.getTempCByIndex(i);

    // 记录结束时间
    unsigned long endTime = micros();

    // 计算读取温度所花费的时间(以微秒为单位)
    float duration = (endTime - startTime) / 1000.0;

    // 打印温度和读取时间到串行监视器
    Serial.print("IO口2 传感器 ");
    Serial.print(i);
    Serial.print(" 温度: ");
    Serial.print(temperatureC);
    Serial.print(" °C, 读取时间: ");
    Serial.print(duration, 3);
    Serial.println(" 毫秒");

    // 延时1毫秒
    delay(1);
  }

  // 等待5秒钟
  delay(5000);
}

The serial port displays the following:

4a30abd6fb9a698a1b9e1ec8f01c98d

image There is always a 20ms delay, and I don't know how to fix it. Seeking help.

uzi18 commented 2 weeks ago

This is because every time you use method with ending "byIndex" DallasTemperature library scans bus for your device So device 0 is always first and second needs more time to communicate, as device 0 respond first and device 1 need to wait. Solution is to scan bus first and store address of every sensor. Than use it later directly not by index. By the way use only one code as base, they are almost similar .

mayjack0312 commented 2 weeks ago

This is because every time you use method with ending "byIndex" DallasTemperature library scans bus for your device So device 0 is always first and second needs more time to communicate, as device 0 respond first and device 1 need to wait. Solution is to scan bus first and store address of every sensor. Than use it later directly not by index. By the way use only one code as base, they are almost similar .

Thank you, I'm trying it hard and feel like this is the final solution I want

uzi18 commented 2 weeks ago

Ok write down here if you found problem with this.