Thermocouple / Motor Help

Hello, I have little to no experience with arduinos or electronics and require some help finishing a little project.

Here are the components I am using:

I need help writing that will set a desired temperature for a water flow that the thermocouple will be reading the temperature of. Then code that will use the thermocouple reading to actuate the two motors (one hot water, one cold water) to a degree of movement to open a small valve. The controls do not have to be perfect, I would be ok with a kind of guess and check approach. I mainly need help being able to write code that simultaneously reports temperature and moves the motors by a certain degree.

Ok on what you ultimately want. But where are you starting? Which single component have you tested?

I have working code for the thermocouple, and I also have working code for the two motors. I am able to control how many seconds each motor turns and in what direction. However I am trying to use the encoders to tell the motors to tern a certain degree amount in a certain direction. I am having trouble figuring that part out.

#include "max6675.h"

//Thermocouple outputs and inputs
int thermoDO = 19;
int thermoCS = 20;
int thermoCLK = 21;

const int MA1 =15;
// Motor A input 1
const int MA2 =14;
// Motor A input 2
const int MB1 =17;
// Motor B input 1
const int MB2 =16;
// Motor B input 2

MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);

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

// wait for MAX chip to stabilize
delay(500);

// set pins
pinMode(MA1, OUTPUT);
pinMode(MA2, OUTPUT);
pinMode(MB1, OUTPUT);
pinMode(MB2, OUTPUT);

}

void loop() {

// basic readout test, just print the current temp
Serial.print("C = ");
Serial.println(thermocouple.readCelsius());
Serial.print("F = ");
Serial.println(thermocouple.readFahrenheit());

// For the MAX6675 to update, you must delay AT LEAST 250ms between reads!
delay(1000);

// Turn motor A (forward)
digitalWrite(MA1, HIGH);
digitalWrite(MA2, LOW);

// Turn on Motor B (forward)
digitalWrite(MB1, HIGH);
digitalWrite(MB2, LOW);

delay(2000);
// Run motors for 2 seconds

// Stop both motors
digitalWrite(MA1, LOW);
digitalWrite(MA2, LOW);
digitalWrite(MB1, LOW);
digitalWrite(MB2, LOW);

delay(2000);
// Wait for 5 seconds before repeating

// Turn motor A (reverse)
digitalWrite(MA1, LOW);
digitalWrite(MA2, HIGH);

// Turn on Motor B (reverse)
digitalWrite(MB1, LOW);
digitalWrite(MB2, HIGH);

delay(2000);
// Run motors for 2 seconds

// Stop both motors
digitalWrite(MA1, LOW);
digitalWrite(MA2, LOW);
digitalWrite(MB1, LOW);
digitalWrite(MB2, LOW);

delay(2000);
// Wait for 2 seconds before repeating

}

This is the code I currently have to test that components are working. But I want to be able to use degrees as a increment of motor turning instead of time.

The encoder you linked to seems to have 12 possible positions. How does that relate to how many degrees you turn the motor?

When posting code please use code tags. This makes your code easier to read and follow.

#include "max6675.h"

//Thermocouple outputs and inputs
int thermoDO = 19;
int thermoCS = 20;
int thermoCLK = 21;

const int MA1 =15;
// Motor A input 1
const int MA2 =14;
// Motor A input 2
const int MB1 =17;
// Motor B input 1
const int MB2 =16;
// Motor B input 2

MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);

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

// wait for MAX chip to stabilize
delay(500);

// set pins
pinMode(MA1, OUTPUT);
pinMode(MA2, OUTPUT);
pinMode(MB1, OUTPUT);
pinMode(MB2, OUTPUT);

}

void loop() {

// basic readout test, just print the current temp
Serial.print("C = ");
Serial.println(thermocouple.readCelsius());
Serial.print("F = ");
Serial.println(thermocouple.readFahrenheit());

// For the MAX6675 to update, you must delay AT LEAST 250ms between reads!
delay(1000);

// Turn motor A (forward)
digitalWrite(MA1, HIGH);
digitalWrite(MA2, LOW);

// Turn on Motor B (forward)
digitalWrite(MB1, HIGH);
digitalWrite(MB2, LOW);

delay(2000);
// Run motors for 2 seconds

// Stop both motors
digitalWrite(MA1, LOW);
digitalWrite(MA2, LOW);
digitalWrite(MB1, LOW);
digitalWrite(MB2, LOW);

delay(2000);
// Wait for 5 seconds before repeating

// Turn motor A (reverse)
digitalWrite(MA1, LOW);
digitalWrite(MA2, HIGH);

// Turn on Motor B (reverse)
digitalWrite(MB1, LOW);
digitalWrite(MB2, HIGH);

delay(2000);
// Run motors for 2 seconds

// Stop both motors
digitalWrite(MA1, LOW);
digitalWrite(MA2, LOW);
digitalWrite(MB1, LOW);
digitalWrite(MB2, LOW);

delay(2000);
// Wait for 2 seconds before repeating

}

What exactly are you trying to do? Looks like you want to blend (mix) hot and cold to get a desired temperature. Should that be the case you want a mixing valve, 2 inputs and one output. Like the valve used for example in a shower but one electronically controlled using either current or voltage.

Should this be your goal you need to consider water pressure on both lines because if the pressure changes, even a little, maintaining a stable output gets difficult. Like when you have a shower temperature just right and someone flushes a toilet. Anyway I don't know exactly what your objective is.

Ron

Yes you understand the objective. I'm not really trying to be all too accurate, I'd be willing to accept +- 10 degrees Fahrenheit of the desired temp. However, because the valves we use only turn 90 degrees between open and close, I want to be able to control the motor position using the encoders. Thats the part im having trouble with

I don't know man, thats what I'm asking you. To my understanding, each time one of the magnets in the encoder passes over some sensor it is recorded and therefore the position of the motor shaft is known, I then need to correlate this position of the input shaft to the output shaft motor through the gear ratio, once all this is understand I want to be able to increment the degrees of open and close in each motor.

No it's not known. What is know is there one more pulse being detected. If you know where the motor starts from, you can count the pulses and know it's current position, but I don't see any attempt to know the position the motor starts from.

Yea I understand what you are saying, I need help with writing code that would maybe set the current position when the code starts as 0 then from there keep track. Do you know how this can be done?

Do you not know how to make a named variable with an initial value of zero?

1 Like

I do know how to do that... I don't know how to translate the resolution of my encoder to a degree of the output shaft.

360 divided by number of steps possible.

/* Encoder Library - TwoKnobs Example
 * http://www.pjrc.com/teensy/td_libs_Encoder.html
 *
 * This example code is in the public domain.
 */

#include <Encoder.h>

// Change these pin numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability
Encoder myEnc(2, 3);
//   avoid using pins with LEDs attached

const int MC1 =15;
  // Motor A input 1
const int MC2 =14;
  // Motor A input 2

void setup() {
  Serial.begin(9600);
  Serial.println("TwoKnobs Encoder Test:");
    // set pins
  pinMode(MC1, OUTPUT);
  pinMode(MC2, OUTPUT);
}

long positionLeft  = -999;
long positionRight = -999;

void loop() {
  digitalWrite(MC1, LOW);
  digitalWrite(MC2, HIGH);
  Serial.println(myEnc.read());
  delay(2000);
  
}

This is some code I have been messing around with to try and understand this. However, it only seems to be reading which direction the motor is spinning.
`

What is your gearmotor's gear ratio? The web page you posted shows at least 50 different motors. Which one do you have?
How much torque is required to turn the small valve stems?

It is the 1000:1 version. The exact gear ratio is 986.41:1

Pololu's page shows 4 1000:1 motors, 3 6V and 1 12V. Post the EXACT part number for YOUR motor. Are you sure your motor outputs enough torque to turn the valve stems?
Why not post the valve's datasheet or brand name and part number?

OK, 12 ppr * 986.42 = 11836.92 pulses per output shaft rev. 11836.92 / 360 = 32.8803333333 pulses per degree, to turn the shaft 90° requires 2959.23 (just call it 2960) pulses.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.