Introduction: Arduino and NXT
For a final project, I chose to see how arduino can interface with Lego NXT motors and sensors. I learned a lot about library dependencies, syntax, classes and design during this project and am very happy with my progress.
I work at a school and was given ~10 sets of old NXT education kits that had all of the programming bricks broken and a lot of missing pieces. I want to use arduino in my classroom and have a set of 10 so I started wondering if arduino can work with lego accessories. I found a company called Wayne and Layne who are located in Minneapolis, MN and who create software and hardware for the arduino and NXT/EV3 kits. I highly recommend that you purchase your hardware from them and help support their company!! They have a fantastic support community and are really top notch. They developed the software libraries as well and a lot of knock off brands use their software (it's open source so there is nothing illegal about it, but I choose to vote with my wallet and support the main creators). All of their code can be found on github and added easily to your library to be included in your sketch.
After I got comfortable using the libraries, I started experimenting with designing a lego car using two motors and buttons. I decided that Sonar would be way cooler than buttons though so I shifted my initial design. My vechile body was HORRIBLE starting out. I've never worked with this kind of lego and it was hard to create what I was visualizing. Trying to create a flat space for the battery pack and arduino was very challenging but I was finally successful. I also borrowed a design that can be found here**link**. The final result was two cars that drive themselves based on sonar using an NXT sensor and an arduino ultrasonic sensor. I'll go over the pros and cons of each as we progress.
Step 1: Physical Materials Required
Step 2: Get Your Software Ready
First things first,
head over to github and grab the following libraries:
**Note, this is the minimum needed to replicate my project I suggest you grab everything while you are there so you have it all when you do your own remix**
Once you have downloaded the files, you need to unzip them and add them to your "libraries" folder inside your main Arduino folder. If you don't know how to do this, check out the official documentation.
Once your files are added, make sure you remove the "-master" part of the name for all the folders and files. This is suggested directly by Wayne and Layne but can be missed if you don't read the documentation carefully.
Now that our libraries are good to go, you need to open Arduino so we can make a new sketch (if you had it open already, you must close it so it can update and see the new libraries that we added).
Step 3: Writing Some Code
We can now start to put in some code to use for our car!! I like to add comments to my code to keep track of everything, for an example, see my first image.
Now you need to declare the names for your motors, this can be anything you want but I like keep it short and simple by using "m" for motor and "l" or "r" for left and right. If you are using an ultrasonic NXT sensor, you will need to name that as well, I used a simple "u" for ultrasonic.
Next, you need to "Initialize" your declarations so that they will be ready to use inside of our void loop.
In my code, which you'll see later, I also include an update motor function but I don't believe that it is necessary unless you are using commands like, "goToPosition" but I may be wrong. Either way it doesn't hurt to have it just to be safe.
Now you're ready to code!!! I start my sketch by using and "IF/Else" statement to read the distance in cm from the sensor and then deciding what to do. If the read distance is greater than 15cm, my car should drive forward, else it needs to reverse and turn before going forward again. Please feel free to use my code but I am sure you'll want to tweak it to meet your needs. You might need to change the values of the motors based on how you orientate your motors so play around with it and use what works.
Step 4: Build Option #1- Bricktronics Shield and NXT Sensors
I found the lego body for this design from the website NXT Programs after my first design failed horribly. My first design was unlevel and extremely unstable and frustrating. Using the NXT programs design, I made a few modifications to meet my needs.
- I didn't include the gearing in the front of the car because my shield only supports the use of two motors. (Skip step 12)
- Instead of a color sensor, I included just a light sensor and I reversed the orientation of it to better fit between my gears in the rear. (Steps 3-5 in the linked documentation)
- Stopping around step 15 because I don't have the programming brick, I started to create a holder for the batteries and arduino. To make the battery holder I used eight 2x8 and four 1x15 bricks. (see images)
- To hold the arduino I used two 1x7, two 2x8 and two rounded 1x3 with double sided black pegs to hold the arduino steady.
- To the body of the car, I added two double sided black pegs on the 8th hole to hold the battery pack to the frame.
- Now you can place the arduino on top of the battery pack
- I attached the ultrasonic sensor to the front motor since it won't be used. I did that using a two 4x1 "L" shaped pieces and two 1x3 rounded pieces that include one cross piece.
- I also added a button to the back so that if pressed, the program won't run. This helps with debugging code and not having the whole car shoot across a table.
- My cables are a mess so tidy them however you see fit.
Some final thoughts:
Pros- this is a super solid design that is sturdy and has a lot of potential.
Cons- **EDIT** So I guess the weight isn't such an issue and I thought, it has more to do with the surface you run it on. I was using my wood floors at home and it wouldn't turn but it worked fine on the slick linoleum floor at school** it is sooo heavy that you can't turn it using the back wheels so it can only travel in a straight line and because my shield doesn't support a third motor, not a very practical design choice.
Step 5: Build Option #2- Use a LinkSprite Shield and HC-SR04 Ultrasonic Sensor
This build is an original design that can definitely still be improved.Start by setting up your motors with a 10 hole hook piece and place it so that it is on the outside of each motor with the elbow joint facing the cord port. You'll need to double sided pegs and two peg/cross pieces to hold it and attach the next piece. Attach 1x5 to the top hole of each hook and leave one hole open in the 1x5. This allows us to build towards the center of the motors so that we can attach the battery holder. On the top rear portion of the motor, connect a 5x3 "L" piece with the toe facing the turning gear. You will now add a 1x9 to the back of the "L" piece so that the third hole in the 1x9 connects to the top of the "L". Now connect the two motors using a 1x11 piece and 4 double peg pieces.
Next, we'll assemble the front wheel. You'll need a large wheel hub, two 1x3 pieces that have the cross at the top, one 1x5, two 1x3, two crosscaps and one long cross bar. Start by putting the cross bar through the hub and a bottom round hole in the 1x3 with the cross (red in my picture). Put a cap on the bar on the outside of the 1x3 and then do the same to the other side. Now attach the two 1x3 with the crosses to the two 1x3 using a cross/peg piece in each. Use the 1x5 as a stabilizer and separater between the two 1x3s by attaching the ends to the centers using a double-sided peg. Attach the new wheel apparatus to the cross bar between the motor. Use double sided pegs to put in in the fourth hole of the bar.
Lastly, we'll create the holder for the batteries and the arduino.
You'll need two 1x11 hook pieces, two 7 hole "L" pieces, two 1x15 bars, two 1x11 square pieces, eight 1x7 square pieces, two long cross bars and four crosscaps. Start by joining the hooks to the 1x15 by attaching the hole right before the hook to the first hole in the 1x15. Use either a double-sided peg or a cross/peg peice to attach the other side for support to the 1x15. Skip the hole immediately after the hook and then attach the "L" piece to the 1x15. Put a double sided peg in the 1x15 right across from the elbow in the "L" piece to be used later. I an now going to refer to the front of the "L" piece as the front and the hook piece as the back. Use a double sided peg to attached the the 1x11 square pieces to the third hole back from the front of the "L" on the side with the 1x15. Add a few additional double sided pegs for support towards the back. Now stack up four square 1x7 pieces and attach them to the second peg from the front of the square 1x11. Finally, put the cross bars through the hooks and "L" ends and add caps on the bar in front of the "L".
Attaching the battery holder to the frame.
Using the two pegs in the front of the battery frame, attach it to the top of the 1x5 above the wheel hub in the front of our car. In the rear of the car, level out the battery pack by adding a peg into the 1x9s and attach it to the square 1x9 hole.
Wiring up the sonar:
Use a mini breadboard to wire the "TRIG" peg to 12, "ECHO" to 11, VCC to 5v power and GND to ground. This can't be done with a Bricktronics board because there are no GPIO expanders. The LinkSprit has the expansion which allows for this to be possible.
Place the battery pack with the cord at the rear of the car and place the aruino on top with the sonar facing the front. Wire up the motors and you are good to go!!!
Very light!! Turns great and is super stable!!
Wheel hub in the front is undesirable. I want a wheel that pivots like on a shopping cart but lack the materials to create one.
The breadboard and ultrasonic sensor and a little fragile so some tape or another solution to secure it would be ideal.
Can't run a third motor.
Step 6: YOU'RE DONE!!!
I hope you enjoyed my tutorial and that it gave you cool ideas to build your own crazy contraption. I REALLY hope the pictures helped because writing out directions for lego is very difficult (probably why the company doesn't do it and just has pictures).
We have a be nice policy.
Please be positive and constructive.