Encoder Library - Motor Doesn't Stop?

Hi All,

I am using the motor encoder library found here on an arduino uno using this dc motor and this motor driver. Currently the motor turns but does not stop. I am following the basic example included in the github for the Encoder library, what am I missing?

/* Encoder Library - Basic Example
 * http://www.pjrc.com/teensy/td_libs_Encoder.html
 *
 * This example code is in the public domain.
 */

#include <Encoder.h>
#include "DualTB9051FTGMotorShield.h"
DualTB9051FTGMotorShield md;
volatile long newPosition;
volatile long oldPosition;
boolean runOnce = true;
// Change these two numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability
Encoder myEnc(3, 2);
//   avoid using pins with LEDs attached

void setup() {
  Serial.begin(115200);
  Serial.println("Basic Encoder Test:");
  md.init(); // Initialize the motor driver
  md.enableDrivers();
}




void loop() {
  newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
  }
    if(runOnce){
      turn(1500);
    runOnce = false;
  }
}

void turn(int number) {
  if (newPosition <= number) {
    md.setM1Speed(100);
  } else md.setM1Speed(0);
}

What are the results of the examples that come with the Pololu motor driver shield?

The results of running the demo sketch for the driver board are as expected. The motor turns one direction ramping up to full speed and back to zero before changing direction and ramping to full speed and going back to zero. It outputs the current for the motor in the serial monitor like so:

Dual TB9051FTG Motor Shield
M1 current: 70
M1 current: 450
M1 current: 70
M1 current: 0
M1 current: 70
M1 current: 410
M1 current: 70
M1 current: 0
M2 current: 0
M2 current: 0
M2 current: 0
M2 current: 0
M2 current: 0
M2 current: 0
M2 current: 0
M2 current: 0
M1 current: 80
M1 current: 410
M1 current: 70
M1 current: 0
M1 current: 90
M1 current: 390
M1 current: 50
M1 current: 0
M2 current: 0
M2 current: 0
M2 current: 0
M2 current: 0
M2 current: 0
M2 current: 0
M2 current: 0
M2 current: 0
M1 current: 90
M1 current: 300
M1 current: 70
M1 current: 0
M1 current: 60
M1 current: 320
M1 current: 40

I do not think my problem is a motor driver or motor problem (but I am open to suggestions!) as I have gotten the motor to run and stop without using a library. I believe the issue is in my code structure but I don’t know where. Any thoughts?

I would start with the example code that works and see how it is different from your code that does not. I don't have that library installed...

The challenge is the basic example code provided with the library doesn’t actually do anything. There is no functionality built into to preform any actions. I have added in what I think is the minimum amount of code to get it counting ticks for the dc motor I have and it does do that. The problem seems to be that the code never enters this else condition and I don’t know why:

void turn(int number) {
  if (newPosition <= number) {
    md.setM1Speed(100);
  } else md.setM1Speed(0);
}

OK, I figured out the problem with why the motor wasn't stopping:

    if(runOnce){
      turn(1500);
    runOnce = false;
  }

I needed to not include the runOnce code:

      turn(1500);

It did reveal another issue though. I am telling the encoder to go to 1500 "clicks" and it does overrun a bunch (it stops at around 1575) but that is not the part I am worried about. If I examine the output in the serial monitor I can see that there actually only 1352 lines outputted and of those lines a great number of them are repeats:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
90
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
325
326
327
328
329
330
331
333
334
335
336
337
338
339
341
342
343
344
345
346
347
349
350
351
352
353
354
355
357
358
359
360
361
362
363
365
366
367
368
369
370
371
373
374
375
376
377
378
379
381
382
383
384
385
386
387
389
390
391
392
393
394
395
397
398
399
400
401
402
403
405
406
407
408
409
410
411
413
414
415
416
417
418
419
421
422
423
424
425
427
428
429
430
431
432
433
435
436
437
438
439
441
442
443
444
445
446
447
449
450
451
452
453
454
455
457
458
459
460
461
462
463
465
466
467
468
469
470
471
473
474
475
476
477
479
480
481
482
483
484
486
487
488
489
490
491
493
494
495
496
497
498
499
501
502
503
504
505
507
508
509
510
511
513
514
515
516
517
518
519
521
522
523
524
525
526
527
529
530
531
532
533
534
535
537
538
539
540
541
543
544
545
546
547
549
550
551
552
553
554
555
557
558
559
560
561
563
564
565
566
567
568
570
571
572
573
574
575
577
578
579
580
581
582
583
585
586
587
588
589
590
592
593
594
595
596
597
599
600
601
602
603
605
606
607
608
609
610
611
613
614
615
616
617
619
620
621
622
623
625
626
627
628
629
630
631
633
634
635
636
637
639
640
641
642
643
645
646
647
648
649
650
651
653
654
655
656
657
658
659
661
662
663
664
665
667
668
669
670
671
673
674
675
676
677
678
679
681
682
683
684
685
687
688
689
690
691
693
694
695
696
697
698
699
701
702
703
704
705
707
708
709
710
711
713
714
715
716
717
718
719
721
722
723
724
725
727
728
729
730
731
732
733
735
736
737
738
739
741
742
743
744
745
746
748
749
750
751
753
754
755
756
757
758
759
761
762
763
764
765
767
768
769
770
771
773
774
775
776
777
778
779
781
782
783
784
785
786
787
789
790
791
792
793
795
796
797
798
799
801
802
803
804
805
806
807
809
810
811
812
813
815
816
817
818
819
821
822
823
824
825
826
828
829
830
831
833
834
835
836
837
838
839
841
842
843
844
845
846
847
849
850
851
852
853
855
856
857
858
859
861
862
863
864
865
866
867
869
870
871
872
873
875
876
877
878
879
881
882
883
884
885
887
888
889
890
891
893
894
895
896
897
898
899
901
902
903
904
905
906
907
909
910
911
912
913
915
916
917
918
919
921
922
923
924
925
926
928
929
930
931
932
933
935
936
937
938
939
941
942
943
944
945
947
948
949
950
951
953
954
955
956
957
958
959
961
962
963
964
965
966
968
969
970
971
972
973
975
976
977
978
979
981
982
983
984
985
986
988
989
990
991
992
993
995
996
997
998
999
1001
1002
1003
1005
1006
1008
1009
1011
1012
1013
1015
1016
1018
1019
1021
1022
1023
1025
1026
1027
1029
1030
1032
1033
1035
1036
1037
1039
1040
1042
1043
1044
1046
1047
1049
1050
1051
1053
1054
1056
1057
1059
1060
1061
1063
1064
1066
1067
1069
1070
1071
1073
1074
1075
1077
1078
1080
1081
1083
1084
1086
1087
1088
1090
1091
1093
1094
1095
1097
1098
1099
1101
1102
1104
1105
1107
1108
1109
1111
1112
1114
1115
1117
1118
1119
1121
1122
1123
1125
1126
1128
1129
1131
1132
1134
1135
1137
1138
1139
1141
1142
1143
1145
1146
1148
1149
1151
1152
1153
1155
1156
1158
1159
1161
1162
1163
1165
1166
1167
1169
1170
1172
1173
1175
1176
1177
1179
1180
1182
1183
1185
1186
1187
1189
1190
1191
1193
1194
1196
1197
1199
1200
1201
1203
1204
1206
1207
1209
1210
1211
1213
1214
1215
1217
1218
1220
1221
1223
1224
1226
1227
1228
1230
1231
1233
1234
1235
1237
1238
1239
1241
1242
1244
1245
1247
1248
1250
1251
1252
1254
1255
1257
1258
1259
1261
1262
1264
1265
1267
1268
1269
1271
1272
1274
1275
1277
1278
1279
1281
1282
1283
1285
1286
1288
1289
1291
1292
1293
1295
1296
1298
1299
1301
1302
1303
1305
1306
1307
1309
1310
1312
1313
1315
1316
1317
1319
1320
1322
1323
1325
1326
1327
1329
1330
1331
1333
1335
1336
1337
1339
1340
1342
1343
1345
1346
1347
1349
1350
1351
1353
1354
1355
1357
1358
1360
1361
1363
1364
1366
1367
1369
1370
1371
1373
1374
1375
1377
1378
1380
1381
1383
1384
1385
1387
1388
1390
1391
1393
1394
1395
1397
1398
1399
1401
1402
1404
1405
1407
1408
1410
1411
1413
1414
1415
1417
1418
1419
1421
1422
1423
1425
1426
1428
1429
1431
1432
1433
1435
1436
1438
1439
1441
1442
1443
1445
1446
1448
1449
1451
1452
1453
1455
1456
1458
1459
1461
1462
1463
1465
1466
1467
1469
1470
1472
1473
1475
1476
1477
1479
1480
1482
1483
1485
1486
1487
1489
1490
1491
1493
1494
1496
1497
1499
1500
1501
1503
1504
1506
1507
1509
1510
1511
1513
1514
1515
1517
1518
1519
1520
1521
1523
1524
1525
1526
1527
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1574
1573
1574
1575
1574
1573
1574
1575
1574
1573
1574
1575
1574
1573
1574
1575
1574
1573
1574
1575
1574
1573
1574
1575

I need the encoder count to be as accurate as possible. What am I missing?

GorillaDetective:
I need the encoder count to be as accurate as possible. What am I missing?

The part where you post your code so we can see it, along with whatever output you are getting and how it differs from what you are expecting

Here is the updated full code:

/* Encoder Library - Basic Example
 * http://www.pjrc.com/teensy/td_libs_Encoder.html
 *
 * This example code is in the public domain.
 */

#include <Encoder.h>
#include "DualTB9051FTGMotorShield.h"
DualTB9051FTGMotorShield md;
volatile int newPosition = 0;
volatile int oldPosition = 0;
boolean runOnce = true;
// Change these two numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability
Encoder myEnc(3, 2);
//   avoid using pins with LEDs attached

void setup() {
  Serial.begin(115200);
  Serial.println("Basic Encoder Test:");
  md.init(); // Initialize the motor driver
  md.enableDrivers();
}


void loop() {
  newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
  }
      turn(1500);

}

void turn(int number) {
  if (newPosition <= number) {
    md.setM1Speed(100);
  } else{ 
  md.setM1Speed(0);
  }
}

Here is the output (truncated to meet the post character limit):

Basic Encoder Test:
1
2
1
0
1
2
1
0
1
2
1
0
1
2
1
0
1
2
1
0
1
2
1
0
1
2
1
0
1
2
1
0
1
2
1
0
1
2
1
0
1
2
1
0
1
2
1
0
1
2
1
0
1
2
1
0
1
2
1
0
1
2
1
0
1
2
1
0
1
2
1
0
1
2
1
0
1
2
1
0
1
2
1
0
1
2
1
0
1
2
1
0
1
2
1
0
1
2
1
0
1
2
4
5
6
5
4
5
6
5
4
5
6
8
9
10
9
8
9
10
12
13
14
13
12
13
14
13
12
13
14
15
16
17
18
17
16
17
18
19
20
21
22
21
20
21
22
23
24
25
26
25
24
25
26
28
29
30
29
28
29
30
...
1220
1222
1223
1225
1226
1228
1229
1230
1232
1233
1234
1236
1237
1238
1240
1242
1243
1244
1246
1247
1248
1250
1251
1253
1254
1256
1257
1258
1260
1261
1262
1264
1265
1266
1268
1269
1270
1272
1274
1275
1276
1278
1279
1281
1282
1284
1285
1286
1288
1289
1290
1292
1293
1294
1296
1298
1299
1300
1302
1303
1305
1306
1308
1309
1310
1312
1313
1314
1316
1317
1318
1320
1321
1322
1324
1326
1327
1328
1330
1331
1332
1334
1335
1337
1338
1340
1341
1342
1344
1345
1346
1348
1349
1351
1352
1354
1355
1356
1358
1359
1361
1362
1364
1365
1366
1368
1369
1370
1372
1373
1374
1376
1377
1378
1380
1382
1383
1384
1386
1387
1389
1390
1391
1393
1394
1396
1397
1398
1400
1401
1402
1404
1405
1406
1408
1410
1409
1410
1412
1413
1414
1413
1412
1413
1414
1416
1417
1418
1417
1418
1417
1418
1417
1416
1417
1418
1417
1418
1420
1421
1422
1423
1424
1425
1426
1425
1426
1425
1424
1425
1426
1428
1429
1430
1432
1434
1433
1434
1436
1437
1438
1437
1436
1437
1438
1437
1438
1437
1438
1439
1440
1441
1442
1441
1440
1442
1443
1445
1446
1448
1449
1450
1452
1453
1454
1456
1457
1458
1460
1461
1462
1460
1462
1463
1464
1466
1467
1468
1470
1471
1473
1474
1476
1477
1478
1480
1481
1482
1484
1485
1487
1488
1490
1491
1492
1494
1495
1497
1498
1500
1501
1502
1504
1505
1506
1505
1506
1507
1508
1509
1510
1509
1510
1509
1508
1509
1510
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1549
1548
1549
1550
1549
1548
1549
1550
1549
1548
1549
1550
1549
1548
1549
1550
1549
1550
1549
1550
1549
1548
1549
1550

I am looking for the output to be accurate in terms of encoder count. I need to know the encoder is actually counting 1500 ticks (not including any over run) when I tell the motor to turn 1500 ticks. Right now if you look at the output it is double counting and being crazy inaccurate. I am sure there is a way I can clean this up but I don’t know what it is from looking at the github repo for the library. Any help would be appreciated.

I think the biggest issue is that if you may be using the wrong motor for the job. If you want exact movement, a stepper motor is the common choice. You tell it to move exactly x steps and it does so. A DC motor is not as exact. You may be able to make it better by moving the motor slower so it has more time to react to the encoder feedback.

You also don’t need to continuously set the speed of the motor, just turn it on at the start and then turn it off when finished

/* Encoder Library - Basic Example
   http://www.pjrc.com/teensy/td_libs_Encoder.html

   This example code is in the public domain.
*/

#include <Encoder.h>
#include "DualTB9051FTGMotorShield.h"
DualTB9051FTGMotorShield md;
unsigned long int newPosition = 0;
unsigned long int oldPosition = 0;

// Change these two numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability
Encoder myEnc(3, 2);
//   avoid using pins with LEDs attached

void setup() {
  Serial.begin(115200);
  Serial.println("Basic Encoder Test:");
  md.init(); // Initialize the motor driver
  md.enableDrivers();
}


void loop() {

  const unsigned long number = 1500;

  Serial.println("Moving");
  md.setM1Speed(100);
  while ( newPostion < number ) {
    newPosition = myEnc.read();
    if (newPosition != oldPosition) {
      oldPosition = newPosition;
      Serial.println(newPosition);
    }
  }
  md.setM1Speed(0);
  Serial.println("Done");
}

So with some minor tweeks to what you provided works accurately:

/* Encoder Library - Basic Example
   http://www.pjrc.com/teensy/td_libs_Encoder.html

   This example code is in the public domain.
*/

#include <Encoder.h>
#include "DualTB9051FTGMotorShield.h"
DualTB9051FTGMotorShield md;
unsigned long int newPosition = 0;
unsigned long int oldPosition = 0;

// Change these two numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability
Encoder myEnc(3, 2);
//   avoid using pins with LEDs attached

void setup() {
  Serial.begin(115200);
  Serial.println("Basic Encoder Test:");
  md.init(); // Initialize the motor driver
  md.enableDrivers();
}


void loop() {

  const unsigned long number = 1500;

  if ( newPosition < number ) {
      md.setM1Speed(100);
    newPosition = myEnc.read();
    if (newPosition != oldPosition) {
      oldPosition = newPosition;
      Serial.println(newPosition);
    }
  }
  md.setM1Speed(0);
}

There are a couple things here that I’d rather not do if possible but in all my testing I haven’t found a way for it to work. I’d rather not have the motor controls in the main loop and currently the one IF statement turns off the encoder count after “number” reaches its goal so it does not track any motor over run which I would like it to.

There is no need to continuously set the motor speed each time. Basically, you are setting the speed, then turning it off, then repeating… Why?

I am following your example...where you placed the motor controls in loop()...I did move the "md.setM1Speed(100);" line inside the if statement because if you don't do that the motor never turns off...

Sure it does. And then it prints "Done" on the Serial Monitor. Of course, loop() repeats so the entire process starts over again, but it most certainly does turn off. If you only want to do this once as a demonstration, you can always move all this code into setup() or add this at the end of loop

  md.setM1Speed(0);
  Serial.println("Done");
  while(1); // loop forever
}

OK, so it looks like you answered your question of why it is being called over and over again. I am still looking for direction on my original questions:

There are a couple things here that I’d rather not do if possible but in all my testing I haven’t found a way for it to work. I’d rather not have the motor controls in the main loop and currently the one IF statement turns off the encoder count after “number” reaches its goal so it does not track any motor over run which I would like it to.

If you want accurate position control you need a control loop. Turning the motor off is not position control.

If you are happy to just turn the motor off, you might need to compensate for the overrun.

@MarkT

You are absolutely correct. I will be dealing with the overrun once I’m able to accurately count the encoder(without missing steps or duplicating counts) while operating the motor outside of the loop(). Any thoughts on how to accomplish that? All my attempts to control the motor in a separate function while using this library causes the encoder count to miss steps...(see original post). Blh64 was able to show a way to have it count accurately but only when everything is in loop() which isn’t practical unfortunately.

Having it inside loop() has nothing to do with it. I simply put it there to make it smaller and easier.

/* Encoder Library - Basic Example
   http://www.pjrc.com/teensy/td_libs_Encoder.html

   This example code is in the public domain.
*/

#include <Encoder.h>
#include "DualTB9051FTGMotorShield.h"
DualTB9051FTGMotorShield md;

// Change these two numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability
Encoder myEnc(3, 2);
//   avoid using pins with LEDs attached

void setup() {
  Serial.begin(115200);
  Serial.println("Basic Encoder Test:");
  md.init(); // Initialize the motor driver
  md.enableDrivers();
}


void loop() {

  for ( int i = 50; i <= 400; i += 50) {
    Turn( 1500, i);
    delay(1000);
  }
  for ( int i = -50; i >= -400; i -= 50) {
    Turn( 1500, i);
    delay(1000);
  }
}

void Turn(const unsigned long count, const int speed) {
  myEnc.write(0);
  Serial.println("Moving "); Serial.print(count); Serial.print(" steps at speed "); Serial.println(speed);
  md.setM1Speed(speed);
  while ( myEnc.read() < count) {
    // keep looping until done
  }
  md.setM1Speed(0);
  Serial.print("Done with final count of "); Serial.println(myEnc.read());
}

See what this code produces and it may show you how much overrun you are getting vs. speed

Thanks @blh64!

This was a great test case. I ran your code as you posted it and recieved the following output:

Basic Encoder Test:
Moving 
1500 steps at speed 50
Done with final count of 1500
Moving 
1500 steps at speed 100
Done with final count of 1500
Moving 
1500 steps at speed 150
Done with final count of 1500
Moving 
1500 steps at speed 200
Done with final count of 1501
Moving 
1500 steps at speed 250
Done with final count of 1501
Moving 
1500 steps at speed 300
Done with final count of 1502
Moving 
1500 steps at speed 350
Done with final count of 1502
Moving 
1500 steps at speed 400
Done with final count of -1
Moving 
1500 steps at speed -50
Done with final count of -1
Moving 
1500 steps at speed -100
Done with final count of -1
Moving 
1500 steps at speed -150
Done with final count of -1
Moving 
1500 steps at speed -200
Done with final count of -1
Moving 
1500 steps at speed -250
Done with final count of -1
Moving 
1500 steps at speed -300
Done with final count of -1
Moving 
1500 steps at speed -350
Done with final count of -1
Moving 
1500 steps at speed -400
Done with final count of -1
Moving 
1500 steps at speed 50
Done with final count of 1500
Moving 
1500 steps at speed 100
Done with final count of 1501
Moving 
1500 steps at speed 150
Done with final count of 1500
Moving 
1500 steps at speed 200
Done with final count of 1501
Moving 
1500 steps at speed 250
Done with final count of 1501
Moving 
1500 steps at speed 300
Done with final count of 1502
Moving 
1500 steps at speed 350
Done with final count of 1503
Moving 
1500 steps at speed 400
Done with final count of 1503
Moving 
1500 steps at speed -50
Done with final count of -1
Moving 
1500 steps at speed -100
Done with final count of -1
Moving 
1500 steps at speed -150
Done with final count of -1
Moving 
1500 steps at speed -200
Done with final count of -1
Moving 
1500 steps at speed -250
Done with final count of -1
Moving 
1500 steps at speed -300
Done with final count of -1
Moving 
1500 steps at speed -350
Done with final count of -1
Moving 
1500 steps at speed -400
Done with final count of -1
Moving 
1500 steps at speed 50
Done with final count of 1500
Moving 
1500 steps at speed 100
Done with final count of 1501
Moving 
1500 steps at speed 150
Done with final count of 1500
Moving 
1500 steps at speed 200
Done with final count of 1501
Moving 
1500 steps at speed 250
Done with final count of 1501
Moving 
1500 steps at speed 300
Done with final count of 1502
Moving 
1500 steps at speed 350

It looks as if it is fairly accurate when looking at the outputted count but I notice that the motor turns drastically different amounts each time which makes me think the count is not actually accurate which is what I was observing before but maybe it is something else? When I try to output the count for every encoder tick, like in my first example, there is double counting and missing numbers.

Back to reply#8 - wrong motor for the job. If you need exact steps, use a stepper motor. Doing Serial.print() every time an encoder ticks takes a ton of time vs. the time to increment the encoder.