How to Move Lego Motors Autonomously Using Arduino
Introduction: How to Move Lego Motors Autonomously Using Arduino
You have lego power function motors and receiver, but how do you get the motors to move autonomously? Enter the Arduino! This Instructable will show you how to move your lego power function sets without using the lego infrared transmitter.
Supplies
Lego Servo Motor
Lego Medium, Large, or Extra Large Motor
Lego Battery
Lego Infrared Receiver
Axle - length of 6 or higher (quantity 2)
2 inch piece of masking tape (quantity 2)
9 Volt Battery
Arduino and breadboard with cable to connect to your computer
Infrared Transmitter
330 ohm resistor
Wires
Arduino Software
Power Functions Library
Step 1: Arduino Software Set Up
1) Download the Arduino software from https://www.arduino.cc/en/Main/Software (see photo 1)
Note: It is free - donation is optional.
2) Follow the directions to install the software for your system.
3) Open the Arduino software (see photo 2).
4) Connect Computer to Arduino (see photo 3).
5) Select Board that you are using (see photo 4).
6) Select Processor - information typically comes with the Board (see photo 5).
7) Select Port that you are using for connection (see photo 6).
Step 2: Power Function Library Set Up
The Arduino program (by itself) does not have support for the Lego Power Functions. However, someone has written a piece of code to add this function to the Arduino. Pieces of code like this are called libraries and they have an extension of .h. 991
In order to use a library you have to add it to your version of Arduino:
1) Download LEGOPowerFunctions.zip from http://basbase.com/robocam/LEGOPowerFunctions.zip (see photo 1).
2) Select Sketch - Include Library - Add .zip Library (see photo 2).
3) Select LEGOPowerFunctions.zip (see photo 3) from your hard drive where you downloaded the library.
4) Check that legopowerfunctions has been included in your software (see photo 4).
Step 3: Infrared Transmitter Set Up
Connect infrared transmitter and resistor (photos 1 and 2) to Arduino.
Insert short wire of the infrared transmitter into ground row of the Arduino (see photo 1).
Insert long wire of the infrared transmitter into empty row of the Arduino (see photo 2).
Insert one end of resistor into empty row (same as the long wire from the infrared transmitter) of the Arduino (see photo 2).
Insert other end of resistor into D12 row of the Arduino (see photo 1).
Step 4: Write Arduino Code
1) Open Arduino (if it is not already open). When you open Arduino a new file should appear. If it does not, then select File-New (see photo 1).
2) Save (see photo 2) the file using the name "Test_PFMotors".
Every Arduino sketch (file) has two main parts (see photo 3):
void setup() - sets things up that have to be done once and then don’t happen again.
Under void setup() - the { } are important - they define what is included in setup.
void loop() - contains the instructions that get repeated over and over until the Arduino board is turned off.
Under void loop() - the { } are important - they define what is included in the loop.
Note: Anything following // are simply notes - the computer, when running the file/program, ignores these.
Note: A semicolon should complete each line of code.
Step 5: Writing Power Functions Motor Code
Select a medium, large, or extra large lego motor (the code is the same for each).
The basic code for controlling lego motors is
lego. 991