Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learning JavaScript Robotics

You're reading from  Learning JavaScript Robotics

Product type Book
Published in Nov 2015
Publisher
ISBN-13 9781785883347
Pages 160 pages
Edition 1st Edition
Languages
Concepts
Author (1):
Kassandra Perch Kassandra Perch
Profile icon Kassandra Perch

Table of Contents (16) Chapters

Learning JavaScript Robotics
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Getting Started with JS Robotics Working with Johnny-Five Using Digital and PWM Output Pins Using Specialized Output Devices Using Input Devices and Sensors Moving Your Bot Advanced Movement with the Animation Library Advanced Components – SPI, I2C, and Other Devices Connecting NodeBots to the World, and Where to Go Next Index

Chapter 6. Moving Your Bot

In this chapter, we will cover the following topics:

  • The different kinds of servos and motors

  • Special concerns when using motors and servos

  • Wiring up servos and motors

  • Creating a project with a motor and using the REPL

  • Creating a project with a servo and a sensor

What you'll need for this chapter


You'll need your microcontroller—I highly recommend the Arduino Uno for this chapter because of its compatibility. You'll also need your USB cable, a breadboard, some breadboard wires, and a 10k ohm resistor. Grab a photocell, or any other sensor you'd like to try.

You'll also need a motor that runs on 5V and a standard hobby servo that uses 5V. These can be found easily at hobby stores or on Adafruit, SparkFun, Seeed studio, and many other online shops. Check out the following section to see some of your options.

The different kinds of servos and motors


First, we'll go over a few common servos and motors that you'll run into. But first, for those who are new to this, let's look at a quick description of motors and servos.

Defining motors and servos

A motor (or an electrical motor for our purposes) converts electrical energy to motion. Electricity goes in, motion comes out. This motion is output as a rotation motion; one of the most common uses of electronic motors is to turn wheels. Note that while you can control the speed of a motor by 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 these in a moment. Servos are technically 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...

Special concerns when using motors and servos


Projects that use servos and motors have some special considerations for them that are mostly focused around power and the fact that Johnny-Five projects are tethered to the computer running the Johnny-Five code.

Power concerns

Servos and motors draw a lot of power. This can be an issue when you are using several of them. If you are using 5V servos and motors and more than two or three at a time, you should use an external power supply for your Arduino to draw this extra current without affecting performance. These power supplies are usually plugged in to an external outlet, and look like the following figure.

Note

WARNING!

Before plugging any external power supply into your board, make sure that 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, use a 5V power supply for Arduinos. Also, follow proper safety protocols when dealing with outside power sources. SparkFun...

Wiring up servos and motors


Wiring up servos will look similar to wiring up sensors, except the signal maps to an output. Wiring up a motor is similar to wiring up an LED.

Wiring up servos

To wire up a servo, you'll have to use a setup similar to the following figure:

A servo wiring diagram

Tip

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 doubt, 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 place itself in a position it cannot reach. Usually, there is a small screw at the bottom of most servos that you can use to calibrate them. 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...

Creating a project with a motor and using the REPL


Grab your motor and board, and follow the diagram in the previous section to wire a motor. Let's use pin 6 for the signal pin, as shown in the preceding diagram.

What we're going to do in our code is create a Motor object and inject it into the REPL, so we can play around with it in the command line. Create a motor.js file and put in the following code:

var five = require('johnny-five');

var board = new five.Board();

board.on('ready', function(){

  var motor = new five.Motor({
    pin: 6
  });

  this.repl.inject({
    motor: motor
  });
});

Then, plug in your board and use the motor.js node to start the program.

Exploring the motor API

If we take a look at the documentation on the Johnny-Five website, there are a few things we can try here. First, let's turn our motor on at about half speed:

> motor.start(125);

The .start() method takes a value between 0 and 255. Sounds familiar? That's because these are the values we can assign to a PWM...

Creating a project with a servo and a sensor


Let's start with just a servo and the REPL, then we can add in a sensor. Use the diagram from the previous section as a reference to wire up a servo, and use pin 6 for signal.

Before we write our program, let's take a look at some of the options the Servo object constructor gives us. You can set an arbitrary range by passing [min, max] to the range property. This is great for low quality servos that have trouble at very low and very high values.

The type property is also important. We'll be using a standard servo, but you'll need to set this to continuous if you're using a continuous servo. Since standard is the default, we can leave this out for now.

The offset property is important for calibration. If your servo is set too far in one direction, you can change the offset to make sure it can programmatically reach every angle it was meant to. If you hear clicking at very high or low values, try adjusting the offset.

You can invert the direction of...

Summary


We now know how to use servos and motors to move our robotics projects. Wheeled robots are good to go! But what about more complex projects, such as the hexapod? Walking takes timing. As we mentioned in the .to() function, we can time servo movement, thanks to the Animation library.

In the next chapter, we'll talk about the Animation library and do some projects that move a few servos in sequence and as a group.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Learning JavaScript Robotics
Published in: Nov 2015 Publisher: ISBN-13: 9781785883347
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}