DC Motor speed and directional control

I have a FIT0185 Motor and i have connected it to a LilyPad Arduino USB (ATmega32u4) following the coding and installation instructions on DFrobot for the motor:

Motor detail: (https://wiki.dfrobot.com/12V_DC_Motor_251rpm_w_Encoder__SKU__FIT0186_).

LilyPad PIN details: (https://jpralves.net/post/2016/11/15/arduino.html#mega-2560)

The installation as follows, I have connected the motor label 1 and 2 to a positive and negative terminal on a 12 v battery. Motor label 3 (hall sensor GND) connected to GND on LilyPad and Motor label 4 (hall sensor Vcc) connected to Vcc on LilyPad. Motor label 5 (hall sensor A) connected to PIN 3 (INT 0) on LilyPad. Motor label 6 connected to PIN 2 (INT 1) on LilyPad. The board is powered by a 5v USB cable.

I want the motor to rotate 45 degrees and then back to starting position. Also adjust the speed to 50rpm. However I’m struggling to get the motor to go back and forth or be able to control the speed. And I am not sure what the fault is? Is this because the lily pad does not support the motor? Or am I missing a step or a part? I have used the code sample already given for the motor, changed to suite the layout of the LilyPad. I have done hours of research but no luck. Please note, I’m a beginner in terms of programming.

Any help would be appreciated, hopefully I have made myself clear if I haven’t or if you have any question please ask. Thank you.

The code I have used is as follows:

const byte encoder0pinA = 3;//A pin -> the interrupt pin 3
const byte encoder0pinB = 2;//B pin -> the digital pin 2
byte encoder0PinALast;
int duration;//the number of the pulses
boolean Direction;//the rotation direction

void setup()
Serial.begin(57600);//Initialize the serial port
EncoderInit();//Initialize the module

void loop()
duration = 3000;

void EncoderInit()
Direction = true;//default -> Forward
attachInterrupt(1, wheelSpeed, CHANGE);

void wheelSpeed()
int Lstate = digitalRead(encoder0pinA);
if((encoder0PinALast == LOW) && Lstate==HIGH)
int val = digitalRead(encoder0pinB);
if(val == LOW && Direction)
Direction = false; //Reverse
else if(val == HIGH && !Direction)
Direction = true; //Forward
encoder0PinALast = Lstate;

if(!Direction) duration ;
else duration–;


I see that this topic was resolved in the internal support.

We will close this topic.

1 Like