Encoder DC motor

Hi all,

I'm trying to program a DC motor and I want to make it stop at a cirtain point (for example at a PWM value).

Can someone help me with this?

Kind regards,


Can you be more specific, please?

I have this motor: GB37Y3530
I want to let the motor spin untill a certain point (for example that the motor as spins 6 times), but i don't know how.

Hi, @jasperdek26
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

This will help with advice on how to present your code and problems.

What is your project?
Is this your motor/encoder?
FIT0186_Web.pdf (222.6 KB)


Tom.. :grinning: :+1: :coffee: :australia:

Hi @TomGeorge ,

That's the motor!
For my project, I need to simulate the working of a lineair actuator. If I push on the button on my breadboard, the motor as going to sping from his original position, to a certain point. When it arrived at that location, a Green LED will shine. If the button is pressed again, the motor as will spin back to it's original position and a green LED will shine. If the button is pressed during the spinning of the motor, the motor as will go back to it's position where it begon. This is my first project with a microcontroller in a while (and programming with C for the first time), so I'm kinda new to this.

Kind regards,


Which Arduino? Which motor driver? What kind of power supply (voltage and current)? How do you turn rotary motion into linear motion? Post a wiring diagram and your code.

Arduino Uno, driver L298N, power suply 12V. I only do rotary motion, because it's a test situation. Currently I have this:

int In1 = 8;
int In2 = 3;
int switch1 = 12;
int PushSwitch = 7;
int EncoderA = 2;
int EncoderB = 4;
int PWMA = 5;
int greenLED = 10;
int redLED = 11;

int state = HIGH;
int buttonNew;
int buttonPrevious = LOW;
int pos = 0;
int poss = 0;

void setup() {
// put your setup code here, to run once:
pinMode(EncoderA, INPUT);
pinMode(EncoderB, INPUT);
pinMode(PWMA, OUTPUT);
pinMode(In1, OUTPUT);
pinMode(In2, OUTPUT);
pinMode(switch1, INPUT);
pinMode(PushSwitch, INPUT);

void loop() {
buttonNew = digitalRead(PushSwitch);
if (buttonNew == HIGH && buttonPrevious == LOW){
if (state == HIGH){
analogWrite(5, 100);
digitalWrite(In1, HIGH);
digitalWrite(In2, LOW);
delay (10);
state = LOW;
buttonPrevious == buttonNew;
if (buttonNew == HIGH && buttonPrevious == HIGH){
analogWrite(5, 100);
digitalWrite(In1, LOW);
digitalWrite(In2, HIGH);
if (poss==5000){
digitalWrite(In1, LOW);
digitalWrite(In2, LOW);



void readEncoder(){
int b = digitalRead(EncoderB);
return poss;

Didn't the compiler object to that?

Please remember to use code tags when posting code

state = LOW;
buttonPrevious == buttonNew;



Returning a value from a void function?

Oh that

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.