Ks0313 keyestudio Desktop Mini Bluetooth Smart Car V2.0 Kit

From Keyestudio Wiki
Jump to navigation Jump to search

keyestudio keyestudio Desktop Mini Bluetooth Smart Car V2.0 Kit


thumb


Overview

Arduino is an open-source electronics platform based on easy-to-use and flexible hardware and software, pretty suitable for beginners to learn entry level hardware. Many beginners around me were caught in such a dilemma. They have learned all the tutorials on the official website of Arduino, however, there is still not much sense of accomplishment. It is very envious to see various developments online based on Arduino. They want to start small projects but do not know how to get started with. Because the development process of complete project is less visible on the Internet. Our KEYESTUDIO team has integrated its own many years of ARDUINO development experience to develop this desktop mini Bluetooth smart car kit.
From the basics up to complex projects, through this kit you can study the motor driving, as well as the principle and source code of our smart car with tracking, obstacle avoidance, infrared remote control and Bluetooth remote control functions. Take your brain on a fun and inspiring journey through the world of programming and electronics. Believe you will enjoy the fun of DIY production and programming while learning. Is it a very great thing? If you are looking for inspiration, you can find a great variety of tutorials here. Okay, in the following let’s get started with those interactive and amazing projects!


Parameters

  • 1)Motor Voltage range: 1-6V; motor shaft length: 10mm; speed: 6.0V 100rpm/min.
  • 2)Motor control is driven by L298P.
  • 3)Three groups of line tracking modules, to detect black-white line with higher accuracy and can also be used for anti-fall control.
  • 4)Two groups of obstacle detector modules, to detect whether there are obstacles on the left or right side of smart car; Ultrasonic module is used to detect the distance between ultrasonic and obstacles, forming the smart car’s obstacle avoidance system.
  • 5)Bluetooth wireless modulecan be paired with Bluetooth device on mobile phone to remotely control smart car.
  • 6)Infrared receiver module is matched with an infrared remote control to control the smart car.
  • 7)Can access the external 7 ~ 12V voltage.


Component List

No. Component Quantity Picture
1 Keyestudio UNO R3 Main Board 1
thumb
2 Keyestudio quick connectors motor driver shield 1
thumb
3 Keyestudio quick connectors IR receiver module 1
thumb
4 Keyestudio quick connectors line tracking sensor 1
thumb
5 Keyestudio quick connectors 12FN20 motor 2
thumbthumb
6 Keyestudio quick connectors obstacle detector module 2
thumbthumb
7 Keyestudio quick connectors ultrasonic module 1
thumb
8 keyestudio Bluetooth HC-06 1
thumb
9 Keyestudio JMFP-4 17-button 86*40*6.5MM yellow (eco-friendly) (no battery) 1
thumb
10 Double-Connector JST-PH2.0MM-5P 24AWG blue-green-yellow-red-black wire 15CM (reverse direction) 1
thumb
11 Double-Connector JST-PH2.0MM-4P 24AWG green-yellow-red-black wire 8CM (reverse direction) 1
thumb
12 Double-Connector JST-PH2.0MM-3P 24AWG yellow-red-black wire 8CM (reverse direction) 3
thumb

thumb

thumb
13 Double-Connector JST-PH2.0MM-2P 24AWG red-black wire 160mm 2
thumb

thumb
14 Battery holder with JST-PH2.0MM-2P socket lead, black-red lead length 150mm 1
thumb
15 Screw M2*10MM round head 6
thumbthumbthumbthumbthumbthumb
16 Nut M2 nickle plating 6
thumbthumbthumbthumbthumbthumb
17 Screw M3*10MM round head cross 11
thumbthumbthumbthumbthumbthumbthumbthumbthumb
thumbthumb
18 Screw M3*6MM round head 18
thumbthumbthumbthumb thumb thumb thumb thumb thumb thumb thumb thumb thumb thumb thumb thumb thumb thumb
19 Screw M3*8MM flat head 4
thumb thumbthumb thumb
20 Nut M3 nickle plating 19
thumb thumb

thumbthumbthumb thumbthumbthumb thumbthumbthumb thumb thumbthumb thumbthumbthumbthumb thumb

21 Dual-pass M3*40MM Copper Pillar 4
thumb thumb thumb thumb
22 Single-pass M3*8+6MM 4
thumb thumb thumb thumb
23 Single-pass M3*5+5MM 2
thumb thumb
24 White Plastic Wheel Diameter: 32mm; Width: 7mm ; Aperture: 3mm D-type hole 2
thumbthumb
25 N20 motor holder white U-type plastic 2
thumb thumb
26 Transparent acrylic panel (3PCS) thickness 3MM 1
thumb
27 Black-yellow Handle 3*40MM cross screwdriver 1
thumb
28 Lithium Ion Battery Charger 18650 double-cell with plug 1
thumb
29 18650 battery pack 3.7V blue 2PCS CS100% 1
thumb
30 USB cable AM/BM transparent blue OD:5.0 L=1m 1
thumb
31 W420 Ball Caster Wheel (Ball Diameter 15MM; Holder Material: Nylon) 1
thumb


Assembly Steps

When all the components have been counted well, do you cannot wait to assemble it? Woah, in the following let’s begin to assemble this smart car together, so try to experience the fun of DIY. Follow the assembly steps here to build your own robot, believe it or not, you will be full of delight and get a great sense of achievement. If still confused, you can also refer to our assembly video for this smart car.

(1)Begin with the bottom parts, you should hook up two motors on the acrylic board. How to get started with? Firstly, you should prepare the components as follows:

  • M2 Nut *2
  • White N20 motor holder *2
  • 12FN20 motor connector *2
  • M2*10MM round-head screw *4
  • Acrylic bottom board *1
  • 2P 160mm red-black connector wire *2


KS0313 4-1.png

After that, place the white N20 holders onto the motors.
KS0313 4-2.png

After that, place the white N20 holders onto the motors.
KS0313 4-3.png

Then fix these two 12FN20 motor connectors on the back of acrylic bottom board with four M2*10MM round-head screws.
KS0313 4-4.png


KS0313 4-5.png
Here you should connect two pieces of 2P160mm red-black connector wire to the 12FN20 motor connectors. Shown below.
KS0313 4-6.png


(2)Next, mount the battery case on the acrylic bottom board. You should first get some parts below:

  • 18650 red battery pack*1
  • Battery case*1
  • M3*8MM flat-head screws *2
  • M3 Nut *2


KS0313 4-7.png

KS0313 4-8.png

Then fix well the battery case on the top of Acrylic board using two M3*8MM flat-head screws and two M3 Nuts. Insert the 18650 red battery pack into battery case.
KS0313 4-9.png


(3)Followed by assembling the line tracking sensor, you should get these components as follows:

  • Line tracking sensor *1
  • M3 Nut *2
  • M3*5+5MM single-pass copper pillars *2
  • M3*6MM round-head screws *2
  • 5P 15CM blue-green-yellow-red-black connector wire *1


KS0313 4-10.png

Prepared well the components above, let's first insert two M3*5+5MM single-pass copper pillars into the holes on the tracking sensor, then tighten two M3 Nuts on the copper pillars. Shown below.
KS0313 4-11.png

Installed well, you should see the details of tracking sensor as below:
KS0313 4-12.png

After that, mount the tracking sensor on the red holes circled on the Acrylic board with two M3*6MM round-head screws. Shown below.
KS0313 4-13.png

For easy wiring, here you should connect a5P 15CM blue-green-yellow-red-black connector wire to the connector of tracking sensor. Shown as below.
KS0313 4-14.png


(4)Completed the above assembly, let's install the wheels for this small car.

  • W420 ball caster wheel *1
  • M3*10MM round-head screws *2
  • M3 Nut *2
  • Plastic wheel *2


KS0313 4-15.png

First of all, fix the W420 ball caster wheel on the Acrylic bottom board with two M3*10MM round-head screws and two M3 Nuts.
KS0313 4-16.png

You can get the view as below:
KS0313 4-17.png

Then, plug two small wheels into the two motors.
KS0313 4-18.png

And you should get the view as follows:
KS0313 4-19.png

Finally, put those wires on the back through the holes on the acrylic board.
KS0313 4-20.png


(5)Installed well the bottom part, now let's move on to the top part of this smart car. First step you should get ready for these components mentioned below.

  • M3*40MM dual-pass copper pillars * 4
  • M3*6MM round-head screws *4

For the four holes circled on acrylic bottom board, screw down four M3*40MM dual-pass copper pillars on four M3*6MM round-head screws.
KS0313 4-21.png

Finally, you should view it as below:
KS0313 4-22.png


(6)Pretty good. Okay, now you should install the particular eyes for this smart car, i.e. Ultrasonic module.

  • Ultrasonic module *1
  • M3*10MM round-head screw *4
  • M3 Nut *4
  • Small acrylic board *1
  • 4P 8CM connector wire *1


KS0313 4-23.png

Look at the figure below, fix the ultrasonic module on the acrylic board with four M3*10MM round-head screws and four M3 Nuts.
KS0313 4-24.png

For easy wiring, then you should connect the 4P 8CM connector wire to ultrasonic module.
KS0313 4-25.png


(7)After that, you should fix other sensors on the acrylic top board. Prepare well several parts as follows:

  • Acrylic top board *1
  • Obstacle detector sensor *2
  • IR receiver sensor *1
  • M3*10MM round-head screw *3
  • M3 Nut *7
  • M3*8+6MM single-pass copper pillar *4


KS0313 4-26.png

For those holes circled on the acrylic top board, separately mount two obstacle detector sensors and an IR receiver sensor on it with three M3*10MM screws and three M3 Nuts.
KS0313 4-27.png

Please refer to the photos shown as below:
KS0313 4-28.png

Then, fix four M3*8+6MM single-pass copper pillars and four M3 Nuts on the acrylic top board as well.
KS0313 4-29.png

Finished with the above assembly, you should get the correct view shown as below.
KS0313 4-30.png


(8)Until now, you should get all the assembled parts shown below. And prepare four M3*6MM round-head screws to assemble them together.
KS0313 4-31.png

Firstly, insert the ultrasonic module into the two holes of acrylic bottom board. Shown below.
KS0313 4-32.png

Then, screw down the acrylic top board onto the copper pillars with four M3*6MM round-head screws. See the red circle.
KS0313 4-33.png

For easy wiring, here you should connect each 8CM 3P connector wire to two obstacle detector sensors and an IR receiver sensor. And put those connector wires through the holes on the acrylic top board.
KS0313 4-34.png


(9)Wow, the smart car is almost installed well as yet. The next step is to install the control board as follows.

  • UNO R3 main board *1
  • Motor drive shield *1
  • Bluetooth module*1
  • M3*6MM round-head screw *4


KS0313 4-35.png

First, place the UNO R3 main board onto the acrylic top board, pay attention to the fixed holes (red circle).
KS0313 4-36.png

Then screw down the board with four M3*6MM round-head screws. Shown below.
KS0313 4-37.png

After that, stack the motor drive shield onto UNO R3 main control board.
KS0313 4-38.png

Up to now, you have installed well the hardware of this smart car. Congrats! Okay, the final step here is to connect well all the connector wires.

(10)For the wiring, you can connect them according to the corresponding silk-screen on the board. Or you can refer to the connection diagram as below.
KS0313 4-39.png

All connector wires are connected well as the photos shown below.
KS0313 4-40.png

Finally, plug keyestudio Bluetooth HC-06 module onto the motor drive shield. Pay more attention to the connection direction.
KS0313 4-41.png

So far, this smart car is wholly installed well. Congrats!
KS0313.png

In the following section, everyone can follow our step-by-step learning process to make the little guy move in accordance with the instructions, so that complete a variety of fancy motions.


Project Details for Smart Car

Project 1: Getting Started with ARDUINO

1)CorePart of Robot

When it comes to the core part, you may think of the sparkling energy block on the front of Iron Man's chest, or something in R2D2 Robot's head that can make him talk nonsense. The core is the part that really matters today. In fact, it is very easy to understand the core. In other word, the core is just like the human brain. It can receive various kinds of information every day and will send out various instructions every day. So actually the core of our robot is a development board compatible with ARDUINO.
Well, let's first look at this development board as the figure shown below:

thumb
So why choose to use the UNO R3 development board as the core of robot? Well, because it is the most popular one in Arduino board series. In addition, it is also the best choice for beginners to learn to build electronic circuits and write the source code. Let's take a look at the details of this development board with the following chart:
UNO.png

KS0313 5.1-1.png USB Connection

Arduino board can be powered via USB connector.
All you needed to do is connecting the USB port to PC using a USB cable.

KS0313 5.1-2.png DC Power Jack

Arduino board can be supplied with power from the DC power jack

KS0313 5.1-3.png Voltage Regulator

To control the voltage provided to the Arduino board, as well as to stabilize the DC voltage used by the processor and other components.

KS0313 5.1-4.png Crystal Oscillator

Helping Arduino deal with time problems. How does Arduino calculate time? by using a crystal oscillator. The number printed on the top of the Arduino crystal is 16.000H9H. It tells us that the frequency is 16,000,000 Hertz or 16MHz.

KS0313 5.1-5.png Arduino RESET

You can reset your Arduino board, for example, start the program from the very beginning. Firstly, use the RESET button(17). Or you can connect an external reset button to Arduino pin 5 labeled RESET

KS0313 5.1-6.png Pin Header(3.3V,5V,GND,Vin

KS0313 5.1-7.png3.3V - provides 3.3V output voltage
KS0313 5.1-8.png5V - provides 5V output voltage
Using 3.3 volts and 5 volts, most components can normally operate with Arduino board together.
KS0313 5.1-9.pngGND(Ground pins)- two GND headers on Arduino, each of which can be used for circuit ground.
KS0313 5.1-10.pngVin - You can supply an external power (like AC power supply) through this pin to Arduino board.

KS0313 5.1-11.png Analog Pins

Arduino UNO board has 6 analog inputs, labeled A0 through A5. These pins can read the signal from analog sensors (such as humidity sensor or temperature sensor), and convert it into the digital value that can read by microcontrollers)

KS0313 5.1-12.png Microcontroller

Each Arduino board has its own microcontroller. You can regard it as the brain of your board.
The main IC (integrated circuit) on the Arduino is slightly different from the panel pair. Microcontrollers are usually from ATMEL. Before you load a new program from the Arduino IDE, you must know what IC is on your board. This information can be checked at the top of IC.

KS0313 5.1-13.png ICSP (In-Circuit Serial Programming) Header

In most case, ICSP is the AVR,an Arduino micro-header consisting of MOSI, MISO, SCK, RESET, VCC, and GND.It is often called the SPI (serial peripheral interface) and can be considered an "extension" of the output.In fact, put the output devices subordinateto the SPI bus host.

KS0313 5.1-14.png Power LED Indicator

Powering the Arduino, LED on means that your circuit board is correctly powered on. If LED is off, connection is wrong.

KS0313 5.1-15.png TX and RX LED

Onboard you can find two labels: RX(receive ) and TX (transmit)
First appear on digital pin 0 and 1 for serial communication;
Besides, the RX LED on the board will flash in different speed when serial data is being transmitted. The flash speed depends on the baud rate set by board. And RX LED will also flash during the receiving process.

KS0313 5.1-16.png Digital I/O

Arduino UNO has 14 digital input/output pins (of which 6 can be used as PWM outputs). These pins can be configured as digital input pin to read the logic value (0 or 1). Or used as digital output pin to drive different modules like LED, relay, etc. The pin labeled “〜” can be used to generate PWM.

KS0313 5.1-17.png AREF

Reference voltage( 0-5V) for the analog inputs. Used with analogReference().


Installing Arduino IDE

When you get theUNO development board, first you should install the software and driver of Arduino. Here you can browse the ARDUINO website at this link, https://www.arduino.cc, pop up the following interface.
KS0313-1.png

Then click the SOFTWARE on the browse bar, you will have two options ONLINE TOOLS and DOWNLOADS.
KS0313-2.png

Click DOWNLOADS, it will appear the latest software version of ARDUINO 1.8.5 shown as below.
KS0313-3.png

In this software page, on the right side you can see the version of development software for different operating systems. So ARDUINO has a rather powerful compatibility. You should download the software that is compatible with the operating system of your computer.
In our project, we will take WINDOWS system as an example here. There are also two options under Windows system, one is installed version, the other is non-installed version. For simple installed version, first click Windows Installer, you will get the following page.

KS0313-4.png

KS0313-5.png

This way you just need to click JUST DOWNLOAD, then click the downloaded file to install it.
For non-installed version, first click Windows ZIP file, you will also get the pop-up interface as the above figure.
Click JUST DOWNLOAD, and when the ZIP file is downloaded well to your computer, you can directly unzip the file and then click the icon of ARDUINO program to start it.

Installing Arduino (Windows)

Install Arduino with the exe. Installation package
713.png

Click“I Agree”to see the following interface.
714.png

Click “Next”. Pop up the interface below.
715.png

You can press Browse… to choose an installation path or directly type in the directory you want.
Then click “Install” to initiate installation.

Wait for the installing process, if appear the interface of Window Security, just continue to click Install to finish the installation.
716.png

All right, up to now, you have completed the Arduino setup! The following icon will appear on your PC desktop.
Ks0313图片1.png

Double-click the icon of Arduino to enter the desired development environment shown as below.
717.png

Installing Installing 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 WIN 7 system.
The Arduino folder contains both the Arduino program itself and the drivers thatallow 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.
Plug one end of your USB cable into the Arduino and the other into a USB socket on your computer.
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”, you should see an icon for“Unknown device” with a little yellow warning triangle next to it. This is your Arduino.

Driver 1.png

Then right-click on the device and select the top menu option (Update Driver Software...) shown as the figure below..
Driver 2.png

It will then be prompted to either “Search Automatically for updated driversoftware” or “Browse my computer for driver software”. Shown as below. In this page, select “Browse my computer for driver software”.
Driver 3.png

After that, select the option to browseand navigate to the “drivers” folder of Arduino installation.
Driver 4.png

Click “Next” and you may get a security warning, if so, allow the software to be installed. Shown as below.
Driver 5.png

Once the software has been installed, you will get a confirmation message. Installation completed, click “Close”.
Driver 6.png

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.
Driver 7.png

2) Example Use of ARDUINO IDE

STEP 1: Open Arduino
In the previous, we have introduced the driver installation of UNO R3 development board. So this time let’s first have basic understanding of the development environment of ARDUINO. After master that, we are about to upload the program to Arduino board.
First of all, open the unzipped folder of ARDUINO development software and click icon of ARDUINO to open the software, as the figure shown below.

KS0313-6.png


STEP 2: Build Projects
When open the Arduino software, you will have two options as below:

  • Build a new project
  • Open an exiting project example

If you want to build a new project, please select “File”→then click “New”, you will see the software interface as follows.

KS0313-7.1.pngKS0313-7.2.png

If you want to open an example project, please select File→Example→Basics→Blink. Shown below.

KS0313-8.1.png KS0313-8.2.png


STEP 3: Select Arduino Board
In order to avoid any mistakes during the process of uploading the code to the board, you must first select the correct Arduino board that matches the board connected to your computer. Or you can directly open the Control Panel of your computer, then click to open Device Manager, you can check your board and port here. Shown as below.

KS0313-9.png
Then come back to the Arduino software, you should click Tools→Board , select the correct board. Here in our tutorial we should select Arduino Uno. Shown as below.
KS0313-10.png


STEP 4: Select Serial Port
Next, we should select the serial device of Arduino board. You can click Tools→Serial Port. It may be COM3 or higher (COM1 and COM2 are usually reserved as hardware serial port). If you want to find out your Arduino board, you could disconnect your Arduino board, then reopen the menu, you will see the Arduino board disappear. After that reconnect the board and select the serial port.
You should select the correct Serial port that matches the port showed on the Device Manager of your computer.
The figure shown below is only for your reference.
Attach your Arduino board to your computer with the USB cable and check that the “Board Type” and “Serial Port” are set correctly.

KS0313-11.png


STEP 5: Upload the Code to Your Board
Before showing you how to upload the code to your board, first of all let me introduce the function of each icon on the Tool bar of Arduino IDE. Look at the picture showed below.
KS0313-12.png
A- Used to verify whether there is any compiling mistakes or not.
B- Used to upload the sketch to your Arduino board.
C- Used to create shortcut window of a new sketch.
D- Used to directly open an example sketch.
E- Used to save the sketch.
F- Used to send the serial data received from board to the serial monitor.


3) Light up an LED

Overview:
In the above courses, you have learned how to use the development software. So want to try it out with an example project? Well, let’s do it right now. Getting started with one more basic program, bring you enter the wonderful programming world of ARDUINO. Cheerful? Great, follow the project process below to have your first try! It is really awesome. LED experiment is one of the more basic experiments in learning ARDUINO. Here we will use our keyestudio LED module. On the module, you will see a light emitting diode (LED), which has two states of on and off. Since our module itself has done well the circuit, you can use it in a simple way. Just need to connect its pins. There are three lead-out pins on the module, respectively negative pin(marked -), positive pin(marked +) and signal pin(marked S). Note that the modules from different manufactures may have different pin labels. Next, connect the three pins of LED module to the shield of keyestudio UNO R3 board using three dupont jumper wires. Connect negative pin to the ground, positive pin to 5V, and signal pin to Digital 11. Shown as below.

Wiring Diagram:
KS0313(3).png
After wire it up well as the above wiring diagram, you can upload the source code to the board, so as to light up an LED light.

Source Code as below:

int ledpin=11;// define the LED pin as Digital 11
void setup() 
{  pinMode(11, OUTPUT); // initialize digital pin 11 as an output.
}
void loop()
 {
  digitalWrite(11, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(11, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second
}

Note: in the code pay attention to Single line comment (//),so that your can know how your program works.
Comments are lines in the program that are used to inform yourself or others about the way the program works. They are ignored by the compiler, and not exported to the processor, so they don’t take up any space in the microcontroller’s flash memory. Comments' only purpose is to help you understand (or remember), or to inform others about how your program works.

So up till now, compile well the code and then click on the 'Upload' button. The second button from the left on the toolbar of Arduino IDE.
KS0313(3)-1.png

When upload well the code to the board, you will see the status at the bottom of window will change to “Done uploading”.
KS0313(3)-2.png
Eventually, you will see the LED on the shield is on for one second, then off one second. Congrats! Your first programming is done successfully.

4) LED Brightness Controlled by PWM

Overview:
In the previous project, you have learned how to turn on or off an LED. So this time, you may be interested in changing the brightness of an LED light, just making it like the bedside lamp in your bedroom.
Well, so how to do that? Here it is indeed important for you to master the knowledge of PWM. Right, PWM is short for Pulse Width Modulation. How can it be understood in a simple way? We all know that the voltage output of Arduino Digital port only has two states, LOW and HIGH, corresponding to the voltage output of 0V and 5V. If merely make use of LOW and HIGH state, it cannot control the brightness of an LED light. However, if convert the voltage output of 0 Volts and 5 Volts into the value within 0-255, this way you can change the value within 0-255 to control the brightness of light. It is much more feasible, isn’t it ?
Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means. Digital control is used to create a square wave of different duty cycle, a signal switched between on and off. This on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off.
  The Arduino controller has totally 6 PWM outputs, which are Digital 3, 5, 6, 9, 10 and 11. Shown as follows.

KS0313(4).png

These pins can be used as Digital output or Analog output. If used as Analog output, it needs to call the analogWrite() function of ARDUINO, and this analogWrite() function can be controlled in the range of 0-255.

In the graphic below, the green lines represent a regular time period. This duration or period is the inverse of the PWM frequency. In other words, with Arduino's PWM frequency at about 500Hz, the green lines would measure 2 milliseconds each. A call to analogWrite() is on a scale of 0-255, such that analogWrite(255) requests a 100% duty cycle (always on), and analogWrite(127) is a 50% duty cycle (on half the time) for example.

KS0313(4)-1.png
In fact, PWM can be applied to dimming lamps, motor speed, sound production, etc. In the following, we are going to control the brightness of the LED. On the aspect of hardware, we still utilize the LED connected to D11 in the last section. You can refer to the wiring diagram as follows:

KS0313(3).png

Source Code as below:

int LED= 11;            //define the LED pin 
int i = 0;                   //define a variable i,
void setup(){
  pinMode(LED,OUTPUT);       //set LED pin as OUTPUT
}
void loop(){
  for(i = 0;i < 255;i++){    //variable is changed from 0 to 254(fade in)
    analogWrite(LED, i);     //set LED brightness
    delay(10);               //delay 10ms,analogWritefunction will be finished in a short time. 
                             //speed is too fast to observe
                             
  }
  for(i =255;i > 0; i--){    //variable is changed from 255 to 1(fade out)
     analogWrite(LED, i);    //set LED brightness
     delay(10);              //delay 10ms
  }
}

Code Explanation:
analogWrite(LED, i);
Writes an analog value (PWM wave) to a pin. Can be used to light a LED at varying brightnesses or drive a motor at various speeds. After a call to analogWrite(), the pin will generate a steady square wave of the specified duty cycle until the next call to analogWrite() (or a call to digitalRead() or digitalWrite()) on the same pin. The frequency of the PWM signal on most pins is approximately 490 Hz.

Syntax: analogWrite(pin,value)
It has two parameters:

  • pin: the pin to write to. Allowed data types: int.
  • value: the duty cycle: between 0 (always off) and 255 (always on). Allowed data types: int

In the later course of motor driving, we will also apply that PWM. So try to figure it out.
Upload the above code to the board, you should get the example result as below.

KS0313(4)-2.png

Project 2: Line Tracking Function for Smart Car

1)Principle and Application of Line Tracking Sensor

Overview:
The tracking sensor is actually an infrared sensor. The component used here is the TCRT5000 infrared tube. Its working principle is to use the different reflectivity of infrared light to the color, then convert the strength of the reflected signal into a current signal. During the process of detection, black is active at HIGH level, but white is active at LOW level. And detection height is 0-3 cm. The following figure is our keyestudio 3-channel line tracking module. We have integrated 3 sets of TCRT5000 infrared tube on a single board, which is more convenient for wiring and control. By rotating the adjustable potentiometer on the sensor, it can adjust the detection sensitivity of the sensor.
thumb

TECH SPECS:

  • Operating Voltage: 3.3-5V (DC)
  • Interface: 5PIN
  • Output Signal: Digital signal
  • Detection Height: 0-3 cm

Wiring Diagram:
Okay, next let’s do a simple test for this tracking module. Connect the line tracking module to the shield using connector wire. Then connect the LED module to the pin11 male header on the shield, this way you can observe the testing result. The connection diagram is shown as below.
thumb

Source Code as below:
Wire it up well as the above diagram, then you can type the following test code.

int sensor1 = 6; // define the pin of left sensor as pin D6
int ledPin =11; //define LEDpin as Digital 11
void setup() 
{
    pinMode(sensor1, INPUT); //define the sensor as INPUT
    pinMode(ledPin,OUTPUT); //define LED as OUTPUT
}

void loop() 
{
  if( digitalRead(sensor1)==LOW)  // read the state of sensor, if detect the white paper, it is at LOW level.
    {digitalWrite(ledPin, HIGH);  //light an LED
  }
   else // or else
   {
      digitalWrite(ledPin, LOW); //turn off an LED
  }
}

So how do you think about that? It is really simple. For another two-channel, you can refer to the above code to finish the testing steps.

int sensor2 = 7; // define the pin of middle sensor as pin D7
int ledPin =11; //define LEDpin as Digital 11
void setup() 
{
    pinMode(sensor2, INPUT); //define the sensor as INPUT
    pinMode(ledPin,OUTPUT);//define LED as OUTPUT
}
void loop() 
{
   if( digitalRead(sensor2)==LOW)  //read the state of sensor, if detect the white paper, it is at LOW level.
    {digitalWrite(ledPin, HIGH);  //light an LED
  }
   else //or else
   {
      digitalWrite(ledPin, LOW); //turn off an LED
  }
}
int sensor3 = 8; //  define the pin of right sensor as pin D8
int ledPin =11; // define LEDpin as Digital 11
void setup() 
{
    pinMode(sensor3, INPUT); // define the sensor as INPUT
    pinMode(ledPin,OUTPUT); // define LED as OUTPUT
}

void loop() 
{
if( digitalRead(sensor3)==LOW)  //read the state of sensor, if detect the white paper, it is at LOW level.
    {digitalWrite(ledPin, HIGH);  // light an LED
  }
   else // or else
   {
      digitalWrite(ledPin, LOW); // turn off an LED
  }
}

Test Result
Upload well the code to the board, you should see the result as the following figure shown. Actually these actions are only for the testing of basic digital sensor. In the latter case, we are about to match those sensors with some other modules to make interactive works.
thumb thumb

2) Driving and Speed Control of Motor


thumb

Overview
There are many ways to drive the motor. Our robot uses the most commonly used L298P solution. L298P is an excellent high-power motor driver IC produced by STMicroelectronics. It can directly drive DC motors, two-phase and four-phase stepping motors. The driving current up to 2A, and output terminal of motor adopts eight high-speed Schottky diodes as protection. We have designed the motor driver shield based on the L298P circuit. The stackable design can make it be plugged directly into the Arduino, reducing the technical difficulty of using and driving the motor.
When stack the driver shield onto UNO R3 board, after the BAT is powered on, press the POWER button lightly. The external power will be supplied to both the driver shield and UNO R3 board at the same time. In order to facilitate wiring, the driver shield comes with an anti-reverse interface. When connecting the motor, power supplyand sensor modules, you just need to plug in directly.
The Bluetooth interface on the driver shield is fully compatible with keyestudio HC-06 Bluetooth module. When connecting, you just need to plug HC-06 Bluetooth module into the corresponding interface. At the same time, thedrive shield solders 2.54mm pin headers tolead outsome unused digital ports and analog ports, so that you can continue to add other sensors for experiments extension.

Specifications:
1. Logic part input voltage: 5V
2. Driving part input voltage: DC 7-12V
3. Logic part working current: <36mA
4.Driving part working current: <2A
5. Maximum powerdissipation: 25W (T=75℃)
6. Control signal input level:

  • High level: 2.3V<Vin<5V
  • Low level: -0.3V<Vin<1.5V

7. Working temperature: -25℃~+130℃

Pinout Instructions:
As the diagram shown below, you can get the detailed information of connectors on the motor drive shield.

thumb

Driving DC Motor:
In the previous section, we have shown you the basic principle and parameters of L298P motor drive module. You can get the details of all interfaces on the board. So in the following, we will formally introduce how to drive the motor? First, you should connect well two motors to the shield, i.e. motor A and motor B shown as below.
thumb

Well, next let’s create the sketch.
The code logic of the smart car is nothing more than 5 kinds of movement modes, namely go forward, go backward, turn left, turn right and stop. So think about it. How could it implement those functions? Simply, for example, both the left and the right motor of the smart car turn in the forward direction, so that the smart car is able to go forward. If both the left and the right motors turn in reverse, the smart car will go backward. Besides, if the motor on the left side of car turns forward but the right side one turns reverse, it means that the smart car will turn right. If the motor on the right side of car turns forward but the left side one turns reverse, it means that the smart car will turn left.
You may be a little bit confused that how to control the forward or reverse turning of the motor?Actually, you can achieve that by controlling the microcontroller pin of motor direction to active at HIGH or LOW level.
It is much more easier to understand the motor turning, however, it would be a little bit complicated to work out the speed control of motor.
As for the speed control of motor, it involves the PWM mode. So what is PWM? Actually PWM is the short for Pulse Width Modulation. PWM is a technique for getting analog results with digital means. Digital control is used to create a square wave (a signal switched between on and off) to control the analog output. The output voltage of Arduino Digital port only has LOW and HIGH level, corresponding to the output voltage of 0 Volts and 5 Volts.

In the graphic below, the green lines represent a regular time period. This duration or period is the inverse of the PWM frequency. In other words, with Arduino's PWM frequency at about 500Hz, the green lines would measure 2 milliseconds each. A call to analogWrite() is on a scale of 0-255, such that analogWrite(255) requests a 100% duty cycle (always on), and analogWrite(127) is a 50% duty cycle (on half the time) for example.
KS0313(4)-1.png PWM analog output

For example, as for the UNO board below, we have marked the PWM pins that can be used for analog output. You can see the PWM pins on UNO board are D3, D5, D6, D9, D10, and D11.
KS0313(4).png

The function called by the PWM is: analogWrite(pin, value).
Note that the value is between 0 (always off) and 255 (always on). The speed of the motor is controlled actually by this value. The bigger the value is, the faster the speed is. Rather, the smaller the value is, the slower the speed it is until it stops.
In the following figure, look at the language logic of motor states: go forward, backward, turn left, turn right and stop. M1 and M2 represent the directional control of the 2-way motor, that is, forward and reverse rotation. E1 and E2 represent the speed control of motor. We control the speed at 1.

E1 M1 E2 M2
Go forward 150 HIGH Motor 1 forward 150 HIGH Motor 2 forward
Go backward 150 LOW Motor 1backward 150 LOW Motor 2backward
Turn left 150 LOW Motor 1 forward 150 LIGH Motor 2backward
Turn right 150 HIGH Motor 1backward 150 LOW Motor 2 forward
Stop 0 LOW Motor 1 stop 0 LOW Motor 2 stop


Example Code as below:
Okay, next we will start to write the example code. The part of Single line comment (//) is the explanation of the code. Based on that, you can understand it better.

int E1 = 9; //  set the speed pin of motor A as D9
int E2 = 5; //  set the speed pin of motor B as D5
int M1 = 2; // set the direction pin of motor A as D2
int M2 = 4; // set the direction pin of motor B as D4

void setup(void)
{
  pinMode(M1,OUTPUT); // set M1 as OUTPUT mode
  pinMode(M2,OUTPUT); // set M2 as OUTPUT mode
  pinMode(E1,OUTPUT); //  set E1 as OUTPUT mode
  pinMode(E2,OUTPUT); //  set E2 as OUTPUT mode
}
void advance(void)        // set the forward motion
{
  digitalWrite(M1,HIGH);  // motor A turns forward, the wheel will go forward.
  digitalWrite(M2,HIGH);  // motor B turns forward, the wheel will go forward. 
  analogWrite(E1,150);    // speed of motor A(can be adjusted according to the actual speed of motor. Turn up the value to accelerate, lower the value to decelerate.) 
  analogWrite(E2,150);    // speed of motor B(can be adjusted according to the actual speed of motor. Turn up the value to accelerate, lower the value to decelerate.) 
}  
void back(void)        // set the backward motion
{
 digitalWrite(M1,LOW);   // motor A turns reverse and the wheel will go backward
 digitalWrite(M2, LOW);   //  motor B turns reverse and the wheel will go backward
 analogWrite(E1,150);     //  speed of motor A
 analogWrite(E2, 150);    //  speed of motor B
}  

void turnL(void)     // set the left turn 
{
   digitalWrite(M1,LOW);    // motor A turns reverse and the wheel will go backward
   digitalWrite(M2, HIGH);  // motor B turns forward and the wheel goes forward, the smart car will turn left.
   analogWrite(E1,150);  // speed of motor A
   analogWrite(E2, 150);  // speed of motor B
}

void turnR(void)            //  set the right turn 
{
   digitalWrite(M1,HIGH);   // motor A turns forward and the wheel will go forward
   digitalWrite(M2,LOW);    // motor B turns reverse and the wheel goes backward, the smart car will turn right.
   analogWrite(E1,150);     // speed of motor A
   analogWrite(E2, 150);    // speed of motor B
}

void stopp(void)   // set the STOP
{
   digitalWrite(M1,LOW);  // motor A turns reverse
   digitalWrite(M2, LOW);  //  motorBturns reverse
   analogWrite(E1, 0);      //  speed of motor A, speed as zero, means stop
   analogWrite(E2, 0);      // speed of motor B, speed as zero, means stop
}

void loop() 
{ 
advance();   // go forward
delay(1000); //delay1S
back();      //backward
delay(1000);//delay1S

turnL();    //turn left
delay(1000);//delay1S
turnR();    //turn right
delay(1000); //delay1S
stopp();   // stop
delay(1000);// delay1S
}

Test Result:
Stack well the drive shield onto UNO R3 board, and upload the above code to the board, then press down the POWER button, you should see the motor go forward for one second, backward one second, then turn left for one second, turn right for one second and stop one second, alternately repeating.
thumb

3)Line Tracking Smart Car


thumb

Project Overview:
In the previous sections, you have learned the principles and applications of both tracking module and the motor drive module. After master that knowledge, let’s combine these two modules to make a smart car with line tracking function. Sound great? Well, let’s do it right now.
So first what does line tracking mean? You should figure it out. It refers to following the line trajectory. For instance, the smart car will always follow or track the black line. The principle is using the tracking sensor to detect the black track on the pavement, and detection signal will feed back to ARDUINOmain control board. Then main control board will analyze and judge the collected signals to control and drive the motor in time, thus can adjust the turning direction of smart car. That is why the smart car can automatically follow the black track, achieving the automatic line tracking function.
This technology has been applied to many areas such as driverless vehicles, unmanned factories, warehouses, and service robots.

Project Principle:
Using the characteristic that black has low reflectivity to light. When flat surface is not black, the infrared light transmitted by the sensor will be reflected back mostly, so the sensor outputs low level 0.When the flat surface has a black line and the sensor is above the black line, the reflected infrared light is very less due to the weak reflectivity of black, so it does not reach theaction level and sensor outputs high level 1.
Use the main control board to determine whether the output end of sensor is 0 or 1, finally detect the black line.The main control board will control the turning direction of motoraccording to the received signal, so finally can control the movement of smart car. This is a simple line tracking smart car.

Wiring Diagram:
Connect the tracking sensor, two motors and battery pack to the motor drive shield as follows.

thumb

Example Code as below:
Wire it up well as the above diagram. Okay, let’s move on to write the test code. Think about the logic of code.
There are two kinds of tracking sensor’s states as follows:
1. The middle tracking sensor detects a black line, if the sensor on the left side detects a white line, while the sensor on the right side detects a black line, the smart car will turn right. On the contrary, if the sensor on the right detects a white line, but the left one detects a black line, the smart car will turn left. If both sides detect a white line or a black line, car will go forward.
2. The middle tracking sensor does not detect a black line, if the sensor on the left side detects a white line, while the sensor on the right side detects a black line, the smart car will turn right. On the contrary, if the sensor on the right detects a white line, but the left one detects a black line, the smart car will turn left. If three sensors all detect a white line, car will stop.

Well, figure out the logic, then combine the example code of motor driving mentioned in the above section, you can have a try to write out the logic of line tracking.

int E1 = 9;   // set the speed pin of motor A as D9
  int E2 = 5;   // set the speed pin of motor B as D5
  int M1 = 2;   // set the directional pin of motor A as D2
  int M2 = 4;   //  set the directional pin of motor B as D4
  int SensorLeft = 6;   // input pin of tracking sensor on left side
  int SensorMiddle= 7 ; //  input pin of tracking sensor on middle
  int SensorRight = 8;  //  input pin of tracking sensor on right side
  int SL;     // define the state of left side sensor
  int SM;    // define the state of left side sensor
  int SR;    // define the state of left side sensor
  void setup(void)
{
  pinMode(M1,OUTPUT);   // set M1 as OUTPUT mode
  pinMode(M2,OUTPUT);   // set M2 as OUTPUT mode 
  pinMode(E1,OUTPUT);   // set E1 as OUTPUT mode 
  pinMode(E2,OUTPUT);   // set E2 as OUTPUT mode 
  pinMode(SensorLeft, INPUT);    // define the left side sensor as INPUT 
  pinMode(SensorMiddle, INPUT);  // define the left side sensor as INPUT 
  pinMode(SensorRight, INPUT);    // define the left side sensor as INPUT 
}
void advance(void)        // set the forward motion
{
  digitalWrite(M1,HIGH);  // motor A turns forward, wheel will go forward
  digitalWrite(M2,HIGH);  // motor B turns forward, wheel will go forward
  analogWrite(E1,250);    //speed of motor A(can be adjusted according to the actual speed of motor. Turn up the value to accelerate, lower the value to decelerate.) 
  analogWrite(E2,250);    //speed of motor B(can be adjusted according to the actual speed of motor. Turn up the value to accelerate, lower the value to decelerate.) 
}  
void back(void)        // set the backward motion
{
 digitalWrite(M1,LOW);   // motor A turns reverse, wheel will go backward
 digitalWrite(M2, LOW);   // motor B turns reverse, wheel will go backward 
 analogWrite(E1,250);     // rotating speed of motor A
 analogWrite(E2,250);    // rotating speed of motor B
}  
void turnL(void)     // set the left turn motion
{
   digitalWrite(M1,LOW);    //motor A turns reverse, wheel will go backward 
   digitalWrite(M2, HIGH);  // motor B turns forward, wheel will go forward, the whole car turn left.
   analogWrite(E1,250);  // rotating speed of motor A
   analogWrite(E2,250);  //  rotating speed of motor B
}
void turnR(void)            // set the right turn motion
{
   digitalWrite(M1,HIGH);   // motor A turns forward, wheel will go forward.
   digitalWrite(M2,LOW);    // motor B turns reverse, wheel will go backward, the whole car turn right. 
   analogWrite(E1,250);     // rotating speed of motor A
   analogWrite(E2,250);    //  rotating speed of motor B
}
void stopp(void)   // set the stop motion
{
   digitalWrite(M1,LOW);  //  motor A turns reverse
   digitalWrite(M2, LOW);  //  motor B turns reverse
   analogWrite(E1, 0);      // speed of motor A, speed as zero, means stop.
   analogWrite(E2, 0);      // speed of motor B, speed as zero, means stop.
}

void loop() 
 {
 SL = digitalRead(SensorLeft);     //  left state value read is the digital input of left side sensor
 SM = digitalRead(SensorMiddle);  //  middle state value read is the digital input of middle sensor
 SR = digitalRead(SensorRight);    // right state value read is the digital input of right side sensor
      if(SM == HIGH)  // if middle tracking sensor detects a black line
     {
      if(SL == HIGH && SR == LOW)  
      turnL();  //  if left side sensor detects a white line, but right side sensor detects a black line, the car turn right. 
    else if(SR == HIGH && SL == LOW) 
      turnR();  //  if right side sensor detects a white line, but left side sensor detects a black line, the car turn left.
     else advance(); // if not the above two states, means that both side sensor detect a white line or a black line, the car goes forward.
     }

     else  // if the middle sensor detects white, but not detect the black line. 
     {
       if(SL == HIGH && SR == LOW)
       turnL();       // left side sensor detects a black line, but right side sensor detects a white line, the car turn left. 
      else if(SR == HIGH && SL == LOW)  
       turnR();     // left side sensor detects a white line, but right side sensor detects a black line, the car turn right. 
      else stopp();  // if all white, means that the three sensors do not detect the black line, car will stop. 
     }
}

Example Picture:

thumb
Upload well the above code to the main board, then press down the POWER button on the motor drive shield. If draw a black line on the ground, you should see that the smart car will track the black line.
thumb


Project 3: Obstacle Avoidance Function for Smart Car

1)Principle and Application of Infrared Obstacle Detector Sensor

Principle:
The infrared obstacle detector sensor is actually a distance-adjustable obstacle avoidance sensor designed for a wheeled robot. It 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 lights up. After processed by the circuit, the signal output terminal will output Digital signal.
You can rotate the potentiometer knob on the sensor to adjust the detection distance. The effective distance is 2-40cm and the working voltage is 3.3V-5V. Due to the wide range of operating voltage, it can still work stably under the condition of relatively large fluctuating power supply voltage, suitable for use of various single-chip, Arduino controller andRaspberry Pi. Installed this sensor on the robot, make it can sense the changes in the surrounding environment. It’s really great!

thumb

TECH SPECS:
1)Operating Voltage: DC 3.3-5V
2)Operating Current: ≥20mA
3)Operating Temperature: -10℃ to +50℃
4)Detection Distance: 2-40cm
5)IO Interface: 3-PIN (-/+/S/)
6)Output Signal: TTL level (LOW level with obstacle, HIGH level without obstacle)
7)Regulation Mode: multi-coil resistance regulation
8)Effective Angle: 35°

Wiring Diagram:
thumb
In the following, we will combine an infrared obstacle detector module and an LED module connected at pin D11 to make the application of IR obstacle avoidance. You can refer to the wiring diagram below.

Example Code as below:
Before write the code, you need to understand a common sense. On UNO board, pins D0-D13 are Digital pins, pins A0-A5 are Analog pins. Actually analog pins can also used as digital pins, like A0=14, A1=15, A2=16, A3=17, A4=18, and A5=19.

int sensorPin = 16;     // define sensor pin as A2, that is digital pin 16
 int ledPin = 11;      // define LED pin as digital pin 11
void setup()
 {
  pinMode(ledPin, OUTPUT); // define LED pin as OUTPUT
  pinMode(sensorPin, INPUT); // define sensor pin as INPUT
 }
void loop()
{
  if (digitalRead(sensorPin)==0) // if sensor pin is read as Low level, means that obstacle is detected, to execute if function.
{ 
 digitalWrite(ledPin, HIGH); // light an LED 
   }
 else{ // or else                               
digitalWrite(ledPin, LOW);  // turn off an LED  
}
}

Example Picture:
Wire it well as the above diagram, then upload the above code to the board, press down the POWER button on the shield lightly. You can also rotate the potentiometer knob on the sensor to adjust the sensitivity, just letting the right side LED on the sensor between on and off state.
Once the sensor detects an object ahead, both the right side LED on the sensor and external LED module are turned on. If no obstacle ahead, LED on the sensor and LED module are off.
thumb


2)Principle and Application of Ultrasonic Module


thumb

Description:
There is an animal called bat in nature. The batscan fly at night, not depend on its eyes, but on its ears and vocal organs. When the bat flies, it will emit a scream, an ultrasonic signal that humans cannot hear because of its high audio frequency. If these ultrasonic signals hit other objects on the flight path, they will be reflected back immediately. After receive the returned information, the bats complete the whole process of listening, seeing, calculating and bypassing obstacles during the flutter.
The principle of the ultrasonic rangefinder module is as the same as the above principle. The ultrasonic module will emit the ultrasonic waves after trigger signal. When the ultrasonic waves encounter the object and are reflected back, the module outputs anecho signal, so it can determine the distance of object from the time difference between trigger signal and echo signal.
Ultrasonic sensor has a wide range of sensitivity, no blind area, and no interference with obstacles. This technology has been used in business and security area for more than 25 years and has been proven to be the most effective method for detecting the movement of small objects.
As the following picture shown, it is our keyestudio ultrasonic module. You can see it has two somethings like eyes. One is transmitting end, the other is receiving end.
thumb thumb


TECH SPECS:

  • Operating Voltage: 5V(DC)
  • Operating Current: 15mA
  • Operating Frequency: 40khz
  • Maximum Detection Distance: 3-5m
  • Minimum Detection Distance: 3-4cm
  • Sensing Angle: less than 15 degrees

Wiring Diagram:
Connect the ultrasonic module to the shield. Shown as below.
thumb

【Notice:】
1.Must first connect the ultrasonic module and then power up. Or connect the ground first.
2.Measurement period is better at more than 60ms. To prevent the impact of the transmitted signal to the echo signal.

When using it:
(1) Use IO trigger ranging, at least 10us HIGH level signal; that is, first pull the Trip Low, then give a HIGH level signal of 10us.
(2) The module automatically sends eight square waves of 40khz to automatically detect whether there is a signal return back;
(3) There is a signal return, through the IO output a High level, and the duration period of High level is the time of Ultrasonic wave from emission to return.
Test distance = (High level time * speed of sound (340M/S))/2;
Then you can get the formula: detection distance = (High level time/58) (cm);

Example Code as below:

int pinTrip=12; // connect the SR04 Trip , give more than 10us High level
int pinEcho=13;  // connect the Echo pin , the time to receive the High level
float distance=0; // save the distance
void setup() {
  // put your setup code here, to run once:
pinMode(pinTrip,OUTPUT);
pinMode(pinEcho,INPUT);
Serial.begin(9600);
}
void loop() {
  // put your main code here, to run repeatedly:
digitalWrite(pinTrip,LOW);
delayMicroseconds(2); // pull down Level
digitalWrite(pinTrip,HIGH);
delayMicroseconds(12);// give 12us High level
digitalWrite(pinTrip,LOW);// pull down Level
distance=pulseIn(pinEcho,HIGH);// check the High level time
delay(10);
distance=distance/58; // get the distance
Serial.print("distance=");
Serial.print(distance);
Serial.println("cm");
delay(500);
}

Test Result:
Stack well the shield on UNO R3 board, and upload well the above code, then open the serial monitor of Arduino IDE, set the baud rate as 9600. When ultrasonic sensor detects the obstacle ahead, on the monitor you should see the distance detected between obstacle and sensor. Shown below. thumb thumb


3)Obstacle Avoidance Smart Car

Description:
It is not suitable for human to work in some relatively harsh environments. At this moment, if we have a robot that can shuttle freely in such environments, then how good should it be! Based on this original intention, our team develop this smart car that be able to automatically avoid an obstacle when running on complicated terrain.
Application occasions and market prospects: It can be used for material transportation of unmanned cars in harsh geographical environments, and automatic obstacle avoidance of toy cars. So it can be seen that application prospect of this obstacle avoidance car is indeed extensive.
This project is a simple and automatic obstacle avoidance car system based on Arduino. The smart car with UNO R3 as the controlling core, makes use of ultrasonic module and obstacle detector sensor to detect the obstacle in front of the car, and the detection signal will feed back to the control board.
Arduinomain board will then analyze and judge the collected signals to control the motordriving in time, thus adjust the direction of smart car. Finally control the smart car automatically avoid an obstacle ahead to run forward smoothly.

Project Principle:
1. Use the ultrasonic module to detect the distance between the smart car and the obstacle ahead.
2. Use the obstacle detector sensor to detect the obstacle ahead or on the left or right side of smart car. The sensitivity can be adjusted through rotating the two small potentiometers on the sensor. If detectsan obstacle, the signal end of sensor will output 0; if no, the signal end of sensor will output 1.
3. According to both output signal of obstacle detector sensor and distance value detected by ultrasonic sensor between the car and obstacle, thus Arduino can control the turning direction of motor.

Wiring Diagram:
Next, let’s start connecting the ultrasonic module, obstacle detector sensor and motors to the shield board. Have a try to make an amazing smart car with obstacle avoidance function. Think about it and do it right now!
Firstly you can follow the connection diagram below.

thumb

Example Code as below:
Wire it up well as the above figure. Let’s move on to an example code for this obstacle avoidance smart car. You can see the source code for reference as follows:

int E1 = 9;   // set the speed pin of motor A as D9
int E2 = 5;   // set the speed pin of motor B as D5
  int M1 = 2;   // set the direction pin of motor A as D2
  int M2 = 4;   // set the direction pin of motor B as D4

  int pinTrip=12; // connect the SR04 Trip , more than 10us High level
  int pinEcho=13;//connect the SR04 Echo,the time to receive High level
  float distance=0; // save the distance 
  int sensorleft = 16; // input pin of obstacle detector sensor on the left side
  int sensorright= 14;// input pin of obstacle detector sensor on the right side
  int RS;// set the state of right side obstacle sensor
  int MS;// set the detection distance of ultrasonic module
  int LS;// set the state of left side obstacle sensor

  void setup(void)
{

  pinMode(M1,OUTPUT); // set M1 as OUTPUT mode
  pinMode(M2,OUTPUT); // set M2 as OUTPUT mode
  pinMode(E1,OUTPUT); //  set E1 as OUTPUT mode
  pinMode(E2,OUTPUT); //  set E2 as OUTPUT mode
  pinMode(pinTrip,OUTPUT);//define the SR04 Trip as OUTPUT
  pinMode(pinEcho,INPUT);// define SR04 Echo as INPUT
  pinMode(sensorleft,INPUT);// define the left side obstacle sensor as INPUT
  pinMode(sensorright,INPUT);// define the right side obstacle sensor as INPUT

}
void advance(void)        // set the forward motion
{
  digitalWrite(M1,HIGH);  // motor A turns forward, and wheels go forward.
  digitalWrite(M2,HIGH);  // motor B turns forward, and wheels go forward. 
  analogWrite(E1,150);    // speed of motor A(can be adjusted according to the actual speed of motor. Turn up the value to accelerate, lower the value to decelerate.) 
  analogWrite(E2,150);    // speed of motor B(can be adjusted according to the actual speed of motor. Turn up the value to accelerate, lower the value to decelerate.) 
}  
void back(void)        // set the backward motion
{
 digitalWrite(M1,LOW);   // motor A turns reverse, and wheels go backward.
 digitalWrite(M2, LOW);   // motor B turns reverse, and wheels go backward.
 analogWrite(E1,150);     // speed of motor A
 analogWrite(E2,150);    //speed of motor B
}  

void turnL(void)     // set the left turn motion
{
   digitalWrite(M1,LOW);    // motor A turns reverse, and wheels go backward.
   digitalWrite(M2, HIGH);  // motor B turns forward, and wheels go forward, the whole car turns left.
   analogWrite(E1,150);  // speed of motor A
   analogWrite(E2,150);  // speed of motor B
}

void turnR(void)            // set the right turn motion
{
   digitalWrite(M1,HIGH);   //  motor A turns forward, and wheels go forward 
   digitalWrite(M2,LOW);    //  motor A turns reverse, and wheels go backward, and the whole car turns right. 
   analogWrite(E1,150);     // speed of motor A
   analogWrite(E2,150);    // speed of motor B
}

void stopp(void)   // set the stop motion
{
   digitalWrite(M1,LOW);  //  motor A turns reverse
   digitalWrite(M2, LOW);  //  motor B turns reverse
   analogWrite(E1, 0);      // speed of motor A, speed as zero, means Stop.
   analogWrite(E2, 0);      // speed of motor B, speed as zero, means Stop.
}


void Sensor_Scan()
{
  // put your main code here, to run repeatedly:
digitalWrite(pinTrip,LOW);
delayMicroseconds(2); // pull down level 
digitalWrite(pinTrip,HIGH);
delayMicroseconds(12);// input 12us HIGH level
digitalWrite(pinTrip,LOW);// pull down Level 
distance=pulseIn(pinEcho,HIGH);// check the HIGH level time
delay(10);
distance=distance/58; // get the distance
MS=distance;//set the detection distance for the middle ultrasonic module
delay(500);
RS = digitalRead(sensorleft);// right state value read is the digital input of right side obstacle sensor
LS = digitalRead(sensorright);// left state value read is the digital input of left side obstacle sensor
}

void loop() 
{
Sensor_Scan();//ultrasonic module detects the distance; right and left side obstacle sensors detect the state of right and left side.
if(MS>8)  // if detection distance of ultrasonic module is greater than 8cm.
{
if(LS==1&&RS==1)
advance();  // if both left and right side sensors fail to detect an obstacle, the car goes forward.
else if(LS==0&&RS==1) 
turnR();  // if left side sensor detects an obstacle, but right side sensor fails to detect it, the car will turn right.
else if(LS==1&&RS==0) 
turnL();  // if left side sensor detects no obstacle, but right side sensor detects an obstacle, the car will turn left. 
 else  //none of the above three states. That is, both side sensors detect the obstacle, the car will go backward one second and then turn left for 0.5 second, finally go on to the detection state.
 {
  back();// gobackward
  delay(1000); //delay1S
  turnL();// turn left
  delay(500); // delay0.5S
  }
 }  
 else  // if detection distance of ultrasonic module is not greater than 8cm
     {
       if(LS==0&&RS==1)
       turnR();    // if left side sensor detects the obstacle, but right side sensor detects no obstacle, the car will turn right. 
       else if(LS==1&&RS==1)
       turnR();    // if left side sensor detects no obstacle, but right side sensor detects no obstacle, the car will turn right. 
       else if(LS==1&&RS==0)  
       turnL();  // if left side sensor detects no obstacle, but right side sensor detects the obstacle, the car will turn left. 
       else  // none of the above three states. That is, both side sensors detect the obstacle, the car will go backward one second and then turn left for 0.5 second, finally go on to the detection state.
       {
       back();// go backward
       delay(1000); // delay 1S
       turnL();// turn left
       delay(500); // delay 0.5S
       }}}

Test Result:
Upload the above program code to the main board, then stack well the motor shield onto main board, press lightly down the POWER button on the motor drive shield. When detects an obstacle ahead, our smart car will be able to automatically avoid it to run forward freely. You can try it out and see if it works that way.
thumb

Project 4: Infrared Remote Control for Smart Car

1)Principle and Application of Infrared Receiver

Principle of IR 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. In reality, 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. It is so practical. 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.
thumb

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 signal0 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 signal0 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.
As for an infrared receiver module, it is mainly composed of an infrared receiving head.This device integrates with reception, amplification and demodulation. Its internal IC has been demodulated, able to complete all the work from infrared reception to output TTL level signal compatible.It outputs Digital signal. Suitable for IR remote control and infrared data transmission.
The infrared receiver module has only three pins (Signal line, VCC, GND), very convenient to communicate with Arduino and other microcontrollers.

Parameters of IR Receiver:
1)Operating Voltage: 3.3-5V(DC)
2)Interface: 3PIN
3)Output Signal: Digital signal
4)Receiving Angle: 90 degrees
5)Frequency: 38khz
6)Receiving Distance: 18m


Decoding and Control of IR Remote Control:
First of all, let’s figure out the part of decoding. You can refer to the steps as follows:
Step 1: Connect the infrared receiver module to the P4 connector on the shield using the connector wire.

thumb

Step 2: Download the libraries of infrared remote control inside the Arduino Libraries directory.
thumb
The infrared remote control libraries include transmitting and receiving functions of various remote controls, so that you just need to call the internal functions to control the remote control. It can achieve to make all kinds of productions. You can download the libraries from the below link: [1] thumb IRremote.zip

Step 3: Open the Arduino IDE, at first we use an example IRrecv Demo in Arduino IRremote to read the value of remote control. Shown as the figure below.

thumb

As shown, select the example IRrecv Demo, you can get the code as below.

#include <IRremote.h>library file
int RECV_PIN = 15;  //receiver module is connected to A1,namely Digital 15
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
  Serial.begin(9600); // set the baud rate
  irrecv.enableIRIn(); // Start the receiver
}
void loop() {
  if (irrecv.decode(&results)) {  // if receive the code
    Serial.println(results.value, HEX);  // print the value of remote control
    irrecv.resume(); // Receive the next value
  }
}

Note: Before verify the above code in ArduinoIDE, do remember to add IRremote folder into \Arduino\compiler librariesdirectory, or it will fail to compile it.

Step 4: Upload well the above code to ARDUINO controller, then open the serial monitor and set the baud rate as 9600. Aimed at the IR receiver sensor, press down the button of remote control, you will see the corresponding encode of button is displayed on the monitor. If you press the button too long, it will easily appear a messy code like FFFFFF shown as below.
thumb
Here we have worked out each button value of keyestudio remote control as follows. You can keep it for reference.
thumb

Okay, next let’s use this remote control to make a small experiment, controlling an LED on and off. At this moment, based on the program mentioned above, just need to add the program part of light. So it is much more easier to write the code. Below is an example code.

#include <IRremote.h>
int RECV_PIN = 15; // IR Receiver module is connected to pin 15
int LEDpin = 11; // set LED output pin 
IRrecv irrecv(RECV_PIN);
decode_results results; // used to save the decoding result
void setup()
{
pinMode(LEDpin,OUTPUT);
irrecv.enableIRIn(); // initialize the infrared decoding
}

void loop() {
if (irrecv.decode(&results))
{
Serial.println( results.value);
if( results.value == 0xFF629D) //if receive the command of button ON
{
digitalWrite(LEDpin,HIGH);// turn on LED
}
else if(results.value == 0xFFA857) // if receive the command of button OFF
{
digitalWrite(LEDpin,LOW);// turn off LED
}
irrecv.resume(); // receive the next encode
}
}

Test Result:
Upload well the code to the board, then press the up or down button on the remote control, thus control an LED on and off.
thumb

2) IR Remote Control Smart Car

Principle:
In the previous section, we have talked about how to control the state of an LED by IR remote control. So think in another side. If want to control a smart car by IR remote control, how can we achieve that? Actually it is very easy. The ARDUINO board is able to analyze and judge the infrared signals collected, finally drive the forward, backward and direction of motor.
In the previous part, we have showed you about the button value of an infrared remote control. Next just need to type those values in the test code of infrared remote control for smart car.

Wiring Diagram:
You should first connect the circuit according to the wiring diagram below.

thumb

Let's review the decoding value of buttons on the remote control again.
thumb
You can get the data information of buttons, such as: forward button 0x00FF629D; backward button 0x00FFA857; Stop button 0x00FF02FD; left turn button 0x00FF22DD; right turn button 0x00FFC23D.
These buttons information can be used to control the smart car go forward, backward, turn left or turn right, and stop. So in the following, we type these data into the program.

Example Program as below:

#include <IRremote.h>libraries file
  int RECV_PIN = 15;// connect receiver module to A1,that is D15 interface
  int E1 = 9;   // set the speed pin of motor A as D9
  int E2 = 5;   // set the speed pin of motor B as D5
  int M1 = 2;   // set the direction pin of motor A as D2
  int M2 = 4;   // set the direction pin of motor B as D4
  long advance1 = 0x00FF629D;// set the forward button of car control 
  long back1 = 0x00FFA857;//  set the backward button of car control 
  long stop = 0x00FF02FD;//  set the Stop button of car control 
  long left = 0x00FF22DD;//  set the left turn button of car control 
  long right = 0x00FFC23D;//  set the right turn button of car control 
  IRrecv irrecv(RECV_PIN);
  decode_results results; // used to save the decoding result 
 void setup(void)
{
  pinMode(RECV_PIN, INPUT);// set D15 as INPUT mode
  pinMode(M1,OUTPUT); // set M1 as OUTPUT mode
  pinMode(M2,OUTPUT); // set M2 as OUTPUT mode
  pinMode(E1,OUTPUT); //  set E1 as OUTPUT mode
  pinMode(E2,OUTPUT); //  set E2 as OUTPUT mode
  irrecv.enableIRIn();// initialize the infrared decoding
}
void advance(void)        // set the forward motion
{
  digitalWrite(M1,HIGH);  // motor A turns forward, car wheel goes forward.
  digitalWrite(M2,HIGH);  // motor B turns forward, car wheel goes forward.
  analogWrite(E1,150);    //speed of motor A(can be adjusted according to the actual speed of motor. Turn up the value to accelerate, lower the value to decelerate.) 
  analogWrite(E2,150);    //speed of motorB(can be adjusted according to the actual speed of motor. Turn up the value to accelerate, lower the value to decelerate.) 
}  
void back(void)        // set the backward motion 
{
 digitalWrite(M1,LOW);   // motor A turns reverse, car wheel goes backward.
 digitalWrite(M2, LOW);   // motor B turns reverse, car wheel goes backward.
 analogWrite(E1,150);     // speed of motor A
 analogWrite(E2,150);    // speed of motor B
}  
void turnL(void)     // set the left turn motion 
{
   digitalWrite(M1,LOW);    // motor A turns reverse, car wheel goes backward.
   digitalWrite(M2, HIGH);  // motor B turns forward, the wheels go forward, smart car turns left.
   analogWrite(E1,150);  // speed of motor A
   analogWrite(E2,150);  // speed of motor B
}
void turnR(void)            // set the right turn motion 
{
   digitalWrite(M1,HIGH);   // motor A turns forward, wheels go forward.
   digitalWrite(M2,LOW);    // motor B turns reverse, wheels go backward, smart car turns right.
   analogWrite(E1,150);     // speed of motor A
   analogWrite(E2,150);    // speed of motor B
}
void stopp(void)   // set the Stop motion
{
   digitalWrite(M1,LOW);  // motor A turns reverse
   digitalWrite(M2, LOW);  //  motor B turns reverse 
   analogWrite(E1, 0);      // speed of motor A, speed as zero, means Stop. 
   analogWrite(E2, 0);      //  speed of motor B, speed as zero, means Stop. 
}
void loop()
{
on = !on;
if (irrecv.decode(&results))
{
if (results.value == advance1 )// If receive the command of forward button pressed
advance();// car goes forward 
if (results.value == back1 )// If receive the command of backward button pressed
back();//car goes backward 
if (results.value == left )//  If receive the command of leftbutton pressed
turnL();// car turns left
if (results.value == right )//  If receive the command of right turn button pressed
turnR();// car turns right 
if (results.value == stop )//  If receive the command of Stop button pressed
stopp();// car stops
irrecv.resume(); // receive next encode
}
}

Note: Before verify the above code in ArduinoIDE, do remember to add IRremote folder into \Arduino\librariesdirectory, otherwiseit will fail to compile the code. For example: C:\Program Files\Arduino\libraries

Example Result:
Upload well the program to main board, then press down the POWER button on the motor drive shield lightly. Aimed at the IR receiver module, press any buttons on the remote control, you should see it control the motor, thus can control smart car run freely.

thumb

thumb

Project 5: Bluetooth Remote Control for Smart Car

1) Principle and Application of Bluetooth Remote Control

Principle:
Bluetooth, as the name implies, blue teeth, and he is not used to bite people, but a wireless data transmission method. Bluetooth technology is a wireless 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.485 GHz).
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. TheHC-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, lettingthe subordinates obey the order without any nonsense. So in such situation, it is enough to use the HC-06 module shown as below.
thumb

Specification Parameters:
1) Bluetooth Protocol: Bluetooth 2.1+ EDR Standard
2) USB Protocol: USB v1.1/2.0
3) Operating Frequency: 2.4GHz ISM Frequency Band
4) Modulation Mode: Gauss Frequency Shift Keying
5) Transmit Power: ≤ 4dBm, Second Stage
6) Sensitivity: ≤-84dBm at 0.1% Bit Error Rate
7) Transmission Speed: 2.1Mbps(Max)/160 kbps(Asynchronous);1Mbps/1Mbps(Synchronous)
8) Safety Feature: Authentication and Encryption
9) Supported Configuration: Bluetooth Serial Port (major and minor)
10) Supply Voltage: DC 5V
11) Operating Temperature: -20 to 55℃

Wiring Diagram:
Next, we are going to do a small experiment. When Bluetooth module receives a signal sent by phone, finally control an LED on and off.
thumb

Source Code as below:

int val;  
int led=11;
void setup() 
{ 
Serial.begin(9600);
pinMode(11,OUTPUT);
}
 void loop()
{ val=Serial.read(); 
if(val=='a')
 { 
 digitalWrite(11, HIGH);   // turn the LED on (HIGH is the voltage level)
 }
if(val=='b')
 { 
 digitalWrite(11, LOW);    // turn the LED off by making the voltage LOW
 }
}

Example Result:
After wiring, upload the above code to the board, and connect well the Bluetooth module. Pay more attention to the connecting direction of Bluetooth module. Plug it correctly and you should see an LED on the module flash.
thumb
Pay more attention that here you must first upload the code to the board and then plug in the Bluetooth module, otherwise the program fails to compile.
The principle is that data transmits of Bluetooth module will occupy the microcontroller’s TX and RX pins that are also used for the code upload of microcontroller, so it exists a conflict.
After uploading the code, you have to do another thing, that is, install an application of Bluetooth serial assistant on the phone.
You can click the icon to download it or click here thumb

The Bluetooth we used here is Bluetooth 2.0. Currently, it only supports the Android devices. Do not support Apple's devices. Please pay attention to this when using it.
After the serial assistant is installed, we must first connect the device, open the mobile Bluetooth, search for a Bluetooth device. If find a Bluetooth device named HC-06, pair and enter 1234, finally you should see the paired device shown as below.
thumb thumb
Then open the Bluetooth serial communication APP, namely BT Client, and connect well the Bluetooth just paired.
Done connecting, an LED on the Bluetooth module is always on. If enter the letter a in the Bluetooth APP, the LED connected on the pin 11 is on; if enter the letter b, the LED will be off.

2)Bluetooth Controlled Smart Car

Principle:
In the previous section, you have learned the principles of Bluetooth and how to use Bluetooth to control a small light. Okay, based on that, can we use Bluetooth to send a command to control the movement of a smart car? Absolutely yeah, in the previous section, we can use a mobile APP to send a character. The ARDUINO board can use a Bluetooth module to receive the Bluetooth signal from the mobile phone, and feed it back to the main control board. Then main control board will analyze and judge the collected signals. If correct, it will control the movement of the smart car.
Here we don't need a Bluetooth serial assistant as mentionedabove. We use an Android APP developed by our keyestudio team to control the smart car.
You can click the icon to download it or clink here thumb
The interface of this APP is very simple, as shown below.
thumb

Connected the Bluetooth, let’s make use of a little program that can read the serial data, to check what character the five buttons send. So that we can apply it to the example code of Bluetooth smart car in the following projects.

char val;// define the variable val
void setup()
{
Serial.begin(9600);// set the baud rate as 9600, the same as software setting. When connecting the particular device like Bluetooth, it should be consistent with the baud rate of other devices.  
}
void loop()
{
val=Serial.read();//read the data received from serial port, and assign it to val
Serial.println(val);// print val data
delay(300);//delay 0.3S
}

Through the above program, we can get that the five buttons are Upward (“U”), Downward (“D”), Left (“L”), Right (“R”), and Stop (“S”). The principle is very simple. When Bluetooth module receives these characters sent by the mobile phone, and then it will send them to ARDUINO. ARDUINO will control the rotation direction of motor according to the preset value in the code. When receive the information "U", the smart car will move forward. When receive "D", the smart car goes backward. If receive "L", turn left. If receive "R", turn right. The smart car stops when it receives the "S".
As shown below, connect the components to test it.
thumb
Notice: the Bluetooth module is directly plugged into the shield. 
 

After wiring, you can get the project code as follows:

int E1 = 9;   //set the speed pin of motor A as D9
  int E2 = 5;   //set the speed pin of motor B as D5
  int M1 = 2;   //set the direction pin of motor A as D2
  int M2 = 4;   //set the direction pin of motor B as D4 
  int val;// set the phone APP data received by Bluetooth
  void setup(void)
{
  Serial.begin(9600);
  pinMode(M1,OUTPUT);   // set M1 as OUTPUT mode
  pinMode(M2,OUTPUT);   // set M2 as OUTPUT mode 
  pinMode(E1,OUTPUT);   // set E1 as OUTPUT mode
  pinMode(E2,OUTPUT);   // set E2as OUTPUT mode
  stopp();
}
void advance(void)        // set the forward motion 
{
  digitalWrite(M1,HIGH);  // motor A turns forward, wheels go forward.
  digitalWrite(M2,HIGH);  // motor B turns forward, wheels go forward.
  analogWrite(E1,250);    //speed of motor A(can be adjusted according to the actual speed of motor. Turn up the value to accelerate, lower the value to decelerate.) 
  analogWrite(E2,250);    //speed of motor B(can be adjusted according to the actual speed of motor. Turn up the value to accelerate, lower the value to decelerate.)
}  
void back(void)        // set the backward motion 
{
 digitalWrite(M1,LOW);   // motor A turns reverse, wheels go backward. 
 digitalWrite(M2, LOW);   // motor B turns reverse, wheels go backward.  
 analogWrite(E1,250);     // speed of motor A
 analogWrite(E2,250);    // speed of motor B
}  

void turnL(void)     // set the left turn motion 
{
   digitalWrite(M1,LOW);    // motor A turns reverse, wheels go backward. 
   digitalWrite(M2, HIGH);  // motor B turns forward, wheels go forward, smart car will turn left. 
   analogWrite(E1,250);  // speed of motor A
   analogWrite(E2,250);  // speed of motor B
}

void turnR(void)            // set the right turn motion 
{
   digitalWrite(M1,HIGH);   // motor A turns forward, wheels go forward. 
   digitalWrite(M2,LOW);    // motor B turns reverse, wheels go backward, smart car will turn right. 
   analogWrite(E1,250);     // speed of motor A
   analogWrite(E2,250);    // speed of motor B
}

void stopp(void)   // set the Stop motion 
{
   digitalWrite(M1,LOW);  //  motor A turns reverse 
   digitalWrite(M2, LOW);  // motor B turns reverse 
   analogWrite(E1, 0);      // speed of motor A, speed as zero, means Stop. 
   analogWrite(E2, 0);      // speed of motor B, speed as zero, means Stop. 
}

void loop() 
{
val=Serial.read(); // Bluetooth module reads the data sent by phone APP
if(val=='U')advance();// Bluetooth module receives the character U , smart car goes forward. 
if(val=='D')back();// Bluetooth module receives the character D, smart car goes backward. 
if(val=='L')turnL();// Bluetooth module receives the character L, smart car turns left.
if(val=='R')turnR();//  Bluetooth module receives the character R, smart car turns right .
if(val=='S')stopp();//  Bluetooth module receives the character S, smart car stops.
}


Example Result:
Done uploading the above code to control board, open APP, connect the Bluetooth, you should see the LED on the module is always on. Press down any buttons on APP, you can control the smart car running.
thumb
Is it great? Well, it's just the beginning of ARDUINO's journey. There are more and more awesome projects for you to explore. Furthermore, our KEYESTUDIO research and development team will continue to explore on this path, walking you through the basics up to complex projects. Hope that you can enjoy our works!

Get One Now

Our Tutorial
This tutorial is designed for everyone to play the smart car. You will learn all the basic information about how to control this Arduino smart car with controller board, sensors and components. Easy play and enjoy your time!
Is it great? Well, it's just the beginning of ARDUINO's journey. There are more and more awesome projects for you to explore. Furthermore, our KEYESTUDIO research and development team will continue to explore on this path, walking you through the basics up to complex projects. Hope that you can enjoy our works!


About keyestudio
Located in Shenzhen, the Silicon Valley of China, KEYES DIY ROBOT CO.,LTD is a thriving technology company dedicated to open-source hardware research & development, production and marketing. Keyestudio is a best-selling brand owned by KEYES Corporation, our product lines rang from Arduino boards, shields, sensor modules, Raspberry Pi, micro:bit extension boards and smart car to complete starter kits designed for customers of any level to learn Arduino knowledge.
All of our products comply with international quality standards and are greatly appreciated in a variety of different markets throughout the world. For more details of our products, you can check it from the links below.

Official website: http://www.keyestudio.com/
US Amazon storefront: http://www.amazon.com/shops/A26TCVWBQE4D9T
CA Amazon storefront: http://www.amazon.ca/shops/A26TCVWBQE4D9T
UK Amazon storefront: http://www.amazon.co.uk/shops/A39F7KX4U3W9JH
DE Amazon storefront: http://www.amazon.de/shops/A39F7KX4U3W9JH
FR Amazon storefront: http://www.amazon.de/shops/A39F7KX4U3W9JH
ES Amazon storefront: http://www.amazon.de/shops/A39F7KX4U3W9JH
IT Amazon storefront: http://www.amazon.de/shops/A39F7KX4U3W9JH
US Amazon storefront: http://www.amazon.com/shops/APU90DTITU5DG
CA Amazon storefront: http://www.amazon.ca/shops/APU90DTITU5DG
JP Amazon storefront: http://www.amazon.jp/shops/AE9VWCCXQIC6J


Customer Service
As a continuous and fast growing technology company, we keep striving our best to offer you excellent products and quality service as to meet your expectation. We look forward to hearing from you and any of your critical comment or suggestion would be much valuable to us.
You can reach out to us by simply drop a line at keyestudio@126.com
Thank you in advance.

Resources Download

You can get more reference from below links: