Hello.
I am very new to this, i have bought a starter kit with a mega2560. My mission is to use it to control a linear stepper motor. The motor is linked to a tray that is used to load printed cartridge's in trays. Once the tray is full it will be detected by a proximity sensor. I want this to signal the linear motor to run forward to move the tray 15mm to the next rack. I also want to use a push button which when pressed will activate the linear actuator to reverse the tray back to the first rack on the tray. I need to learn the code to do this but need advice on which books to buy or videos to watch. Any help would be very appreciated
Hello martinscribens
Welcome to the worldbest Arduino forum ever.
This is a nice project to get started.
Keep it simple and stupid firstly.
Run some tutorials for the hardware selected.
If you are happy with the results of the tutorials you can merge these to your project.
Have a nice day and enjoy coding in C++.
Take a walk in the Arduino IDE and check the example codes in there.
... and upload a drawing of the system to show devices to be used (mega2560, motor(s), tray, positions the tray will be, cartridge(s), proxmity sensor, pushbutton, power supply).
I have learnt some basic sketching. i can move the motor but I am struggling to move it when i signal the PIR. I am using the IF function but it doesn't work. Can anyone tell me what i need to use please
#include <Stepper.h>
#define STEPS 32
int ledPin = 2; // LED on Pin 2 of Arduino
int pirPin = 4; // Input for HC-S501
int pirValue; // Place to store read PIR Value
// initialize the stepper library on pins 8 through 11:
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(pirPin, INPUT);
digitalWrite(ledPin, LOW);
if (ledPin == HIGH){
delay(1000);
Stepper small_stepper(STEPS, 8, 9, 10, 11);
int Steps2Take;
small_stepper.setSpeed(2200); // set the speed at 2200 rpm:
Steps2Take = -2300;
small_stepper.step(Steps2Take);
}
Serial.begin(9600);
}
void loop() {
pirValue = digitalRead(pirPin);
digitalWrite(ledPin, pirValue);
}
Hi, @martinscribens
Welcome to the forum.
What PIR?
Can you please post your code in code tags as described in the instruction above?
Can we please have a circuit diagram?
An image of a hand drawn schematic will be fine, include ALL power supplies, component names and pin labels.
Can you please list ALL your hardware.
I know its a lot, but we need this information to give you good information.
Thanks.. Tom...
![]()
Continuing the discussion from Stepper motor control:
Hello Again
I am using
- 2830 T61 6.7omh 24VDC 0.6A (this is a linear stepper motor to move a plate forwards and backwards)
- 1 servo
- 2 x TCRT500 IR Infrared Reflective Obstacle Avoidance Module
3.1 L298N motor/stepper driver. - button
5.microswitch
I want the first IR to signal to move the servo forwards and backwards.
I want the second IR to signal the stepper motor to move forward (set length)
I want the button when pressed to return the stepper motor plate back to the start. (-set length)
I want the micro switch use a a button to pause (delay) if hit. This will be put a the start area of the plate. I want to use this to stop or delay the motor when hit. I would like to set the return button maximum distance from end to start position. If the return is pressed at half way point it would hit the microswitch which i would like if to end the motor sequence.
Any help with this would be hugely appreciated.
Why did you start a new topic ?
Doing so removes all context of the original topic
Your 2 topics on the same subject have been merged
hello again
i am now using a A4988 driver module which is very good. I have got the motor running when the trt500 ir is touched. I have also got the motor to return to start on a button press but I am still struggling to stop the motor with the overshoot button. I can stop it on the overshoot button and move it away but then it reverts back to its settings to move full distance.
this is my code. Need help with the code area to fully stop the motor when buttonBpin is pressed.
// Include the AccelStepper library:
#include <AccelStepper.h>
// Define pin connections & motor's steps per revolution
#define dirPin 10
#define stepPin 11
#define motorInterfaceType 1
// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
//setup for tcrt500 ir in analogue mode
const int analogPin = A0; // pin that the sensor is attached to
const int ledPin = 2; // pin that the LED is attached to
const int threshold = 300; // an arbitrary threshold level that's in the range of the analog input
//setup for push button reset
const int ledPin2 = 5;
const int buttonApin = 4;
const int buttonBpin = 3;
const int disablePin = 6;
void setup() {
// Set the maximum speed in steps per second:
stepper.setMaxSpeed(1000);
// initialize the LED's pin as an output:
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(buttonApin, INPUT_PULLUP);
pinMode(buttonBpin, INPUT_PULLUP);
//pinMode(disablePin, OUTPUT);
// initialize serial communications:
Serial.begin(9600);
}
void loop(){
// Set the current position to 0:
stepper.setCurrentPosition(0);
// read the value of the potentiometer:
int analogValue = analogRead(analogPin);
// if the analog value is high enough, turn on the LED:
if (analogValue < threshold) {
digitalWrite(ledPin, HIGH);
//move stepper motor 200 steps
while(stepper.currentPosition() != 200)
{
stepper.setSpeed(690);
stepper.runSpeed();
}
delay(2000);
}
//turn off green led and reset motor when no dection
else {
digitalWrite(ledPin, LOW);
stepper.setCurrentPosition(0);
delay(500);
}
delay(1000); // Wait a second
stepper.setCurrentPosition(0);
if (digitalRead(buttonApin) == LOW) // return motor back to start position
{
digitalWrite(ledPin2, HIGH);
while(stepper.currentPosition() != -1000)
{
stepper.setSpeed(-690);
stepper.runSpeed();
if (digitalRead(buttonBpin) == LOW) // Overshoot button
{
digitalWrite(ledPin2, LOW);
stepper.stop(); // doesn't stop the program
stepper.setCurrentPosition(0);
while(stepper.currentPosition() != 50) //If overshot move motor back
{
stepper.setSpeed(690);
stepper.runSpeed();
}
digitalWrite(disablePin, LOW);
stepper.setCurrentPosition(0);
}
}
}
}
Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'
Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination
https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum
Do need help in to trying to control a stepper motor with a overshoot protection.
I am using a Nema17 with a A4988 driver to control a loading plate. The pate holds racks which get loaded. whin 1 rack is full a sensor reads the last load and moves the rack forward for the next rack to be loaded. When finished in what ever position the plate now is I return button is needed to put the plate back to the start position. My idea is to use a return button to power a led, while the led is in high mode move the motor constantly back...when the plate has reached passed the start position it hits and overshoot button which puts the LED in LOW mode, this hopefully stops the continuous rotation of the motor and is the set to move forward to the specific start area. anyone got any ideas on how to do this please?
Hi, @martinscribens
PLEASE READ post #11.
It will make getting any valuable and informative answers to your problem much easier.
Also to make things easier;
Can we please have a circuit diagram?
An image of a hand drawn schematic will be fine, include ALL power supplies, component names and pin labels.
Thanks.. Tom..
![]()
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.