Hi,
I have DS18B20 temperature sensor hooked up like this
and a bipolar stepper motor and A4988 , 1.8deg/step, 200 steps. Size Nema 17 hooked up like this. dir and step outputs are in pins 12 and 13.
I have downloaded onewire and Accelstepper stepper libraries and placed them in the arduino libraries folder.
The code I'm using to get the temperature reading from the DS18b20 sensor is working and I'm getting correct ambient temperature to show up in the serial monitor window.
Code looks like this:
#include <OneWire.h>
int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2
//Temperature chip i/o
OneWire ds(DS18S20_Pin); // on digital pin 2
void setup(void) {
Serial.begin(9600);
}
void loop(void) {
float temperature = getTemp();
Serial.println(temperature);
delay(100); //just here to slow down the output so it is easier to read
}
float getTemp(){
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;
return TemperatureSum;
I have also been able to get the stepper motor to spin in both directions with varying speed and accleration. The codes that I have tried are as follows:
//simple A4988 connection
//jumper reset and sleep together
//connect VDD to Arduino 3.3v or 5v
//connect GND to Arduino GND (GND near VDD)
//connect 1A and 1B to stepper coil 1
//connect 2A and 2B to stepper coil 2
//connect VMOT to power source (9v battery + term)
//connect GRD to power source (9v battery - term)
int stp = 13; //connect pin 13 to step
int dir = 12; // connect pin 12 to dir
int a = 0; // gen counter
void setup()
{
pinMode(stp, OUTPUT);
pinMode(dir, OUTPUT);
}
void loop()
{
if (a < 200) //sweep 200 step in dir 1
{
a++;
digitalWrite(stp, HIGH);
delay(10);
digitalWrite(stp, LOW);
delay(10);
}
else
{
digitalWrite(dir, HIGH);
a++;
digitalWrite(stp, HIGH);
delay(10);
digitalWrite(stp, LOW);
delay(10);
if (a>400) //sweep 200 in dir 2
{
a = 0;
digitalWrite(dir, LOW);
}
}
}
The other code looks like this:
#include <AccelStepper.h>
AccelStepper Stepper1(1,13,12); //use pin 12 and 13 for dir and step, 1 is the "external driver" mode (A4988)
int dir = 1; //used to switch direction
void setup() {
Stepper1.setMaxSpeed(3000); //set max speed the motor will turn (steps/second)
Stepper1.setAcceleration(13000); //set acceleration (steps/second^2)
}
void loop() {
if(Stepper1.distanceToGo()==0){ //check if motor has already finished his last move
Stepper1.move(1600*dir); //set next movement to 1600 steps (if dir is -1 it will move -1600 -> opposite direction)
dir = dir*(-1); //negate dir to make the next movement go in opposite direction
delay(1000); //wait 1 second
}
Stepper1.run(); //run the stepper. this has to be done over and over again to continously move the stepper
}
Now the fun part is that with my next to none experience in coding I've been trying to get the stepper to rotate one full cycle in a given direction when temperature reaches certain threshold temperature value. Then it should stop, wait for a new temperature and repeat the process.
I have been trying through trial and error to compile a working code by taking pieces of code from the example codes above. I have set certain threshold values where the motor should change direction and it's working, but not quite how I expected. The motor keeps on stepping in one direction till it reaches the given temperature and never stops and it totally ignores the 200 pulses and speed value. I also realized that the motor is stepping as fast as the Ds18B20 is putting out temperature data. So if I set delay(1000) after serial.printIn(temperature) the motor steps 1step/1 sec. There is some sort of conflict here that I can't get my head around. So can someone push me in the right direction so I can quit banging my head on the wall continuously?
Here's the code that I have compiled so far. It's ugly by all standards for sure
#include <OneWire.h>
int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2
int stepPin = 13;
int dirPin = 12;
int temp;
int tempMin = 25;
int tempMax = 25;
//Temperature chip i/o
OneWire ds(DS18S20_Pin); // on digital pin 2
void setup(void) {
Serial.begin(9600);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(DS18S20_Pin, INPUT);
}
void loop(void) {
float temperature = getTemp();
Serial.println(temperature);
delay(1000);
float temp = getTemp(); // get the temperature
if(temp < tempMin) // if temp is lower than minimum temp
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x < 200; x++)
digitalWrite(stepPin,HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin,LOW);
delayMicroseconds(2000);
if(temp > tempMax)
digitalWrite(dirPin,LOW); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x < 200; x++)
digitalWrite(stepPin,HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin,LOW);
delayMicroseconds(2000);
}
float getTemp(){
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;
return TemperatureSum;
}
Thanks,
JP