View: 4516|Reply: 2

Control DC motor with motor shield

[Copy link]

1

threads

2

posts

17

credits

Novice

Rank: 1

credits
17
Published in 2018-4-12 17:36:37 | Show all floors |Read mode
I have an OrangePi Lite, and a DK Electronics Motor shield(https://cdn-learn.adafruit.com/d ... it-motor-shield.pdf).
I use C# .NET Core, on my Orange Pi, with Armbian operation system, and WiringPi library to handle GPIO ports. I tried several examples from the internet like this: http://blog.janlipovsky.cz/2016/ ... y-pi-part2.html?m=1
but it was not success, I can't control my DC motor. I followed the cable connections between OrangePi and Motor Shield from the previous example, the green led lights on the shield, I translated his python source code to C#, but the motor do nothing. I tried to give direct voltage for motor shield, and tried the OrangePi power too, I switched the modes with PWR jumper.
I tested the motor directly with 5 voltage, that works. I used the gpio with leds, that works too.
I will very thankful for every help(links, C# code examples, others)

Thank you!

0

threads

5

posts

38

credits

Novice

Rank: 1

credits
38
Published in 2018-4-15 13:15:38 | Show all floors
SInce its an arduino shield, have you tried just porting the arduino code to your opi?  change the code or pin assignments to match your setup and use wiringOPi.  Got all this from: https://www.raspberrypi.org/forums/viewtopic.php?t=16118

  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <string.h>

  4. #include <wiringPi.h>
  5. #include <softPwm.h>

  6. typedef unsigned char uint8_t;

  7. #define _BV(bit) (1 << (bit))

  8. #define MOTORLATCH  0
  9. #define MOTORCLK    1
  10. #define MOTORENABLE 2
  11. #define MOTORDATA   3
  12. #define MOTOR_1_PWM 4
  13. #define MOTOR_2_PWM 5

  14. #define MOTOR1_A 2
  15. #define MOTOR1_B 3
  16. #define MOTOR2_A 1
  17. #define MOTOR2_B 4
  18. #define MOTOR4_A 0
  19. #define MOTOR4_B 6
  20. #define MOTOR3_A 5
  21. #define MOTOR3_B 7

  22. #define FORWARD  1
  23. #define BACKWARD 2
  24. #define BRAKE    3
  25. #define RELEASE  4

  26. #define RANGE           100
  27. #define        DC_MOTORS  4

  28. static unsigned char latch_state;

  29. void latch_tx(void)
  30. {
  31.    unsigned char i;

  32.    digitalWrite(MOTORLATCH, LOW);

  33.    digitalWrite(MOTORDATA, LOW);

  34.    for (i=0; i<8; i++)
  35.    {
  36.       digitalWrite(MOTORCLK, LOW);

  37.       if (latch_state & _BV(7-i))
  38.       {
  39.          digitalWrite(MOTORDATA, HIGH);
  40.       }
  41.       else
  42.       {
  43.          digitalWrite(MOTORDATA, LOW);
  44.       }
  45.       digitalWrite(MOTORCLK, HIGH);
  46.    }
  47.    digitalWrite(MOTORLATCH, HIGH);
  48. }


  49. void enable(void)
  50. {
  51.    pinMode(MOTORLATCH,  OUTPUT);
  52.    pinMode(MOTORENABLE, OUTPUT);
  53.    pinMode(MOTORDATA,   OUTPUT);
  54.    pinMode(MOTORCLK,    OUTPUT);

  55.    latch_state = 0;

  56.    latch_tx();

  57.    digitalWrite(MOTORENABLE, LOW);
  58. }

  59. typedef struct motorInf_t
  60. {
  61.    uint8_t pwmfreq;
  62. };

  63. motorInt_t motorInf[DC_MOTORS];

  64. void DCMotorInit(uint8_t num, uint8_t freq)
  65. {
  66.    motorInf[num].pwmfreq = freq;

  67.    enable();

  68.    switch (num)
  69.    {
  70.       case 1:
  71.          latch_state &= ~_BV(MOTOR1_A) & ~_BV(MOTOR1_B);
  72.          latch_tx();
  73.          initPWM(1, freq);
  74.          break;
  75.       case 2:
  76.          latch_state &= ~_BV(MOTOR2_A) & ~_BV(MOTOR2_B);
  77.          latch_tx();
  78.          initPWM(2, freq);
  79.          break;
  80.       case 3:
  81.          latch_state &= ~_BV(MOTOR3_A) & ~_BV(MOTOR3_B);
  82.          latch_tx();
  83.          initPWM(3, freq);
  84.          break;
  85.       case 4:
  86.          latch_state &= ~_BV(MOTOR4_A) & ~_BV(MOTOR4_B);
  87.          latch_tx();
  88.          initPWM(4, freq);
  89.          break;
  90.    }
  91. }

  92. int motorMap [] = {MOTOR_1_PWM, MOTOR_2_PWM};

  93. void setPWM(uint8_t num, int freq)
  94. {
  95.    softPwmWrite (motorMap [num], freq) ;
  96. }

  97. void initPWM(uint8_t num, int freq)
  98. {
  99.    softPwmCreate (motorMap [num], 0, RANGE) ;
  100.    setPWM(num, freq);
  101. }

  102. void DCMotorRun(uint8_t motornum, uint8_t cmd)
  103. {
  104.    uint8_t a, b;

  105.    switch (motornum)
  106.    {
  107.       case 1:
  108.          a = MOTOR1_A; b = MOTOR1_B;
  109.          break;
  110.       case 2:
  111.          a = MOTOR2_A; b = MOTOR2_B;
  112.          break;
  113.       case 3:
  114.          a = MOTOR3_A; b = MOTOR3_B;
  115.          break;
  116.       case 4:
  117.          a = MOTOR4_A; b = MOTOR4_B;
  118.          break;
  119.       default:
  120.          return;
  121.    }
  122.   
  123.    switch (cmd)
  124.    {
  125.       case FORWARD:
  126.          latch_state |= _BV(a);
  127.          latch_state &= ~_BV(b);
  128.          latch_tx();
  129.          break;
  130.       case BACKWARD:
  131.          latch_state &= ~_BV(a);
  132.          latch_state |= _BV(b);
  133.          latch_tx();
  134.          break;
  135.       case RELEASE:
  136.          latch_state &= ~_BV(a);
  137.          latch_state &= ~_BV(b);
  138.          latch_tx();
  139.        break;
  140.    }
  141. }

  142. void DCMotorSetSpeed(uint8_t motornum, int speed)
  143. {
  144.    switch (motornum)
  145.    {
  146.       case 1:
  147.          setPWM(1, speed);
  148.          break;
  149.       case 2:
  150.          setPWM(2, speed);
  151.          break;
  152.       case 3:
  153.          setPWM(3, speed);
  154.          break;
  155.       case 4:
  156.          setPWM(4, speed);
  157.          break;
  158.    }
  159. }

  160. int main ()
  161. {
  162.    int i, j ;

  163.    if (wiringPiSetup () == -1)
  164.    {
  165.       fprintf (stdout, "oops: %s\n", strerror (errno)) ;
  166.       return 1 ;
  167.    }

  168.    for (i = 0 ; i < NUM_MOTORS ; ++i)
  169.    {
  170.       softPwmCreate (motorMap [i], 0, RANGE) ;
  171.    }

  172.    // Bring all up in stages 25%, 50%, 75% 100%

  173.    for (j = 25 ; j <= RANGE ; j+=25)
  174.    {
  175.       printf("pwm %d%%\n", j);
  176.       for (i = 0 ; i < NUM_MOTORS ; ++i)
  177.       {
  178.          softPwmWrite (motorMap [i], j) ;
  179.       }
  180.       delay(6000);
  181.    }
  182.    // Bring all up in stages 25%, 50%, 75% 100%

  183.    for (j = RANGE-25 ; j >= 0; j-=25)
  184.    {
  185.       printf("pwm %d%%\n", j);
  186.       for (i = 0 ; i < NUM_MOTORS ; ++i)
  187.       {
  188.          softPwmWrite (motorMap [i], j) ;
  189.       }
  190.       delay(6000);
  191.    }
  192.    return 0;
  193. }
Copy code

1

threads

2

posts

17

credits

Novice

Rank: 1

credits
17
 Author| Published in 2018-4-20 05:56:27 | Show all floors
Edited by steve1116 at 2018-4-20 05:58

Hi,

I tried Arduino examples too, but not this maybe. One other people suggested, that it will be far easier, when I remove the L293D chip from the shield, and put into a breadboard. I tried it, and it works fine, since that the motor runs. There was a lot of example to direct connect to L293D, and the wiring, debugging is too easier. So I will prefer this way.
Thank you for help.
You need to log in before you can reply login | Register

Points Rule

Quick reply Top Return list