Introduction: DIY - Control Off-road Lego Car Through Internet (IoT) - Part 1 (Back Wheel)

About: interest in iot, automation, technology. today still work as sofware engineer in largest Telco Company in Indonesia

Playing lego is pretty much fun, we can custom what we will build and add some geek parts to make it more awesome. In this case, I am trying to control off-road lego car through internet (of course without Lego power function pack). The disadvantage using default lego power function is limited controlled-distance due to infrared protocol that is used. So let's get started to make it move :

Step 1: Parts

Hardware parts :

1. Raspberry pi 3 (preferable)

2. Multimeter

3. Soldering Iron + Tin

4. Scissors

5. Wire + Jumper Wire

6. Pin headers

7. Lego off-road car (Im using 42037 type)

8. Mini breadboard

9. Driver DC Motor TB6612FNG and its data sheet

10. Turnigy Lipoly battery 2200mAh 3S (11.1v)

11. Ubec 3A 5V-6V

12. Lego L Motor (if you have better DC motor, it can be used as long as it applies minimum torque at 5Ncm and rpm 300)

Software requirement :

1. Node.js

2. AWS IoT

3. AWS EC2

Step 2: Powering Raspberry Pi With Lipoly Battery and BEC

As RasPi 3B works as Lego car controller, it needs power supply with specification 5V and 2.5 to function normally (read for more detail: https://www.raspberrypi.org/documentation/hardware/raspberrypi/power/README.md). IMO UBEC 3A 5V or 6V would be fit in my case to convert a single Lipoly battery 3S to 5V. Besides, UBEC as switch regulator keeps the power more efficient instead of linear one (with 85% efficiency percentage). Even it's a little bit expensive, it avoids damaging your raspberry as it keeps voltage amount very still and I recommend you very strongly to use BEC. Furthermore, please ensure the output voltage range with multimeter digital and it must be in range 5V - 5.3V or it can fry your RasPi. Then, Plug the output BEC into GPIO Pin 4 and 6 (Board Mode). Wait for a while until your raspberry pi blinks green signal and lastly you might check to login to RasPi through router (wont be discussed in this topic)

Step 3: Hack Lego L Motor Wiring

Lego motor wiring is simple as many dc motors out there, there are always four pins at least (VC, Ground, Controller Input 1, Controller Input 2). Lego motor is designed to be suitable with only lego battery by clicking male and female connector. It wont absolutely match with my power supply, so let's do hack lego L motor wiring. Please see the figure. The red wire attach to Controller 1 (C1) and black attach to Controller 2 (C2) respectively with soldering stuff.

(for the detail please see https://scuttlebots.com/2014/03/02/lego-pf-hacking-wiring/)

Step 4: Add Driver Motor, Put Them In

Last mechanical part we connect our power supply and lego motor wires from previous part to driver motor and RasPi. Again it's super simple (please see the figure) and attachment file for driver motor data sheet.

Connections

VM (max 15V) to 12V Power Supply within breadboard

VCC (5v) to 5V Power Supply within breadboard

GND to GND within breadboard

AO1 to C1 within Lego L motor

AO2 to C2 within Lego L motor

PWMA within Pin 31 RasPi (you may choose another GPIO Pins as long as it's not 5v, 3.3v, GND or ID EE Prom)

AIN1 within Pin 33 RasPi (you may choose another GPIO Pins as long as it's not 5v, 3.3v, GND or ID EE Prom)

AIN2 within Pin 35 RasPi (you may choose another GPIO Pins as long as it's not 5v, 3.3v, GND or ID EE Prom)

STBY within Pin 37 RasPi (you may choose another GPIO Pins as long as it's not 5v, 3.3v, GND or ID EE Prom)

BO1, BO2, BIN1, BIN2, PWMB is not used

Step 5: Write Simple Code and It's Running

We are finally on the last part, controlling the DC Motor from your device through internet, let's have fun.

Anyway I wont discuss the detail process of how to get started in AWS or Installation because it has its own page much more detail that has complete information. Please see the figure for simple architecture

A. Preparation or Installation :

--- Inside RasPi ---

1. Ensure your raspberry pi has wireless element and make it automatically connect to nearby hotspot while booting up (Supposed to use GSM Module if out of wifi range, but it's still under development)

2. Install NodeJs in your RasPi

3. Install Pigpio for your NodeJS Module use NPM (please see https://www.npmjs.com/package/pigpio) to communicate with your Pin

4. Install AWS IoT Device SDK for your NodeJS Module use NPM (please see https://github.com/aws/aws-iot-device-sdk-js)

--- Inside AWS ---

1. Create AWS Account unless you already have one, 1 instance for AWS EC2, 1 instance for AWS IoT

2. In EC2 Instance install Apache (for Web GUI)

3. In EC2 Instance install NodeJs (to communicate with AWS IoT)

4. In EC2 Instance Install HapiJs as Application Framework for NodeJs and add Good as HapiJs plugin for logging activities

5. Install AWS IoT Device SDK for your NodeJS Module use NPM (please see https://github.com/aws/aws-iot-device-sdk-js)

6. In AWS IoT Instance, create one thing, one policy and one key as authentication (please see the figure)

B. Develop Application

Before go to the next step, ensure list-to-do above is completely done

-- Raspi NodeJs Application --

Overall process in NodeJs application in RasPi

1. Connect to AWS IoT use the key that Thing AWS IoT provided

2. Listening instruction from AWS IoT Thing if any delta data

3. If any delta data is coming in and then move the motor as instruction specified

4. Logic Controllling Motor Procedure (please see the figure)

- Rotary speed depends on PWMA Pin Input

- Rotary direction depends on AIN1 and AIN2 Pin Input

- On Off depends on STBY Pins

As you can see in the video, I control the speed using the button N, 1, 2, 3, 4, 5.

N = STBY => 0, PWMA => 0 (certainly stop even speed direction is up or down)

1 = STBY => 1, PWMA => 128

2 = STBY => 1, PWMA => 161

3 = STBY => 1, PWMA => 194

4 = STBY => 1, PWMA => 225

5 = STBY => 1, PWMA => 255

And I control the speed direction use the virtual joystick

Neutral = AIN1 => 0, AIN2 => 0, PWMA => 0 (certainly stop even speed no matter of speed state)

Up = AIN1 => 0, AIN2 =>1

Down = AIN1 => 1, AIN2 =>0

5. Please see the code in attachment

-- AWS EC2 Apache --

1. Only one html page that consists of Joystick and button

2. Need virtualjoystick.js (please see https://github.com/jeromeetienne/virtualjoystick.j... )

3. Need bootstrap 3 (please see http://getbootstrap.com/)

4. Need jquery to hit API through EC2 NodeJs

5. Please build the mockup by your self :D

-- AWS EC2 NodeJs API --

1. Encapsulate the MQTT message using API (direction and speed message)

2. You should authenticate your API using HapiJs or JWT to secure your API protocol

3. You should implement it using SSL certification

4. Please see the code in attachment (Authentication and JWT Code is not provided, This API IN THIS CODE IS NOT SECURE, it's just to show you how does it work, use it at your own risk.)

Hopefully it's done and happy playing with your lego. For the next part I will write about how to control steer direction (left or right using servo motor S3003 (not using Lego Servo Motor).

Thank you

To Be Continued ...