I apologize for any base concepts or obvious errors I may have made in my wiring/coding, I did as much research as I could. This is my first project that I am making that isn't just the Arduino starter kit tutorial and I am very much a beginner in this domain.
The parts in the project are as follows:
- 1 Arduino Uno (Rev3) (900mA capacity if powered by power adapter)
- 2 28BYJ-48 Stepper motors (200mA at full step)
- 1 ULN2003 driver board
- 2 DS18B20 waterproof temperature probes (1mA)
- 1 4.7K resistor
- 1 12VDC 2A power adapter
The goal of this project is to automate my root cellar ventilation by opening and closing valves based on the inside and outside temperature . I would also like to eventually add an LCD readout, as such I want to keep some digital pins open for future expansion.
I saw after some research that the data cables from the probes can go to the same pin because they have unique 64 bit serial code that can be recognized. I will run another code while everything is plugged to recognize the probe order before uploading the final code.
I also found information pointing towards being able to run multiple 28BYJ-48 steppers on the same ULN2003 driver, but I couldn't find any examples of people actually doing it. Some people ran the steppers directly from the board and others said it would fry the board.
Since each motor is 200mA and the probes are 1mA it should total to about 402mA which is far less than 900mA. Heating problems from using a 12V adapter could also be mitigated since I only want the motors to move at 5 minute intervals for a quarter turn.
Some parts are still missing for me to physically build it, but I wanted to pass this by people more familiar with such projects before risking burning the board.
Here is the code I wrote based on a few projects I saw online and testing. There is only one driver for both stepper motors which is why there is only one set of outputs for the steppers.
#include <OneWire.h>
#include <DallasTemperature.h>
#include <AccelStepper.h>
#define motorPin1 2 // IN1 on the ULN2003 driver
#define motorPin2 3 // IN2 on the ULN2003 driver
#define motorPin3 4 // IN3 on the ULN2003 driver
#define motorPin4 5 // IN4 on the ULN2003 driver
#define MotorInterfaceType 4 // set the motors to FULL steps
#define ONE_WIRE_BUS 6 //sets the temperature bus to be in digital pin 6
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices
DallasTemperature sensors(&oneWire); // Pass oneWire reference to Dallas Temperature.
float inC=0; //creates inC variable
float outC=0;//creates outC variable
float MaxC=4;
float MinC=0;//creates variables for max temperature and minimum temperature tolerated for easy change
unsigned long previousMillis = 0; // create time variable previousMillis
const long interval = 300000; //5 minute interval
AccelStepper Ventstep = AccelStepper(MotorInterfaceType, motorPin1, motorPin3, motorPin2, motorPin4); //set up rotation order for steppers
void setup() {
Ventstep.setMaxSpeed(1000); //set max speed
Ventstep.setCurrentPosition(0); //define current position as 0
Serial.begin(9600); // open serial port
sensors.begin();
// Start up the temperature sensors
}
void loop() {
unsigned long currentMillis = millis(); //make a variable which equates to the amount of time passed
if (currentMillis - previousMillis >= interval) {// start code when 5 minute interval has passed
previousMillis = currentMillis; //set previousMillis to currentMillis for the next cycle
sensors.requestTemperatures();
inC=sensors.getTempCByIndex(0);//uses the libraries to read the sensor and save the reading as inC variable (0) refers to first sensor on the pin assumed to be inside for now
outC=sensors.getTempCByIndex(1);//uses the libraries to read the sensor and save the reading as outC variable (1) refers to second sensor on the pin assumed to be outside for now
Serial.print("Cold room: "); //sends (Cold room: ) as text to the serial port
Serial.print(inC);//sends (inC) value to serial port
Serial.print("°C ");//sends (°C) to serial port
Serial.print("Outside: "); //sends (Outide: ) as text to the serial port
Serial.print(inC);//sends (inC) value to serial port
Serial.println("°C ");//sends (°C) to serial port
Serial.print ("Vent state: "); //sends (Vent state: ) as text to the serial port
if (Ventstep.currentPosition() == 0){ // checks step of Ventstep and send back text to serial port accordingly
Serial.println ("Closed");}
else if(Ventstep.currentPosition() == 509){
Serial.println ("Open");}
else if(Ventstep.currentPosition() != 509 && Ventstep.currentPosition() != 0){
Serial.println ("PosErr");}
if(inC >= MinC && inC <= MaxC && outC >= MinC && outC <= MaxC ){//opens valve 90 degrees if Inside and outside temperatures are within margins
while (Ventstep.currentPosition() != 509) {
Ventstep.setSpeed(-500);
Ventstep.runSpeed();}
}
else if(inC >= MinC && inC <= MaxC && outC <= MinC){//closes valve if inside temperature is within margins but outside is cold
while (Ventstep.currentPosition() != 0) {
Ventstep.setSpeed(500);
Ventstep.runSpeed();}
}
else if(inC >= MinC && inC <= MaxC && outC >= MaxC){//closes valve if inside temperature is within margins but outside is hot
while (Ventstep.currentPosition() != 0) {
Ventstep.setSpeed(500);
Ventstep.runSpeed();}
}
else if (inC <= MinC && outC <= MinC){//closes valve if inside and outside temperatures are cold
while (Ventstep.currentPosition() != 0) {
Ventstep.setSpeed(500);
Ventstep.runSpeed();}
}
else if (inC <= MinC && outC > MinC){//opens valve if inside temperature is cold but outside is above minimum
while (Ventstep.currentPosition() != 509) {
Ventstep.setSpeed(-500);
Ventstep.runSpeed();}
}
else if (inC >= MaxC && outC >= MaxC){//closes valve if inside and outside temperatures are hot
while (Ventstep.currentPosition() != 0) {
Ventstep.setSpeed(500);
Ventstep.runSpeed();}
}
else if (inC >= MaxC && outC < MaxC){//opens valve if inside temperature is hot but outside lower than maximum
while (Ventstep.currentPosition() != 509) {
Ventstep.setSpeed(-500);
Ventstep.runSpeed();}
}
}
}
I will add two analog inputs to reset Ventstep.currentPosition either to 0 or 509 when after a power outage.
I wanted to post the wiring diagram I drew in Fritzing but as I am a new user, the forums won't let me.
I know I am posting a lot of information, but I want to know if I am making any major beginner errors.
Edit: i had put 2 ULN2003 drivers, but I am only using 1