What you’ll need
To begin working, you’ll need your microcontroller – I highly recommend the Arduino Uno, because of its compatibility. You’ll also need a USB cable, a breadboard, some breadboarding wires, and a 10k ohm resistor. Grab a photocell, or any other sensor you’d like to try.
Different kinds of servos and motors
First, we’ll go over a few of the common kinds of servos and motors you’ll run into. But first, for those new to this, let’s give a quick description of motors and servos.
Defining “motor” and “servo”
A motor, or an electrical motor for our purposes, converts electrical energy into motion. Electricity goes in, motion comes out. This motion is output as rotation: one of the most common uses of electronic motors is to turn wheels. Note that while you can control the speed of a motor through controlling the power being input, you cannot change the position of a motor precisely.
This is where servos come in—servos use electricity to move to a set point—most commonly in an arc of 180 degrees. However, there are some servos that can rotate 360 degrees – we’ll discuss those in a moment. Servos are technically more specialized motors, and while motors are used to propel projects, servos are used to control them.
Things to keep in mind
There are quite a few servos and motors available for use with your projects. While we are going to go over many of these, there are a few things to keep in mind when looking at motors to use.
For instance, be sure to be aware of the voltage and current needed to power the motor or servo: if it’s above 5 volts, you’ll need to power them with something other than the Arduino’s 5v OUT, and if they draw a lot of current but only use 5V, you might consider an external power supply for your Arduino board. Note that current is added whenever you have multiple servos but the voltage required stays the same: for instance, if you have two servos that require 5V voltage but 200mA current, you still only need 5 volts but you need 400mA current! Most of the information should be found on the data sheet for the motor or servo you are using.
Types of motors
While we’re going to go over a few types of motors here, keep in mind that the functionality of most of these motors is the same: they have an on and off, and you can slightly adjust speed with power adjustments. However, this doesn’t rule out the use of motors entirely – there are some interesting uses!
Your standard hobby motor looks like a large cylinder with a few wires on one end and a rod on the other end, like the figure below:
Figure 1 A standard DC hobby motor
They come in varied sizes, and can generate various levels of work. These are mainly used for motion and propelling projects, like remote-control vehicles. Some of these motors are called directional motors- this means you can control the direction. With non-directional, you can only turn the motor in one direction. Keep that in mind when considering your project.
There are also vibration motors—these are the kind found in your cell phone that alert you to calls without an audible ring. These look much like regular motors, but the rod at the end has an asymmetrical weight attached, like the figure below:
Figure 2 – A vibration motor
These motors are primarily used for tactile vibration, but in the wearables and cell phone world, that can be a vital piece of the puzzle!
Finally, there are stepper motors. They are usually larger, and require more power:
Figure 3 – A stepper motor
However, stepper motors move in discreet amounts, giving you a large amount of control and precision with full rotation. One very common use for these motors is 3D printing- these motors are at the heart of most 3D printers, because of their precision and speed.
Types of servos
There are two main types of servos. The first is generally called a standard servo – they are regularly used in hobby remote-control vehicles, like cars and planes, as well as hobby robotics. These tend to look like a box with a plastic cylinder and a propeller, arm, or disc shape on top, like the following figure:
Figure 4- A standard servo
These servos move in a range of motion that spans 180 degrees, or half of a full range of motion. These can be set to a desired position, however, giving them precision over motors.
There are also continuous servos that span the full 360 degrees. Note that the two types of servos look remarkably similar, as shown by the figure below:
Figure 5- a continuous servo
Most continuous servos have a label on them that indicates as such, but you’ll have to keep track of different types when purchasing your own.
Do I use a servo, or a motor?
This is a fantastic question: if you want movement, which should you use? The rule of thumb is if you want precise movement, you’ll want a servo: while servos can be configured to certain angles, motors have an on and off, with a speed setting varied by the amount of power sent to it, not a discrete unit like degrees/sec.
While you may think that if you want a full 360 degree range of motion, you’re limited to the use of motors, this isn’t always the case: continuous servos combine the precise nature of servos with the full range of motion offered by a motor.
Servo and motor controllers
Sometimes, you want to use many, many motors or servos on a project—a great example is the Hexapod project, which uses 18 high-powered servos! There are not that many pins on an Arduino Uno, but with the use of a servo or motor controller, you can control many motors or servos using 2 data pins. These controllers use the I2C method. Johnny-Five have built in support for quite a few of these controllers—if you’d like to use one, make sure to check the documentation at johnny-five.io to make sure yours is compatible.
Figure 6- Example of a servo controller
Motor and servo Shields
A Shield is an Arduino term for a board that is placed on top of an Arduino, usually an Uno, to give it extra capability. Shields can be used to add Wi-Fi, LED matrices, etc. A very common type is a shield for motors and servos. Johnny-Five supports a few shields for motor use. Again, if you wish to use lots of motors, or high-powered motors, check out the documentation at johnny-five.io to see if a motor shield is right for your project.
Figure 7- Example of a motor shield
Special concerns when using motors and servos
Projects that use servos and motors have some special considerations, mostly focused around power and the fact that Johnny-Five projects are tethered to the computer running the Johnny-Five code.
Servos and motors draw a lot of power which can be an issue when you are using several. If you are using 5V servos and motors, and more than 2 or 3 at a time, you should use an external power supply to your Arduino to allow for that extra current draw without affecting performance. These power supplies usually plug into an external outlet, and look like the figure below.
WARNING: Before plugging any external power supply into your board, make sure the board you are using is voltage regulated for the voltage of the power supply—for an Arduino Uno, this is 12V. When in doubt, on Arduinos, use a 5v power supply.
If you are using servos or motors that require more than 5V, you will need to supply power externally to your motors or servos.
Tethering and Cables
Johnny-Five usage means that the code running on the board is receiving messages form the host computer—if that connection is lost, the project cannot run. This means that for most Johnny-Five projects, you will need to maintain a USB cable connection. So if you’re going to make a project that moves, you’ll want a long USB cable.
Wiring up servos and motors
Wiring up servos will look similar to wiring up sensors, except signal maps to an output. Wiring up a motor is similar to wiring an LED.
Wiring up servos
To wire up a servo, you’ll want to use a setup similar to the figure below:
Figure 8 – Servo wiring diagram
The wire colors may vary for your servo: if your wires are red, brown, and orange, red is 5V, brown is GND, and orange is signal. When in question, check the data sheet that came with your servo.
After wiring up the servo, plug the board in, and listen to your servo. If you hear a clicking noise, quickly unplug the board—this means your servo is trying to set to a position it cannot reach. There is a small screw at the bottom of most servos that you can use to calibrate it—use a small screwdriver to rotate this until it stops clicking when the power is turned on.
This procedure is the same for continuous servos; the diagram does not change much either. Just replace the regular servo with a continuous one and you’re good to go.
Wiring up Motors
Wiring up motors looks like the following diagram:
Figure 9 – Motor wiring diagram
Again, you’ll want the signal pin to go to a PWM pin. Because there are only 2 pins, it can be confusing where the power pin goes—it goes to a PWM pin because, like our LED got its power from the PWM pin in Chapter 2, the PWM pin will provide the power to run the motor.
Now that we know how to wire these up, let’s work on a project involving a motor and Johnny-Five’s REPL.
To give you an overview, we dwelled upon the different servos and motors, and how they work. We also looked into the setting up and wiring of the servos and motors.