Menu

28BY J-48 bibolar stepper driver full wave at every 2ms

2020-06-12
2020-06-15
  • stan cartwright

    stan cartwright - 2020-06-12

    Hi.I got several 28BY J-48 bibolar stepper motors cheap on ebay and they are popular cos they are cheap
    but they need pulsing with 4 phases every 2ms to rotate at max torque.
    So this is a mega328p UNO solution.
    I got the 1ms timer 0 demo and the timer calculator and got the values for 2ms,easy.
    It uses vars forward,stop,reverse to control the motors.
    The demo shows the motors running while wait command.
    It is just a handler for these cheap geared stepper motors.
    https://www.youtube.com/watch?v=0eZFZsmpvR4&feature=youtu.be

    ;28BY J-48 bibolar stepper driver full wave at every 2ms
    ;this uses portD for 2 motors
    #chip mega328p,16
    #option Explicit
    
    dim l_mot_pos,r_mot_pos as byte
    dim l_mot_dir,r_mot_dir as byte
    
    #define forward 1   ;|
    #define reverse 255 ;|--motor direction
    #define stop 0      ;|
    
    l_mot_pos=1:r_mot_pos=1
    l_mot_dir=forward:r_mot_dir=forward
    ;
    ;setup stepper motor interrupts step every 2ms
        On Interrupt Timer0Match1 Call motors
        Dim OCR0  AS byte alias OCR0A
        Dim TCCR0 AS  byte alias TCCR0B
        WGM01 = 1                'Timer in CTC mode - required
        'Timer0 Prescaler = 64
        OCR0 = 124 ;2ms
        TCCR0 = 0x28
        TCCR0 = TCCR0 or 0x04
    ;
    do
    l_mot_dir=forward:r_mot_dir=forward
    wait 5 s
    l_mot_dir=reverse:r_mot_dir=reverse
    wait 5 s
    l_mot_dir=reverse:r_mot_dir=forward
    wait 5 s
    l_mot_dir=forward:r_mot_dir=reverse
    wait 5 s
    loop
    
    sub motors
      if l_mot_dir=stop then
        set portd.0 off:set portd.1 off:set portd.2 off:set portd.3 off
      else if l_mot_dir=forward then
        l_mot_pos++
        if l_mot_pos=5 then
          l_mot_pos=1
        end if
      else
        l_mot_pos---
        if l_mot_pos=0 then
          l_mot_pos=4
        end if
      end if
    ;
      select case l_mot_pos
        case 1
          set portd.0 on:set portd.1 on:set portd.2 off:set portd.3 off
        case 2
          set portd.0 off:set portd.1 on:set portd.2 on:set portd.3 off
        case 3
          set portd.0 off:set portd.1 off:set portd.2 on:set portd.3 on
        case 4
          set portd.0 on:set portd.1 off:set portd.2 off:set portd.3 on
      end select
    ;
      if r_mot_dir=stop then
        set portd.4 off:set portd.5 off:set portd.6 off:set portd.7 off
      else if r_mot_dir=forward then
        r_mot_pos++
        if r_mot_pos=5 then
          r_mot_pos=1
        end if
      else
        r_mot_pos---
        if r_mot_pos=0 then
          r_mot_pos=4
        end if
      end if
    ;
     select case r_mot_pos
      case 1
        set portd.4 on:set portd.5 off:set portd.6 off:set portd.7 on
      case 2
        set portd.4 off:set portd.5 off:set portd.6 on:set portd.7 on
      case 3
        set portd.4 off:set portd.5 on:set portd.6 on:set portd.7 off
      case 4
        set portd.4 on:set portd.5 on:set portd.6 off:set portd.7 off
     end select
    end sub
    
     
  • mmotte

    mmotte - 2020-06-13

    Hi Stan,
    Here's mine.

    '28BYJ_48_12F683_ver092.gcb
    '2017/ 1/ 3
    'A program to Run a 4096 step geared step motor 28BYJ-48
    'actually this is doing full step mode in which 2 coils are active at a Time
    ' so it only has 2048 steps/turn (seems like a couple less)
    'Another array could be built to do eight half steps
    'The outputs run a ULN2003 like supplied by so many chinese suppliers
    'The input Port GPIO.4 has a pushbutton
    'Setup: routine allows the operator to align the shaft on start up
    'MainLoop: routine rotates the motor one full turn
    'If you change the MainLoop in line 44 to MainLoop2 then it
    'will demo a 5 station/position pattern
    'by Mike Otte
    '2013
    
    'Chip model
    #chip 12F683, 8
    
      dim Full(5)
          Full(0) = b'00000110'
          Full(1) = b'00000011'
          Full(2) = b'00001001'
          Full(3) = b'00001100'
    
    'Set the pin directions
    dir GPIO.0 out  'These are  wired to the uln2003 inputs
    dir GPIO.1 out
    dir GPIO.2 out
    dir GPIO.5 out
    
    dir GPIO.4 IN   'pushbutton input
    
    Dim position as Word  'Could be integer if we go below 0 position
    position =0
    
    'Initialize position routine
    Setup:
      ' let motor turn until operator Pushes Button at home Position
      If GPIO.4 = 1 Then StepCW
      'wait for button push
      If GPIO.4 = 0 Then
        position =0
        wait 500 ms
        goto MainLoop2  ' to demo 5 position
      end if
      goto Setup
    
    'One turn MainLoop ,get here from 3 lines above ,line44?
    MainLoop:
      'wait for button push
      If GPIO.4 = 1 Then goto MainLoop
    
      StepTo(2040)  'one turn
    
      position =0   'resets position counter for next move
      wait 500 ms   'Gives time for button release if only a few steps
    
     goto MainLoop
    
    ' Example2: of going to 5 different positions and waiting for the button to be pushed
    'Button is a pull down
    MainLoop2:
      If GPIO.4 = 1 Then goto MainLoop2
      If GPIO.4 = 0 Then
        station++
        If station > 5 then station = 1
    
        wait 500 ms
        goto newstation
      end if
      goto MainLoop2
    newstation:
      Select case station
        case 1
            StepTo(500) 'station position 1
            wait 500 ms   'Gives time for button release if only a few steps
        case 2
            StepTo(1500)  'station position 2
            wait 500 ms   'Gives time for button release if only a few steps
        case 3
            StepTo(2500)  'station position 3
            wait 500 ms   'Gives time for button release if only a few steps
        case 4
            StepTo(3000)  'station position 4
            wait 500 ms   'Gives time for button release if only a few steps
        case 5
            StepTo(3100)  'station position 5
            wait 500 ms   'Gives time for button release if only a few steps
        end Select
     goto MainLoop2
    
    Sub StepTo(newpos as word)
        EnMotor
       Do until Position = newpos
        If newpos < position then StepCCW
    
        If newpos > position then StepCW
    
       loop
       DisEnMotor
    end sub
    
    Sub EnMotor
      dir GPIO.0 out
      dir GPIO.1 out
      dir GPIO.2 out
      dir GPIO.5 out
    end sub
    
    Sub DisEnMotor
      dir GPIO.0 in 'disables motor so it converves energy and don't heat up
      dir GPIO.1 in
      dir GPIO.2 in
      dir GPIO.5 in
    end sub
    
    Sub StepCW
        Position++
        myStep = position mod 4
        thisStep = Full(myStep)
        GPIO.0 = thisStep.0
        GPIO.1 = thisStep.1
        GPIO.2 = thisStep.2
        GPIO.5 = thisStep.3
        wait 2 ms
    end sub
    
    Sub StepCCW
        Position--
        myStep = position mod 4
        thisStep = Full(myStep)
        GPIO.0 = thisStep.0
        GPIO.1 = thisStep.1
        GPIO.2 = thisStep.2
        GPIO.5 = thisStep.3
        wait 2 ms
    end sub
    
    End
    
     
  • stan cartwright

    stan cartwright - 2020-06-13

    Hi Mike. These geared steppers and drivers seem popular,loads of youtube and guides.
    I posted code for running them before but used a i2c port expander. I think running that and a i2c rangefinder would do my head.
    As before I used an interrupt so the motors run at a constant most efficient speed.
    Seems they can be run with halfwave,fullwave or a combo of both. Fullwave is simple and faster. Before I used the combo mode which is 8 different phases, more steps so slower rotation.
    2ms is optimum rotation time. Any faster and they don't turn.

     
  • stan cartwright

    stan cartwright - 2020-06-14

    mike
    I send off to each port

    Sub DisEnMotor
      dir GPIO.0 in 'disables motor so it converves energy and don't heat up
      dir GPIO.1 in
      dir GPIO.2 in
      dir GPIO.5 in
    end sub
    

    does gcb have portx=255 so you can set all bit in one go?
    I don't think so or it would be easy to control these motors by just rotating the phase pattern left/right.
    only 4 bits so 1 byte does 2 motors ie the port.
    it would be portd= left phase + right phase * 8
    if using 4 bits for each motor,

     

    Last edit: stan cartwright 2020-06-14
  • mmotte

    mmotte - 2020-06-15

    Hi Stan,
    Here's another.
    Astro photography require to move the camera as the Earth turns so you do not get "star trails". So a "Barn Door" tracker was built. Hinge on one end of two boards and a threaded rod turned by a motor on the other end. Mount the camera on the top board and screw the bottom board to a tripod. Then align the hinge pivot to the north star thus making a primative "equatorial mount". I did this with my Cannon 1000 and a 300 mm lens. Took some great pictures.

    'A program to Run a 4096 step geared step motor on a 10-32 threaded rod 
    ' for a "barn Door" tracker to hold a camera and track the stars
    'by Mike Otte
    '2013
    
    'Chip model
    #chip 16F886, 8
    
      dim Full(5)
          Full(0) = b'00000110'
          Full(1) = b'00000011'
          Full(2) = b'00001001'
          Full(3) = b'00001100'
    
    dim   RFull(5)
          RFull(3) = b'00000110'
          RFull(2) = b'00000011'
          RFull(1) = b'00001001'
          RFull(0) = b'00001100'
    
    ' momentary switches
    #define Home PORTC.0
    #define RAstop PORTC.1
    #define RAplus PORTC.2
    #define limit PORTC.3
    'define PORTC upper 4 bits for adjusting the time in additional millisec / step
    
    'Set the pin directions
    dir PORTB out
    dir PORTC IN
    ' i am not using interrupt because this steps fast 
    'On Interrupt PORTBChange Call buttons
    
    
     wttm = 17    'wait time was 25 msec
     wtmodify= PORTC
     wtmodify = wtmodify AND 0xf0  ' mask off the upper four bits
      rotate wtmodify right simple  'shift them to the lower four bits
      rotate wtmodify right simple  ' i put pullups and board jumper pins to act as switches
      rotate wtmodify right simple
      rotate wtmodify right simple
      wttm = wttm + wtmodify        ' add or subtract for tuning delay from portc upper four bits
    ' wttm (wait time) now has the total value
       PortB = 0          'initialize
      myj=0
     wait 1 s
    'Main routine
    
    Start:
            myStep = myj mod 4
          thisStep = Full(myStep)
    
            PortB = thisStep 
    
            wait wttm ms
          myj++
            gosub  buttons
    
    goto Start
    
    sub buttons
      if RAplus = 0 then    ' these were going to make it go faster or slower 
    
        DO UNTIL RAplus = 1
        myStep = myj mod 4
          PortB =  Full(myStep)
          wait  7 ms
        myj++
        loop    
      end if
    
      if RAstop = 0 then   ' but i don't use these two switches
    
        DO UNTIL RAstop = 1
        myStep = myj mod 4
          PortB =  RFull(myStep)
          wait  7 ms
        myj++
        loop
      end if
    
       if home = 0 then     'I really only use these two switches
                    '"home" switch sends the barn door towards home (closed limit)
        DO UNTIL limit = 0   ' the "limit"  stops it when it get home 
        myStep = myj mod 4   'and returns it to going forward again
           PortB =  RFull(myStep)
           wait  2 ms
        myj++
        loop
      end if
    
    
    End Sub 
    

    Pretty much only used the Battery switch, run switch and the limit switch.

     
  • stan cartwright

    stan cartwright - 2020-06-15
     
  • mmotte

    mmotte - 2020-06-15

    That's it. mine is less refined and inexpensive.

     
  • mmotte

    mmotte - 2020-06-15

    That's it. mine is less refined and inexpensive.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.