Stepper motor (28BYJ-48) with controller (ULN2003)

Another interesting type of motor is the stepper motor. Together with DC motors and servos, they make the three types of motors that you see on most designs. We will talk about servos in the next chapter.

A stepper motor allows you to tell it how many steps you want it to take in a given direction. This allows for fine-tuned movement, and it can be used to move a latch on a door, activate a candy dispenser, roll curtains up and down, and so on:

A stepper motor needs a controller circuit; a common one is ULN2003. The original version of this driver can be found at https://github.com/Polidea/Polithings but the maintainer is no longer active, so I have updated it to support Android Things 1.0 and uploaded it to PlattyThings. You can add this line to the dependencies of your project:

dependencies {
[...]
implementation 'com.plattysoft.things:uln2003:+'
}

As in the case of the DC motor controller, this component has four pins, but the usage is completely different; they are used to send signals to the motor to make it move. The way to make the motor take a step is to change the value of the input pins in a specific sequence. The driver is reasonably complex, so we won't be looking at it. However, using it is quite simple:

val stepper = ULN2003StepperMotor(in1Pin, in2Pin, in3Pin, in4Pin)

// Perform a rotation and add a rotation listener
stepper.rotate(degrees = 180.0,
direction = Direction.CLOCKWISE,
resolutionId = ULN2003Resolution.HALF.id,
rpm = 2.5,
rotationListener = object : RotationListener {
override fun onStarted() {
Log.i(TAG, "rotation started")
}
override fun onFinishedSuccessfully() {
stepper.close()
}
override fun onFinishedWithError(degreesToRotate: Double, rotatedDegrees: Double, exception: Exception) {
Log.e(TAG, "error, degrees to rotate: {$degreesToRotate} rotated degrees: {$rotatedDegrees}")
}
})

We just need to create an object of the type ULN2003StepperMotor, passing the name of the four GPIO pins and then call rotate with the amount of degrees, direction, resolution and rpm (revolutions per minute). We also pass a listener to be notified of when the rotation is completed. In this simple example we use the listener to close the peripheral once the rotation is completed.

Typically stepper motors are not very fast because they go step by step counting the rotation until they are done. The model 28BYJ-48 can get up to 10 rpm, but not much faster than that.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.190.217.134