Welcome! Log In Create A New Profile

Advanced

borrowed (thermistor for toaster reflow) need help

Posted by terramir 
borrowed (thermistor for toaster reflow) need help
March 24, 2015 07:44PM
Nophead's python script that I hacked to get the format I needed i.e. kelvin*10 (so I can convert to float with one decimal point later in the firmware),
The script worked fine with 5V but now I discovered that my hardware i.e. old cell charger is outputting 5.2V this gives me temps that are off, well thought no problem but turns out that both 5.0V and 5.2V spit out the same numbers on the tables as far as I saw with a quick check. I think the rounding is happening too soon in the script to differenciate between those two values.

here is my hacked code works fine for 5V but for 5.2 my toaster is not reaching sufficient temp for proper reflow:
#!/usr/bin/python
#
# Creates a C code lookup table for doing ADC to temperature conversion
# on a microcontroller
# based on: [hydraraptor.blogspot.com]
"""Thermistor Value Lookup Table Generator

Generates lookup to temperature values for use in a microcontroller in C format based on: 
[hydraraptor.blogspot.com]

The main use is for Arduino programs that read data from the circuit board described here:
[make.rrrf.org]

Usage: python createTemperatureLookup.py [options]

Options:
  -h, --help            show this help
  --r0=...          thermistor rating where # is the ohm rating of the thermistor at t0 (eg: 10K = 10000)
  --t0=...          thermistor temp rating where # is the temperature in Celsuis to get r0 (from your datasheet)
  --beta=...            thermistor beta rating. see [reprap.org]
  --r1=...          R1 rating where # is the ohm rating of R1 (eg: 10K = 10000)
  --r2=...          R2 rating where # is the ohm rating of R2 (eg: 10K = 10000)
  --num-temps=...   the number of temperature points to calculate (default: 20)
  --max-adc=...     the max ADC reading to use.  if you use R1, it limits the top value for the thermistor circuit, and thus the possible range of ADC values
"""

from math import *
import sys
import getopt

class Thermistor:
    "Class to do the thermistor maths"
    def __init__(self, r0, t0, beta, r1, r2):
        self.r0 = r0                        # stated resistance, e.g. 10K
        self.t0 = t0 + 273.15               # temperature at stated resistance, e.g. 25C
        self.beta = beta                    # stated beta, e.g. 3500
        self.vadc = 5.20                     # ADC reference
        self.vcc = 5.20                      # supply voltage to potential divider
        self.k = r0 * exp(-beta / self.t0)   # constant part of calculation

        if r1 > 0:
            self.vs = r1 * self.vcc / (r1 + r2) # effective bias voltage
            self.rs = r1 * r2 / (r1 + r2)       # effective bias impedance
        else:
            self.vs = self.vcc                   # effective bias voltage
            self.rs = r2                         # effective bias impedance

    def temp(self,adc):
        "Convert ADC reading into a temperature in Celcius"
        v = adc * self.vadc / 1024          # convert the 10 bit ADC value to a voltage
        r = self.rs * v / (self.vs - v)     # resistance of thermistor
        return (self.beta / log(r / self.k))*10  # temperature /*times 10 in kelvin*/

    def setting(self, t):
        "Convert a temperature into a ADC value"
        r = self.r0 * exp(self.beta * (1 / (t + 273.15) - 1 / self.t0)) # resistance of the thermistor
        v = self.vs * r / (self.rs + r)     # the voltage at the potential divider
        return round(v / self.vadc * 1024)  # the ADC reading

def main(argv):

    r0 = 100000;
    t0 = 25;
    beta = 3987;
    r1 = 0;
    r2 = 4700;
    num_temps = int(65);
    
    try:
        opts, args = getopt.getopt(argv, "h", ["help", "r0=", "t0=", "beta=", "r1=", "r2="])
    except getopt.GetoptError:
        usage()
        sys.exit(2)
        
    for opt, arg in opts:
        if opt in ("-h", "--help"):
            usage()
            sys.exit()
        elif opt == "--r0":
            r0 = int(arg)
        elif opt == "--t0":
            t0 = int(arg)
        elif opt == "--beta":
            beta = int(arg)
        elif opt == "--r1":
            r1 = int(arg)
        elif opt == "--r2":
            r2 = int(arg)

    if r1:
        max_adc = int(1023 * r1 / (r1 + r2));
    else:
        max_adc = 1023
    increment = int(max_adc/(num_temps-1));
            
    t = Thermistor(r0, t0, beta, r1, r2)

    adcs = range(1, max_adc, increment);
#   adcs = [1, 20, 25, 30, 35, 40, 45, 50, 60, 70, 80, 90, 100, 110, 130, 150, 190, 220,  250, 300]
    first = 1

    print "// Thermistor lookup table for RepRap Temperature Sensor Boards (http://make.rrrf.org/ts)"
    print "// Made with createTemperatureLookup.py (http://svn.reprap.org/trunk/reprap/firmware/Arduino/utilities/createTemperatureLookup.py)"
    print "// ./createTemperatureLookup.py --r0=%s --t0=%s --r1=%s --r2=%s --beta=%s --max-adc=%s" % (r0, t0, r1, r2, beta, max_adc)
    print "// r0: %s" % (r0)
    print "// t0: %s" % (t0)
    print "// r1: %s" % (r1)
    print "// r2: %s" % (r2)
    print "// beta: %s" % (beta)
    print "// max adc: %s" % (max_adc)
    print "#define NUMTEMPS %s" % (len(adcs))
    print "short temptable[NUMTEMPS][2] = {"

    counter = 0
    for adc in adcs:
        counter = counter +1
        if counter == len(adcs):
            print "   {%s, %s}" % (adc, int(t.temp(adc)))
        else:
            print "   {%s, %s}," % (adc, int(t.temp(adc)))
    print "};"
    
def usage():
    print __doc__

if __name__ == "__main__":
    main(sys.argv[1:])
help
terramir
Re: borrowed (thermistor for toaster reflow) need help
March 25, 2015 04:29AM
The supply voltage makes no difference if your ADC reference voltage is taken from the same supply as the thermistor resistor. The voltage from the thermistor will be a bit higher but the ADC value won't change because its full scale value will still correspond to the full supply voltage.

Note that I didn't write that script, Zach Smith did. I wrote a more accurate one here: http://hydraraptor.blogspot.co.uk/2012/11/more-accurate-thermistor-tables.html. The supply voltage is not mentioned in that one because all boards I know of use the thermistor supply as the ADC reference, which means they effectively measure resistance ratios rather than voltage.


[www.hydraraptor.blogspot.com]
Re: borrowed (thermistor for toaster reflow) need help
March 25, 2015 06:10AM
The main reason to use table lookup for determining the temperature is if when there is not enough processing power to do it any other way. This may be the case when using Marlin on a 8 bit processor and the processor has lots of other things to do (i.e. driving stepper motors), but that is not true of a toaster oven. You can use this code instead:

const float ABS_ZERO = -273.15;
const float R25 = 100000;       // put your thermistor resistance @ 25C here
const float Beta = 4200;        // put you thermistor beta value here
const float SeriesR = 4700;     // put your thermistor series resistance here
const float Rinf = R25 * exp(-Beta / (25.0 - ABS_ZERO));
const unsigned int adRange = 1023;    // 1023 for a 10-bit ADC, 4095 for 12-bit

float GetTemperature()
{
  unsigned int reading = analogRead(thermistorPin);
  float resistance = reading * SeriesR / (adRange + 1 - reading);
  return (resistance <= Rinf)
        ? 2000.0			// thermistor short circuit, return a high temperature
        : ABS_ZERO + Beta / log(resistance / RInf);
}



Large delta printer [miscsolutions.wordpress.com], E3D tool changer, Robotdigg SCARA printer, Crane Quad and Ormerod

Disclosure: I design Duet electronics and work on RepRapFirmware, [duet3d.com].
Re: borrowed (thermistor for toaster reflow) need help
March 26, 2015 01:59AM
how many clock cycles will this code take because I got buttons to monitor and well most of the time is used watching the buttons
and I'm running this at 8mhz and seriously considering clocking it down to 1mhz or less.
if I can figure out how to find a custom bootloader.
terramir
Re: borrowed (thermistor for toaster reflow) need help
March 26, 2015 04:57AM
To find out how long it takes, why not write a sketch to measure it?

I normally use a tick interrupt to poll keypads, push buttons, rotary encoders etc. because that way I can get a consistent debounce interval no matter what the main program is doing.

I guess you must be planning to run the mcu from a battery if you are planning to clock it at 1MHz.



Large delta printer [miscsolutions.wordpress.com], E3D tool changer, Robotdigg SCARA printer, Crane Quad and Ormerod

Disclosure: I design Duet electronics and work on RepRapFirmware, [duet3d.com].
Re: borrowed (thermistor for toaster reflow) need help
March 26, 2015 01:29PM
actually I will be running it from an adapter in this case, however I want to experiment with power consumption and conservation strategies in the long run.
as for measuring the interval, how would I do that? that is something I never have done.
terramir
Re: borrowed (thermistor for toaster reflow) need help
March 26, 2015 01:33PM
Quote
terramir
as for measuring the interval, how would I do that? that is something I never have done.
terramir

Assuming you have an Arduino to prototype this on, just put the code I supplied in an Arduino sketch, with one call to micros() just after the analogRead call, and another at the end of the computation. Then output the difference in the returned values of the two calls to micros() to the serial monitor.

The analogRead call on a 16MHz 8-bit Arduino takes about 110us.

Edited 1 time(s). Last edit at 03/26/2015 01:36PM by dc42.



Large delta printer [miscsolutions.wordpress.com], E3D tool changer, Robotdigg SCARA printer, Crane Quad and Ormerod

Disclosure: I design Duet electronics and work on RepRapFirmware, [duet3d.com].
Sorry, only registered users may post in this forum.

Click here to login