Programming the BeagleBone Black: Getting Started with JavaScript and BoneScript
By Simon Monk
()
About this ebook
Build creative BeagleBone Black devices--no prior programming or electronics experience required. In Programming the BeagleBone Black, electronics guru Simon Monk explains essential application development methods through straightforward directions and cool downloadable examples. Discover how to navigate the board, write and debug code, use expansion capes, and control external hardware. Easy-to-follow plans show you how to wire up and program a Web-controlled roving robot and an e-mail notifier that lights an incandescent lamp.
- Set up the BeagleBone Black and explore its features
- Connect to your computer via USB or Ethernet
- Use the BeagleBone Black as a stand-alone PC
- Write and execute BoneScript code
- Use JavaScript functions and timers
- Perform analog and digital I/O
- Work with expansion capes and modules
- Design Web interfaces that control electronics
- Assemble and program a robot and an e-mail notifier
Simon Monk
Simon Monk is a full-time author and maker, mostly writing about electronics for makers. Some of his better-known books include Programming Arduino: Getting Started with Sketches, Raspberry Pi Cookbook, and Hacking Electronics. He is also the co-author of Practical Electronics for Inventors and wrote Minecraft Mastery with his son, Matthew Monk.
Read more from Simon Monk
Programming the Raspberry Pi, Third Edition: Getting Started with Python Rating: 5 out of 5 stars5/5Minecraft Mastery: Build Your Own Redstone Contraptions and Mods Rating: 0 out of 5 stars0 ratingsProgramming Arduino: Getting Started with Sketches Rating: 4 out of 5 stars4/5Arduino + Android Projects for the Evil Genius: Control Arduino with Your Smartphone or Tablet Rating: 5 out of 5 stars5/5Hacking Electronics: Learning Electronics with Arduino and Raspberry Pi, Second Edition Rating: 0 out of 5 stars0 ratingsProgramming FPGAs: Getting Started with Verilog Rating: 4 out of 5 stars4/530 Arduino Projects for the Evil Genius, Second Edition Rating: 5 out of 5 stars5/5Hacking Electronics: An Illustrated DIY Guide for Makers and Hobbyists Rating: 4 out of 5 stars4/5Programming Arduino: Getting Started with Sketches, Third Edition Rating: 0 out of 5 stars0 ratingsTeach Yourself Electricity and Electronics, Seventh Edition Rating: 0 out of 5 stars0 ratingsTeach Yourself Electricity and Electronics, 6th Edition Rating: 4 out of 5 stars4/515 Dangerously Mad Projects for the Evil Genius Rating: 4 out of 5 stars4/5The Maker's Guide to the Zombie Apocalypse: Defend Your Base with Simple Circuits, Arduino, and Raspberry Pi Rating: 4 out of 5 stars4/5Programming Arduino Next Steps: Going Further with Sketches, Second Edition Rating: 3 out of 5 stars3/5Programming Arduino Next Steps: Going Further with Sketches Rating: 3 out of 5 stars3/5Fritzing for Inventors: Take Your Electronics Project from Prototype to Product Rating: 0 out of 5 stars0 ratingsProgramming the BBC micro:bit: Getting Started with MicroPython Rating: 0 out of 5 stars0 ratingsPractical Electronics for Inventors, Fourth Edition Rating: 4 out of 5 stars4/5The TAB Book of Arduino Projects: 36 Things to Make with Shields and Proto Shields Rating: 5 out of 5 stars5/5Make Your Own PCBs with EAGLE: From Schematic Designs to Finished Boards Rating: 5 out of 5 stars5/5Programming the Raspberry Pi, Second Edition: Getting Started with Python Rating: 0 out of 5 stars0 ratingsMicro:bit for Mad Scientists: 30 Clever Coding and Electronics Projects for Kids Rating: 0 out of 5 stars0 ratings
Related to Programming the BeagleBone Black
Related ebooks
Programming the BeagleBone Rating: 0 out of 5 stars0 ratingsLearn Arduino Prototyping in 10 days Rating: 0 out of 5 stars0 ratingsMake Your Own PCBs with EAGLE: From Schematic Designs to Finished Boards Rating: 5 out of 5 stars5/530 BeagleBone Black Projects for the Evil Genius Rating: 0 out of 5 stars0 ratingsBeagleBone Home Automation Blueprints Rating: 0 out of 5 stars0 ratingsArduino Electronics Blueprints Rating: 4 out of 5 stars4/5Learning Raspbian Rating: 5 out of 5 stars5/5Raspberry Pi Robotics Essentials Rating: 0 out of 5 stars0 ratingsRaspberry Pi 3 Projects for Java Programmers Rating: 0 out of 5 stars0 ratingsBeagleBone For Dummies Rating: 0 out of 5 stars0 ratingsDigital Electronics with Arduino: Learn How To Work With Digital Electronics And MicroControllers Rating: 5 out of 5 stars5/5Programming the Intel Edison: Getting Started with Processing and Python Rating: 1 out of 5 stars1/5Android Application Programming with OpenCV Rating: 3 out of 5 stars3/5Internet of Things with Intel Galileo Rating: 0 out of 5 stars0 ratingsBuilding Wireless Sensor Networks Using Arduino Rating: 4 out of 5 stars4/5Learning C for Arduino Rating: 0 out of 5 stars0 ratingsRaspberry Pi LED Blueprints Rating: 0 out of 5 stars0 ratingsGetting Started with Electronic Projects Rating: 5 out of 5 stars5/5Microsoft .NET Gadgeteer: Electronics Projects for Hobbyists and Inventors Rating: 0 out of 5 stars0 ratingsProgramming with STM32: Getting Started with the Nucleo Board and C/C++ Rating: 3 out of 5 stars3/5Building Smart Homes with Raspberry Pi Zero Rating: 0 out of 5 stars0 ratingsBuilding a Home Security System with Raspberry Pi Rating: 0 out of 5 stars0 ratingsMastering BeagleBone Robotics Rating: 5 out of 5 stars5/5Projects With Microcontrollers And PICC Rating: 5 out of 5 stars5/5Programming the Photon: Getting Started with the Internet of Things Rating: 5 out of 5 stars5/5Electronic Troubleshooting, Fourth Edition Rating: 5 out of 5 stars5/5BeagleBone Robotic Projects Rating: 5 out of 5 stars5/5Raspberry Pi Server Essentials Rating: 0 out of 5 stars0 ratingsFritzing for Inventors: Take Your Electronics Project from Prototype to Product Rating: 0 out of 5 stars0 ratings
Electrical Engineering & Electronics For You
Electrical Engineering | Step by Step Rating: 0 out of 5 stars0 ratingsRaspberry Pi Electronics Projects for the Evil Genius Rating: 3 out of 5 stars3/5How to Diagnose and Fix Everything Electronic, Second Edition Rating: 4 out of 5 stars4/5Raspberry Pi Projects for the Evil Genius Rating: 0 out of 5 stars0 ratingsElectrician's Pocket Manual Rating: 0 out of 5 stars0 ratingsCircuitbuilding Do-It-Yourself For Dummies Rating: 0 out of 5 stars0 ratingsBasic Electricity Rating: 4 out of 5 stars4/5The Innovators: How a Group of Hackers, Geniuses, and Geeks Created the Digital Revolution Rating: 4 out of 5 stars4/5Upcycled Technology: Clever Projects You Can Do With Your Discarded Tech (Tech gift) Rating: 5 out of 5 stars5/5Beginner's Guide to Reading Schematics, Fourth Edition Rating: 4 out of 5 stars4/5The Homeowner's DIY Guide to Electrical Wiring Rating: 5 out of 5 stars5/5Electricity for Beginners Rating: 4 out of 5 stars4/5THE Amateur Radio Dictionary: The Most Complete Glossary of Ham Radio Terms Ever Compiled Rating: 4 out of 5 stars4/5Ramblings of a Mad Scientist: 100 Ideas for a Stranger Tomorrow Rating: 0 out of 5 stars0 ratingsBasic Electronics: Book 2 Rating: 5 out of 5 stars5/5The Off Grid Solar Power Bible For Beginners Rating: 0 out of 5 stars0 ratingsUnderstanding Electricity Rating: 4 out of 5 stars4/5Two-Stroke Engine Repair and Maintenance Rating: 0 out of 5 stars0 ratingsElectrical Engineering Rating: 4 out of 5 stars4/5Solar & 12 Volt Power For Beginners Rating: 4 out of 5 stars4/5Beginner's Guide to Reading Schematics, Third Edition Rating: 0 out of 5 stars0 ratingsSoldering electronic circuits: Beginner's guide Rating: 4 out of 5 stars4/5Electric Circuits Essentials Rating: 5 out of 5 stars5/5Hacking Electronics: An Illustrated DIY Guide for Makers and Hobbyists Rating: 4 out of 5 stars4/5DIY Lithium Battery Rating: 3 out of 5 stars3/5Nikola Tesla: Colorado Springs Notes, 1899-1900 Rating: 0 out of 5 stars0 ratingsProgramming FPGAs: Getting Started with Verilog Rating: 4 out of 5 stars4/5The Electrician's Trade Demystified Rating: 0 out of 5 stars0 ratings
Reviews for Programming the BeagleBone Black
0 ratings0 reviews
Book preview
Programming the BeagleBone Black - Simon Monk
this.
INTRODUCTION
The BeagleBone Black combines the power of a Linux-based single-board computer with the GPIO (General Purpose Input/Output) capabilities of an Arduino.
This book shows you how to program a BeagleBone Black using its hardware library (BoneScript) and JavaScript. The book is suitable for beginner programmers and does not assume any prior programming experience.
Downloads
The book includes many example programs, which are all open source and available from the book’s website at http://beaglebonebook.com. You will need to follow the link to the code. You will also find an up-to-date list of errata for the book here.
What Will I Need?
This is a book primarily about software. So, for most of the examples, all you really need is BeagleBone Black. However, when you are using GPIO pins, a low-cost multimeter is helpful. Male-to-male jumper leads, or even just a short length of solid core wire that can be poked into the sockets of the GPIO connector, will be needed.
The two hardware project chapters require specific parts, which are listed in those chapters.
Other parts that are recommended when learning to use the GPIO pins are a solderless breadboard and/or a BeagleBone Breadboard cape and a selection of basic components such as LEDs and switches.
Appendix A at the end of this book indicates possible suppliers for these parts.
Using This Book
This book’s main focus is on learning to program the BeagleBone Black from the viewpoint of someone who is new to programming. It therefore starts with basic concepts, gradually building on the earlier material while heading toward more advanced topics. It therefore needs to be read more or less in order.
This book is organized into the following chapters:
• Chapter 1: Introduction and Setup This chapter introduces the BeagleBone Black. We look at what you can do with it as well as setting it up so that you can connect to it from your main computer over a USB or network connection.
• Chapter 2: A Linux Computer The BeagleBone Black is capable of functioning as a tiny general-purpose computer, using a keyboard, mouse, and monitor. In this chapter, you will explore this aspect of the BeagleBone Black.
• Chapter 3: JavaScript Basics Although you can program the BeagleBone Black in many different languages, the standard and BeagleBoard-recommended language is BoneScript, which is an extension of JavaScript. In this chapter, we get started with JavaScript programming from the point of view of the novice programmer. We will also start some simple code examples, including turning on and off the LEDs built into the BeagleBone Black.
• Chapter 4: JavaScript Functions and Timers JavaScript is different from many of the languages used to program embedded electronics. It does not have a delay
or sleep
function. Instead, it has the concept of timers.
In this chapter, you will also start to develop an example to flash Morse code signals using the built-in LEDs.
• Chapter 5: Arrays, Objects, and Modules In this chapter we get into more advanced topics and continue with the Morse code example, building it up to a general-purpose translator that will flash any message you supply as text as Morse code.
• Chapter 6: BoneScript This chapter concentrates on the BoneScript library itself. You will learn how to use it to control digital and analog outputs as well as read values from analog and digital inputs.
• Chapter 7: Hardware Interfacing Although this is a book primarily about programming, the basics of attaching electronics, such as LEDs, switches, servos, and so on, are described in this chapter, along with just enough electronics theory to get your project going.
• Chapter 8: Using Capes and Modules This chapter is concerned with programming and using expansion capes and modules, including I2C and serial modules as well as motor controllers.
• Chapter 9: Web Interfaces The web-serving capabilities of the BeagleBone Black provide a great way of making a web interface for your projects.
• Chapters 10 and 11 These two chapters bring together topics from throughout the book to provide end-to-end project examples. The first project is a web-controlled roving robot, and the second is an e-mail notifier that uses a 12V incandescent lamp to notify you of incoming e-mails.
Resources
This book is supported by web pages located at http://beaglebonebook.com. You will find all the source code used in the book here, as well as other resources such as errata.
1
Introduction and Setup
This chapter introduces you to the BeagleBone Black. You’ll discover what it can be used for and, more importantly, what it is most suited to. You’ll also learn how to connect to your BeagleBone Black over USB and immediately get started running some simple sample programs that are shipped with the BeagleBone Black.
What Is the BeagleBone Black?
The BeagleBone Black, shown in Figure 1-1, is America’s answer to the massively successful British invention, the Raspberry Pi. The BeagleBone Black is a $45 credit-card-sized single-board computer that is currently being shipped in huge numbers to hobbyists and makers keen to learn how to program the board and connect it up to external electronics.
Figure 1-1 A BeagleBone Black
Sitting somewhere between a Raspberry Pi and an Arduino, the BeagleBone Black is a single-board Linux computer that can be used both as a platform for embedded hardware projects and as a general-purpose Linux computer. Some of the important features of the board are:
• A 1 GHz ARM processor with 512MB of RAM.
• An operating system preinstalled to built-in flash storage (2GB). The operating system is upgradable by microSD card, or you can boot directly from the microSD card.
• A 10-second boot time. The BeagleBone Black boots up very quickly, which is important when you are using it to control electronics.
• Over 60 digital input/output pins.
• Seven analog inputs.
• An ever-expanding range of plug-in Capes
that provide ready-made electronic expansion for LCD touch screens, battery powering your BeagleBone Black, and so on.
• An Ethernet network port.
• A micro-HDMI socket for video.
• A USB port to attach peripherals such as a keyboard and mouse.
• Low-power operation (less than 2W).
The BeagleBone Black is actually the latest in a long line of BeagleBone boards. The reason for its success is that it is much cheaper than its predecessors, being priced to compete with the Raspberry Pi. It is also a very sleek and aesthetically pleasing board.
The BeagleBoard line of single-board computers was originally designed to showcase Texas Instrument’s family of System on a Chip devices, but has gathered a momentum of its own as a platform for embedded computing.
A Tour of the BeagleBone Black
Figure 1-2 provides a quick tour of the BeagleBone Black’s hardware features and connections.
Figure 1-2 The BeagleBone Black hardware features and connections
Starting with the Ethernet socket, let’s work our way around the board clockwise:
• Ethernet port Allows the board to be connected to a wired network. The maximum speed is 100 Mbps.
• User LEDs LEDs USR0 to USR3 can be controlled by your code, allowing you to experiment with digital outputs without having to attach external electronics.
• Power button If the BeagleBone Black has been shut down from the Linux command line, pressing this button will turn the BeagleBone Black back on.
• Reset button Press this button to cause a reset of the BeagleBone Black.
• GPIO expansion header For attaching external electronics or expansion boards (Capes).
• Boot button You only use this button when updating the flash memory of the board from a ROM image on a microSD card.
• MicroSD card holder You can use an SD card to hold an image to flash the BeagleBone Black’s built-in flash storage, or to boot the BeagleBone Black from it directly.
• Micro-HDMI port This port allows you to connect the BeagleBone Black to HDMI using a micro-HDMI-to-HDMI lead. These leads are also used to connect smartphones to TVs and are readily available for a low cost.
• USB host port You can use this port to connect a variety of USB devices to the BeagleBone Black, including a keyboard, mouse, Wi-Fi dongle, and web cam.
• Micro-USB port This port is used for power and communication with your computer.
Example BeagleBone Black Projects
If you’re wondering exactly what you might like to do with your BeagleBone Black, the Internet is a great source of ideas. Many interesting projects out there use the BeagleBone Black. If you visit www.instructables.com and search for BeagleBone Black, you will find all sorts of projects, including the following:
• Environmental monitoring (measuring temperature, humidity, and so on, and displaying the information on a screen, logging it to an SD card, or providing a web interface to the data)
• Building robots of many different shapes and sizes
• Creating a home automation system
• Controlling a beer-brewing apparatus, coffee machine, and so on
First Boot of the BeagleBone Black
That’s definitely enough talk. It’s now time to take the BeagleBone Black out of its elegant little box and boot it up!
Connecting via USB
You have two ways of interacting with a BeagleBone Black. One is to connect it up to a keyboard, mouse, and monitor and use it like a regular computer. We will do this in Chapter 2. However, for most uses of the BeagleBone Black, where you use it to control electronics, it is more convenient to interact with the BeagleBone Black using a second computer (Windows, Mac, or Linux) and use a USB cable to power the BeagleBone Black as well as to communicate with the board.
The BeagleBone Black ships with a USB lead; the standard-sized USB plug end goes into a free port on your main computer and the mini-USB plug fits into the mini-USB port next to the Ethernet socket on the BeagleBone Black (see Figure 1-3).
Figure 1-3 Connecting the BeagleBone Black using USB
The BeagleBone Black is ready to go, out of the box. It has an operating system (OS) installed. You will probably want to update the OS later, but for now everything is good to go, so plug the BeagleBone Black into a spare USB socket on your computer.
The BeagleBone Black will immediately start to boot up. You can tell it is doing this because the blue LEDs above the mini-USB socket will start to flash. When it has finished booting, the LED furthest from the Ethernet port will start flashing in a kind of heartbeat pattern (ba-dum… ba-dum…).
The BeagleBone Black is drawing its power from the USB connection. It will be drawing about 300 mA of the normal maximum of 500 mA that a USB port can provide. Therefore, it should be fine on just about any USB connection, as long as you do not add any extra hardware such as a Wi-Fi dongle to it.
As the BeagleBone Black boots, you may notice your operating system mounting the BeagleBone Black as a USB storage device. You can ignore this for now.
Using the USB Network Bridge
Having booted from its out-of-the-box operating system (Ångström Linux), the BeagleBone Black runs a mini web server. It is through this web server that we are going to communicate with and program the BeagleBone Black.
You can connect to the BeagleBone Black’s web server by hooking it up to your network using an Ethernet patch cable connected to your home network hub or router. However, you do not need to do this, because those clever folks at BeagleBone have included software on the BeagleBone Black that lets its USB connection (the one we are already plugged in to) act as a network connection to