How to Move Lego Motors Autonomously Using Arduino

by bmohnsen in Living > LEGO & K'NEX

4877 Views, 14 Favorites, 0 Comments

How to Move Lego Motors Autonomously Using Arduino

LegoBatteries.jpg
LegoBattery.jpg
LegoInfrared.jpg
9VoltBattery.jpg
Arduino_Cord_Breadbd.jpg
infraredTransmitter.png
Resistor.jpg
wires.jpg

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

Arduino Software Set Up

1Download.png
ArduinoSoftware.png
computerConnect.jpg
2Board.jpg
3Processor.jpg
4Port.jpg

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).

Power Function Library Set Up

Screen Shot 2020-09-07 at 10.39.58 AM.png
Screen Shot 2020-09-07 at 10.41.17 AM.png
Screen Shot 2020-09-07 at 10.41.33 AM.png
Screen Shot 2020-09-07 at 10.41.57 AM.png

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. 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).

Infrared Transmitter Set Up

IMG_6106.jpg
IMG_6107.jpg

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).

Write Arduino Code

Step_1.jpg
Step_3.jpg
Step_2.jpg

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.

Writing Power Functions Motor Code

IMG_6115.jpg
IMG_6114.jpg

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.SingleOutput(0, direction/speed, connection color on Lego Receiver, channel on Lego Receiver)

Options for direction/speed

PWD_FWD - moves the motor clockwise

FWD1, FWD2, FWD3, FWD4, FWD5, FWD6, FWD7 are speed options from 1 (slow) to 7 (fast)

PWD_REV - moves the motor counterclockwise

REV1, REV2, REV3, REV4, REV5, REV6, REV7 are speed options from 1 (slow) to 7 (fast)

PWM_BRK - stops the motor

PWM_FLT - floats the motor (essentials stops the motor but not immediately)

Options for Color

red or blue - the two color connections on the Lego Infrared Receiver (see photo 1). We will use blue for motor.

Options for Channel

1 or 2 or 3 or 4 - the four channel options on the Lego Infrared Receiver (from high to low using the orange switch (see photo 2 for channel 1 setting).

Sample Code

#include <legopowerfunctions.h> //this step is required for use of legopowerfunctions library

LEGOPowerFunctions lego(12); //pin where infrared transmitter is connected

void setup() {

//nothing required

}

void loop() {

lego.SingleOutput(0, PWM_FWD4, BLUE, CH1); // clockwise medium speed

delay (1000); //1 second delay between commands

lego.SingleOutput(0, PWM_BRK, BLUE, CH1); // stops the motor

delay (1000); //1 second delay

lego.SingleOutput(0, PWM_REV4, BLUE, CH1); // counterclockwise medium speed

delay (1000); //1 second delay

lego.SingleOutput(0, PWM_FLT, BLUE, CH1);

delay (1000); //1 second delay

}

Copy and paste the code into an Arduino file you set up in the previous step.

Upload and Run Motor Code - Make It Move

IMG_6113.jpg
upload.jpg
Verify.jpg

1) Ensure that the lego motor is connected to the blue port on the lego receiver and that the lego receiver is connected to the battery. Also, ensure that the Arduino is connected to your computer (see photo 1).

2) Put an axle into the motor along with a strip of masking tape around the end. The masking tape makes it easier to watch the turning of the motor (see photo 1).

3) Verify the code (see photo 2).

4) Upload this file to the Arduino (see photo 3).

5) Watch the motor as the Arduino runs the code.

Write and Run (Make It Move) Power Functions Code for Servo Motor

IMG_6116.jpg

1) Writing code for servo motor.

The basic code for controlling lego servo is:

lego.SingleOutput(0, direction/distance, connection color on Lego Receiver, channel on Lego Receiver)

Options for direction/distance
PWD_FWD - moves the motor clockwise

FWD1, FWD2, FWD3, FWD4, FWD5, FWD6, FWD7 are distance options from 1(short distance) to 7 (longer distance)

PWD_REV - moves the motor counterclockwise

REV1, REV2, REV3, REV4, REV5, REV6, REV7 are distance options from 1 (short distance) to 7 (longer distance)

PWM_BRK - stops the motor

PWM_FLT - turns the servo motor to 90 degree or neutral position

Options for Color

red or blue - the two color connections on the Lego Infrared Receiver. We will use red for the servo.

Options for Channel

1 or 2 or 3 or 4 - the four channel options on the Lego Infrared Receiver. Leave receiver at channel 1.

Sample Code

#include <legopowerfunctions.h> //this step is required for use of legopowerfunctions library

LEGOPowerFunctions lego(12); //pin where infrared transmitter is connected


void setup() {

//nothing required

}

void loop() {

lego.SingleOutput(0, PWM_FLT, RED, CH1); // Center

delay(1000);

lego.SingleOutput(0, PWM_FWD1, RED, CH1); //clockwise delay(1000);

lego.SingleOutput(0, PWM_FLT, RED, CH1); // Center

delay(1000);

lego.SingleOutput(0, PWM_FWD2, RED, CH1); //clockwise

delay(1000);

lego.SingleOutput(0, PWM_FLT, RED, CH1); // Center

delay(1000);

lego.SingleOutput(0, PWM_FWD3, RED, CH1); //clockwise

delay(1000);

lego.SingleOutput(0, PWM_FLT, RED, CH1); // Center

delay(1000);

lego.SingleOutput(0, PWM_FWD4, RED, CH1); //clockwise

delay(1000);

lego.SingleOutput(0, PWM_FLT, RED, CH1); // Center

delay(1000);

lego.SingleOutput(0, PWM_FWD5, RED, CH1); //clockwise

delay(1000);

lego.SingleOutput(0, PWM_FLT, RED, CH1); // Center

delay(1000);

lego.SingleOutput(0, PWM_FWD6, RED, CH1); //clockwise

delay(1000);

lego.SingleOutput(0, PWM_FLT, RED, CH1); // Center

delay(1000);

lego.SingleOutput(0, PWM_FWD7, RED, CH1); //clockwise

delay(1000);

lego.SingleOutput(0, PWM_FLT, RED, CH1); // Center

delay(1000);

lego.SingleOutput(0, PWM_REV1, RED, CH1); //counter clockwise

delay(1000);

lego.SingleOutput(0, PWM_FLT, RED, CH1); // Center

delay(1000);

lego.SingleOutput(0, PWM_REV2, RED, CH1); //counter clockwise

delay(1000);

lego.SingleOutput(0, PWM_FLT, RED, CH1); // Center

delay(1000);

lego.SingleOutput(0, PWM_REV3, RED, CH1); //counter clockwise

delay(1000);

lego.SingleOutput(0, PWM_FLT, RED, CH1); // Center

delay(1000);

lego.SingleOutput(0, PWM_REV4, RED, CH1); //counter clockwise

delay(1000);

lego.SingleOutput(0, PWM_FLT, RED, CH1); // Center

delay(1000);

lego.SingleOutput(0, PWM_REV5, RED, CH1); //counter clockwise

delay(1000);

lego.SingleOutput(0, PWM_FLT, RED, CH1); // Center

delay(1000);

lego.SingleOutput(0, PWM_REV6, RED, CH1); //counter clockwise

delay(1000);

lego.SingleOutput(0, PWM_FLT, RED, CH1); // Center

delay(1000);

lego.SingleOutput(0, PWM_REV7, RED, CH1); //counter clockwise

delay(1000);

lego.SingleOutput(0, PWM_FLT, RED, CH1); // Center

delay(2000);

}

2) Copy and paste the code into a new Arduino file. Save as servoTest.

3) Ensure that the lego servo is connected to the red port on the lego receiver and that the lego receiver is connected to the battery. Put an axle into the servo motor along with a strip of masking tape around the end. The masking tape makes it easier to watch the turning of the motor.

4) Verify the code.

5) Upload this file to the Arduino and test it.

6) Watch the servo motor.

Next Steps

IMG_6112.jpg
Sonic.jpg
7BatteryConnect.jpg

1) Connect both the servo motor and medium/large/extra large motor to the infrared receiver (see photo 1). Select one of your power function sets and use parts of the code in the previous steps to create your own code.

2) Add sensors, (e.g., ultrasonic (see photo 2)) as input. Then, using if/then coding have the motors respond according to sensor input.

3) Remove connection from computer and add 9 volt battery so you are not tethered to your computer.

Connect positive (blue in photo) to port Vin (see photo 3).

Connect negative (dark red in photo) to port Ground (see photo 3).