Motor Hookup

For energizing actuators you need the Motor Hat. The raspberry pi can not provide enough current to drive strong servos. Some motor hats require external power supply to initialize.

You need to connect the power supply to the two input pins. Start with 5V.

Each motor coil is connected to either M1, M2, M3 or M4. You can switch polarity of single motor by software. For stepper motors the order of the wires matters.

You need software for the motor hat.

You can not operate servo motors and other motors on the same hat.

You can stack hats for multiple motor types.

Do not short out wires and make sure there is insulation at the clamps.

You MUST use the standoffs when mounting the hats to the RasPi.

Materials

Motor Hat from Adafruit: https://www.adafruit.com/products/2348 (detailed tutorial https://learn.adafruit.com/adafruit-dc-and-stepper-motor-hat-for-raspberry-pi )

It contains:

2 x motor driver (TB6612) takes 15V and can deliver 1.2A of current and each chip

1 x PWM driver (PCA9685) creates precise pulses to control the speed of DC motors or the rotation of stepper motors or orientation of a servo motor.

The motor hat is more percise than the Raspberry Pi to create the pulse timing.

The Raspberry Pi communicates to the PWM driver with I2C. 12 channels of the PWM driver are used to create signals for the motor drivers and 4 channels are available to control servo motors (0,1,14,15).

Motors & Actuators

Software

To install the motor library use the following commands. Like for the humidity and pressure sensor, this library depends on Adafruit GPIO and Adafruit simpl IO library which should already be installed.

git clone https://github.com/adafruit/Adafruit-Motor-HAT-Python-Library.git
cd Adafruit-Motor-HAT-Python-Library
sudo python setup.py install
cd examples

There is a new version of the motor library available here: https://github.com/adafruit/Adafruit_CircuitPython_MotorKit but I have not tested it yet.

Assembly of Motor Hat

The motor-hat does not have its connectors attached when it is first time used. We will need to attach the 40 pin connector to the Raspberry Pi, the screw in terminals and pins for 5V, GND, and the PWM outputs. It is critical that you solder those pins onto the chip precisely and do not use too much solder, otherwise you will not be able to connect other parts to the pins.

Do NOT use the provided 40 pin connector. Use the custom 40 pin connector that allows you to stack a second hat onto the motor-hat. It is very tedious to de-solder 40 pins if you used the wrong connector. If you mount the parts incorrectly, I will ask you to order a replacement hat. Based on pictures on the Adafruit website it should be pretty clear which side of the board points upwards and which connector goes where.

There are also standoffs that have the correct length to attach the board properly to the Raspberry Pi.

The pins can NOT have solder on them along at least the top 2/3 of the pin. When you make the solder connections do the following:

  • Insert the connector and place the parts onto a flat surface making sure they are mounted perpendicular.
  • Solder one pin on one side and on the opposite side of the connector to fasten the connector to the Raspberry Pi. Inspect the placement and make adjustments if necessary. Only then start soldering all the other connections. As soon as you have more than two solder joints you will not be able to make adjustments to the connector placement anymore.

You will also need to place 4 pins to the PWM output 0,1,14,15 and 4 times 3 pins to the 5V, GND and 3V lines as well as the screw in terminals.

The screw in terminals come in 3 connector and 2 connector types. In order to create a 5 pin screw in terminal you need to slide a 2 pin and a 3 pin terminal into each other. It will take a close look to figure out how the plastic housings can be mated.

Under no circumstance will you power up the Raspberry Pi with the Motor Hat installed without having the standoffs in place. If you lost the stand offs you can place electrical tape between the HDMI connector and the bottom of the motor hat.

Servo Motor Setup

https://www.jameco.com/jameco/workshop/howitworks/how-servo-motors-work.html

You will need to connect your Power Supply to the power input of the motor-hat. Use black and red stranded wire and connect it to the power input of the motor-hat marked with large (+) and (-). Set the power supply to 5V and verify that it provides 5V between the red and the black banana connectors. Connect the wires from the motor-hat to the output of the power supply you built. Place the wires and aligator clips in a fashion that minimizes the risk of shorting out your cables.

Take the jumper wires and connect one to a PWM output (one of 4 pins) and the others to 5V and GND. On the servo motor, GND is black/brown, 5V is red and the PWM signal goes into the white/yellow signal cable.

Use the following code as an example: PWM Test Code

The pulse signal should be at 50Hz (20mS) and 1mS pulse with is full counter clockwise, 1.5mS center location and 2mS full clockwise. Some servo motors might require other settings please check the servo motor database. Servo Motors can not be operated together on same shield as stepper motors. It is advisable to stack a second motor hat for different motor types.

Stepper Motor Setup

We have a bi polar NEMA stepper motor that runs at up to 10V but will also run at 5V. It has 200 steps per revolution. The motor has two coils with one coil between red and yellow and the other between blue and orange. The H bridge connection on the motor controller should follow the order of the wires on the motor connector with M1 connecting red and yellow and M2 connecting blue and orange and the GND not being connected.

https://learn.adafruit.com/adafruit-dc-and-stepper-motor-hat-for-raspberry-pi/using-stepper-motors

https://en.wikipedia.org/wiki/Stepper_motor

DC Motor

One of the DC motors is a JGA25-370 6V motor with an attached transmission free running 300 rotations per minute (RPM) and a gear down to of 20.4 to 1 resulting making the motor run at 6000 RPM. It has a quadrature position encoder giving 11 pulses per revolution and on the output shaft 224.4 pulses per revolution. The power to the motor is connected to the outermost wires (white and red) and need to be connected to M3. Black and Blue are power to encoder electronics and require 3.3V on blue and black being GND. Yellow and Green are the pulses indicating a radiation. Its possible to determine forward backward rotation by comparing the pulses on yellow and green.

The two outer most pins are powering the motor.

The other pins are for the encoder. You can run the motor without the encoder.

The encoder needs 3.3V, GND and to input pins on the RasPi extensions board.

The encoder design is listed here (the picture and pin numbers do not match ours but we have the same number of pins with the same functions): https://sc01.alicdn.com/kf/HTB1e6dRGpXXXXctaXXXq6xXFXXX3/200666655/HTB1e6dRGpXXXXctaXXXq6xXFXXX3.jpg

The key element for a call back that simply counts the pulses is below. Pin is the pin on the raspberry you used to count the pulses. You can detect if motor rotates clockwise or counter clockwise by connecting both pins and figuring out which of the two pulses arrive first.

encoderPin = 21
counter = 0

def myCallback(pin)
   global counter
   counter+=1

GPIO.add_event_detect(endcoderPin, GPIO.RISING, callback=myCallback)