Notifications
Clear all

Need help with stepper motor project

31 Posts
5 Users
7 Reactions
3,418 Views
(@pappasteve)
Member
Joined: 3 years ago
Posts: 27
Topic starter  

Hello,

 

My project consists of using the following compnents.

Arduino UNO

20 character,4 row LCD display

Hall effect sensor

Nema 17 stepper motor

DS18B20 temp sensor

A4988 motor driver

 

The project consists of using the temp sensor to move the stepper motor to a couple different positions after it is homed. 

So far I have gotten the temp sensor to display the temp on the LCD screen. Using a if, else if, and else conditional statements, when the temp changes to certain levels, the LCD displays a change.  I am now looking for info on how to make the stepper motor move to a certain position when the specific if and  else statements are true.  

I have gotten the stepper motor to a home position on start up.

I will post the code later, since I'm sure you will probably need to give suggestions.

 

I do have a short you tube video of it working, I will see if I can post it.  

 

 

 

  


   
Quote
(@pappasteve)
Member
Joined: 3 years ago
Posts: 27
Topic starter  

This is the video of it working.  

 

 

I will need to copy the code and post it here. Will be later before I can post it.  


   
ReplyQuote
(@pappasteve)
Member
Joined: 3 years ago
Posts: 27
Topic starter  
Here is the code I'm using.




/ Define connections
#define HALL_SENSOR 3
#define DIR 10
#define STEP 11
// Include the Arduino Stepper Library
#include <Stepper.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
#include<LiquidCrystal_I2C.h>

// Direction Variable
boolean setdir = LOW;
int hallSensorPin = 3;
int hallSensorValue = 0;
int temp = hallSensorValue;
// Number of steps per output rotation
const int stepsPerRevolution = 200;

// Create Instance of Stepper library
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 20 chars and 4 line display

const int SENSOR_PIN = 2; // Arduino pin connected to DS18B20 sensor's DQ pin
OneWire oneWire(SENSOR_PIN); // setup a oneWire instance
DallasTemperature sensors(&oneWire); // pass oneWire to DallasTemperature library

float tempCelsius; // temperature in Celsius
float tempFahrenheit; // temperature in Fahrenheit// Direction Variable

void homefunction() {
// Set motor speed pulse duration

// Move motor until home position reached
while (digitalRead(HALL_SENSOR) == 1) {

digitalWrite(DIR, setdir);
digitalWrite(STEP, HIGH);
delayMicroseconds(4000);
digitalWrite(STEP, LOW);
delayMicroseconds(4000);
}

}

void setup() {


// Setup the stepper controller pins as Outputs
pinMode(DIR, OUTPUT);
pinMode(STEP, OUTPUT);
hallSensorValue = digitalRead(hallSensorPin);


// Setup the Hall Effect switch as an Input
pinMode(HALL_SENSOR, INPUT);

// Home the motor
homefunction();
{
sensors.begin(); // initialize the sensor
lcd.init(); // initialize the lcd
lcd.backlight(); // open the backlight
Serial.begin(9600);
pinMode(hallSensorPin,INPUT);

}

}

void loop()

{
sensors.requestTemperatures(); // send the command to get temperatures
tempCelsius = sensors.getTempCByIndex(0); // read temperature in Celsius
tempFahrenheit = tempCelsius * 9 / 5 + 32; // convert Celsius to Fahrenheit

lcd.clear();
lcd.setCursor(0, 0); // start to print at the second row
lcd.print(" Temp: ");
lcd.print(tempFahrenheit); // print the temperature in Fahrenheit
lcd.print((char)223); // print ° character
lcd.print("F"); // print F

if(tempFahrenheit > 80){
lcd.setCursor(0,2);
lcd.print("Heated Air");
lcd.setCursor(0,1);
lcd.print(" Door Position:");
lcd.print(" A");

}

else if (tempFahrenheit > 72){
lcd.setCursor(0,2);
lcd.print (" 50/50 mix") ;
lcd.setCursor(0,1);
lcd.print(" Door Position:");
lcd.print(" B");

}
else {
lcd.setCursor(0,2);
lcd.print (" Cold Air");
lcd.setCursor(0,1);
lcd.print(" Door Position:");
lcd.print(" C");

}



delay(500);

}

   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2571
 

@pappasteve

First: I find the AccelStepper library easier to use than the one you've selected, take a look at it and see if it fits your needs better.

 

Second: what stepper driver are you using ?

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
(@davee)
Member
Joined: 3 years ago
Posts: 1859
 

Hi @pappasteve,

  I am not familiar with the specific kit you are using, and the last time I programmed a stepper motor was a very long time ago, so apologies if there is something 'clever' I don't know about.

However, in general a stepper motor will move a certain angle (e.g. 5 degrees) for each step, the angle depending upon the internal physical design, and printed on the data sheet. I don't know what your motor is, so I'll pretend it is 5 degrees.

So a 5 degree/step motor will have 360/5 = 72 possible positions.

The first general problem is not knowing which of the 72 positions it is at, when first powered on. You may have sorted that, assuming it starts by driving round to same position each time.

Then, to go to another position

  1. Work out how many steps, and in which direction to go from present angle to the new desired angle ... e.g , 8 steps clockwise (for 8 X 5 = 40 degrees)
  2. Send the required sequence to the motor ... essentially similar to your homing command, but with a 'for' loop for a fixed number of steps.

...

The above assumes you only want to go to one specific place. By having two (or more) different sets of move sequence, you should be able to put some 'if' wrappers to pick the right one. You may want to remember where you are, so that a second move will be from the 1st 'destination', not the power on 'Home' position.

..........

And as a coding hint, if you find yourself writing the same sequence of instructions twice, with different data, consider putting the sequence in a function, and calling with the appropriate data each time. Looking at the code, about 5 lines of 'lcd.setCursor' through to 'lcd.print' might be a candidate.

And a new function 'doAStep(direction)', which sweeps up the directWrites and delays.

Then maybe a second new funtion 'doNsteps (direction, N)' which calls doAStep N times.

After that, you might just need an 'if' or two, calling up doNsteps(), as appropriate.

---------------

Hope that helps. Just do a small bit at a time, and build up to the final 'system'.

Good luck,

Dave


   
ReplyQuote
(@pappasteve)
Member
Joined: 3 years ago
Posts: 27
Topic starter  

@will

 

It is a  A4988.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2571
 

@pappasteve

 

OK, thanks.

Declare a new value "long location" in your program.  After you home your stepper, set this value to zero.

Use DaveE' suggestion to add functions "doAStep( and "doNSteps" to your program and add the line

location = (direction>0) ? location+1 : location-1;

into the addAStep function. This will increment your location or decrement your location according to the direction you stepped.

You can now calculate your (angular) position as 360*location/(steps per revolution).

To calculate how far (and in which direction) you need to move, you can calculate the new position as:

360/(desired angle)/(steps per revolution) (or 0 for new angle==0 :))

Then all you need to do is move from (current) position to (new) position by moving abs(old-new) in the direction of new-old.

 

Hope that helps.

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2571
 
Posted by: @will

@pappasteve

360/(desired angle)/(steps per revolution) (or 0 for new angle==0 :))

Ooops, typing too fast

new position = (new angle)*(steps per rev)/360;

Sorry about that.

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2187
 

   
ReplyQuote
(@davee)
Member
Joined: 3 years ago
Posts: 1859
 

Hi @pappasteve,

  One other point. The framework of two empty functions 'setup' and 'loop' that you are required to 'populate' with this design system can be a bit confusing when trying to get something to work the first time.

Obviously, the idea is that 'setup' gets the initialisation work, whilst 'loop', is continually being called, assuming the code in loop executes in a 'short' time.

You might wish to get your stepper motor to perform a 'dance' e.g.

  1. move to home position
  2. wait 5 seconds
  3. move to position 1 ... say 20 steps clockwise
  4. wait 5 seconds
  5. move to position 2 .. say 30 steps anticlockwise

just once .. by putting all of the code (or better still, calls to separate functions) in 'setup', so the dance only occurs once.

Then when you have figured out how to make it 'dance', move the required 'dance moves' into 'loop', with 'if wrappers', so that the motor only moves when a particular condition is true.

Hope that helps. Dave


   
ReplyQuote
(@pappasteve)
Member
Joined: 3 years ago
Posts: 27
Topic starter  

@robotbuilder

 

I am very new to this,  been working on it maybe 3 weeks now.  Some of the code I have written,  other parts I have copied from other postings on the internet and then modified to work in my application.  I'm sure it is not maybe the proper way to do it,  but at my current skill level. It is all I have.  Hence the reason to come here and ask for help.   Absolutely,  I don't understand the code completely,  but I am understanding more as I go.

I will post my specific intentions for the project later.  At work right now.  I just want to be clear on what I amv trying to do.


   
ReplyQuote
(@pappasteve)
Member
Joined: 3 years ago
Posts: 27
Topic starter  

I do appreciate all the responses and suggestions. 


   
DaveE reacted
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2571
 

@pappasteve

 

Don't get frustrated, we all started the same way and had the same problems you did 🙂

Anything seems possible when you don't know what you're talking about.


   
MadMisha, LydaRA and DaveE reacted
ReplyQuote
(@davee)
Member
Joined: 3 years ago
Posts: 1859
 

Hi @pappasteve,

  There isn't a single right way of coding ... thanks to YouTube, etc. you can watch/join in how to program in some language, which can explain various points, but eventually you just have to dive in, and try to figure it out yourself. Then when something doesn't click, look around and ask questions. And by all means try taking examples, and fiddling with them to understand how they work, etc.

------------------

In my opinion, systems like Arduino are a double edged sword. They provide a tremendous amount of help and support, so that it is possible for someone with very limited experience to put some very clever and capable stuff  together in next to no time ... and if that works in your situation, that is absolutely fine.

The other edge of that sword, is that if you want to something 'a bit different' from the 'standard' for that system, or it doesn't play nicely, it suddenly gets a lot harder as you have to dig through the 'cloaks' that made the standard stuff look easy. It is like the iceberg, with 90% hidden from view.

This is when you need a little grit, patience and 'I am going to beat it' attitude .. and you can beat it .. because like the iceberg, the stuff you didn't see at first is made of the same stuff as the bit you could see .. just lots more of it!

You are currently doing at least some of things you need to do, which is a great start!!! From your video, you have put your project hardware together, and I presume it is moving to the 'home' position. That is a real achievement ... it is much harder when you connect everything together and it just sits there ... or worse a small component expires in a puff of smoke!!

So take pride  in your achievement so far. The next move is to gradually make it do MORE things... just try adding one at a time.

-------------------

A suggestion ... maybe you need to ask another question or two .. When you have time, read through the story so far. If you see a suggestion above that you think you can understand, maybe give it a try. Then when you get stuck, try to formulate a specific question.

Something like "I have a system that does xx. How do I make it do xxx? I tried yyy, with this code zzz, but it goes round in circles when I wanted it to move by only 200 degrees?"

Try to make your 'xx' to 'xxx' just a small step, so the answer is likely to be fairly simple and easier to understand.

I look forward to seeing your next post!!

Best wishes, Dave


   
MadMisha and LydaRA reacted
ReplyQuote
(@pappasteve)
Member
Joined: 3 years ago
Posts: 27
Topic starter  

Thanks, I'm not frustrated,  just know I need help. 


   
ReplyQuote
Page 1 / 3