Controlling A Nema 17 With A Temperature Set Point

Hello There

First time poster, i have been lurking around for a couple of months to try and get a sketch working for my particular project. I have read several forums and researched coding but i just cant quite nail this.

Hardware: ATmega32U4 Micro
Nema 17 Stepper Motor
A4988 Stepper Drive
DS18b20 Temp Sensor
LCD 1602 Module

Project: I want to define a personal ideal ambient temperature, for example 25 Deg Cel. I then want to control the stepper motor to "if temp less than ideal ambient temp" rotate anticlock wise 2 steps, then wait say 5 mins then read temp if its higher or still lower than ideal temp rotate 2 steps anticlockwise again or if higher rotate 2 steps clockwise. And so on.

Ive acheived the easy parts with my limited knowledge by using sample codes but cant get the motor to "bounce" off the temperature.

#include <LiquidCrystal.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into port 10 on the Arduino
#define ONE_WIRE_BUS 10

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

int Contrast=120;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int tempPin = 10; //arduino pin used for temperature sensor

int tempRight = 25; // the ideal temperature

int tempC = (sensors.getTempCByIndex(0));

const int stepPin = 7;
const int dirPin = 8;

void setup() {


    // start serial port
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");

  // Start up the library
  sensors.begin();
analogWrite(6,Contrast);

lcd.begin(16,2);

pinMode (7,OUTPUT);
pinMode (8,OUTPUT);
}

void loop() {


lcd.setCursor(3,0);
lcd.print("ROOM  TEMP");

lcd.setCursor(3,1);
lcd.print(sensors.getTempCByIndex(0));

lcd.setCursor(10,1);
lcd.print("Cel");

  // call sensors.requestTemperatures() to issue a global temperature 
  // request to all devices on the bus
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");
  // After we got the temperatures, we can print them here.
  // We use the function ByIndex, and as an example get the temperature from the first sensor only.
  Serial.print("Temperature for the device 1 (index 0) is: ");
  Serial.println(sensors.getTempCByIndex(0)); 
{
   if(tempC <  tempRight)    // if temp is lower than ideal temp
       digitalWrite(dirPin,HIGH);

         for(int x = 0; x < 2; x++) {
         digitalWrite(stepPin,HIGH);
         delayMicroseconds(5000);
         digitalWrite(stepPin,LOW);
         delayMicroseconds(500);
         }
    if(tempC > tempRight) // If temp is higher than ideal temp 

         digitalWrite(dirPin,HIGH);

         for(int x = 0; x < 2; x++) {
         digitalWrite(stepPin,HIGH);
         delayMicroseconds(5000);
         digitalWrite(stepPin,LOW);
         delayMicroseconds(500);
         }}}

The motor just goes in 2 steps in one direction everytime the temp is printed into the serial monitor ignoring the temp set points.

Any help or guidance would be very much appreciated thanks

Hi
Welcome to the forum

This needs to be at the start of the loop, not outside it.

int tempC = (sensors.getTempCByIndex(0));

You need to read the sensor each time your loop.

Tom... :slight_smile:

This is where you declare tempC:

int tempC = (sensors.getTempCByIndex(0));

But you never read it again, so your code bases its behaviour on what the temperature was when it started, not what it is now. You need this in loop somewhere:

tempC = (sensors.getTempCByIndex(0));

Edit: Too slow :slight_smile:

The conditional statements are not formatted properly with curly braces for the correct code blocks to be executed.

With what you have, only the digitalWrite() statement for DIR is conditional on the temperature. The Motor code will be executed every pass through loop().

if (tempC <  tempRight)   // if temp is lower than ideal temp
{ //add curly bracket here to start conditional code block
  digitalWrite(dirPin, HIGH);
  for (int x = 0; x < 2; x++)
  {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(5000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(500);
  }
} //add curly bracket here to end conditional code block

TomGeorge:
Hi
Welcome to the forum

This needs to be at the start of the loop, not outside it.

int tempC = (sensors.getTempCByIndex(0));

You need to read the sensor each time your loop.

Tom... :slight_smile:

Dont i feel like a spud as soon as i put that into the loop bang it worked oh my, i was that close thanks for helping out apreciate it alot.