Menu

28BYJ_48 Geared step motor with ULN2003

mmotte
2017-01-04
2017-01-04
  • mmotte

    mmotte - 2017-01-04

    I have been using these small steppers for a couple different projects over the last 3 years. They are not very strong but work for light duty projects.

    '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
    
     
  • Anobium

    Anobium - 2017-01-04

    THANK YOU. Very nice example that others can learn from.

    And, the first of the year!!!

    :-)

     

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.