You are previewing Make: Lego and Arduino Projects.

Make: Lego and Arduino Projects

Cover of Make: Lego and Arduino Projects by John Baichtal... Published by O'Reilly Media, Inc.
  1. Special Upgrade Offer
  2. A Note Regarding Supplemental Files
  3. Foreword
  4. Preface
    1. Assumptions This Book Makes
    2. Contents of This Book
    3. Conventions Used in This Book
      1. Lego CAD Conventions
    4. Using Code Examples
    5. Safari® Books Online
    6. How to Contact Us
    7. Acknowledgments for John
    8. Acknowledgments for Matthew
    9. Acknowledgments for Adam
  5. 1. Project: Drawbot
    1. Parts List
      1. Tools & Electronics
      2. Lego Elements
    2. Assembly Instructions
      1. Build the Lego Model
      2. Attach the Arduino and Bricktronics Shield
      3. Attach the Pen
    3. Program the Robot
      1. Processing and Arduino
      2. Setting Up the Programming Environment
      3. Don’t Forget Your Libraries!
      4. The Arduino Sketch
    4. The Next Chapter
  6. 2. Anatomy of Lego Robotics
    1. Mindstorms
      1. The NXT Brick
      2. Sensors
      3. Motors
      4. Wires
      5. Technic Beams, Mechanics & Connectors
    2. Expanding on the Mindstorms Set
      1. Buying More
      2. Add-On Electronics
      3. Third-Party Bricks
      4. Non-Mindstorms Lego Bricks
      5. Power Functions
    3. Programming Your Brick
    4. The Next Chapter
  7. 3. Arduino Interlude
    1. History of the Arduino Project
    2. What Is OSHW?
    3. Anatomy of the Uno
    4. The Arduino Ecosystem
      1. Uno Alternatives
      2. Older Models
      3. Shields
    5. Arduino Resources
      1. Books
      2. Websites
      3. Code
    6. The Next Chapter
  8. 4. Project: Clock
    1. Parts List
      1. Tools & Electronics
      2. Lego Elements
    2. Assembly Instructions
      1. Prepare the Gear
      2. Build the Lego Model
      3. Install the Arduino
    3. Program the Robot
    4. Setting the Clock
    5. The Next Chapter
  9. 5. Project: Chocolate Milk Maker
    1. Parts List
      1. Tools & Electronics
      2. Beverage Handling
      3. Food
      4. Lego Elements
    2. Assembly Instructions
      1. Build the Pump Assembly
      2. Build the Lego Model
      3. Attach the Syrup Bottle
      4. Build the Mixing Attachment
      5. Wire up and Install the Electronics
    3. Program the Robot
    4. The Next Chapter
  10. 6. Basic Electronic Theory
    1. Basic Concepts in Electricity
      1. Voltage, Current & Resistance
      2. Ohm’s Law
      3. Other Concepts
    2. Know Your Electronic Components
      1. Resistors
      2. Capacitors
      3. Diodes and LEDs
      4. Inductors
      5. Transistors
      6. Integrated Circuits
    3. Sensors 101
      1. Digital Sensors
      2. Analog Sensors
    4. Further Study
  11. 7. Gripperbot
    1. Parts List
      1. Electronics Parts
      2. Lego Elements
    2. Building Instructions
      1. Bracers
      2. Gripperbot
    3. Assembling the Gripperbot’s Electronics
    4. Programming the Gripperbot
    5. The Next Chapter
  12. 8. Project: Keytar
    1. Parts List
      1. Tools & Electronics
      2. Lego Elements
    2. Assembly Instructions
      1. Build the Lego Model
      2. Install the Electronics
    3. Program the Keytar
    4. Play Some Music!
    5. The Next Chapter
  13. 9. Project: Lamp
    1. Parts List
      1. Tools & Electronics
      2. Lego Elements
    2. Assembly Instructions
      1. Build the Lego Model
      2. Install the Electronics
      3. Download and Install the App
    3. Program the Lamp
    4. The Next Chapter
  14. 10. Advanced Techniques
    1. Wiring Up Bricktronics Equivalents
    2. Mounting PCBs to Legos
    3. Adding Molex Connectors to Lego Wires
    4. All About Motors
      1. DC Brushed Motors
      2. Stepper Motors
      3. Hobby Servo Motors
    5. Powering Your Robot
      1. DC Power Jack
      2. USB Connector
      3. Batteries
      4. Battery Dos and Don’ts
    6. NXT to Arduino Communication
    7. XBee Wireless Modules
    8. Epilogue
  15. Index
  16. About the Authors
  17. Colophon
  18. Special Upgrade Offer
  19. Copyright
O'Reilly logo

Chapter 4. Project: Clock

IN THIS CHAPTER

Parts List

Assembly Instructions

Program the Robot

Setting the Clock

The Next Chapter

In this chapter you’ll learn how to build a fully functional Mindstorms clock
Figure 4-1. In this chapter you’ll learn how to build a fully functional Mindstorms clock

Our next project is a simple one—or so we imagined when we began designing it. We thought, “What could be simpler than a Lego clock? Sounds like a great project for a book about Lego and Arduino projects.” Stick a couple of motors on a Lego frame and have the Arduino move the motors to tell us what time it is.

Fortunately, it wasn’t that simple, and we ended up adding a lot of complexity to the project. We built this project before we created the Bricktronics Shield, so we had to figure out how to control motors and display the time via clock hands (see ABOUT THE L293D CHIP). We say “fortunately” because we learned an insane amount about interweaving the two systems, and we’re sharing what we learned with you. Read on!

The clock hands move with the help of two standard Mindstorms motors, controlled by an Arduino Uno, and a couple of buttons allow you to set the time.

Parts List

The following tools, electronic components, and Lego parts will be needed to complete the clock:

Tools & Electronics

  • Arduino Uno

  • Bricktronics Shield

  • 4 Mindstorms wires

  • Power supply rated for 9V at 1.3A or greater with a 2.1mm center-positive plug. This provides power to the Arduino and Lego motors.

  • Drill & 3/16th inch drill bit

  • Hot glue gun

The L293D is a handy chip if you want to drive motors with an Arduino
Figure 4-2. The L293D is a handy chip if you want to drive motors with an Arduino

Lego Elements

You’ll need these elements in the quantities listed below to build the clock
Figure 4-3. You’ll need these elements in the quantities listed below to build the clock
  1. 2 Mindstorms motors

  2. 2 touch sensors

  3. 4 3M Technic beams

  4. 2 7M Technic beams

  5. 1 9M Technic beam (blue or some other bright color[12])

  6. 6 11M Technic beams

  7. 4 15M Technic beams[13]

  8. 12 double angle beams, 3x7[14]

  9. 11 half bushes (2 of them yellow)

  10. 23 bushes

  11. 26 connector pegs

  12. 28 cross connectors

  13. 6 3M connector pegs

  14. 7 tubes (3 of them brightly colored)[15]

  15. 1 90-degree angle element

  16. 2 double cross blocks

  17. 1 4M cross axle

  18. 2 6M cross axles

  19. 2 7M cross axles

  20. 2 9M cross axles

  21. 5 10M cross axles[16]

  22. 1 12M cross axles

  23. 1 8M cross axle with end stop

  24. 2 8-tooth gears[17]

  25. 3 24-tooth gears[18]

  26. 1 40-tooth gear[19] (modified; see Prepare the Gear, later in this chapter)

Assembly Instructions

Once you have gathered together the parts, it’s time to build! The following instructions show you how to build the clock. While you put it together, feel free to experiment and alter the design—the worst that could happen is that the clock just won’t work! If that happens, just back up to the previous step and start over again.

The completed clock model—just add electronics
Figure 4-4. The completed clock model—just add electronics

Prepare the Gear

The only tricky part of the Lego build is that you’ll have to modify the central 40-tooth gear in order for the clock to work. The reason is that in order for the hour and minute hands to both rotate freely without fear of collision, the gear upon which the hour hand is attached must rotate freely around the axle that turns the minute hand. Unfortunately, Lego doesn’t make a gear with a smooth central hub. Their gears all feature cross-axle hubs, which makes sense—you usually want your gear to derive its energy from a cross axle, or impart energy to one.

Since Lego has not seen fit to create such a gear, we did. We took a 40-tooth gear and filled in the holes around the axis with hot glue, in order to strengthen the hub for drilling. Then, using a 3/16th-inch bit and power drill, we drilled out the hub so it rotates freely around a standard cross axle.

We modified the 40-tooth Technic gear to have a smooth hub; We filled in the surrounding holes with hot glue to ensure the drilling doesn’t weaken the gear
Figure 4-5. We modified the 40-tooth Technic gear to have a smooth hub; We filled in the surrounding holes with hot glue to ensure the drilling doesn’t weaken the gear

Build the Lego Model

Next, we’ll assemble the clock itself. This is your opportunity to experiment with the model’s design and customize it to your own liking.

  1. Begin with a couple of 3x7 double angle beams as shown in Figure 4-6.

  2. Add two 15M beams and one 11M beam (see Figure 4-7).

    Step 1: Setting up the angle beams
    Figure 4-6. Step 1: Setting up the angle beams
    Step 2: Adding some more beams
    Figure 4-7. Step 2: Adding some more beams
  3. Now work on the other side with two more 3x7 double angle beams, as shown in Figure 4-8.

  4. You’ll need supports as well; add one 3M and one 11M beam. Figure 4-9 shows how they go together.

    Step 3: Making the other side with angle beams
    Figure 4-8. Step 3: Making the other side with angle beams
    Step 4: Adding two supports
    Figure 4-9. Step 4: Adding two supports
  5. Combine the two assemblies (Figure 4-10) and set them aside for now.

  6. Next, connect two pins to an 11M beam as shown in Figure 4-11.

    Step 5: Combining the two assemblies
    Figure 4-10. Step 5: Combining the two assemblies
    Step 6: Connecting the pins to the beam
    Figure 4-11. Step 6: Connecting the pins to the beam
  7. Next, connect an 11M beam (see Figure 4-12).

  8. Take what you have so far (Figure 4-13) and build another one just like it!

    Step 7: Adding the beam from the previous step
    Figure 4-12. Step 7: Adding the beam from the previous step
    Step 8: Make one just like this
    Figure 4-13. Step 8: Make one just like this
  9. Add four 10M cross axles and bushes to one of the assemblies you built as shown in Figure 4-14, but leave the other one alone for now.

  10. Add half bushes to the back to keep the cross axles in place as shown in Figure 4-15. (By the way, don’t use the yellow half-bushes we specified; you’ll need them for the clock’s hands.)

    Step 9: Adding 10M cross axles and bushes
    Figure 4-14. Step 9: Adding 10M cross axles and bushes
    Step 10: Adding half bushes
    Figure 4-15. Step 10: Adding half bushes
  11. Add four pipes and four more half bushes. Figure 4-16 shows how it goes together.

  12. Add some 9M cross axles and bushes (Figure 4-17).

    Step 11: Adding pipes and half bushes
    Figure 4-16. Step 11: Adding pipes and half bushes
    Step 12: Adding more cross axles and bushes
    Figure 4-17. Step 12: Adding more cross axles and bushes
  13. Now let’s work on the motors. Add three 3M connector pegs as shown in Figure 4-18.

  14. Connect a 7M Technic beam (see Figure 4-19).

    Step 13: Add connector pegs to the motors
    Figure 4-18. Step 13: Add connector pegs to the motors
    Step 14: Connecting the beam
    Figure 4-19. Step 14: Connecting the beam
  15. Grab the second motor, and add three 3M pegs as shown in Figure 4-20.

  16. Connect the two motors together with the help of the Technic beam (see Figure 4-21).

    Step 15: Adding pegs to the other motor
    Figure 4-20. Step 15: Adding pegs to the other motor
    Step 16: Connecting the two motors
    Figure 4-21. Step 16: Connecting the two motors
  17. Throw another 7M beam on there (Figure 4-22).

  18. Add the motors to the structure you built earlier, as shown in Figure 4-23.

    Step 17: Adding another beam
    Figure 4-22. Step 17: Adding another beam
    Step 18: Combining the motors and the assembly
    Figure 4-23. Step 18: Combining the motors and the assembly
  19. Throw the duplicate assembly onto the back (see Figure 4-24).

  20. Add six bushes as shown in Figure 4-25.

    Step 19: Attaching the duplicate assembly
    Figure 4-24. Step 19: Attaching the duplicate assembly
    Step 20: Adding bushes
    Figure 4-25. Step 20: Adding bushes
  21. Add angle beams for legs. Figure 4-26 shows this.

  22. Insert two 7M cross axles as shown in Figure 4-27.

    Step 21: Adding the legs
    Figure 4-26. Step 21: Adding the legs
    Step 22: Inserting the cross axles
    Figure 4-27. Step 22: Inserting the cross axles
  23. Two more angle beams complete the legs (Figure 4-28).

  24. Let’s work on gears. Add a 24-tooth gear to the end of a 12M cross axle (see Figure 4-29).

    Step 23: Adding more angle beams
    Figure 4-28. Step 23: Adding more angle beams
    Step 24: Connecting the 24-tooth gear to a cross axle
    Figure 4-29. Step 24: Connecting the 24-tooth gear to a cross axle
  25. Thread the cross axle through the center hole, between the motors’ orange hubs. Figure 4-30 shows where to put it.

  26. Build another gear and axle assembly, this one with a couple of bushes, as shown in Figure 4-31.

    Step 25: Threading the cross axle
    Figure 4-30. Step 25: Threading the cross axle
    Step 26: Building another gear and axle assembly
    Figure 4-31. Step 26: Building another gear and axle assembly
  27. Insert the cross axle through the topmost visible hole in the vertical support (Figure 4-32).

  28. Secure the axles with two bushes each as shown in Figure 4-33.

    Step 27: Inserting the cross axle through the top
    Figure 4-32. Step 27: Inserting the cross axle through the top
    Step 28: Securing the axles
    Figure 4-33. Step 28: Securing the axles
  29. Add two bushes and an 8-tooth gear to a 6M cross axle. Figure 4-34 shows how they go together.

  30. Connect the assembly from Step 29 to the upper motor’s hub so that the top two gears mesh (Figure 4-35).

    Step 29: Adding the bushes and 8-tooth gear to a cross axle
    Figure 4-34. Step 29: Adding the bushes and 8-tooth gear to a cross axle
    Step 30: Connect to the upper motor’s hub
    Figure 4-35. Step 30: Connect to the upper motor’s hub
  31. Add a bush and an 8-tooth gear to a 4M cross axle as shown in Figure 4-36.

  32. Connect the cross axle to the lower motor’s hub, and make sure the lower two gears mesh. Figure 4-37 shows how it all connects.

    Step 31: Adding a bush and 8-tooth gear to a cross axle
    Figure 4-36. Step 31: Adding a bush and 8-tooth gear to a cross axle
    Step 32: Connecting the cross axle to the lower motor’s hub
    Figure 4-37. Step 32: Connecting the cross axle to the lower motor’s hub
  33. Add a 24-tooth gear and half-bush (Figure 4-38).

  34. Add the drilled out 40-tooth gear. Make sure it rotates freely, then mesh it with the top gear as shown in Figure 4-39.

    Step 33: Adding a 24-tooth gear and half-bush
    Figure 4-38. Step 33: Adding a 24-tooth gear and half-bush
    Step 34: Adding the 40-tooth gear
    Figure 4-39. Step 34: Adding the 40-tooth gear
  35. Add the angle element and two pegs (Figure 4-40).

  36. Connect a 9M beam to the 40-tooth gear (see Figure 4-41). This is your hour hand.

    Step 35: Adding the angle element and pegs
    Figure 4-40. Step 35: Adding the angle element and pegs
    Step 36: Building the hand
    Figure 4-41. Step 36: Building the hand
  37. Build the minute hand out of a cross axle with end stop, three pipes, and two half bushes. Figure 4-42 shows the assembly.

  38. Add the minute hand to the angle element as shown in Figure 4-43.

    Step 37: Building the minute hand
    Figure 4-42. Step 37: Building the minute hand
    Step 38: Attaching the minute hand
    Figure 4-43. Step 38: Attaching the minute hand
  39. Next, let’s work on the button assembly. Add two double cross blocks to a pair of 6M cross axles (Figure 4-44).

  40. Add two touch sensors to the cross axles as seen in Figure 4-45.

    Step 39: Adding cross blocks
    Figure 4-44. Step 39: Adding cross blocks
    Step 40: Attaching touch sensors
    Figure 4-45. Step 40: Attaching touch sensors
  41. Secure the ends of the cross axles with bushes (see Figure 4-46).

  42. Add a couple of connector pegs (Figure 4-47).

    Step 41: Adding bushes
    Figure 4-46. Step 41: Adding bushes
    Step 42: Attaching two pegs
    Figure 4-47. Step 42: Attaching two pegs
  43. Add a couple of connector pegs to attach the buttons (Figure 4-48).

Step 43: Add the button assembly
Figure 4-48. Step 43: Add the button assembly

Install the Arduino

As with the Drawbot in Chapter 1, you’ll need to use Bricktronics mounting plates to attach your Arduino to the clock, as shown in Figure 4-49. Use the exposed cross connectors and cross axle ends on the back of the clock and thread them through the Technic holes in the plates, just like you did in Chapter 1. Attach wires per Figure 4-50. Power the clock with an Arduino-compatible wall wart (9-12V, 2.1mm center positive barrel connector, 1.3A or more) to run the clock. All you have to do is program the clock and you’re done!

Use your Bricktronics mounting plates to add the Arduino
Figure 4-49. Use your Bricktronics mounting plates to add the Arduino
Attach the Mindstorms wires as you see here
Figure 4-50. Attach the Mindstorms wires as you see here

Program the Robot

Once you’ve built the clock, it’s time to program it! Plug in your Arduino and upload the program code found in the Bricktronics library (found on http://wayneandlayne.com/bricktronics if you haven’t snagged it already!)

The clock code follows a general form which is seen throughout this book. First, information about the environment is gathered. Second, we determine a reaction. Third, we react until we gather more information about the environment.

We do this in the clock with a main loop that only iterates every hundred milliseconds or so, and if the code in the loop executes faster than that, it idles until the next iteration. We’ve found this to be a useful code framework for mechatronics.

The libraries used are the Time library, which brings POSIX-style time structs to Arduino, and the Bricktronics libraries.

The main loop checks to see if the time-setting buttons have been pushed, and then it calculates where the minute and hour hands should be. It sets those positions using the PID calls, and waits for the next main loop.

#include <Wire.h>
#include <Adafruit_MCP23017.h>
#include <Bricktronics.h>
#include <Time.h>

#define TIME_STEP 100
time_t t;

Bricktronics brick = Bricktronics();
PIDMotor h = PIDMotor(&brick, 1);
PIDMotor m = PIDMotor(&brick, 2);
Button hour_button = Button(&brick, 1);
Button minute_button = Button(&brick, 2);

void setup()
{
  Serial.begin(115200); 
  Serial.println("starting!");

  brick.begin();
  m.begin();
  h.begin();
  hour_button.begin();
  minute_button.begin();
}

void digitalClockDisplay() 
{
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
}
void printDigits(int digits) 
{
  Serial.print(":");
  if (digits < 10)
  {
    Serial.print('0');
  }
  Serial.print(digits);
}

void increment_minute()
{
  adjustTime(60);
  Serial.println("Advance minute pressed");
}

void increment_hour()
{
  adjustTime(3600);
  Serial.println("Advance hour pressed");
}

void check_buttons() 
{
  static char last_minute_status = HIGH;
  static char last_hour_status = HIGH;

  char minute_status = minute_button.is_pressed();
  char hour_status = hour_button.is_pressed();

  if (minute_status == HIGH && last_minute_status == LOW)
  {
   increment_minute();
  }

  if (hour_status == HIGH && last_hour_status == LOW)
  {
   increment_hour();
  }

  last_minute_status = minute_status;
  last_hour_status = hour_status;
}


void loop() 
{
  long next_loop = millis() + TIME_STEP; 
  check_buttons(); 
  t = now(); 
  int full_hours = hour(t) % 12;
  int minute_position; 
  minute_position = minute(t)*6 + 360*full_hours; _
  int m_pos;
  m_pos = -3 * 2 * minute_position; 

  int hour_position; 
  hour_position = (hour(t) % 12) * 30 + minute(t)/2; 
  int h_pos = 2 * 5 * hour_position; 
  digitalClockDisplay();
  Serial.println();

  m.go_to_pos(m_pos);
  h.go_to_pos(h_pos); 

  while (millis() < next_loop)
  {
    h.update();
    m.update();
    delay(50);
  }
}
  1. We use the USB serial port for debugging our Arduino code. It allows us to send information from the Arduino to the computer.

  2. digitalClockDisplay() prints out the time to the serial port.

  3. printDigits() is a helper function for digitalClockDisplay().

  4. check_buttons() handles most of the logic of the clock, outside of moving the hands. It is only called once each time loop() runs, which is every 100 ms, so debouncing (adding additional program logic to handle electrical noise in button presses) the buttons isn’t really necessary in this application.

  5. loop() runs over and over again. It handles moving the hands, as well as reading the buttons.

  6. next_loop is used for the inner PID loop, which runs for TIME_STEP.

  7. check_buttons() checks the minute and hour buttons for presses.

  8. t holds the current time in a time_t struct.

  9. minute_position is the desired position of the minute hand (in degrees) for the current time, where 0 degrees is at the 12:00 position and increases in a clockwise manner.

  10. To convert from minutes to degrees on a clock, you multiply by 6. For example, 15 minutes*6 = 90 degrees.

  11. m_pos is the position we want the minute hand motor to go to. Because of gearing, this is not just minute_position!

  12. The negative sign is here because of the direction the motor is mounted in. The 3 is based on the gear ratio: there is an 8-tooth gear on the motor, geared to a 24-tooth gear which is connected to the hand. This is a 3:1 ratio, so we need to move our motor three degrees for every one degree we want the minute hand to go. The 2 is because we read two steps per degree.

  13. hour_position is the desired position of the hour hand (in degrees) for the current time, where 0 degrees is at the 12:00 position and increases in a clockwise manner.

  14. To get the position of the hour hand, we have to move the hand 30 degrees for every full hour, and then move the hour hand proportionally for how far the minute hand has gone. So, for example, 6:00 would work out to be exactly 180 degrees (hour hand pointing at the 6). And at 1:30, the hour hand will be at exactly 45 degrees.

  15. There is an 8-tooth gear driving a 24-tooth gear, for a 3:1 ratio and then that 24-tooth gear is driving a 40-tooth gear, for a 1.667:1 ratio. This works out to a 5:1 ratio overall. The reason for the 2 in the equation is that we read two steps per degree.

  16. The program will continue to try to get the hands to the right positions for about 100 ms.

Setting the Clock

The clock keeps accurate time, but it doesn’t start off knowing the time. The clock’s code assumes that it’s beginning at 12 o’clock, so you’ll need to set it accordingly. First, plug in the clock and hit the minute and hour buttons to move the hands to 12:00, if they’re not there already. Then unplug the clock and plug it back in, and you’ll be ready to use the buttons to set the clock to its true time.

The Next Chapter

In Chapter 5 we’ll tackle what is perhaps the most complicated robot in the book, a machine that actually prepares a glass of chocolate milk at the touch of a button! What’s not to like about that?



[12] Not found in the Mindstorms set at all or in insufficient quantities. See A NOTE ABOUT SOURCING LEGO to learn what you can do.

[13] Not found in the Mindstorms set at all or in insufficient quantities. See A NOTE ABOUT SOURCING LEGO to learn what you can do.

[14] Not found in the Mindstorms set at all or in insufficient quantities. See A NOTE ABOUT SOURCING LEGO to learn what you can do.

[15] Not found in the Mindstorms set at all or in insufficient quantities. See A NOTE ABOUT SOURCING LEGO to learn what you can do.

[16] Not found in the Mindstorms set at all or in insufficient quantities. See A NOTE ABOUT SOURCING LEGO to learn what you can do.

[17] Not found in the Mindstorms set at all or in insufficient quantities. See A NOTE ABOUT SOURCING LEGO to learn what you can do.

[18] Not found in the Mindstorms set at all or in insufficient quantities. See A NOTE ABOUT SOURCING LEGO to learn what you can do.

[19] Not found in the Mindstorms set at all or in insufficient quantities. See A NOTE ABOUT SOURCING LEGO to learn what you can do.

The best content for your career. Discover unlimited learning on demand for around $1/day.