Skip to content

Fix analog thermocouple Boards reading incorrectly. #27884

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

ellensp
Copy link
Contributor

@ellensp ellensp commented May 26, 2025

Description

RainMotorsports on discord noticed that thermistor type -1 (AD595 with Thermocouple) was not working.
They traced it back to #26021 where a float to int conversion was introduced

The macro TEMP_AD595(RAW) is not working correctly.

I generated a quick Arduino test with values for a RAMPS

#define ADC_VREF_MV   5000
#define HAL_ADC_RESOLUTION  10
#define OVERSAMPLENR 16
#define TEMP_SENSOR_AD595_GAIN    1.0
#define TEMP_SENSOR_AD595_OFFSET  0.0

#define _BV(b) (1 << (b))
#define HAL_ADC_RANGE _BV(HAL_ADC_RESOLUTION)

// For a 5V input the AD595 returns a value scaled with 10mV per °C. (Minimum input voltage is 5V.)

#define TEMP_AD595_a(RAW)  ((RAW) * (ADC_VREF_MV / 10) / float(HAL_ADC_RANGE) / (OVERSAMPLENR) * (TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET)
#define TEMP_AD595_b(RAW)  ((RAW) * (ADC_VREF_MV / 10.0) / float(HAL_ADC_RANGE) / (OVERSAMPLENR) * (TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET)

void processTemperature() {
  for (int raw_adc_value = 0; raw_adc_value < (1024 * 8); raw_adc_value += 64) {
    // Calculate the temperature using the TEMP_AD595 macro
    float temperature_a = TEMP_AD595_a(raw_adc_value);
    float temperature_b = TEMP_AD595_b(raw_adc_value);

    // Print the raw ADC value and the calculated temperature
    Serial.print("Raw ADC Value: ");
    Serial.print(raw_adc_value);
    Serial.print(" -> Temperature A: ");
    Serial.print(temperature_a);
    Serial.print(" B: ");
    Serial.print(temperature_b);
    Serial.println(" °C");
    delay(100);
  }
}

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // Wait for Serial to be ready
  }
  Serial.println("AD595 Temperature Sensor Test");
  processTemperature();
}

void loop() {
} 

Temperature A: is the current code
Temperature B: is the fixed code.

Truncated results

AD595 Temperature Sensor Test
Raw ADC Value: 0 -> Temperature A: 0.00 B: 0.00 °C
Raw ADC Value: 64 -> Temperature A: 1.95 B: 1.95 °C
Raw ADC Value: 128 -> Temperature A: -0.09 B: 3.91 °C
Raw ADC Value: 192 -> Temperature A: 1.86 B: 5.86 °C
Raw ADC Value: 256 -> Temperature A: -0.19 B: 7.81 °C
Raw ADC Value: 320 -> Temperature A: 1.77 B: 9.77 °C
Raw ADC Value: 384 -> Temperature A: -0.28 B: 11.72 °C
Raw ADC Value: 448 -> Temperature A: 1.67 B: 13.67 °C
Raw ADC Value: 512 -> Temperature A: -0.37 B: 15.63 °C
Raw ADC Value: 576 -> Temperature A: 1.58 B: 17.58 °C
Raw ADC Value: 640 -> Temperature A: -0.47 B: 19.53 °C
Raw ADC Value: 704 -> Temperature A: 1.48 B: 21.48 °C
Raw ADC Value: 768 -> Temperature A: -0.56 B: 23.44 °C
Raw ADC Value: 832 -> Temperature A: 1.39 B: 25.39 °C
Raw ADC Value: 896 -> Temperature A: -0.66 B: 27.34 °C
Raw ADC Value: 960 -> Temperature A: 1.30 B: 29.30 °C
...

On examination this bug is also in thermistor type -4 (AD8495 with Thermocouple)

Updated both TEMP_AD595 and TEMP_AD8495

Requirements

type -1 or -4 thermistor

Benefits

Works as expected

Related Issues

https://discord.com/channels/461605380783472640/1376429364303892480

@thinkyhead thinkyhead force-pushed the Correct-TEMP_AD595-and-TEMP_AD8495 branch from 65748ee to 5977436 Compare May 27, 2025 02:39
@thinkyhead thinkyhead force-pushed the Correct-TEMP_AD595-and-TEMP_AD8495 branch from ea70e63 to 80fded4 Compare May 27, 2025 03:17
@ellensp
Copy link
Contributor Author

ellensp commented May 27, 2025

Still works as expected in testing function., good to go

@thinkyhead thinkyhead merged commit 88d368a into MarlinFirmware:bugfix-2.1.x May 27, 2025
66 checks passed
@thinkyhead
Copy link
Member

Whew, there sure are a lot of parts and pieces to this project….
Thanks for this very important fix, and the great analysis!

@ellensp ellensp deleted the Correct-TEMP_AD595-and-TEMP_AD8495 branch May 28, 2025 02:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants