Control a step motor 28BYJ-48 with ULN2003

Moderators: grovkillen, Stuntteam, TD-er

Message
Author
bastler11
Normal user
Posts: 127
Joined: 11 Jan 2021, 21:58
Location: Luebeck

Control a step motor 28BYJ-48 with ULN2003

#1 Post by bastler11 » 08 Aug 2025, 08:28

Hello,

how could I control a step moter 28BYJ-48 with a ULN2003 board? I only need the possibility to drive for example 45° right, stop, 45° left. I have an ESP32 and ESP8266.

Thanks and best regards

TD-er
Core team member
Posts: 10193
Joined: 01 Sep 2017, 22:13
Location: the Netherlands

Re: Control a step motor 28BYJ-48 with ULN2003

#2 Post by TD-er » 09 Aug 2025, 11:15

Hmm those are a bit harder to do as you need to toggle 4 pins in a specific sequence and you need to know what the position/orientation of the motor is.
I don't think there actually is a plugin to do the pin toggling for you and doing it in rules is kinda pushing it I guess.
On an ESP32, you could do in the order of 10 rule calls per second.
I think that stepper motor has 48 steps per rotation and there is a 1:64 gear. (or it was 64 steps and 1:48 gear...)
So please check those numbers ;)
45 degree is 1/8th rotation, so you must make 384 steps. (768 half steps), which takes over a minute

Rules could be something like this (untested, on the ... you need to fill in the GPIO pins connected to theULN2003)

Code: Select all

On system#boot do
  let,20,... // GPIO pin for Blue
  let,21,... // GPIO pin for Yellow
  let,22,... // GPIO pin for Pink
  let,23,... // GPIO pin for Orange
  
  let,10,0  // keep current position
  let,11,0  // target position
  let,12,2*48*64 // Number of half steps for full rotation
  let,13,0 // temporary value for modulo 8 calculatoin of position
  
  // Now a look-up table to see which pin should be active
  let,1,0b1000
  let,2,0b1100
  let,3,0b0100
  let,4,0b0110
  let,5,0b0010
  let,6,0b0011
  let,7,0b0001
  let,8,0b1001
  
endon

on resetStepperPos do
  let,11,[int#12]/2  // Set to 'middle' of range
  let,10,[int#12]/2  // Set to 'middle' of range
  LoopTimerSet,1,0 // disable loop timer
endon

// Call with "event,stepTo=123" to move to position 123
on stepTo do
  let,13,[int#12]/2  // upper limit is 'middle of range'
  let,14,-1*[int#13]  // set lower limit
  let,11,{constrain:%eventvalue1%:[int#14]:[int#13]}
  LoopTimerSet_ms,1,100 // Start loop timer with 100 msec interval
endon

on Rules#Timer=1 do
  if [int#10] < [int#11]
    let,10,[int#10]+1
  else
    let,10,[int#10]-1
  endif
  let,13,([int#10]%8) + 1  // Calculate position modulo 8, add 1 to use variables 1 ... 8
  let,14,[int#%v13%]  // Get the bit set we need to know which pins to set
  gpio,[int#20],{bitread:0:[int#14]}
  gpio,[int#21],{bitread:1:[int#14]}
  gpio,[int#22],{bitread:2:[int#14]}
  gpio,[int#23],{bitread:3:[int#14]}
    
  if [int#10] = [int#11] 
      LoopTimerSet,1,0 // disable loop timer
  endif
endon

bastler11
Normal user
Posts: 127
Joined: 11 Jan 2021, 21:58
Location: Luebeck

Re: Control a step motor 28BYJ-48 with ULN2003

#3 Post by bastler11 » 09 Aug 2025, 21:09

Thanks, I will try it.

But in the moment there is an other problem: after I installed Collection-F over your webtool and tried to connect for setting the SSID and PW I choose CollectionF in my Wlan settings. After that I have to write a PW for this connection? What is it?

Best regards
Last edited by bastler11 on 09 Aug 2025, 21:23, edited 1 time in total.

User avatar
Ath
Normal user
Posts: 4541
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Control a step motor 28BYJ-48 with ULN2003

#4 Post by Ath » 09 Aug 2025, 21:23

The AP password has been configesp for quite a long time... ;)
/Ton (PayPal.me)

bastler11
Normal user
Posts: 127
Joined: 11 Jan 2021, 21:58
Location: Luebeck

Re: Control a step motor 28BYJ-48 with ULN2003

#5 Post by bastler11 » 10 Aug 2025, 22:13

I realized the circuit with the rules from TD-er. Nice project, but the motor runs really not very fast. Nevertheless it was a step to learn a little bit more how to use rules.

Thanks and good night!

TD-er
Core team member
Posts: 10193
Joined: 01 Sep 2017, 22:13
Location: the Netherlands

Re: Control a step motor 28BYJ-48 with ULN2003

#6 Post by TD-er » 10 Aug 2025, 22:22

Yep, using the rules to toggle the GPIO pins like this is really slow.
Maybe we can add a plugin for it, which then can run this much faster.

Still running over 100 steps/sec might cause other issues with this specific ULN2003 driver.

Problem is that the coils in the stepper motor will try to keep the magnetic field when you switch it off.
So they try to deliver some voltage, which will be dissipated in the ULN2003.
However this takes some time.
When running more than N steps per second, the average voltage over the coils will be significantly lower and thus the current through the coils will be (much) lower, which is directly related to the torque of the motor.
So above some stepper speed, the torque of the motor will be too low and thus the motor will skip steps.
This is of course depending on the actual driving force required.
But I know from experiments in the past that this 'driver' and this stepper motor will start missing steps at some point. Even when they only need to 'carry' a paper arrow stuck to the axis.

Who is online

Users browsing this forum: Anthropic Claude Bot [bot] and 2 guests