Hi, I am new to the Arduino and looking for some help on an issue. I am trying to control a DC motor and get it to rotate a number of turns then stop. Can this be done and how.
Cheers.
You'll need some method of detecting and counting the number of revolutions, but yes, in theory what you want to do is possible, but you'll need to provide more detail.
Hi, I have an optical encoder and a DC motor. all I am trying to do is get the motor to rotate 4 turns. I have a Arduino mega + motor shield but unsure of the code to use to achieve this. I know the code to turn the motor on and off but how do I use the encoder to tell it to turn 4 times.
Have you connected the encoder and can you get readings from it?
encoder connected and getting a reading used this code to test encoder
it is a basic command to give a low display when encoder is in 0v and to count when in 5v
int cycleCount = 0;
int setPoint = 20;
int rotationSensor =2;
int val = 0; // variable to store the read value
void setup()
{
pinMode(rotationSensor, INPUT);
Serial.begin(9600);
}
void loop()
{
val = digitalRead(rotationSensor); // read the input pin
if(val == HIGH)
{
cycleCount = cycleCount + 1;
Serial.println(cycleCount);
}
if(val == LOW)
{
//cycleCount = cycleCount + 1;
Serial.println("Low");
}
delay(1000);
}
Find out about PID controller and use this to set the motor drive level according to the error
between wanted and actual rotor position? Basically reinvent the servo-motor.
I do not think IamTom needs full PID to solve his problem. He does need proper code to count encoder signals. The code as shown with 'delay(1000)' is not likely to work.
I usually use 'attachInterupt()' to count the rising or falling edges. Polling is also OK if the RPM is not too high.
We need more info about the optical encoder to give more detailed advice.