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 8. Advanced Components – SPI, I2C, and Other Devices

We've covered a lot of different types of device already—including input, output, and movement. This chapter delves into how these devices can be implemented in different ways and for different reasons. In this chapter, we'll take a look at the I2C and SPI protocols and their advantages and setbacks with Johnny-Five. We'll also take a look at how to add your own components to Johnny-Five, which gives us a good look into how these devices work and how you can contribute to the development of Johnny-Five! In this chapter, we will cover the following topics:

  • Why do we need the I2C and SPI protocols?

  • The SPI devices

  • The I2C devices

  • External devices

What you'll need for this chapter


You'll need your microcontroller, a USB cable, and computer. You'll also need an ADXL345I2C Accelerometer such as the Adafruit Industries product ID 1231 or the SparkFun product ID SEN-09836, and an LED matrix kit from SparkFun—product number DEV-11861. You'll also need your LCD display from Chapter 4, Using Specialized Output Devices. Finally, you'll need a USB gamepad—I recommend the N64RetroLink controller that is available for about $15 on Amazon, but if you have a spare PS3DualShock 3 controller, I will also include instructions for that.

Why do we need the I2C and SPI protocols?


This is all starting to get really complicated, right? Why even bother? We have digital pins and analog pins; shouldn't reading values from these be enough?

Not when you go outside the realm of LEDs. Think about how much information goes into this text you're reading on a page (or, likely, a screen)! Bytes and bytes of information. This is true for many peripherals you'll use in Johnny-Five applications.

For instance, the accelerometer we'll be using—without the I2C protocol, it would use three analog pins. That's the majority of the analog pins on an Arduino Uno, and many platforms don't have analog pins at all! Not to mention the LCD we used in Chapter 4, Using Specialized Output Devices—without I2C, we have to correctly wire 11 different pins, six of which are separate data pins.

The complexity of data being sent and received is also an issue. There are sensors that send back data that doesn't fit in an analog range of 0-1024. There are output devices...

Exploring SPI (Serial Peripheral Interface) devices


Serial Peripheral Interface (SPI) is a protocol to be used with certain devices in Johnny-Five and in general, robotics. It came about as a response to typical serial connections (which you don't see often in hobbyist robotics anymore), which were asynchronous in nature. This led to a lot of overhead, so SPI was developed as a way to ensure data was sent and received in a way that was efficient.

Tip

Keep in mind that when we talk about synchronous/asynchronous in this context, we are NOT talking about it as we would in JavaScript. You can still write async JavaScript functions around SPI methods!

How SPI works

In typical serial connections, you have a line from which data goes out (TX) and a line to which the data comes in (RX), and this makes communication difficult. How does the receiver know how fast the sender would be sending bytes, and when are they done sending? This lack of a synchronized time clock is what we mean when we say asynchronous...

Exploring I2C devices


We touched on I2C devices back in Chapter 4, Using Specialized Output Devices—you may have used an I2C LCD to display some data. You may have noticed that the I2C LCD was much easier to wire up, and had the same API as the non-I2C LCD. Luckily, this is because Johnny-Five strives to give every component a similar API, regardless of its implementation or protocol. But how does I2C work, and what makes it a useful protocol?

How I2C works

I2C, which stands for Inter-Integrated Circuit, is a protocol that input and output components can use to communicate with microcontrollers. It's very standardized, and nearly all I2C devices operate in the same way. I2C also has the major benefit of being recognized and implemented by many major microcontroller manufacturers—including Arduino. Any Arduino platform that is compatible with Johnny-Five is also compatible with I2C devices.

However, implementing an I2C device in Johnny-Five can be a tricky proposition. To understand why, we...

External Devices


SPI, I2C, and some other protocols get really complex, but there's a whole other world of devices we can also use with our Johnny-Five projects. These technically fall outside the scope of the Johnny-Five library, but works well with the library due to their use of Node.JS. Let's take a look at the "why" and the "how" of external devices with Johnny-Five.

Why External Devices?

Microcontrollers are amazing! They can do a litany of things—input, output, and so on - but sometimes you see a new device that doesn't plug in to a microcontroller. Perhaps it contains a microcontroller of its own—such as a quadcopter or a drone! This section covers the use of these devices with Node and integrating them into your Johnny-Five projects.

Some really cool examples that have been integrated into Johnny-Five projects are as follows: video game controllers; the LeapMotion controller, a gesture sensor that tracks hand position and movement; and Wi-Fi-enabled quadcopters, such as the Parrot...

Summary


We've done a lot in this chapter. You've learned about SPI, I2C, and how to use external libraries and devices with Johnny-Five. In the next (and sadly, final) chapter, we'll discuss the use of different microcontrollers with Johnny-Five and how to connect your projects to the Internet.

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}