Ben Dixon
Published © MIT

3D Printer Heat Bed Control Using XinaBox

Dusted off my ancient Prusa i3 3D printer and needed to rebuild the heat bed controller. Got it running quickly using some xChips.

IntermediateFull instructions provided30 minutes81
3D Printer Heat Bed Control Using XinaBox

Things used in this project

Hardware components

CW01
XinaBox CW01
×1
OD01
XinaBox OD01
×1
XinaBox SX02
×1
XinaBox OC10
×1
XinaBox PG01
×1
IP01
XinaBox IP01
×1
XC10
XinaBox XC10
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

HeatBedControl

Arduino
#include <math.h>
#include <xCore.h>
#include <xSX01.h>
#include <xOC03.h>
#include <xOD01.h>

xSX01 SX01;
xOC03 OC03;
xOD01 OD01;

int setTemp = 60;
int led = 13;

double Thermistor(float RawADC) {
  // Inputs ADC Value from Thermistor and outputs Temperature in Celsius
  //  requires: include <math.h>
  // Utilizes the Steinhart-Hart Thermistor Equation:
  //    Temperature in Kelvin = 1 / {A + B[ln(R)] + C[ln(R)]^3}
  //    where A = 0.000824164, B = 0.000202842 and C = 0.000000127
  float Resistance;  double Temp;  // Dual-Purpose variable to save space.
  Resistance = 100000.0 * ((3.3 / RawADC) - 1); // Assuming a 100k Thermistor.  Calculation is actually: Resistance = (1024 /ADC -1) * BalanceResistor
  // For a GND-Thermistor-PullUp--Varef circuit it would be Rtherm=Rpullup/(1024.0/ADC-1)
  Temp = log(Resistance); // Saving the Log(resistance) so not to calculate it 4 times later. // "Temp" means "Temporary" on this line.
  Temp = 1 / (0.000824164 + (0.000202842 * Temp) + (0.000000127 * Temp * Temp * Temp));   // Now it means both "Temporary" and "Temperature"
  Temp = Temp - 273.15;  // Convert Kelvin to Celsius                                         // Now it only means "Temperature"

 
  return Temp;  // Return the Temperature
}

void printDouble(double val, byte precision) {
  // prints val with number of decimal places determine by precision
  // precision is a number from 0 to 6 indicating the desired decimal places
  // example: printDouble(3.1415, 2); // prints 3.14 (two decimal places)
  Serial.print (int(val));  //prints the int part
  OD01.print(int(val));
  OD01.println(" C");
  if ( precision > 0) {
    Serial.print("."); // print the decimal point
    unsigned long frac, mult = 1;
    byte padding = precision - 1;
    while (precision--) mult *= 10;
    if (val >= 0) frac = (val - int(val)) * mult; else frac = (int(val) - val) * mult;
    unsigned long frac1 = frac;
    while (frac1 /= 10) padding--;
    while (padding--) Serial.print("0");
    Serial.print(frac, DEC) ;
  }
}

void setup() {

#ifdef ESP8266
  Wire.pins(2, 14);
  Wire.setClockStretchLimit(15000);
#endif

  Wire.begin();
  Serial.begin(115200);

  SX01.begin();
  OC03.begin();
  OD01.begin();
  OD01.set2X();
}

double temp;

void loop() {
  int state = 0;
  SX01.poll();
  temp = Thermistor(SX01.getVoltage());         // read ADC and convert it to Celsius

  if (temp < (setTemp - 1))
  {
    state = 1;
    OC03.write(HIGH);
  }
  else if (temp > (setTemp + 1))
  {
    state = 0;
    OC03.write(LOW);
  }


  OD01.clear();
  Serial.print("STATUS: ");

  if (state == 1)
  {
    Serial.println("HEATING");
    OD01.println("HEATING");
  }
  else if (state == 0)
  {
    Serial.println("OFF");
    OD01.println("OFF");
  }

  Serial.print("Celsius: "); printDouble(temp, 3);    // display Celsius
  Serial.println("");
  Serial.println("");
  delay(1000);
}

Credits

Ben Dixon

Ben Dixon

2 projects • 1 follower
I am an electronic engineer with a true love for the art of electronics. I live for anything technology. Yes, I'm a geek just like you!

Comments