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 5. Using Input Devices and Sensors

We've handled outputs, but what makes robots truly interesting is the use of inputs to generate outputs! In this chapter, we'll go over basic input devices, such as buttons, and environmental sensors, such as a sensor that detects ambient light. We'll talk about how Johnny-Five uses events to make these devices easy to use, and build some projects. After finishing this chapter, you should have all the knowledge that you need to handle most input/output projects.

This chapter will cover the following topics:

  • How analog input pins work

  • Johnny-Five's sensor events

  • Using basic inputs – buttons and potentiometers

  • Using sensors – light and temperature

  • Other types of sensors and their uses

What you'll need for this chapter


For the project in this chapter, you'll need your board, a USB cable, and a few inputs and sensors.

First, you'll want a button. You can find these aplenty in most starter kits, but you can also buy them separately. We're going to consider a button with a four-prong design, as shown in the following screenshot:

A common push button for robotics projects

However, a design with two prongs is fine too—four-prong buttons still represent two sides of the button, so you can replicate these with two-prong designs.

You'll also need a rotating potentiometer—these are knobs that you twist to set a value, similar to the volume knob on a speaker. You'll want one that's breadboard-friendly with a three-prong design, as shown in the following screenshot:

A basic rotating potentiometer

Tip

Please note that you may have access to a sliding potentiometer (one that looks like a sliding switch or similar to a dimmer switch) or some other potentiometer. These will work fine, but check...

How analog input pins work


Inputs are the first half of awesome robots equations; robots that know the weather, can tell how fast they are moving, or see where things are. Input pins make this possible, so in this section we'll discuss how they work and how we're going to utilize them.

Analog input pins work by reading a voltage level from a sensor and converting this voltage level to an integer value between 0 and 1,023. Input device manufacturers usually give a scale to their device that tells us how to turn this integer value into a real-world value; for instance, we'll do this with our temperature sensor to change the value into a temperature that we as humans can understand.

We're going to utilize these pins by plugging in both the human input devices and the environmental sensors, and map ping the inputs that they give. This way, we can start developing projects that use inputs to generate outputs.

Johnny-Five sensor events


As discussed in the previous chapters, Johnny-Five is dependent on events for a lot of its functionality. This is not different for inputs and sensors—most of the time, you'll interact with these by waiting for events. While most input devices have ways way to read data at any given moment, these are mostly used for debugging, and you'll need to have a good grasp of the events available for your input device or sensor when coding your projects.

Most input devices and sensors have a data event; this event tells the program when data is read from the device. This is a kind of a firehose: it reports quite quickly and can be a little overwhelming. This is usually used for debugging, as most robotics programs are more interested in when the sensor or input data changes rather than when there is new input to be read.

The change event, also available on most devices, is a very commonly-used event—it only fires when the incoming data has changed. A good example is a temperature...

Using basic inputs – buttons and potentiometers


Let's take a look at using some basic input devices first. We'll start with a button and a potentiometer—two of the easiest input devices to use with Johnny-Five, and a good way to get acquainted with both specialized input objects, such as buttons, and general Sensor objects, which we'll use for the potentiometer.

Wiring up our button and LED

First, we're going to wire up a button and write some code to measure whether it is pressed or not, using an LED as our indicator. The wiring of the project will look like this:

Wiring diagram for a button and an LED

Make sure that if you're using the four-prong button, it crosses over the center trough of your breadboard as shown in the diagram. If it does not, the button will not work properly. If you are using a two-prong button, the wiring is very similar, and it does not have to cross over.

Coding button-led.js

Now, let's take a look at the Button object in Johnny-Five to see what we'll need to use for...

Using sensors – Light and Temperature


Now, we'll build a couple of projects that demonstrate how to use more advanced sensors: a photocell and a temperature sensor. We'll learn how specialized Johnny-Five Sensor objects allow us to make these easier to use, and how to play with inputs in the REPL and show some input data in the console using a module called barcli.

Wiring up our photocell

First, we'll start with the photocell—see the following wiring diagram:

The wiring diagram for the photocell

Tip

Note that the resistor setup is how we wire up a sensor with only two leads and we need three—input, power, and ground.

Coding our photocell example

As we code our photocell example, we'll note that there is no photocell object, so we're going to use the generic Sensor object, as we did with the potentiometer.

As for outputting the data that we get from the sensor, we're going to use a handy utility called barcli to make our output much easier to read.

barcli

In the early days of Johnny-Five, one of the...

Summary


In this chapter, we've learned how to use many different input devices and sensors in order to build Johnny-Five projects that listen to the world around them. We've learned how to listen for events, as well as use both the generic Sensor object and specialized objects like Button and Temperature.

In the next chapter, we'll start looking at moving robots using sensors.

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}