# DC Motor with encoder issues

**URL:** <https://forum.arduino.cc/t/dc-motor-with-encoder-issues/1211793>\
**Category:** Motors, Mechanics, Power and CNC\
**Created:** [January 16, 2024, 11:06am UTC](https://forum.arduino.cc/t/dc-motor-with-encoder-issues/1211793 "2024-01-16T11:06:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tmcg1702](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@tmcg1702](https://forum.arduino.cc/u/tmcg1702)\
**Post date:** [January 16, 2024, 11:06am UTC](https://forum.arduino.cc/t/dc-motor-with-encoder-issues/1211793/1 "2024-01-16T11:06:14Z")

</div>

Hi there,

I am working on a group project which requires a 12V DC motor with encoder to be controlled by an arduino uno wifi.

Motor: [https://www.robotshop.com/products/555-size-dc-motor-with-encoder-12v-8000-rpm](https://www.robotshop.com/products/555-size-dc-motor-with-encoder-12v-8000-rpm)

Motor shield: [Arduino Motor shield | Electronics For You](https://elektronicavoorjou.nl/en/product/arduino-motor-shield-original/)

The issue is we are all unfamiliar with arduino and no prior expereince working with it. So far we've tried different codes and setups, some of which dont work, others result in a "buzzing" coming from the motor but no rotation. The channel A lights aren't on, but the rest are signifying its recieving power. If anyone can help with the code or setup we'd greatly appreciate it!

Thanks

Thomas

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [January 16, 2024, 11:17am UTC](https://forum.arduino.cc/t/dc-motor-with-encoder-issues/1211793/2 "2024-01-16T11:17:49Z")

</div>

what's the circuit?  
what's the code?  
what's the power supply for the arduino and the motor?

---

<div class="post-metadata">

**Author:** ![zwieblum](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/zwieblum/32/427702_2.png) [@zwieblum](https://forum.arduino.cc/u/zwieblum)\
**Post date:** [January 16, 2024, 11:33am UTC](https://forum.arduino.cc/t/dc-motor-with-encoder-issues/1211793/3 "2024-01-16T11:33:19Z")

</div>

Homework?

---

<div class="post-metadata">

**Author:** ![tmcg1702](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@tmcg1702](https://forum.arduino.cc/u/tmcg1702)\
**Post date:** [January 16, 2024, 11:39am UTC](https://forum.arduino.cc/t/dc-motor-with-encoder-issues/1211793/4 "2024-01-16T11:39:06Z")

</div>

The code:  
// This alternate version of the code does not require  
// atomic.h. Instead, interrupts() and noInterrupts()  
// are used. Please use this code if your  
// platform does not support ATOMIC\_BLOCK.

#define ENCA 2 // YELLOW  
#define ENCB 3 // WHITE  
#define PWM 5  
#define IN2 6  
#define IN1 7

volatile int posi = 0; // specify posi as volatile: [volatile - Arduino Reference](https://www.arduino.cc/reference/en/language/variables/variable-scope-qualifiers/volatile/)  
long prevT = 0;  
float eprev = 0;  
float eintegral = 0;

void setup() {  
Serial.begin(9600);  
pinMode(ENCA,INPUT);  
pinMode(ENCB,INPUT);  
attachInterrupt(digitalPinToInterrupt(ENCA),readEncoder,RISING);

pinMode(PWM,OUTPUT);  
pinMode(IN1,OUTPUT);  
pinMode(IN2,OUTPUT);

Serial.println("target pos");  
}

void loop() {

// set target position  
//int target = 1200;  
int target = 250\*sin(prevT/1e6);

// PID constants  
float kp = 1;  
float kd = 0.025;  
float ki = 0.0;

// time difference  
long currT = micros();  
float deltaT = ((float) (currT - prevT))/( 1.0e6 );  
prevT = currT;

// Read the position  
int pos = 0;  
noInterrupts(); // disable interrupts temporarily while reading  
pos = posi;  
interrupts(); // turn interrupts back on

// error  
int e = pos - target;

// derivative  
float dedt = (e-eprev)/(deltaT);

// integral  
eintegral = eintegral + e\*deltaT;

// control signal  
float u = kp_e + kd_dedt + ki\*eintegral;

// motor power  
float pwr = fabs(u);  
if( pwr \> 255 ){  
pwr = 255;  
}

// motor direction  
int dir = 1;  
if(u\<0){  
dir = -1;  
}

// signal the motor  
setMotor(dir,pwr,PWM,IN1,IN2);

// store previous error  
eprev = e;

Serial.print(target);  
Serial.print(" ");  
Serial.print(pos);  
Serial.println();  
}

void setMotor(int dir, int pwmVal, int pwm, int in1, int in2){  
analogWrite(pwm,pwmVal);  
if(dir == 1){  
digitalWrite(in1,HIGH);  
digitalWrite(in2,LOW);  
}  
else if(dir == -1){  
digitalWrite(in1,LOW);  
digitalWrite(in2,HIGH);  
}  
else{  
digitalWrite(in1,LOW);  
digitalWrite(in2,LOW);  
}  
}

void readEncoder(){  
int b = digitalRead(ENCB);  
if(b \> 0){  
posi++;  
}  
else{  
posi--;  
}  
}

The circuit:

 ![e9971b55-1861-4c3a-95ee-1af332a67a95](https://europe1.discourse-cdn.com/arduino/original/4X/4/2/2/422bb807c3fdc73c3107d46226a1238fc1010f55.jpeg)

Were using 12V external power supply wired to the motor shield.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [January 16, 2024, 12:45pm UTC](https://forum.arduino.cc/t/dc-motor-with-encoder-issues/1211793/5 "2024-01-16T12:45:26Z")

</div>

> [@tmcg1702](#):
>
> The code:

missing the code tags... please edit your post and add them

your motor is rated @ 12V and 0.4A with no load (up to 3.5A)  
what's this small battery ?  
 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/d/6/a/d6ac75b2661325c75a136f0b4d342753b2658f72.png)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 14, 2024, 12:46pm UTC](https://forum.arduino.cc/t/dc-motor-with-encoder-issues/1211793/6 "2024-07-14T12:46:08Z")

</div>

Closed on July 14, 2024, 12:46pm UTC

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