Book Image

Hands-On Robotics with JavaScript

By : Kassandra Perch
Book Image

Hands-On Robotics with JavaScript

By: Kassandra Perch

Overview of this book

JavaScript has an effective set of frameworks and libraries that provide support for embedded device programming and the robotics ecosystem. You’ll be able to put your JavaScript knowledge to work with this practical robotics guide. The book starts by guiding you in setting up an environment to program robots with JavaScript and Rasberry Pi 3. You will build beginner-level projects, such as a line-following robot, and then upgrade your robotics skills with a series of projects that help you get to grips with the Johnny-Five library. As you progress, you’ll learn how you can improve your projects by enabling advanced hardware components and programming concepts. You’ll even build an advanced AI-enabled robot, connect its NodeBots to the internet, create a NodeBots Swarm, and explore Message Queuing Telemetry Transport (MQTT). By the end of this book, you will have enhanced your robot programming skills by building a range of simple to complex projects.
Table of Contents (19 chapters)
Title Page
Dedication
Packt Upsell
Contributors
Preface
Index

PWM pins and GPIO expanders


Before we wire up and run our RGB LED project, a discussion about PWM pins and GPIO expanders is warranted, because these are topics that will affect most Johnny-Five projects that you will complete.

How do PWM pins work?

You don't always want an LED to at its full brightness, especially in the case of RGB LEDs, where the brightness of each channel (red, green, and blue) determines the perceived color of the LED. The pins on most microcontrollers are digital: they are either HIGH at 5V or LOW at 0V. So how do you adjust the brightness of an LED with these types of pins? The answer involves the idea of average voltage and the speed at which we can flip a digital pin from HIGH to LOW. 

Pulse-width modulation, or PWM, pins operate by setting, effectively, the percentage of time that a pin is HIGH and LOW. The following screenshot shows an oscilloscope reading for the state of a pin running at 50% over a short period of time:

 

 

 

 

 

 

 

 

 

 

Oscilloscope reading for a 50% PWM...