LAFVIN Smart Robot Tank Kit
LAFVIN Smart Robot Tank Kit
Content
Packing List ....................................................................................................................................... 1
Smart Robot Tank Kit Introduction .................................................................................................. 2
Lesson 1 Getting Started with Arduino IDE .....................................................................................7
How to Install Arduino IDE ......................................................................................................... 7
How to Install Arduino Driver ................................................................................................... 15
How to Add Libraries ................................................................................................................. 22
Blink Test .................................................................................................................................... 26
Lesson 2 Getting Started with Mixly ..............................................................................................39
Introduction of Mixly Software ................................................................................................. 39
How to Install Mixly Software ...................................................................................................44
How to Add Mixly Libraries ...................................................................................................... 47
How to open the reference program ...........................................................................................53
Lesson 3 Motor Speed and Direction Control ................................................................................99
Lesson 4 Infrared Obstacle Avoidance Robot Tank ..................................................................... 108
Lesson 5 Ultrasonic Obstacle Avoidance Robot Tank ................................................................. 128
Lesson 6 Infrared Remote Control Robot Tank ........................................................................... 154
Lesson 7 APP Control Robot Tank ............................................................................................... 163
Company Profile
Established in 2011, lafvin is a manufacturer and trader specialized in research,development and production of 2560 uno,nano boards,and all
kinds of accessories or sensors use for arduino,rasperrry.We also complete starter kits designed for interested lovers of any levels to learn
Arduino or Raspberry.We are located in Shenzhen,China.All of our products comply with international quality standards and are greatly
appreciated in a variety of different markets throughout the world.
Customer Service
We are cooperating with a lot of companies from diffirent countries.Also help them to purchase electronic component products in china,and
became the biggest supplier of them. We look forward to build cooperate with more companies in future.
By the way,We also look forward to hearing from you and any of your critical comment or suggestions.Pls email us by [email protected]
if you have any questions or suggestions.As a continuous and fast growing company. We keep striving our best to offer you excellent products
and quality service.
Our Store
Aliexpress store: https://www.aliexpress.com/store/1942043 Brand in Amazon:LAFVIN
Product Catalog
https://drive.google.com/drive/folders/0BwvEeRN9dKllblZING00TkhYbGs?usp=sharing
Tutorial
This tutorial include codes,libraries and lessons. It is designed for beginners. It will teach every users how to assembly the robot kit and use
Arduino UNO controller board, sensors,servo and bluetooth. Simultaneously supports mixly graphical programming and arduino IDE code
programming, which is convenient for beginners to learn.The easy-to-install DIY tank chassis is adopted for easy installation. The power supply
uses two 18650 lithium batteries with long-lasting battery life.The newly created APP control software, the five major functions of the tank kit
are comprehensively controlled by the APP, and the function mode can be switched freely.
Packing List
1/193
Smart Robot Tank Kit Introduction
The robot tank kit is mainly manufactured using the arduino uno R3 main control and the TB6612 Motor drive expansion
board, two reduction motors, the tank DIY chassis and some sensors.
Smart tanks have the following advantages:
1)TB6612 motor drive expansion board integrates the driver chip, eliminating the traditional L298N complex wiring and
installation space, the expansion board uses standard interface wiring, simple and easy to understand, fast installation
wiring.
2/193
2) The newly created APP control software, the five major functions of the tank kit are comprehensively controlled by the
APP, and the function mode can be switched freely.
3/193
3) The newly designed Bluetooth module is connected to the circuit, and the upload program does not require manual
disconnection of the Bluetooth module. Help beginner learners to successfully avoid the trap of uploading program failure.
4/193
4)Simultaneously supports mixly graphical programming and arduino IDE code programming, which is convenient for
beginners to learn.
5/193
5)The easy-to-install DIY tank chassis is adopted for easy installation. The power supply uses two 18650 lithium batteries
with long-lasting battery life.
6/193
Lesson 1 Getting Started with Arduino IDE
How to Install Arduino IDE
Introduction
The Arduino Integrated Development Environment (IDE) is the software side of the Arduino platform.In this Project, you
will learn how to setup your computer to use Arduino and how to set about the Projects that follow.The Arduino software
that you will use to program your Arduino is available for Windows, Mac and Linux. The installation process is different for
all three platforms and unfortunately there is a certain amount of manual work to install the software.
The version available at this website is usually the latest version, and the actual version may be newer than the
version in the picture.
7/193
STEP2:Download the development software that is compatible with the operating system of your computer.
Take Windows as an example here.
8/193
Click Windows Installer.
9/193
Installing Arduino (Windows)
Install Arduino with the exe. Installation package.
10/193
Click Next
11/193
You can press Browse… to choose an installation path or directly type in the directory you want.
Click Install to initiate installation
12/193
Wait for the installing process, if appear the interface of Window Security, just continue to click
Install to finish the installation.
13/193
Double-click to enter the desired development environment
14/193
How to Install Arduino Driver
Next, we will introduce the driver installation of UNO R3 development board. The driver installation may have slight
differences in different computer systems. So in the following let’s move on to the driver installation in the Window system.
The Arduino folder contains both the Arduino program itself and the drivers that allow the Arduino to be connected to your
computer by a USB cable. Before we launch the Arduino software, you are going to install the USB drivers.
When you connect UNO board to your computer at the first time, right click the icon of your
“Computer” —>for “Properties”—> click the “Device manager” ,
under “Other Devices”or“USB-Serial”, you should see an icon for “Unknown device” with a little yellow warning triangle
next to it. This is your Arduino.Or you can search for "devi" in your computer, or you can open the device manager of your
computer.
15/193
16/193
Then right-click on the device and select the top menu option (Update Driver Software...) shown as the figure below.
Then it will be prompted to either “Search Automatically for updated driver software” or “Browse my computer for driver
17/193
software”. Shown as below. In this page, select “Browse my computer for driver software”.
Right-click on the device and select the top menu option (Update Driver Software...).
You will then be prompted to either ‘Search Automatically for updated driver software’ or ‘Browse my computer for driver
18/193
software’. Select the option to browse and navigate to the :C\Program Files(x86)\Arduino\drivers.(Note: Here is the path
you choose to install arduino IDE. The path chosen in the installation tutorial in the previous section is that, so the path I
chose isC\Program Files(x86)\Arduino\drivers)
19/193
Click “Next” and you may get a security warning, if so, allow the software to be installed.
Once the software has been installed, you will get a confirmation message. Installation completed,click “Close”.
20/193
Up to now, the driver is installed well. Then you can right click “Computer”—>“Properties”—>“Device manager” , you
should see the device as the figure shown below.
21/193
How to Add Libraries
Installing Additional Arduino Libraries
Once you are comfortable with the Arduino software and using the built-in functions, you may want to extend the ability of
your Arduino with additional libraries.
22/193
Importing a .zip Library
Libraries are often distributed as a ZIP file or folder. The name of the folder is the name of the library. Inside the folder will
be a .cpp file, a .h file and often a keywords.txt file, examples folder, and other files required by the library. you can install
3rd party libraries in the IDE. Do not unzip the downloaded library, leave it as is.
In the Arduino IDE, navigate to Sketch > Include Library. At the top of the drop down list, select the option to "Add .ZIP
Library''.
23/193
You will be prompted to select the library you would like to add. Navigate to the .zip file's location and open it.
(Note: This is just to demonstrate how to add a zip library file, whether you need to add a library file depends on your actual
program needs)
24/193
Return to the Sketch > Import Library menu. You should now see the library at the bottom of the drop-down menu. It is
ready to be used in your sketch. The zip file will have been expanded in the libraries folder in your Arduino sketches
directory.
NB: the Library will be available to use in sketches, but examples for the library will not be exposed in the File > Examples
until after the IDE has restarted.
Those two are the most common approaches. MAC and Linux systems can be handled likewise. The manual installation to
be introduced below as an alternative may be seldom used and users with no needs may skip it.
25/193
Blink Test
you will learn how to program your UNO controller board to blink the Arduino’s built-in LED, and how to download
programs by basic steps.
The UNO board has rows of connectors along both sides that are used to connect to several electronic devices and plug-in
'shields' that extends its capability.
It also has a single LED that you can control from your sketches. This LED is built onto the UNO board and is often
referred to as the 'L' LED as this is how it is labeled on the board.
26/193
You may find that your UNO board's 'L' LED already blinks when you connect it to a USB plug. This is because the boards
are generally shipped with the 'Blink' sketch pre-installed.
In this Project, we will reprogram the UNO board with our own Blink sketch and then change the rate at which it blinks.
In the previous chapter-How to install Arduino IDE, you set up your Arduino IDE and made sure that you could find the
right serial port for it to connect to your UNO board. The time has now come to put that connection to the test and program
your UNO board.
The Arduino IDE includes a large collection of example sketches that you can load up and use. This includes an example
sketch for making the 'L' LED blink.
Load the 'Blink' sketch that you will find in the IDE's menu system under File > Examples > 01.Basics
27/193
When the sketch window opens, enlarge it so that you can see the entire sketch in the window.
28/193
The example sketches included with the Arduino IDE are 'read-only'. That is, you can upload them to an UNO R3 board, but
if you change them, you cannot save them as the same file.
Since we are going to change this sketch, the first thing you need to do is save your own copy.
From the File menu on the Arduino IDE, select 'Save As..' and then save the sketch with the name 'MyBlink'.
29/193
You have saved your copy of 'Blink' in your sketchbook. This means that if you ever want to find it again, you can just
open it using the File > Sketchbook menu option.
30/193
Attach your Arduino board to your computer with the USB cable and check that the
'Board Type' and 'Serial Port' are set correctly.
31/193
32/193
Note: The Board Type and Serial Port here are not necessarily the same as shown in picture. If you are using 2560,
then you will have to choose Mega 2560 as the Board Type, other choices can be made in the same manner. And the
33/193
Serial Port displayed for everyone is different, despite COM 6 chosen here, it could be COM3 or COM4 on your
computer. A right COM port is supposed to be COMX (arduino XXX), which is by the certification criteria.
The Arduino IDE will show you the current settings for board at the bottom of the window.
Click on the 'Upload' button. The second button from the left on the toolbar.
If you watch the status area of the IDE, you will see a progress bar and a series of messages. At first, it will say 'Compiling
Sketch...'. This converts the sketch into a format suitable for uploading to the board.
Next, the status will change to 'Uploading'. At this point, the LEDs on the Arduino should start to flicker as the sketch is
transferred.
34/193
Finally, the staus will change to 'Done'.
The other message tells us that the sketch is using 928 bytes of the 32,256 bytes available.After the 'Compiling Sketch..'
stage you could get the following error message:
35/193
It can mean that your board is not connected at all, or the drivers have not been installed (if necessary) or that the wrong
serial port is selected.
If you encounter this, go back to Project 0 and check your installation.
Once the upload has completed, the board should restart and start blinking. Open the code
Note that a huge part of this sketch is composed of comments. These are not actual program instructions; rather, they just
explain how the program works. They are there for your benefit.
Everything between /* and */ at the top of the sketch is a block comment; it explains what the sketch is for.
Single line comments start with // and everything up until the end of that line is considered a comment.
The first line of code is: int led = 13;
As the comment above it explains, this is giving a name to the pin that the LED is attached to. This is 13 on most Arduinos,
36/193
including the UNO and Leonardo.
Next, we have the 'setup' function. Again, as the comment says, this is executed when the reset button is pressed. It is also
executed whenever the board resets for any reason, such as power first being applied to it, or after a sketch has been
uploaded.
void setup() {
// initialize the digital pin as an output. pinMode(led, OUTPUT);
}
Every Arduino sketch must have a 'setup' function, and the place where you might want to add instructions of your own is
between the { and the }.
In this case, there is just one command there, which, as the comment states tells the Arduino board that we are going to use
the LED pin as an output.
It is also mandatory for a sketch to have a 'loop' function. Unlike the 'setup' function that only runs once, after a reset, the
'loop' function will, after it has finished running its commands, immediately start again.
void loop()
{ digitalWrite(led, HIGH); delay(1000);
digitalWrite(led, LOW); delay(1000);
}
37/193
Inside the loop function, the commands first of all turn the LED pin on (HIGH), then 'delay' for 1000 milliseconds (1
second), then turn the LED pin off and pause for another second.
You are now going to make your LED blink faster. As you might have guessed, the key to this lies in changing the
parameter in () for the 'delay' command.
// turn the LED off (LOW is the voltage level) // wait for a second
// turn the LED on (HIGH is the voltage level) // wait for a second
This delay period is in milliseconds, so if you want the LED to blink twice as fast, change the value from 1000 to 500. This
would then pause for half a second each delay rather than a whole second.
Upload the sketch again and you should see the LED start to blink more quickly
38/193
Lesson 2 Getting Started with Mixly
Introduction of Mixly Software
Mixly is a free open-source graphical Arduino programming software, based on Google’s Blockly graphical programming
framework, and developed by Mixly Team@ BNU. It is a free open-source graphical programming tool for creative
electronic development; a complete support ecosystem for creative e-education; a stage for maker educators to realize
their dreams.Although there is an Ardublock graphical programming software launched by Arduino official, Ardublock is
Design Concept:
(1) Usability
Mixly is designed to be completely green. Currently. It can run on Windows xp and above.
(2) Simplicity
Mixly uses the Blockly graphical programming engine to replace complex text manipulation with graphical building
39/193
1 Use the different color icons to represent different types of functional blocks, very convenient for users to classify.
2 Provide default options in the composite function block to effectively reduce the number of user drags.
(3) Functionality
It has versatile functions. Mixly can almost implement all the functions that Arduino IDE has. Support all official
(4) Continuity
The goal of the graphical programming system is definitely not to replace the original text programming method,but to
better understand the programming principles and program thinking through graphical programming, and lay the
foundation for future text programming. It is also the design philosophy for Mixly. More continuous content has been
added to the design of the software to protect the user's learning outcomes. To be specific, it includes the introduction of
variable types, the consistency of text programming as much as possible in the design of the module, and the support of
40/193
both graphical and text programming.
(5) Ecological
The most important design concept of Mixly is its ecological feature, which can distinguish it from other Arduino
graphical programming.In order to achieve sustainable development, Mixly is designed to allow manufacturers to
develop their own unique modules.But users require JavaScript programming foundation to make this part of the
module).It also allows users directly use Mixly's graphical programming function to generate common modules (such as
LED digital display, buzzer broadcast, etc. Users are able to make this part of the module only using Mixly). Both of the
two kinds of modules mentioned above can be imported into the Mixly system through the "Import" function, thereby
User Groups:
From the above design concept, it can be seen that Mixly is suitable for primary and secondary school students to
cultivate programming thinking. It is also available for quick programming when creating a work.it is good for those
lovely friends who don't want to learn text programming, but want to do some small works with intelligent control.
41/193
Mixly Blocks Functions:
System Functions
Look at the main interface of Mixly, it includes five parts, that is, Blocks selection, code edit, text code (hidden),
42/193
Some common functions:
Through this interface, you can complete the code compile、upload、save and manage. It support four remove
methods: drag it left out code window, or drag to Recycle Bin, delete key, or right-click to delete block. It supports
43/193
How to Install Mixly Software
First decompress the mixly programming software file
After unzipping you will get the following files(Note: The name of the uncompressed path cannot have a space bar. If
the named name needs a separator, you can use an underscore to replace the space bar, for example,you should name the
44/193
45/193
Double-click the mixly application file to open the software. And select the main control board as Arduino/Genuino Uno
46/193
How to Add Mixly Libraries
The mixly library file is an integrated graphical code customized for a certain control board. For example, we have written a
mixly library file for smart robot tanks. You can directly load it into mixly software and use it directly, which brings
convenience to your programming process.The general library file contains the following files, the last .xml file is the
47/193
After opening the mixly software, select "import" and then select the path where the "Smart_Robot_Tank.xml" folder is
48/193
The figure below shows that the ”Smart_Robot_Tank”library file has been successfully imported into the mixly software.
49/193
Next we write a program on the software and upload it to smart robot tank.
Drag the block to control the tank advance to the programming area, and modify the PWM variable to 200.
50/193
Connect the Arduino UNO development board to the computer with a USB data cable, and turn on the power switch. Select
the development board type as "Arduino UNO" on the software and you will see a new connection serial port "COM5"
appears.
51/193
Click Upload and wait for the upload to complete.After the program upload is completed, your Tank will go forward at a
PWM of 200. The variable range of PWM is 0-255, 0 stands for stop, 255 stands for the maximum speed
52/193
How to open the reference program
Click "OPEN" on the mixly software and select the reference program
53/193
54/193
Maybe the program software you opened displays Chinese, you can set the language in the upper right corner of the
software, you can choose English.Currently, Smart_Robot_Tank library does not support Spanish. You choose Spanish, the
55/193
56/193
57/193
The use of each statement block is described below
In/Out Block:
58/193
NO. BLOCK ICON DEFINITION
59/193
Write analog value between 2 and 255 to a
60/193
8 Set the IO pins as Output or Input state
ranging)
61/193
Control Block:
62/193
NO
BLOCK ICON DEFINITION
.
63/193
specified as parameter. There are 1000 milliseconds in
a second.)
block.)
64/193
switch function. You can click the blue gear icon to
65/193
7 A while loop statement.
66/193
millis() function, returns the system running time since
parameter.
67/193
11 Timer interrupt start block
68/193
Math Block:
69/193
NO. BLOCK ICON DEFINITION
1 A number
70/193
Click to select the sin; cos; tan; asin; acos; atan; ln;
71/193
square root of a number.
72/193
Text Block:
73/193
NO. BLOCK ICON DEFINITION
2 A character
74/193
Returns the char corresponding to an ASCII code
5
(Decimal number 97 corresponding to a)
75/193
The first string equals or startsWith or endsWith the
76/193
List Block:
77/193
NO. BLOCK ICON DEFINITION
3
Returns the length of a list
78/193
Logic Block:
79/193
NO
BLOCK ICON DEFINITION
.
logic comparision =: Return true if both inputs equal each
other. <: Return true if the first input is smaller than the
input.
80/193
and: Return true if both inputs are true; or: Return true if
2
at least one of the inputs is true
5 Returns null
81/193
SerialPort Block:
82/193
NO. BLOCK ICON DEFINITION
83/193
Print the specified number in hexademical
5
format on newline of monitor.
Bluetooth communication)
84/193
Read the serial data by byte (generally used to
an interrupt function)
85/193
Monitor Block:
86/193
NO. BLOCK ICON DEFINITION
87/193
Variables Block
88/193
NO. BLOCK ICON DEFINITION
constants or variables
89/193
Functions Block:
90/193
NO. BLOCK ICON DEFINITION
Creates a function with no output. Click the blue icon to set the
1
procedure parameter. (no return value)
Creates a function with an output. Click the blue icon to set the
2 procedure parameter. (with return value and can set the data
types)
91/193
Smart_Robot_Tank Block:
92/193
93/193
NO. BLOCK ICON DEFINITION
means stop.
means stop.
94/193
3 Control the tank to rotate counterclockwise
95/193
0~255, when the PWM is equal to 255, the
96/193
9 Infrared remote control receiver, receive the
left_infrared_avoidance_sensor or
97/193
right_infrared_avoidance_sensor
variable bluetooth_val
98/193
Lesson 3 Motor Speed and Direction Control
Overview
In this lesson we will learn how to control the rotation of the motor, and realize the direction and speed control of the tank
Motor Driver
The robot tank uses TB6612FNG as the driving chip of the motor. The expansion board has integrated the TB6612FNG
chip.TB6612FNG is a straight product produced by Toshiba Semiconductor Corporation.Current motor drive device, which
has a large current MOSFET-H bridge junction Structure, dual-channel circuit output, can drive 2 motors at the same
time.TB6612FNG outputs continuous drive power up to 1 A per channel Current, starting peak current up to 2A/3A
(continuous pulse/single pulse (Punch); 4 motor control modes: forward/reverse/brake/stop End; PWM supports frequencies
up to 100 kHz; standby state;Internal low voltage detection circuit and thermal shutdown protection circuit; working
99/193
Specifications:
100/193
101/193
A1 A2 B1 B2 of the motor arduino shield is the interface of the motor. A1 and A2 are the same. B1 and B2 are also the same.
The motor of the A interface is controlled by the D2 PIN, and the D5 PIN outputs the PWG signal to control the speed. The
motor of B interface is controlled by D4 PIN, and the speed of D6 PIN output PWG signal controls the speed.
The PWM value is in the range of 0-255. The greater the value, the faster the motors turn.
Rotate to left HIGH 255 clockwise rotation HIGH 255 clockwise rotation
Rotate to right LOW 255 Anticlockwise rotation LOW 255 Anticlockwise rotation
102/193
How to connect the circuit
103/193
Let's program
Test 1--Motor Speed and Direction Control
In experimental test 1, we will write code to control the tank to move forward, backward, rotate to left, rotate to right, stop.
Mixly Code
Wire it up well as the above diagram. Okay, let’s move on to write the test code.Open mixly software.Click “New” to add
if you want to refer to the program we provide.open the reference code for this lesson
Programming Thinking
104/193
This block controls the direction of the tank to forward, 255 represents the maximum speed, and the range that can be
The character is "left", the speed value is 200, which means that the control tank is rotated to the left, and the rotation speed
is 200.
The character is "turn left", which means that the control tank turns to the left and forward
105/193
Arduino Code
if you want to refer to the program we provide.open the reference code for this lesson
Programming Thinking
In arduino IDE, It means to define a sub-function. Here we define a sub-function to control the progress of the tank. It can
void forward()
{digitalWrite(2,LOW);
analogWrite(5,255);
digitalWrite(4,HIGH);
analogWrite(6,255);
106/193
What will you see
Upload the test code to UNO R3 control board, turn the POWER switch ON.You will see that the tank takes 1 second to
move forward, then 1 second to retreat, 1 second to the left, 1 second to the right, and 1 second to stop. And then loop
107/193
Lesson 4 Infrared Obstacle Avoidance Robot Tank
Overview
In this lesson, we will complete the test of two experimental codes. In Experimental Test 1, we separately learn how to use
infrared obstacle avoidance sensors to detect obstacles, and observe the results returned by the sensors to determine whether
obstacles are detected. In Experimental Test 2, we learned to combine the infrared obstacle avoidance sensor with the motor
108/193
What is infrared obstacle avoidance sensor
The infrared obstacle detector sensor has a pair of infrared transmitting and receiving tubes.The transmitter emits an
infrared rays of a certain frequency. When the detection direction encounters an obstacle (reflecting surface), the infrared
rays are reflected back, and receiving tube will receive it.At this time, the indicator ( green LED) lights up. After processed
by the circuit, the signal output terminal will output Digital signal.You can rotate the potentiometer on the shield to adjust
the detection distance. It is better to adjust the potentiometer to make the green LED in a state between on and off. The
When detects an obstacle, sensor’s signal pin outputs LOW (display 0); otherwise, output HIGH (display 1).
Show the result on the serial monitor, and control the external LED module turn ON/OFF.
109/193
110/193
How to connect the circuit
111/193
Let's program
Test 1--infrared obstacle avoidance sensor
The main purpose of the test experiment is to read the return signal of the infrared obstacle avoidance sensor and print it to
the serial port monitor.When detects an obstacle, sensor’s signal pin outputs LOW (display 0); otherwise, output HIGH
(display 1).
Mixly Code
Wire it up well as the above diagram. Okay, let’s move on to write the test code.Open mixly software.Click “New” to add
if you want to refer to the program we provide.open the reference code for this lesson
112/193
Perhaps the language displayed when you open the mixly software is not English, you can switch the software language
here
113/193
After uploading the program, open the serial monitor to view the signal returned by the infrared obstacle
avoidance sensor.
114/193
Then you can see the data as blow:
When there is an obstacle in the front, -light_value=0, and the indicator light on the sensor lights up.
Important note: Before you upload the program, you need to close the serial monitor, otherwise the serial monitor will
occupy the communication interface of the uploaded program.
115/193
Arduino Code
if you want to refer to the program we provide.open the reference code for this lesson
"Test_1_Infrared_Obstacle_Avoidance_Sensor.ino" in the reference materials we provided.
After uploading the code, click the button in the upper right corner to open the serial monitor to view the measured distance
116/193
Then you can see the data as blow:
When there is an obstacle in the front, light_value=0, and the indicator light on the sensor lights up.
Important note: Before you upload the program, you need to close the serial monitor, otherwise the serial monitor will
occupy the communication interface of the uploaded program.
117/193
Test 2--Infrared Obstacle Avoidance Robot Tank
Mixly Code
Think about the code logic.Open mixly software.Click “New” to add new project, then start your programming .
if you want to refer to the program we provide.open the reference code for this lesson
"Test_2_Infrared_Obstacle_Avoidance_Robot_Tank.mix" in the reference materials we provided.
Programming Thinking
(1)If the infrared obstacle avoidance sensor on the left detects an obstacle, the infrared obstacle avoidance sensor on the
118/193
(2)If the infrared obstacle avoidance sensor on the right detects an obstacle, the infrared obstacle avoidance sensor on the
119/193
(3)If the left and right sensors detect obstacles at the same time,The tank go backward,then the tank randomly chooses to
120/193
121/193
(4)If neither the left nor the right sensors detect obstacles,The tank go straight.
122/193
Arduino Code
if you want to refer to the program we provide.open the reference code for this lesson
"Test_2_Infrared_Obstacle_Avoidance_Robot_Tank.ino" in the reference materials we provided.
Programming Thinking
(1)If the infrared obstacle avoidance sensor on the left detects an obstacle, the infrared obstacle avoidance sensor on the
backward();
delay(500);
rotate_right();
123/193
delay(500);
(2)If the infrared obstacle avoidance sensor on the right detects an obstacle, the infrared obstacle avoidance sensor on the
backward();
delay(500);
rotate_left();
delay(500);
124/193
(3)If the left and right sensors detect obstacles at the same time,The tank go backward,then the tank randomly chooses to
if (left_light==0&& right_light==0)
stopp();
delay(500);
backward();
delay(500);
rotate_left();
125/193
else
rotate_right();
delay(500);
}
(4)If neither the left nor the right sensors detect obstacles,The tank go straight.
else
forward();
126/193
What will you see
Upload the test code to UNO R3 control board, turn the POWER
switch ON.
If there is an obstacle in front of the left of the tank, the tank will retreat
If there is an obstacle in the front right of the tank, the tank will retreat
If there are obstacles in the front left and front right, the tank moves
Note: According to the physical laws: Black objects have a strong ability to absorb light. The infrared obstacle avoidance
function relies on transmitting and receiving infrared rays to detect obstacles in front. When all obstacles in front of the tank
are black objects, the sensitivity of the sensor detection will decrease.
127/193
Lesson 5 Ultrasonic Obstacle Avoidance Robot Tank
Overview
In this lesson we will learn about ultrasonic sensors and servos and combine them. The servo motor drives the ultrasonic
wave to help the tank detect the front left and front right obstacles as well as the front obstacles, thus avoiding obstacles.Use
ultrasound to measure the distance of objects in front of the tank to help the tank to follow the object in front of it.
128/193
What is an ultrasonic sensor
Review the ultrasonic sensor from the previous lesson. It works like a bat's eye. Determine the distance of obstacles in front
As the following picture shown, it is our ultrasonic module. It has two something like eyes.
129/193
What is a servo motor
Servo motors are great devices that can turn to a specified position.Usually, they have a servo arm that can turn 180 degrees.
Using the Arduino, we can tell a servo to go to a specified position and it will go there. As simple as that!Servo motors were
first used in the Remote Control (RC) world, usually to control the steering of RC cars or the flaps on a RC plane. With time,
they found their uses in robotics, automation, and of course, the Arduino world.
There are two ways to control a servomotor with Arduino. One is to use a common digital sensor port of Arduino to produce
square wave with different duty cycle to simulate PWM signal and use that signal to control the positioning of the motor.
Another way is to directly use the Servo function of the Arduino to control the motor. In this way, the program will be
easier.Next, we learn how to control the servo.The servo motor has three leads. The color of the leads varies between servo
motors, but the red lead is always 5V and GND will either be brown.The red one is the power wire and should be connected
130/193
131/193
How to connect the circuit
132/193
Let's program
Test 1--Servo
In experimental test 1, we learned how to control the servo motor and control the servo motor to rotate to the
specified angle.
Mixly Code
Wire it up well as the above diagram. Okay, let’s move on to write the test code.Open mixly software.Click “New” to add
if you want to refer to the program we provide.open the reference code for this lesson "Test_1_Servo.mix" in the reference
materials we provided.
133/193
Programming Thinking
This code block is used for loop counting, counting from 0 to 180, increasing by 1 each time
This code block is used to control the motor rotation angle, the angle of pos code input, the range is 0-180
134/193
Arduino Code
if you want to refer to the program we provide.open Arduino IDE software and open the reference code for this lesson
your code won't work.For details about loading the library file, see Lesson about how to add libraries .
Programming Thinking
//This code block is used for loop counting, counting from 0 to 180, increasing by 1 each time
for (int POS = 0; POS <= 180; POS = POS + (1))
{
}
//This code block is used to control the motor rotation angle, the angle of pos code input, the range is 0-180
myservo.write(POS);
After uploading the code, the servo motor rotates from 0 degrees to 180 degrees, 1 degree at a time. Then rotate from 180
135/193
Test 2--Ultrasonic Sensor Module
In Experimental Test 2, we will learn how to control the ultrasonic sensor, and display the distance measured
by the ultrasonic sensor on the serial monitor.
Mixly Code
Wire it up well as the above diagram. Okay, let’s move on to write the test code.Open mixly software.Click “New” to add
if you want to refer to the program we provide.open the reference code for this lesson "Test_2_Ultrasonic Sensor
136/193
After uploading the program, open the serial monitor to view the distance measured by the ultrasonic sensor.
137/193
Arduino Code
if you want to refer to the program we provide.open Arduino IDE software and open the reference code for this lesson
After uploading the code, click the button in the upper right corner to open the serial monitor to view the measured distance
138/193
Then you can see the data as blow:
139/193
Test 3--Ultrasonic_Obstacle_Avoidance_Robot_Tank
In the experimental test 3, the servo motor, ultrasonic sensor and motor are simultaneously controlled to help the tank
Mixly Code
Think about the code logic.Open mixly software.Click “New” to add new project, then start your programming .
if you want to refer to the program we provide.open the reference code for this lesson
Programming Thinking
First, we define the distance between tank and the obstacle in front left as DL, the distance between tank and the obstacle in
140/193
front is DM, and the distance between tank and the obstacle in front is DR.
If the distance in front of the tank is less than 20cm, the tank stop
At the same time, when the distance of obstacles in front left or front right is less than 50cm, and
The obstacle in front right is closer to the tank than the obstacle in front left, turn left
141/193
The obstacle on the front left is closer to the tank than the obstacle on the right, turn right
142/193
143/193
Arduino Code
if you want to refer to the program we provide.open the reference code for this lesson
Programming Thinking
First, we define the distance between tank and the obstacle in front left as DL, the distance between tank and the obstacle in
front is DM, and the distance between tank and the obstacle in front is DR.
If the distance in front of the tank is less than 20cm, the tank stop
stopp();
delay(1000);
144/193
}
At the same time, when the distance of obstacles in front left or front right is less than 50cm, and The obstacle in
front right is closer to the tank than the obstacle in front left, turn left
procedure(90);
rotate_left();
delay(500);
forward();
145/193
(2)The obstacle on the front left is closer to the tank than the obstacle on the right, turn right
procedure(90);
rotate_left();
delay(500);
forward();
} else {
procedure(90);
rotate_right();
delay(500);
forward();}
146/193
What will you see
Upload the code to UNO R3 control board,, and turn the POWER switch ON.
When there is no obstacle in front of the tank, the tank keeps walking straight. When the distance of the obstacle in front of
the tank is less than 20cm, the tank stops, then detects whether there is an obstacle in the left front and right front, and then
147/193
Test 4--Ultrasonic_Follow_Robot_Tank
In this experimental test,Use ultrasound to measure the distance of objects in front of the tank to help the tank to follow the
Mixly Code
Think about the code logic.Open mixly software.Click “New” to add new project, then start your programming .
if you want to refer to the program we provide.open the reference code for this lesson
148/193
Programming Thinking
When detecting that the distance of the object in front is less than 10cm, the tank moves backward.
When the distance of the object in front is detected between 10~20cm, the tank stops.
149/193
When the distance of the object in front is detected between 20~60cm, the tank moves forward.
When detecting that the distance of the object in front is greater than 60cm, the tank stops.
150/193
Arduino Code
if you want to refer to the program we provide.open the reference code for this lesson
Programming Thinking
//When detecting that the distance of the object in front is less than 10cm, the tank moves backward.
if (distance <= 10)
{
backward();
}
//When the distance of the object in front is detected between 10~20cm, the tank stops.
else if (distance > 10 && distance < 20)
{
stopp();
}
151/193
//When the distance of the object in front is detected between 20~60cm, the tank moves forward.
else if (distance >= 20 && distance <= 60)
{
forward();
}
//When detecting that the distance of the object in front is greater than 60cm, the tank stops.
else
{
stopp();
}
152/193
What will you see
Upload the code to UNO R3 control board,, and turn the POWER switch ON.
The tank detects the distance of the object in front through the ultrasonic sensor. When the front object moves, the tank will
follow the movement, keeping the distance between the tank and the object in front between 10cm~20cm.
153/193
Lesson 6 Infrared Remote Control Robot Tank
Overview
In this lesson, we will learn the infrared remote control, and use the infrared remote control to control the tank go forward,
154/193
What is an infrared remote control
There is no doubt that infrared remote control is commonly seen in our daily life. It's hard to imagine our world without
it.An infrared remote control can be used to control a wide range of home appliances such as television, audio, video
recorders and satellite signal receivers.Well, in the following let’s get a better understanding of the infrared remote
control.Infrared remote control is composed of infrared transmitting and infrared receiving systems.That is, consist of an
infrared remote control, an infrared receiver module and a microcontroller that can decode.You can refer to the figure below.
155/193
The 38K infrared carrier signal transmitted by an infrared remote controller is encoded by an encoding chip inside the
remote controller. It is composed of a pilot code, user code, data code, and data inversion code.The time interval between
pulses is used to distinguish whether it is a signal 0 or 1. (when the ratio of high level to low level is about 1:1, considered
as signal 0.)
And the encoding is just well composed of signal 0 and 1.The user code of the same button on remote controller is
unchanged. Using difference data distinguish the key pressed on the remote control. When press down a button on the
remote control, it will send out an infrared carrier signal. And when infrared receiver receives that signal, its program will
decode the carrier signal, and through different data codes, thus can judge which key is pressed.The microcontroller is
decoded by an received signal 0 or 1 to determine which key is pressed by the remote control.
156/193
What is an infrared receiver
The robot shield comes with infrared receiver module. It is mainly composed of an infrared receiving head. This
device integrates with reception, amplification and demodulation.Its inter IC has been demodulated, outputting Digital
157/193
Let's program
Test 1--Infrared Remote Control Robot Tank
In experimental test 1, learn to receive infrared remote control signals, and distinguish the corresponding
remote control key value, and finally realize infrared remote control tank.
Mixly Code
Think about the code logic.Well, figure out the logic,you can have a try to write out the code logic of infrared obstacle
avoidance.Open mixly software.Click “New” to add new project, then start your programming .
if you want to refer to the program we provide.open the reference code for this lesson
158/193
Programming Thinking
This program block stores the received key values of the infrared remote control into variables ir_rec.
If the value of the pressed key is equal to 0FXX629D, it means that the up key is pressed. Control the tank to
159/193
After completing the code upload, use the infrared remote control to control the tank's direction of advance.
Key on IR
remote
control
Robot status Go forward Go backward Turn left Turn right Rotate to left Rotate to right Stop
160/193
Arduino Code
if you want to refer to the program we provide.open the reference code for this lesson
Before you can run this, make sure that you have installed the < IRremote > library or re-install it, if necessary. Otherwise,
your code won't work.For details about loading the library file, see Lesson about how to add libraries .
Programming Thinking
//This program block stores the received key values of the infrared remote control into variables ir_rec.
if (irrecv.decode(&results))
ir_rec=results.value;
irrecv.resume();
161/193
After completing the code upload, use the infrared remote control to control the tank's direction of advance.
Key on IR
remote
control
Robot status Go forward Go backward Turn left Turn right Rotate to left Rotate to right Stop
162/193
Lesson 7 APP Control Robot Tank
Overview
In this lesson, we mainly study two experimental tests. In Test 1, we learn the communication between the Bluetooth
module and the mobile phone. In Test 2, we can freely switch the various functional modes through the mobile phone app,
such as ultrasonic obstacle avoidance, infrared obstacle avoidance, infrared remote control, gravity sensor remote control.
163/193
What is a Bluetooth module
Principle and Application of Bluetooth Remote Control Bluetooth, as the name implies,
blue teeth, and is not used to bite people, but a wireless data transmission method.Bluetooth
technology is a wir eless standard technology that enables short-range data exchange among
fixed devices, mobile devices, and personal area networks of buildings (UHF radio waves
in the ISM band of 2.4 to 2.485GHz).There are two kinds of commonly used Bluetooth module
on the market,HC-05 and HC-06 models. The difference between them is that the HC-05 is
a master-slave one. It can not only make small reports to its own “master”, but also can receive the command given to it.
The HC-06 can only work in slave mode, which can only accept the superior command. For instance, in many cases you
may want to be an overbearing man, letting the subordinates obey the order without any nonsense. So in such situation, it is
164/193
How to connect the circuit
165/193
Instructions for the use of app
Firstly, download the “APP for LAFVIN iBot.apk” file from the folder to your mobile phone and install it into an
application software.
Then make sure the Bluetooth module is connected. Pair your phone with HC-06. for doing this go to
Settings->Bluetooth->Scan device->select HC-06 and pair it. Pass code to pair is '1234'.Open Bluetooth Terminal software,
go to options and select 'connect a device - secure' option. It it ask for pass code enter 1234.If your phone is connected to the
166/193
Bluetooth module, you will see a usable device called HC-06 on the PAIRED DEVICES(As shown below).If the HC-06
does not appear on the PAIRED DEVICES, reoperate the above steps.
167/193
After the above steps are complete, we open the LAFVIN_iBot app.
168/193
Click the Connect Bluetooth icon button in the upper right corner to enter the Bluetooth connection interface
169/193
Then click on the , and the HC-06 will appear in our scan results.Select HC-06.
170/193
After selecting the hc-06 device, Click to connect will appear in the Gray box of the app.
171/193
click on the button Click to connect .Gray box turns into orange box. wait 5 seconds.
172/193
The button shows the Connected , which means that the app is connected to the Bluetooth module successfully.The icon in the upper right
corner of the connection is changed from red to white (Note: When the Bluetooth module is not successfully
connected, the red LED light will continue to flash. When the connection is successful, the red LED light will be remain
on.)
173/193
If there is a warning in the interface, it means that Bluetooth is not turned on normally or the selected device is wrong.
174/193
Let's program
Test 1--Bluetooth Module Receives Information
In Experimental Test 1, we learned how to receive the information sent by the mobile phone app to the
Bluetooth module HC-06.
Mixly Code
Think about the code logic.Click “New” to add new project, then start your programming .
if you want to refer to the program we provide.open the reference code for this lesson
175/193
Programming Thinking
Set the baud rate for serial communication to 9600. The communication speed between the serial port of the
arduino uno main control board and the serial port of Bluetooth needs to be the same. The communication
baud rate of the HC-06 Bluetooth module is 9600 by default.
Store the information received by Bluetooth in the variable bluetooth_val and display it on the serial
monitor.
176/193
Perhaps the language displayed when you open the mixly software is not English, you can switch the software language
here.After completing the program upload, open the serial monitor to view the information received by the Bluetooth
module.
177/193
Then you can see the data as blow:
When you press the up button , the character received by Bluetooth is ‘F’,
When you press the down button , the character received by Bluetooth is ‘G
178/193
Important note: Before you upload the program, you need to close the serial monitor, otherwise the serial monitor will
occupy the communication interface of the uploaded program.
179/193
Arduino Code
if you want to refer to the program we provide.open the reference code for this lesson
180/193
Programming Thinking
//Set the baud rate for serial communication to 9600. The communication speed between the serial port of the arduino uno
main control board and the serial port of Bluetooth needs to be the same. The communication baud rate of the HC-06
void setup(){
Serial.begin(9600);}
//Store the information received by Bluetooth in the variable bluetooth_val and display it on the serial monitor.
if (Serial.available())
bluetooth_val = Serial.read();
Serial.println(bluetooth_val);
After completing the program upload, open the serial monitor to view the information received by the Bluetooth module.
181/193
Then you can see the data as blow:
When you press the up button , the character received by Bluetooth is ‘F’,
When you press the down button , the character received by Bluetooth is ‘G
182/193
Important note: Before you upload the program, you need to close the serial monitor, otherwise the serial monitor will
occupy the communication interface of the uploaded program.
183/193
Test 2--APP Control Robot Tank
In this experimental test, all our functions are collected into the same program, including ultrasonic obstacle
avoidance function, infrared remote control function, infrared obstacle avoidance function, gravity sensor
control function, and any function can be freely switched through the app.
Mixly Code
Think about the code logic.Click “New” to add new project, then start your programming .
if you want to refer to the program we provide.open the reference code for this lesson "Test_2_APP_Control_Robot_Tank
184/193
Programming Thinking
The method of defining sub-functions, here defines a sub-function called Infrared Remote Control, do is filled with code
The code block do Infrared Remote Control is to directly call the subfunction.
185/193
Arduino Code
if you want to refer to the program we provide.open the reference code for this lesson
Before you can run this, make sure that you have installed the < IRremote > <Servo> library or re-install it, if necessary.
Otherwise, your code won't work.For details about loading the library file, see Lesson about how to add libraries .
Programming Thinking
//After Bluetooth receives the information, "switch" judges what the received information character is, "F" stands for tank
forward, "G" stands for tank backward, "H" stands for tank turn left, and "I" stands for tank right Turn,'J' means the tank
rotates left,'K' means the tank rotates right,'C' means the tank enters infrared obstacle avoidance function mode,'D' means
the tank enters infrared remote control function mode,'E' means the tank enters ultrasonic obstacle avoidance function
Functional mode. Click the return and "STOP" button APP will send the ‘S’ character to control the tank to exit the function
186/193
if (Serial.available())
bluetooth_data = Serial.read();
Serial.println(bluetooth_data);
switch (bluetooth_data) {
case 'F':
forward(250,250);
break;
case 'G':
backward(240,240);
187/193
break;
case 'H':
turn_left(100,255);
break;
case 'I':
turn_right(255,100);
break;
case 'J':
rotate_left(240,240);
break;
case 'K':
rotate_right(240,240);
break;
188/193
case 'S':
stopp();
break;
case 'E':
Infrared_Obstacle_Avoidance();
break;
case 'C':
Ultrasonic_Obstacle_Avoidance();
break;
case 'D':
Infrared_Remote_Control();
break;
189/193
How to use the app to control the robot car
After completing the program upload, and then open the app software to complete the connection of the Bluetooth device
(App and HC-06 Bluetooth module connection reference Test 1 Bluetooth Module Receives Information).
The interface after the app software and Bluetooth module are successfully connected is as follows.
190/193
If you want to select the ultrasonic obstacle avoidance mode, swipe the screen to the right on the homepage, click the
corresponding function icon, the app will send the character'D' to the tank's Bluetooth module HC-06, the tank will judge
the distance of the obstacle through the ultrasonic wave, if Too close to the obstacle, the tank turns and then
advances.avoidance.
191/193
If you want to exit the ultrasonic obstacle avoidance function mode, click the back button in the upper left corner to return
192/193
You can freely switch the various functional modes through the mobile phone app, such as ultrasonic obstacle avoidance,
infrared obstacle avoidance, infrared remote control, gravity sensor remote control.
193/193