Pi IoT In Python Using GPIO Zero - DC Motors
Written by Harry Fairhead & Mike James   
Monday, 23 November 2020
Article Index
Pi IoT In Python Using GPIO Zero - DC Motors
Unidirectional Brushed Motor

Unidirectional Brushed Motor

A brushed motor can be powered by simply connecting it to a DC supply. Reversing the DC supply reverses the direction of the motor. The speed is simply proportional to the applied voltage. If all you want is a unidirectional control then all you need is a PWM driver that can supply the necessary current and voltage.

A single transistor solution is workable as long as you include a diode to allow the energy stored in the windings to discharge when the motor is rotating, but not under power:

driver1

This circuit is simple and will work with motor voltages up to 40V and motor currents up to 5A continuous, 8A peak. The only small point to note is that the TIP120 is a Darlington pair, i.e. it is two transistors in the same case, and as such the base voltage drop is twice the usual 0.6V, i.e. 1.2V, and this has to be taken into account when calculating the current-limiting resistor.

It is sometimes said that the TIP120 and similar are inefficient power controllers because, comprising two transistors, they have twice the emitter collector voltage you would expect, which means they dissipate more power than necessary. If you are running a motor from a battery you might want to use a MOSFET, but as described earlier 3.3V is low to switch a MOSFET on and off. One solution is to use a BJT to increase the voltage applied to the gate:

driver2

The BJT connects the gate to 12V. As the IRFZ44NPBF has a threshold voltage between 2V and 4V devices should work at 5V and sometimes at 3.3V without the help of the BJT, but providing 12V ensures that the MOSFET is fully on. One problem with the circuit is that the use of the BJT inverts the signal. When the GPIO line is high the BJT is on and the MOSFET is off and vice versa. In other words, GPIO line high switches the motor off and low switches it on. This MOSFET can work with voltages up to 50V and currents of 40A. The 2N2222 can only work at 30V, or 40V in the case of the 2N2222A.

A third approach to controlling a unidirectional motor is to use half an H‑bridge. Why this is so-called, and why you might want to do it, will become apparent in the next section on bidirectional motors. Half an H‑bridge makes use of two complementary devices, either an NPN and a PNP BJT or an N- and P-type MOSFET. For example:

driver3

If the GPIO line is high then Q1 is on and Q2 off and the motor runs. If the GPIO line is low then Q1 is off and Q2 is on and the motor is braked – it has a resistance to rotating because of the back EMF generated when the rotor turns. You probably need a BJT to feed the MOSFETs as selected.

There is no GPIO Zero class to control a unidirectional motor, but it is easy to create one from PWMOutputDevice:

class uniMotor(PWMOutputDevice):
    def __init__(self, pin=None, active_high=True,
                        initial_value=0,
pin_factory=None): super(uniMotor, self).__init__( pin, active_high, initial_value,
pin_factory=pin_factory) def speed(self,value): self._write(value) motor=uniMotor(4) motor.speed(0.5) sleep(10)

We have simply added a speed method. The inherited methods on and off are still useful, but it might be better to block access to pulse and blink. Also notice that if you are using the transistor/MOSFET driver, then setting active_high=False solves the need to provide an inverted pulse.

In rest of chapter:

  • Bidirectional Brushed Motor
  • Motor Software
  • Using Full H-Bridge As Two Half H-Bridges
  • Controlling a Servo
  • Brushless DC Motors
  • Stepper Motors
  • Stepper Motor Driver

Summary

  • There are a number of different types of electric motor, but DC brushed or brushless motors are the most used in the IoT.

  • Brushed motors can be speed controlled using a single transistor driver and a PWM signal. There is no unidirectional motor class in GPIO Zero, but it is easy to create one.

  • For bidirectional control you need an H‑bridge. The Motor class can be used to control the direction and speed of a motor with the help of an H‑bridge.

  • Servo motors set their position in response to the duty cycle of a PWM signal.

  • Brushless DC motors are very powerful and best controlled using off-the-shelf electronic modules. They are very powerful and thus dangerous if used incorrectly. They can be driven using a simple PWM signal.

  • Stepper motors are a special case of a Brushless DC motor. They move in discrete steps in response to energizing different coils.

  • A unipolar motor has coils that can be driven in the same direction for every step. A bipolar motor has coils that need to be driven in reverse for some steps.

  • Bipolar motors need two H‑bridges to operate and four GPIO lines.

  • There is no stepper motor class in GPIO Zero, but it is possible to create one.

 

Raspberry Pi IoT In Python Using GPIO Zero
Second Edition

By Harry Fairhead & Mike James

GPIOZero2E360

Buy from Amazon.

Contents

  1. Why Pi for IoT?
  2. Getting Started With Python And GPIO Zero
  3. Introduction to the GPIO
  4. Python - Class and Object
  5. Simple On/Off Devices
      Extract 1: On/Off Devices *
  6. Pins And Pin Factories
      Extract 1: Pins ***NEW!!
  7. Some Electronics
  8. Simple Input
  9. Complex Input Devices
      Extract 1: Complex Input *
  10. Pulse Width Modulation
      Extract 1:  PWM*
  11. Controlling Motors And Servos
      Extract 1: DC Motors *
  12. Working With Compound Devices
      Extract 1: Compound Devices*
  13. The SPI Bus
  14. Custom SPI Devices
  15. Using The Lgpio Library
  16. Appendix Visual Studio Code Remote Python
    *Extracts from first edition - will be updated.

 <ASIN:1871962870>

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


White House Urges Memory Safe Software
29/02/2024

The White House is urging developers to adopt memory safe programming languages, suggesting Rust would be a safer choice than C or C++. 



JetBrains Announces TeamCity Pipelines
19/03/2024

JetBrains has released a public beta of TeamCity Pipelines, a cloud-based Continuous Integration/Continuous Deployment (CI/CD) service for small and medium-sized engineering teams.


More News

raspberry pi books

 

Comments




or email your comment to: comments@i-programmer.info

 

 



Last Updated ( Monday, 23 November 2020 )