Stories on maker education and innovation 

Home 3d printing How to build a Lego Crane using Arduino [Tutorial]
formats

How to build a Lego Crane using Arduino [Tutorial]

Lego Crane

I’m always looking for new ways to play and build with my kids.  In the Rosario house hold, we enjoy building stuff with Lego’s.   My kids have also enjoyed playing with Lego Wedo and Lego Mindstorm kits at our makerspace.   For many families, getting access to a Lego Mindstorm EV3 kit that costs $350 to $400 can be a challenge.   Even the Lego Wedo kits are not inexpensive.   Our family already had a pretty nice collection of traditional Lego and Lego technic pieces.  I started wondering if I could build some pieces that would enable us to combine the world of Lego and Arduino.  In this blog post, I would like to share some of my experiences of building a fun Lego crane for my kids.   I hope to use some of these ideas to engage older students in coding and making at our local makerspace.    You can see a video of the crane in action here:

Here’s some of the materials that you’ll need to build your own!

  • Traditional Lego pieces
  • Lego technic pieces – These lego pieces are used with Lego Mindstorm/Wedo kits for constructing robots.   They include parts like beams, gears, and connectors to enable pieces to turn and spin.  You can learn more about Lego technic from this post.
  • 1 Continuous rotation servo.  You can purchase this servo for about $15.00 . Here’s a link:https://www.parallax.com/product/900-00008
  • 1 standard servo.  This costs about $14.00. You can find it here: https://www.parallax.com/product/900-00005
  • Bread board
  • Arduino
  • Wires
  • Computer to program and control the Arduino.
  • 2 Lego servo horns – I found the following design from Nenzilla from Thingiverse.  It works pretty well.

For this construction, you will need two Lego servo horns.   Connect Lego technic beams to the Lego servo horns.

 

3D printed servo horn for lego

 

In the following picture, you can see how we used Lego, rubber bands, the standard servo to enable the crane to turn left to right.

 

Left and right Lego servo

 

In the following picture, you can see how we used the continuous rotation servo so that the string of the crane can be extended and retracted.  We’re just using rubber bands to connect the servo’s to traditional Lego pieces.   We also used our second Lego servo horn.

 

Up down servo

 

Here’s how we connected the top beam with the support beam.

 

Constructing the top beam

 

What does the Arduino code look like?

In this section, we’ll give you an overview of the source code used to drive the servo’s. In the Arduino platform, the host PC can communicate with the Arduino at runtime using serial communication. In the code, you’ll see that we are using serial communication to send out servo commands in response to characters sent by the host PC.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <Servo.h>


int incomingByte;      // a variable to read incoming serial data into
int angle = 0;
int delta = 3;

Servo servoLeftRight;
Servo servoUpDown;


void setup() {
  // initialize serial communication:
  Serial.begin(9600);
 
  angle = 90;  
 
  servoLeftRight.attach(9);
  servoUpDown.attach(10);  
  servoUpDown.write(90);  
}

void loop() {
  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
   
   
   
    if (incomingByte == 'd') {
     
      angle = angle - delta;
      if(angle <= 0)
        angle = 0;
       
      // move left  
      servoLeftRight.write(angle);
      delay(15);
    }

    if (incomingByte == 'a') {
      angle = angle + delta;
      if(angle >= 180)
        angle = 180;
     
      //move right  
      servoLeftRight.write(angle);
      delay(15);
    }
   
    if (incomingByte == 'w') {
      //raise the hook
      servoUpDown.write(180);
     
    }
   
    if (incomingByte == 's') {
      //lower the hook
      servoUpDown.write(0);
    }

    if (incomingByte == ' ') {
      //stop the continuous rotation servo
      servoUpDown.write(90);
    }



   
  }
}

In the following setup code, we declare variables for the character typed by the user and the angle of the crane. We also declare our servo’s. One servo is used to change the direction of the crane. (servoLeftRight) The other servo moves the hook of the crane up and down. We initialize the direction servo to 90 degrees. “ServoLeftRight” will be attached to Arduino pin 9. “ServoUpDown” is connected to Arduino pin 10.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <Servo.h>


int incomingByte;      // a variable to read incoming serial data into
int angle = 0;
int delta = 3;

Servo servoLeftRight;
Servo servoUpDown;


void setup() {
  // initialize serial communication:
  Serial.begin(9600);
 
  angle = 90;  
 
  servoLeftRight.attach(9);
  servoUpDown.attach(10);  
  servoUpDown.write(90);  
}

The “loop” function keeps repeating code forever. If the Arduino finds bytes incoming from the host computer, we read the byte and do something useful with it.

1
2
3
4
5
6
7
8
9
10
11
void loop() {
  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();

    //Do something!!

  }

}

To send characters to your Arduino, you will need to open the serial monitor window by pressing CTRL+SHIFT+M . By entering ‘d’ or ‘a’, you will move the direction of the crane left and right.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    if (incomingByte == 'd') {
     
      angle = angle - delta;
      if(angle <= 0)
        angle = 0;
       
      // move left  
      servoLeftRight.write(angle);
      delay(15);
    }

    if (incomingByte == 'a') {
      angle = angle + delta;
      if(angle >= 180)
        angle = 180;
     
      //move right  
      servoLeftRight.write(angle);
      delay(15);
    }

In a similar fashion, entering ‘w’ and ‘s’ will raise and lower the hook. Sending a space character will stop the continuous rotation servo.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    if (incomingByte == 'w') {
      //raise the hook
      servoUpDown.write(180);      
    }
   
    if (incomingByte == 's') {
      //lower the hook
      servoUpDown.write(0);
    }

    if (incomingByte == ' ') {
      //stop the continuous rotation servo
      servoUpDown.write(90);
    }

In a future blog post, I might try to recreate this program using ArduBlock to make the programming experience more accessible to kids.

Let us know if you need help building your own Lego robots using Arduino. I would enjoy hearing what you’re trying and building!

Wish you the best!

Top Stories on InspiredToEducate.NET

Learning To Code

Science Education

 

Join the Spark Macon Maker Space Community on Facebook

 
 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
No Comments  comments 
© Inspired To Educate
credit