Arduino and LEGO Projects
By Jon Lazar
()
About this ebook
- Learn that SNOT is actually cool (it means Studs Not on Top)
- See detailed explanations and images of how everything fits together
- Learn how Arduino fits into each project, including code and explanations
Whether you want to impress your friends, annoy the cat, or just kick back and bask in the awesomeness of your creations, Arduino and LEGO Projects shows you just what you need and how to put it all together.
Related to Arduino and LEGO Projects
Related ebooks
The LEGO Arduino Cookbook: Expanding the Realm of MINDSTORMS EV3 Invention Rating: 0 out of 5 stars0 ratingsProgramming Microcontrollers with Python: Experience the Power of Embedded Python Rating: 0 out of 5 stars0 ratingsSmart Home Automation with Linux and Raspberry Pi Rating: 3 out of 5 stars3/5Moving from Unity to Godot: An In-Depth Handbook to Godot for Unity Users Rating: 0 out of 5 stars0 ratingsRobot Operating System (ROS) for Absolute Beginners: Robotics Programming Made Easy Rating: 0 out of 5 stars0 ratingsRaspberry Pi Gaming - Second Edition Rating: 0 out of 5 stars0 ratingsFoundations of Python Network Programming Rating: 4 out of 5 stars4/5Intel Galileo Essentials Rating: 0 out of 5 stars0 ratingsBeginning Android C++ Game Development Rating: 0 out of 5 stars0 ratingsPractical Video Game Bots: Automating Game Processes using C++, Python, and AutoIt Rating: 0 out of 5 stars0 ratingsLearn OpenGL ES: For Mobile Game and Graphics Development Rating: 0 out of 5 stars0 ratingsRaspberry Pi Blueprints Rating: 0 out of 5 stars0 ratingsRaspberry Pi IoT Projects: Prototyping Experiments for Makers Rating: 0 out of 5 stars0 ratingsLearning LEGO MINDSTORMS EV3 Rating: 5 out of 5 stars5/5Corona SDK Mobile Game Development: Beginner's Guide - Second Edition Rating: 0 out of 5 stars0 ratingsRaspberry Pi Retro Gaming: Build Consoles and Arcade Cabinets to Play Your Favorite Classic Games Rating: 0 out of 5 stars0 ratingsLearning Libgdx Game Development Rating: 0 out of 5 stars0 ratingsDeveloping Turn-Based Multiplayer Games: with GameMaker Studio 2 and NodeJS Rating: 0 out of 5 stars0 ratingsBeginning Game AI with Unity: Programming Artificial Intelligence with C# Rating: 0 out of 5 stars0 ratingsGameSalad Essentials Rating: 0 out of 5 stars0 ratingsBuilding JavaScript Games: for Phones, Tablets, and Desktop Rating: 0 out of 5 stars0 ratingsRust for the IoT: Building Internet of Things Apps with Rust and Raspberry Pi Rating: 0 out of 5 stars0 ratingsOptimizing Visual Studio Code for Python Development: Developing More Efficient and Effective Programs in Python Rating: 0 out of 5 stars0 ratingsIoT Development for ESP32 and ESP8266 with JavaScript: A Practical Guide to XS and the Moddable SDK Rating: 0 out of 5 stars0 ratingsSFML Blueprints Rating: 0 out of 5 stars0 ratingsBeginning Application Lifecycle Management Rating: 0 out of 5 stars0 ratingsC# Programming for Absolute Beginners: Learn to Think Like a Programmer and Start Writing Code Rating: 0 out of 5 stars0 ratingsSFML Essentials: Getting Started with Game Development: SFML Fundamentals Rating: 0 out of 5 stars0 ratingsMastering the Raspberry Pi Rating: 3 out of 5 stars3/5Ouya Unity Game Development Rating: 0 out of 5 stars0 ratings
Hardware For You
Build Your Own PC Do-It-Yourself For Dummies Rating: 4 out of 5 stars4/5CompTIA A+ Complete Review Guide: Core 1 Exam 220-1101 and Core 2 Exam 220-1102 Rating: 5 out of 5 stars5/5Exploring Apple iPad: iPadOS 15 Edition: The Illustrated, Practical Guide to Using your iPad Rating: 0 out of 5 stars0 ratingsElectrical Engineering | Step by Step Rating: 0 out of 5 stars0 ratingsExploring Arduino: Tools and Techniques for Engineering Wizardry Rating: 4 out of 5 stars4/5Chip War: The Fight for the World's Most Critical Technology Rating: 4 out of 5 stars4/5CompTIA A+ Complete Review Guide: Exam Core 1 220-1001 and Exam Core 2 220-1002 Rating: 5 out of 5 stars5/5After Steve: How Apple Became a Trillion-Dollar Company and Lost Its Soul Rating: 4 out of 5 stars4/5How To Repair Your iPad - Quickly and Professionally!: Fix It Yourself, #5 Rating: 0 out of 5 stars0 ratingsProgramming Arduino: Getting Started with Sketches Rating: 4 out of 5 stars4/5iPhone For Dummies Rating: 0 out of 5 stars0 ratingsHelp! iOS 17 - iPhone: How to Use iOS17 Rating: 0 out of 5 stars0 ratingsDancing with Qubits: How quantum computing works and how it can change the world Rating: 5 out of 5 stars5/5Raspberry Pi Cookbook for Python Programmers Rating: 0 out of 5 stars0 ratingsLinux All-In-One For Dummies Rating: 2 out of 5 stars2/5Raspberry Pi Electronics Projects for the Evil Genius Rating: 3 out of 5 stars3/5Chromebook For Dummies Rating: 5 out of 5 stars5/5Troubleshooting and Maintaining Your PC All-in-One For Dummies Rating: 5 out of 5 stars5/5Tor Darknet Bundle: Master the Art of Invisibility Rating: 0 out of 5 stars0 ratingsmacOS Sonoma For Dummies Rating: 0 out of 5 stars0 ratingsAmazon Web Services (AWS) Interview Questions and Answers Rating: 5 out of 5 stars5/5Experts' Guide to iPad Note Taking with an Apple Pencil Rating: 3 out of 5 stars3/5MacBook For Dummies Rating: 4 out of 5 stars4/5Creative Selection: Inside Apple's Design Process During the Golden Age of Steve Jobs Rating: 5 out of 5 stars5/5M2 Mac Book Air User Guide: Features, Tips, and Tricks With Pictures Rating: 0 out of 5 stars0 ratings
Reviews for Arduino and LEGO Projects
0 ratings0 reviews
Book preview
Arduino and LEGO Projects - Jon Lazar
Jon LazarArduino and LEGO Projects10.1007/978-1-4302-4930-6_1© Jon Lazar 2013
1. LEGO, Arduino, and The Ultimate Machine
Jon Lazar¹
(1)
NewYork, USA
Abstract
For years LEGO has produced their own computer based system known as Mindstorms. It gave a computer brain to the plastic LEGO bricks that had been around for decades. While Mindstorms has advanced in the 15 years since it was introduced, it was still limited based on the size of the LEGO Intelligent Brick and the available sensors and motors. An alternative to using the LEGO Mindstorms is the Arduino microprocessor, a small computer that can make use of any electrical components with some programming.
For years LEGO has produced their own computer based system known as Mindstorms. It gave a computer brain to the plastic LEGO bricks that had been around for decades. While Mindstorms has advanced in the 15 years since it was introduced, it was still limited based on the size of the LEGO Intelligent Brick and the available sensors and motors. An alternative to using the LEGO Mindstorms is the Arduino microprocessor, a small computer that can make use of any electrical components with some programming.
Introducing the Arduino
An Arduino (as seen in Figure 1-1) is an open source microcontroller that allows for programming and interaction; it is programmed in C/C++ with an Arduino library to allow it to access the hardware. This allows for more flexible programmability and the ability to use any electronics that can interface with the Arduino. Because the Arduino is open source, the plans for the circuits are available online for free to anyone who wants to use and create their own based on the schematics, as long as they share what they create. This allows for a lot of customizability in projects, since people have built Arduinos of different sizes, shapes, and power levels to control their projects.
A978-1-4302-4930-6_1_Fig1_HTML.jpgFigure 1-1.
The Arduino microcontroller
The main advantages of using the Arduino over LEGO’s own motor systems are the open source base, the expandability, and the sizes. With LEGO’s system, the user is locked into the pieces LEGO created. This can be a hindrance with smaller projects where the Mindstorms NXT Intelligent Brick can be too large to easily incorporate or hide the intelligence behind the project. With the smaller Arduino circuit board, less clearance is required to hold the circuit board, which means more flexibility in the design of the project. A comparison of the Arduino and the LEGO NXT brick can be seen in Figure 1-2.
A978-1-4302-4930-6_1_Fig2_HTML.jpgFigure 1-2.
The Arduino and the LEGO Mindstorms NXT Intelligent Brick
The Arduino itself may not be capable of fulfilling all the activities that you would like to carry out with it, but there are circuit boards known as shields that snap on top of the Arduino circuit board to expand the usability of the Arduino. Allowing the use of motors, adding Internet connectivity, making sounds with.wav files, and other activities can be triggered through the use of these add-on boards, thus allowing the Arduino to be programmed to carry out tasks it could not without them. As an example, Figure 1-3 shows an Ethernet shield that allows the Arduino to connect to the Internet.
A978-1-4302-4930-6_1_Fig3_HTML.jpgFigure 1-3.
An Ethernet shield to allow the Arduino to talk to the Internet
Your First Arduino Program
Most commonly, when someone tries out a new computer language, they make the words Hello World
appear on the screen. The Arduino version of this is to make a light-emitting diode (LED) blink. By plugging the LED into two of the ports on the Arduino and writing a simple program, the Arduino can turn the light on and off.
The first step is to put the LED into the Arduino. LEDs are specific to the way they are used. The LED needs to be plugged in so that the longer end goes into a numbered pin and the shorter pin into the ground pin, or the LED will not light up. Figure 1-4 shows the longer side in the socket labeled 13 and the shorter side in the ground.
A978-1-4302-4930-6_1_Fig4_HTML.jpgFigure 1-4.
The LED plugged into the Ardunio
Once the LED is firmly placed in the Arduino, the next step is to connect it to a computer via USB cable. The computer must have the Arduino software installed in order to program the Arduino. The software can be downloaded for free at arduino.cc in the download section for your computer operating system of choice. Once it is downloaded and installed, open the Arduino software. The following program can be found in File ➤ Examples ➤ 01.Basics ➤ Blink or it can be entered by hand, as shown in Listing 1-1.
Listing 1-1. Basic Blink Program
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
This example code is in the public domain.
*/
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
The code in Listing 1-1 is the most basic program for an Arduino. It is read by the Arduino from the top down. The first thing in the program is a global variable definition for the pin that has the LED. A global variable is defined outside the setup() and loop() functions and can be accessed from anywhere in the program. The line int led=13; defines the global variable named led to be an integer with the value of 13. Whenever the word led is used, the program will interpret it as the number 13. Since the variable is defined before the words void setup(); it is what is referred to as a global variable, which means any part of the program can access and make changes to it. If the variable had been defined in the setup or loop sections (as defined below), it would only be a local variable that could only be accessed by that section of code. It is worth noting that anything between the symbols /* and */ or on a line after // are comments and will be ignored by the computer when it reads the program.
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}
Anything between the braces after setup() will be executed when the program first runs. Anything in there will be run only once and never be looked at again. In this case, it using pinMode to tell the Arduino that it will be using pin 13, where you defined led, to be used to send a signal out. It is notable that the pins can be used for either input or output, but must be defined to do so.
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
Once the setup runs, it then executes whatever is between the braces after loop(). The difference is that once the section in the loop() starts, it will start that code over again once it reaches the end. Pin 13 on the Arduino has only two states, off and on. The digitalWrite function tells the light to turn on and off based on whether it is told to be HIGH or LOW. Putting a delay between the digitalWrite statements provides the ability to see the light turn on and off rather than just a strobe effect. The delay statement will wait as long as the number in the parentheses is, in thousandths of a second.
With the code written, it needs to be uploaded to the Arduino. By connecting it with a standard USB cable, the computer can talk to the Arduino. Clicking the arrow in the upper right hand corner will compile the code and upload it to the Arduino. Once installed, it will begin to execute after several seconds and the LED will begin to blink on and off.
Programming the Ultimate Machine
The Ultimate Machine, also known as The Useless Machine, is considered the most efficient machine ever made. Its only task is to turn itself off when it is turned on. The original Ultimate Machine was created by Claude Shannon when he was working at Bell Labs in 1952. The following sections explain the steps involved.
Assembling the Arduino and Motor
In order to build the Useless Machine, a motor is required. To drive the motor, a motor shield will need to be placed on top of the Arduino. While there are a few different shields that would allow for a motor to connect to the Arduino, we will be using the Adafruit Industries motor shield because we can use it to drive the different kinds of motors you will be using in different projects in this book. Figure 1-5 shows the motor shield from Adafruit Industries in its unassembled form.
A978-1-4302-4930-6_1_Fig5_HTML.jpgFigure 1-5.
The unassembled motor shield
There is often considered a do-it-yourself (DIY) aspect to open source hardware, and sometimes manufactures will sell products like shields with some assembly required. With some basic soldering knowledge, they are not too complex to put together. The instructions on how to assemble it can be found at www.ladyada.net/make/mshield/solder.html . Figure 1-6 shows the motor shield assembled.
A978-1-4302-4930-6_1_Fig6_HTML.jpgFigure 1-6.
The assembled motor shield
Once the motor shield is soldered together, it snaps in easily on top of the Arduino. Press them together firmly but do not push too hard. Once they are together, it’s time to add the motor. The Adafruit motor shield supports DC motors, servo motors, and stepper motors. For this project, you’ll be using a servo motor. The motor’s wires plug in on the top left on the three pronged plugs (see Figure 1-7).
In the Blink example, you power the Arduino with the USB cable to the computer. Since this project will eventually be independent of the computer, a battery pack or wall adapter will be required to power the project. If