Closed mayjack0312 closed 2 weeks ago
For example, I want to get the corresponding times in the table below one by one.
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: @.***>
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
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.");
}
}
}
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: @.***>
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"));
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"));
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.
show complete code and point where is your problem ... please show also output from Serial Monitor
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
The only time it succeeded was to display the time it took to read the temperature.
Please paste actual code to review
Please paste actual code to review
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(); // 记录发送读数据命令开始时间
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
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:
There is always a 20ms delay, and I don't know how to fix it. Seeking help.
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 .
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
Ok write down here if you found problem with this.
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?