Table of Contents
In the last article in the Build a Real Robot series I went over the requirements for the motor controller. I also outlined the commands that the controller will need to respond to.
To summarize, these are the basic requirements:
- Controlled via an I2C bus connection.
- Independent control of each motor.
- ATmega328P-based design.
- Provides PWM and DIR signals for Cytron motor drivers.
- Accepts input from motor rotary encoders.
- Emergency Stop feature to bring both motors to an immediate stop.
The controller is now built, and today I will go over the design with you.
I built the motor controller around two Arduino Nano modules. These ATmega328P-based modules are very similar in function to the Arduino Uno but are much smaller.
The Nano is, in many ways, an overkill. It has a USB connection that is only used during development, and a voltage regulator that will never be used in this design.
You don’t need to use an Arduino Nano, there are other choices available that will work just as well:
- The Arduino Pro Mini. This is even smaller (and cheaper) and does not have an integrated USB port. The main disadvantage of this module is the location of the A4 and A5 pins, which would make for an awkward design as these pins are required for the I2C connection.
- The ATmega328P chip itself. This is actually the best choice and I was tempted to use it. The reason I didn’t was that it is slightly more complicated to wire up on perfboard. Had I used a printed circuit board this chip would have been my choice. If you do use an ATmega328P you’ll also need to supply 16-MHz crystals, some capacitors and a couple of resistors.
If you decide to use a Pro Mini or ATmega328P chip instead you’ll still be able to use the same sketches that I’ll present in the next article. So build your motor controller any way you like.
Motor Controller Schematic
Here is the schematic diagram for the motor controller, using two Arduino Nanos:
In addition to the Arduino Nanos, I added a 100uf electrolytic capacitor to eliminate any electrical noise that may have been induced on the power supply lines. I also have the following connectors:
- A 2-pin screw terminal for the 5-volt and ground connections.
- Two 3-pin connectors for the output to the Cytron motor controllers.
- Two 4-pin connectors for the input from the motor rotary encoders.
- A 4-pin connector for the I2C and Emergency Stop connections.
The schematic may seem a bit complex but it really isn’t. I’ll break it down into sections to make it more clear.
Power & Ground
The power and ground connections are illustrated here:
The 100uf capacitor is connected across the power supply lines. You must be careful to observe polarity here. You could substitute another value of capacitor here if you wish.
Power is also connected to the two Arduino Nanos as follows:
- +5-volts to 5V.
- Ground to GND.
There are two GND pins on the Arduino Nano, either one will work.
Note that this article has been corrected, the original showed power going to VIN. Please connect power to 5V.
A ground connection is also made to one of the pins on the 3-pin connector for the motor driver output. A ground connection is also run to one pin of the I2C connector. And both power and ground connections are run to the two rotary encoder connectors, this provides power for the rotary encoders.
You’ll also notice the following conditions:
- “Left Motor” Arduino Nano pin D4 is grounded.
- “Right Motor” Arduino Nano pin D4 is connected to +5-volts.
These connections will allow the software to distinguish which controller it is running on. It needs to do this to set the correct I2C address.
I2C & Emergency Stop
The connections specific to I2C and Emergency Stop are as follows:
The Dupont connector used for these connections is actually a 5-pin connector with one pin removed, as illustrated in the diagram. I did this to allow me to “key” the mating female connector so that it could not be inserted incorrectly.
We have already made the GND connection. The SSL, or I2C clock, connection is routed to pin A5 of both Arduino Nanos. The SDA, or I2C data, connection is sent to pin A4 on both processors.
The Emergency Stop connection is routed to pin D3 on both Arduino Nanos. This is one of the interrupt pins, a signal on this line will cause an interrupt that will bring everything to a stop.
Rotary Encoder Inputs
I used 4-pin, 2.54mm JST connectors for the rotary encoder inputs, these are the same type of connectors used on the encoders on my motors.
You could, of course, use any type of 4-pin connector that you have available.
We have already made connections to the power and ground for these connectors, so there is only one more connection to make. I have connected the Channel B output of each rotary encoder to the D2 pin on the respective Arduino Nano.
This is another interrupt pin, every pulse of the encoder will generate an interrupt, which will be used to measure motor rotation.
If you wish you can use Chanel A instead, either one will work properly. We don’t need to use both of them as there is no need to read the motor direction – we are controlling the motor so we already know its direction!
PWM and DIR Outputs
The outputs to the Cytron motor driver PWM and DIR inputs are shown here:
Each controller uses Arduino Nano pin D10 as the PWM output and pin D12 as the DIR (direction) output.
I used a 3-pin, 2.54mm JST connector for each output connection, I like JST connectors as they are keyed to prevent connector reversal. You could always use another type of connector if you wish.
Construction on Perfboard
I did not make a printed circuit board for the motor controller as I wanted to build it quickly. So I elected to use perfboard.
If you haven’t used perfboard before it is pretty simple stuff to work with, and is great for wiring up prototypes.
Perfboard is essentially a piece of circuit board with solder pads laid out in a 0.1-inch grid, which is the standard spacing for DIP and SIP integrated circuits, Dupont (and several other) connectors, and many other electronic components.
High-quality perfboard, such as the one I used, is made of glass-epoxy and has plated-through solder pads. It is commonly available and very inexpensive.
You need to use hookup wire to connect the components together. I used 22-gauge wire for the power and ground connections and 30-gauge wire (“wire wrap” wire) for the other connections.
I also used some female Dupont headers as sockets for the Arduino Nanos. This does increase the height of the board assembly, but it isn’t too high as to interfere with the shelf that will be mounted above it.
I strongly recommend the use of sockets for the Nanos, but you could just solder then directly in if you want to. A socket will make it a LOT easier to replace the Nano if you ever need to.
A few hints to help you wire things up a bit easier:
- Use a soldering iron or station with a very fine tip.
- Use a pair of small needle-nose pliers to wrap the wire around each connection.
- Try and make a mechanically sound connection before it is soldered.
- Use a minimal amount of solder, be careful not to inadvertently connect two adjacent pins together.
- Make the power and ground connections first, to help secure the components in place.
- You can also solder the unused connections on the Arduino Nanos and the rotary encoder connectors, this will help secure them in place.
- Keep the Nanos in the sockets while you solder them so that they line up correctly when you are done.
Take your time and check your connections with a multimeter when you are done.
Now that we have the motor controller hardware it’s time to move on to the software. I will be showing you some sketches for the controller in the next article.