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

keyestudio 4WD Desktop Mini Bluetooth Smart Car V2.0 Kit is based on microcontroller learning application development system. This smart car is controlled by UNO R3 microcontroller. It can achieve the functions of line tracking, obstacle avoidance, IR remote control, Bluetooth remote control and more. All electronic components of the smart car are soldered with anti-reverse interfaces, making it easy and convenient for you to connect the wires.
This kit also contains plenty of interesting programs, which can help you master the electronic sensor modules and running functions of car. The kit aims to disengage you from boring theories, and you can make further study of microcontroller system development when playing it.


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 module can be paired with mobile phone’s Bluetooth to remotely control smart car.
  • 6) Infrared receiver module is matched with an infrared remote control to control 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 4
thumbthumbthumbthumb
6 Keyestudio quick connectors obstacle detector module 2
thumb
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 4
thumb

thumb

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 10
thumbthumbthumbthumbthumbthumbthumbthumbthumbthumb
16 Nut M2 nickle plating 10
thumbthumbthumbthumbthumbthumbthumbthumbthumbthumb
17 Screw M3*10MM round head cross 9
thumbthumbthumbthumbthumbthumbthumbthumbthumb
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 15
thumb thumb

thumbthumbthumb thumbthumbthumb thumbthumbthumb thumb thumbthumb 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 M3*12MM Copper Pillar 2
thumb thumb
24 N20 motor-dedicated wheel 4
thumbthumb thumb thumb
25 N20 motor holder white U-type plastic 4
thumbthumb 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


Assembly Steps

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


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.
DC Power Jack

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

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.

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.

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

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. 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 driverinstallation of UNO R3 development board. Thedriver 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 welaunch 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 onyour 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 littleyellow 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 shownbelow.
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.

**********************************************************charming dividingline**********************************************************************
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
  }
}

******************************************************charming dividingline***************************************************************************
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

2) Keyestudio quick connectors Line Tracking Sensor


thumb


Description
Keyestudio quick connectors line tracking sensor is designed specifically for this smart car. The sensor should be used together with the driver shield. It is equipped with an anti-reverse interface, which can be connected to driver shield with only one simple wire. The working principle of this sensor is to use the infrared light with different color reflectivity, converting the strength of reflected signal into a current signal. The sensor is active high level at detecting black, but is active low level at detecting white. The detection height is 0-3 cm. In the circuit you can rotate the potentiometer on the sensor to adjust the tracking sensitivity for black and white.


Parameters
1.Operating voltage: 3.3-5V(DC)
2.Interface: 5PIN interface
3.Output signal: digital signal
4.Detection height: 0-3cm


Connection Diagram
thumb


Test Code
You can copy and paste the code below on Arduino software.

const int sensorPin1 = 6;     // the number of the sensor pin1
const int sensorPin2 = 7;     // the number of the sensor pin2
const int sensorPin3 = 8;     // the number of the sensor pin3
const int ledPin1 = 3;      // the number of the LED pin1
const int ledPin2 = 10;      // the number of the LED pin2
const int ledPin3 = 11;      // the number of the LED pin3
void setup() {
  pinMode(ledPin1, OUTPUT); 
  pinMode(ledPin2, OUTPUT);  
  pinMode(ledPin3, OUTPUT);  
  pinMode(sensorPin1, INPUT); 
  pinMode(sensorPin2, INPUT);
  pinMode(sensorPin3, INPUT); 
  }
void loop(){    
     digitalWrite(ledPin1,1-digitalRead(sensorPin1));  
     digitalWrite(ledPin2,1-digitalRead(sensorPin2));  
     digitalWrite(ledPin3,1-digitalRead(sensorPin3));  
}


Test Result
Stack driver shield onto UNO R3 board, wire it up as the above diagram and upload well the code, then press the button on the shield lightly, and you can rotate three potentiometers on the sensor to adjust its sensitivity. It is better to set three LEDs in the critical point between on and off state.
If pick up a black paper to cover the sensing area of sensor, both LED on the sensor and external LED module are not bright. While pick up a white paper to cover the sensing area of sensor, both LED on the sensor and external LED module are turned on.


3) Keyestudio quick connectors IR Receiver Sensor


thumb


Description
Keyestudio quick connectors IR receiver sensor is designed specifically for this smart car. The sensor should be used together with the driver shield. It is equipped with an anti-reverse interface, which can be connected to driver shield with only one simple wire.
Infrared receiver sensor is mainly composed of an infrared receiving head, and it is a device that integrates reception, amplification and demodulation. Its internal IC has been demodulated and the output is a digital signal.


Parameters
1.Operating voltage: 3.3-5V(DC)
2.Interface: 3PIN interface
3.Output signal: digital signal
4.Receiving angle: 90 degrees
5.Frequency: 38khz
6.Receiving distance: 18 meters


Connection Diagram
thumb


Test Code
You can copy and paste the code below on Arduino software.

#include <IRremote.h>
int RECV_PIN = 15; //define the digital port15
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);//set the baud rate
irrecv.enableIRIn(); // enable infrared receiving
}
void loop() {
if (irrecv.decode(&results)) 
{
Serial.println(results.value, HEX);//display the data
irrecv.resume(); // receive the next data
}
}


Note:do remember to add IRremote folder into installation directory \Arduino\compiler libraries, or else it will fail to compile the code.


Test Result
Stack driver shield onto UNO R3 board, wire it up as the above diagram and upload well the code, then open the serial monitor and set the baud rate as 9600.
When IR receiver sensor receives the signal sent from infrared remote control, you can see the decoding of button on the monitor. If press the button too long, it will display the unreadable code like FFFFFFFF. Shown below.
thumb

thumb


4) keyestudio quick connectors Obstacle Detector Sensor


thumb


Description
Keyestudio quick connectors obstacle detector sensor is designed specifically for this smart car. The sensor should be used together with the driver shield. It is equipped with an anti-reverse interface, which can be connected to driver shield with only one simple wire. Done wire connection and powered up, if detecting the objects, the signal terminal of this sensor will output 0. If no, the signal terminal will output 1. Besides, you can rotate the potentiometer on the sensor to adjust the detective sensitivity.


Parameters
1.Operating voltage: 3.3-5V(DC)
2.Interface: 3PIN interface
3.Output signal: digital signal
4.Sensing range: 2-40cm


Connection Diagram


thumb


Test Code
You can copy and paste the code below on Arduino software.

const int sensorPin1 = 14;     // the number of the sensor pin1
const int sensorPin2 = 16;     // the number of the sensor pin2
const int ledPin1 = 3;      // the number of the LED pin1
const int ledPin2 = 10;      // the number of the LED pin2
void setup() {
  pinMode(ledPin1, OUTPUT); 
  pinMode(ledPin2, OUTPUT);   
  pinMode(sensorPin1, INPUT); 
  pinMode(sensorPin2, INPUT);
  }
void loop(){    
     digitalWrite(ledPin1,1-digitalRead(sensorPin1));  
     digitalWrite(ledPin2,1-digitalRead(sensorPin2)); 
}


Test Result
Stack driver shield onto UNO R3 board, wire it up as the above diagram and upload well the code to the board, then press down the POWER button on the shield. Besides, you can rotate two potentiometers on the sensor to adjust the sensitivity. It is better to set the LED on the right sensor in the critical point between on and off state.
When detecting the obstacle in front of sensor, both LED on the right sensor and external LED module are bright. If no, LED on the sensor and external LED module are off.


5) keyestudio quick connectors Ultrasonic Module


thumb


Description
Keyestudio quick connectors ultrasonic module is designed specifically for this smart car. This module should be used together with the driver shield. It is equipped with an anti-reverse interface, which can be connected to driver shield with only one simple wire. It is mainly used for distance detection, with the features of high precision, super closeness in the blind area (3-4cm) and stable performance.


Parameters
1.Operating voltage: 5V (DC)
2.Operating current: 15mA
3.Working frequency: 40khz
4.Maximum detection distance: 3-5m
5.Minimum detection distance: 3-4cm
6.Sensing angle: no more than 15 degrees


Connection Diagram


thumb


Test Code
You can copy and paste the code below on Arduino software.

#define ECHO 13
#define TRIG 12
unsigned char SM;        // middle sensor status 
unsigned long rxTime;
void setup() {
  // put your setup code here, to run once:
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  rxTime = pulseIn(ECHO, HIGH);
  SM = (float)rxTime * 34 / 2000.0;
  Serial.print(SM);
Serial.println("cm");
  delay(300);
}


Test Result
Stack driver shield onto UNO R3 board, wire it up as the above diagram and upload well the code to the board, then open the serial monitor and set the baud rate as 9600.
If you place an obstacle in front of the ultrasonic sensor, you will see the distance between obstacle and sensor is displayed on the monitor with an interval of 0.3 second. Shown below.

thumb


6) keyestudio Bluetooth HC-06 Module


thumb


Description
Keyestudio Bluetooth HC-06 is fully compatible with driver shield. So you just need to connect the Bluetooth module to driver shield using several wires. This Bluetooth module can easily achieve serial wireless data transmission. Its operating frequency is among the most popular 2.4GHz ISM frequency band (i.e. Industrial, scientific and medical).
It adopts Bluetooth 2.1+EDR standard. In Bluetooth 2.1, signal transmit time of different devices stands at an interval of 0.5 second, thus the workload of Bluetooth chip can be reduced substantially and more sleeping time can be saved for Bluetooth. This module is set with serial interface, which is easy-to-use and simplifying overall design/development cycle.


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℃


Connection Diagram


thumb


Test Code
You can copy and paste the code below on Arduino software.

int val;  
void setup() 
{ 
Serial.begin(9600);
}
 void loop()
{ val=Serial.read(); 
if(val=='a')
 { 
 Serial.println("keyestudio");
}
}


Test Result
Wire it up as the above diagram, and stack driver shield onto UNO R3 board, then upload well the code to the board, you will see the power indicator D1 light up, and LED on the Bluetooth module flash.
Open your phone Bluetooth, ready to pair the device, enter 1234, you will see the paired device. Shown as figure below.
Then open an APP- BTClient to connect the Bluetooth, if connected, LED on the Bluetooth module is normally on. Enter the letter “a”, the BTClient software will print out “keyestudio”. Shown as the figure below.
thumb thumb


7) Line Tracking Function for Smart Car


Overview
This project is a simple and automatic tracking car system based on Keyestudio Arduino UNO R3 main control board. The smart car with UNO R3 as the controlling core, makes use of the tracking sensor to detect the black track on the pavement, and detection signal will feed back to the main control board. The main control board then will analyze and judge the collected signals to control the driving motor in time, thus 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 for smart car.


Principle

  • 1. 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.
  • 2. 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 the level of sensor action and sensor outputs high level 1.
  • 3. Use the main control board to determine whether the output of the sensor is 0 or 1, finally detect the black line.
  • 4. The main control board will control the motor-driver shield according to the received signal, so as to control the turning direction of four motors. Finally control the movement of smart car.


Connection Diagram


thumb


Test Code
You can copy and paste the code below on Arduino software.

#define SensorLeft    6   // left sensor input pin
#define SensorMiddle    7   // left sensor input pin
#define SensorRight   8  // right sensor input pin
unsigned int SL;        // left sensor status
unsigned int SM;        // middle sensor status
unsigned int SR;        // right sensor status
#define E1  9    // speed control pin, ENA pin on motor driver shield
#define E2  5   // speed control pin, ENA pin on motor driver shield
#define M1  2            // motor direction control, IN1 pin on motor driver shield
#define M2  4          // motor direction control, IN2 pin on motor driver shield
void Sensor_IO_Config()
{
  pinMode(SensorLeft,INPUT);
  pinMode(SensorMiddle,INPUT);
  pinMode(SensorRight,INPUT); 
}
void Sensor_Scan(void)
{
  SL = digitalRead(SensorLeft);
  SM = digitalRead(SensorMiddle);
  SR = digitalRead(SensorRight);
}
void M_Control_IO_config(void)// initialization function of motor driver shield IO
{
  pinMode(M1,OUTPUT); 
  pinMode(M2,OUTPUT);
  pinMode(E1,OUTPUT);
  pinMode(E2,OUTPUT);  
}

void advance(void)     // move forward
{
   digitalWrite(M1,LOW);  // left wheel moves forward
   digitalWrite(M2,HIGH); // right wheel moves backward
   analogWrite(E1,150);   
   analogWrite(E2, 150);
}
void back(void)            // move backwards
{
   digitalWrite(M1,HIGH);   // left wheel moves backwards
   digitalWrite(M2, LOW);   // right wheel moves forward
   analogWrite(E1,150);   
   analogWrite(E2, 150);
}
void turnR(void)        // turn right
{
   digitalWrite(M1,HIGH);   // both left and right wheel move backward
   digitalWrite(M2, HIGH);  
   analogWrite(E1,150);   
   analogWrite(E2, 150);  
}
void turnL(void)        // turn left
{
    digitalWrite(M1,LOW);    // right wheel moves forward
    digitalWrite(M2, LOW);  // left wheel moves forward
    analogWrite(E1,150);   
    analogWrite(E2, 150);
}    
void stopp(void)              // stop
{
    digitalWrite(M1,LOW);    
    digitalWrite(M2, LOW);   
    analogWrite(E1, 0);   
    analogWrite(E2, 0);  // both right &left wheel stop  
}

void setup() 
{ 
   Sensor_IO_Config();
   M_Control_IO_config(); //  initialization of motor controller module IO 
   stopp();
} 
void loop() 
{ 
Sensor_Scan(); 
if((SL==1&&SM==1&&SR==1)||(SL==1&&SM==1&&SR==0)||(SL==0&&SM==1&&SR==1)||(SL==0&&SM==1&&SR==0))
  {
  advance();
  }
if(/*(SL==0&&SM==0&&SR==0)||*/(SL==0&&SM==0&&SR==1))
  {
  //back();
  //delay(10);
  turnR();
  }
if(SL==1&&SM==0&&SR==0)
  {
  //back();
  //delay(10);
  turnL();
  }
} 


Test Result
Wire it up as the above diagram, stack the motor shield onto main control board, then upload the test code to the board, press down the POWER button on the motor shield. If draw a black line on the ground, you can see that the smart car will track the black line.


thumb


8) Edge Avoidance Function for Smart Car


Overview
The principle of this project is similar to the previous project 7. The smart car with UNO R3 as the controlling core, makes use of the tracking sensor to detect the black track on the pavement, and detection signal will feed back to the main control board.
The main control board then will analyze and judge the collected signals to control the driving motor in time, thus adjust the turning direction of smart car. Finally control the smart car run within the area circled by the black line, achieving the automatic edge avoidance function for smart car.


Principle

  • 1. 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.
  • 2. 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 the level of sensor action and sensor outputs high level 1.
  • 3. Use the main control board to determine whether the output of the sensor is 0 or 1, finally detect the black line.
  • 4. The main control board will control the motor-driver shield according to the received signal, so as to control the turning direction of four motors. Finally control the movement of smart car.


Connection Diagram


thumb


Test Code
You can copy and paste the code below on Arduino software.

#define SensorLeft    6   // left sensor input pin
#define SensorMiddle    7   // left sensor input pin
#define SensorRight   8  // right sensor input pin
unsigned int SL;        // left sensor status
unsigned int SM;        // middle sensor status
unsigned int SR;        // right sensor status
#define E1  9    // speed control pin, ENA pin on motor driver shield
#define E2  5   // speed control pin, ENA pin on motor driver shield
#define M1  2            // motor direction control, IN1 pin on motor driver shield
#define M2  4          // motor direction control, IN2 pin on motor driver shield
void Sensor_IO_Config()
{
  pinMode(SensorLeft,INPUT);
  pinMode(SensorMiddle,INPUT);
  pinMode(SensorRight,INPUT); 
}
void Sensor_Scan(void)
{
  SL = digitalRead(SensorLeft);
  SM = digitalRead(SensorMiddle);
  SR = digitalRead(SensorRight);
}
void M_Control_IO_config(void)// initialization function of motor driver shield IO
{
  pinMode(M1,OUTPUT); 
  pinMode(M2,OUTPUT);
  pinMode(E1,OUTPUT);
  pinMode(E2,OUTPUT);  
}

void advance(void)     // move forward
{
   digitalWrite(M1,LOW);  // left wheel moves forward
   digitalWrite(M2,HIGH); // right wheel moves backward
   analogWrite(E1,150);   
   analogWrite(E2, 150);
}
void back(void)            // move backwards
{
   digitalWrite(M1,HIGH);   // left wheel moves backwards
   digitalWrite(M2, LOW);   // right wheel moves forward
   analogWrite(E1,150);   
   analogWrite(E2, 150);
}
void turnR(void)        // turn right
{
   digitalWrite(M1,HIGH);   // both left and right wheel move backward
   digitalWrite(M2, HIGH);  
   analogWrite(E1,150);   
   analogWrite(E2, 150);  
}
void turnL(void)        // turn left
{
    digitalWrite(M1,LOW);    // right wheel moves forward
    digitalWrite(M2, LOW);  // left wheel moves forward
    analogWrite(E1,150);   
    analogWrite(E2, 150);
}    
void stopp(void)              // stop
{
    digitalWrite(M1,LOW);    
    digitalWrite(M2, LOW);   
    analogWrite(E1, 0);   
    analogWrite(E2, 0);  // both right &left wheel stop  
}

void setup() 
{ 
   Sensor_IO_Config();
   M_Control_IO_config(); //  initialization of motor controller module IO 
   stopp();
} 
void loop() 
{ 
Sensor_Scan(); 
if(SL==0&&SM==0&&SR==0)
  {
  advance();
  }
if((SL==1&&SM==0&&SR==0)||(SL==1&&SM==1&&SR==0)||(SL==0&&SM==1&&SR==0)||(SL==1&&SM==0&&SR==1))
  {
  back();
  delay(500);
  turnR();
  delay(500);
  }
if((SL==0&&SM==0&&SR==1)||(SL==0&&SM==1&&SR==1))
  {
  back();
  delay(500);
  turnL();
  delay(500);
  }
  if(SL==1&&SM==1&&SR==1)
  {
  back();
  }
} 


Test Result
Wire it up as the above diagram, stack the motor shield onto main control board, then upload the test code to the board, press down the POWER button on the motor shield.
If you draw a circle with black line, then place the car inside the circle, finally the smart car will turn back or redirect to avoid the line edge, only run inside the circle.


thumb


9) Obstacle Avoidance Function for Smart Car


Overview
This project is a simple and automatic obstacle avoidance system based on Keyestudio Arduino UNO R3 main control board. The smart car with UNO R3 as the controlling core, uses both ultrasonic module and obstacle detector sensor to detect the obstacle in front of the car, and the detection signal will feed back to the main control board.
The main control board then will analyze and judge the collected signals to control the driving motor in time, thus adjust the direction of smart car. Finally control the smart car automatically avoid the obstacles ahead to run smoothly, achieving the obstacle avoidance function for smart car.


Principle

  • 1. Use the ultrasonic module to detect the distance between the smart car and obstacle.
  • 2. Use the obstacle detector sensor to detect the obstacle ahead or left and right side. The sensitivity can be adjusted through rotating the two small potentiometers on the sensor. If detecting the obstacle, the signal end of sensor will output 0; if no, the signal end of sensor will output 1.
  • 3. The main control board will control the motor drive shield according to both the output signal from obstacle detector sensor and distance value from ultrasonic module, thus control the turning direction of four motors. If so, it can control the movement of smart car.


Connection Diagram


thumb


Test Code
You can copy and paste the code below on Arduino software.

#define ECHO 13
#define TRIG 12
#define IRSensorLeft    A0   // left sensor input pin
#define IRSensorRight   A2  // right sensor input pin
unsigned char IRSL;        // left sensor status
unsigned char SM;        // middle sensor status 
unsigned char IRSR;        // right sensor status
unsigned long rxTime;
#define E1  9    
#define E2  5   
#define M1  2            
#define M2  4         
void Sensor_IO_Config()
{
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(IRSensorLeft,INPUT);
  pinMode(IRSensorRight,INPUT);
}
void Sensor_Scan(void)
{
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  rxTime = pulseIn(ECHO, HIGH);
  SM = (float)rxTime * 34 / 2000.0;
  Serial.println(SM);
  IRSL = digitalRead(IRSensorLeft);
  IRSR = digitalRead(IRSensorRight);
}
void M_Control_IO_config(void)// initialization function of motor driver shield IO
{
  pinMode(M1,OUTPUT); 
  pinMode(M2,OUTPUT); 
  pinMode(E1,OUTPUT); 
  pinMode(E2,OUTPUT);   
}
void advance(void)        // turn left 
{
   digitalWrite(M1,LOW);  // left wheel moves forward
   digitalWrite(M2,HIGH); // right wheel moves backward
   analogWrite(E1,150);   
   analogWrite(E2, 150);
}  
void back(void)        // turn right 
{
   digitalWrite(M1,HIGH);   // left wheel moves backwards
   digitalWrite(M2, LOW);   // right wheel moves forward
   analogWrite(E1,150);   
   analogWrite(E2, 150);
}
void turnR(void)            // move backward
{
   digitalWrite(M1,HIGH);   // both left and right wheels move forward
   digitalWrite(M2, HIGH);  
   analogWrite(E1,150);   
   analogWrite(E2, 150);
}
void turnL(void)     // move forward
{
    digitalWrite(M1,LOW);    // right wheel moves forward
    digitalWrite(M2, LOW);  // left wheel moves forward
    analogWrite(E1,150);   
    analogWrite(E2, 150);
}
void stopp(void)              // stop 
{
    digitalWrite(M1,LOW);    
    digitalWrite(M2, LOW);  
    analogWrite(E1, 0);   
    analogWrite(E2, 0);  // both left and right wheels stop
}
void setup() 
{ 
   Serial.begin(115200);
   Sensor_IO_Config();
   M_Control_IO_config();        // initialization of motor driver shield IO
   stopp();
} 
unsigned char old_IRSL,old_IRSM,old_IRSR;
void loop() 
{
Sensor_Scan();
if(IRSL==1&&SM>8&&IRSR==1)
  advance(); 
if((IRSL==0&&SM<8&&IRSR==1)||(IRSL==0&&SM>8&&IRSR==1)||(IRSL==1&&SM<8&&IRSR==1))
  {
  turnR();
  }
if((IRSL==1&&SM<8&&IRSR==0)||(IRSL==1&&SM>8&&IRSR==0))
  {
  turnL();
  }
  if((IRSL==0&&SM<8&&IRSR==0)||(IRSL==0&&SM>8&&IRSR==0))
  {
  back();
  delay(200);
  turnL();
  delay(200);
  }
} 


Test Result
Wire it up as the above diagram, stack the motor shield onto main control board, then upload the test code to the board, press down the POWER button on the motor shield.
When detects the obstacle ahead, the smart car can automatically avoid it to run freely.
thumb


10) Object Following Function for Smart Car


Overview
The principle of this project is similar to the previous project 9. The smart car with UNO R3 as the controlling core, makes use of both ultrasonic module and obstacle detector sensor to detect the object in front of the car, and the detection signal will feed back to the main control board.
The main control board then will analyze and judge the collected signals to control the driving motor in time, thus adjust the turning direction of smart car. Finally control the smart car automatically follow the object ahead, achieving the object following function for smart car.


Principle

  • 1. Use the ultrasonic module to detect the distance between the smart car and object ahead.
  • 2. Use the obstacle detector sensor to detect the obstacle ahead or left and right side. The sensitivity can be adjusted through rotating the two small potentiometers on the sensor. If detecting the object, the signal end of sensor will output 0; if no, the signal end of sensor will output 1.
  • 3. The main control board will control the motor drive shield according to both output signal from obstacle detector sensor and distance value from ultrasonic module, thus control the turning direction of four motors. If so, it can control the movement of smart car.


Connection Diagram


thumb


Test Code
You can copy and paste the code below on Arduino software.

#define ECHO 13
#define TRIG 12
#define IRSensorLeft    A0   // left sensor input pin
#define IRSensorRight   A2  // right sensor input pin
unsigned char IRSL;        // left sensor status
unsigned char SM;        // middle sensor status 
unsigned char IRSR;        // right sensor status
unsigned long rxTime;
#define E1  9    
#define E2  5   
#define M1  2            
#define M2  4         
void Sensor_IO_Config()
{
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(IRSensorLeft,INPUT);
  pinMode(IRSensorRight,INPUT);
}
void Sensor_Scan(void)
{
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  rxTime = pulseIn(ECHO, HIGH);
  SM = (float)rxTime * 34 / 2000.0;
  Serial.println(SM);
  IRSL = digitalRead(IRSensorLeft);
  IRSR = digitalRead(IRSensorRight);
}
void M_Control_IO_config(void)// initialization function of motor driver shield IO
{
  pinMode(M1,OUTPUT); 
  pinMode(M2,OUTPUT); 
  pinMode(E1,OUTPUT); 
  pinMode(E2,OUTPUT);   
}
void advance(void)        // turn left 
{
   digitalWrite(M1,LOW);  // left wheel moves forward
   digitalWrite(M2,HIGH); // right wheel moves backward
   analogWrite(E1,150);   
   analogWrite(E2, 150);
}  
void back(void)        // turn right 
{
   digitalWrite(M1,HIGH);   // left wheel moves backwards
   digitalWrite(M2, LOW);   // right wheel moves forward
   analogWrite(E1,150);   
   analogWrite(E2, 150);
}
void turnR(void)            // move backward
{
   digitalWrite(M1,HIGH);   // both left and right wheels move forward
   digitalWrite(M2, HIGH);  
   analogWrite(E1,150);   
   analogWrite(E2, 150);
}
void turnL(void)     // move forward
{
    digitalWrite(M1,LOW);    // right wheel moves forward
    digitalWrite(M2, LOW);  // left wheel moves forward
    analogWrite(E1,150);   
    analogWrite(E2, 150);
}
void stopp(void)              // stop 
{
    digitalWrite(M1,LOW);    
    digitalWrite(M2, LOW);  
    analogWrite(E1, 0);   
    analogWrite(E2, 0);  // both left and right wheels stop
}

void setup() 
{ 
   Serial.begin(115200);
   Sensor_IO_Config();
   M_Control_IO_config();        // initialization of motor driver shield IO
   stopp();
} 
void loop() 
{
Sensor_Scan();
if((SM>10)&&(SM<25))
{
 advance(); 
 }
if((SM>=25)||(SM<=10))
  {
  stopp();
  }
if(IRSR==0)
{
 turnR();
 }
 if(IRSL==0)
{
 turnL();
 }
} 


Test Result
Wire it up as the above diagram, stack the motor shield onto main control board, then upload the test code to the board, press down the POWER button on the motor shield.
If detects an object ahead, the smart car will follow the object to run normally.
thumb


11) Infrared Remote Control for Smart Car


Overview
This project is a simple infrared remote control system based on Keyestudio Arduino UNO R3 main control board.The smart car with UNO R3 as the controlling core, makes use of infrared receiver module to receive the infrared signal, and infrared signal will feed back to the main control board.
The main control board then will analyze and judge the collected signals to control the driving motor in time, thus adjust the turning direction of smart car. Finally achieve the function that the smart car can be randomly controlled by infrared remote control.


Principle

  • 1. Connect the IR receiver module to the motor drive shield, enable the IR receiver module communicate with the infrared remote control.
  • 2. Infrared remote control will send the information of those buttons like
    thumb” “thumb” “thumb” “thumb” “thumb” to the IR receiver module.
  • 3. When the control board receives the info from IR receiver module, it will control the motor drive shield so as to control the smart car run.
  • 4. When the main board receives the info of button “thumb”, smart car will go forward; when receives the info of button “thumb”, smart car will go backward. If receives the info of button“thumb”, smart car will turn left; if receives the info of button “thumb”, smart car will turn right. If receives the info of button “thumb ”, smart car will stop.


Connection Diagram


thumb


Test Code
You can copy and paste the code below on Arduino software.

#include <IRremote.h> 
int RECV_PIN = 15;
#define E1  9  
#define E2  5 
#define M1  2          
#define M2  4  
//******IR control part********
long advance1 = 0x00FF629D;
long back1 = 0x00FFA857;
long stop = 0x00FF02FD;
long left = 0x00FF22DD;
long right = 0x00FFC23D;
IRrecv irrecv(RECV_PIN);
decode_results results;
void M_Control_IO_config(void)// initialization function of motor driver shield IO 
{
  pinMode(RECV_PIN, INPUT);
  pinMode(M1,OUTPUT); // 
  pinMode(M2,OUTPUT); // 
  pinMode(E1,OUTPUT); // 
  pinMode(E2,OUTPUT); //   
}
void advance(void)        // turn left 
{
   digitalWrite(M1,LOW);  // left wheel moves forward
   digitalWrite(M2,HIGH); // right wheel moves backward
   analogWrite(E1,150);   
   analogWrite(E2, 150);
} 
void back(void)        // turn right
{
   digitalWrite(M1,HIGH);   // left wheel moves backwards
   digitalWrite(M2, LOW);   // right wheel moves forward
   analogWrite(E1,150);   
   analogWrite(E2, 150);
}  
void turnL(void)     // move forward
{
    digitalWrite(M1,LOW);    // right wheel moves forward
    digitalWrite(M2, LOW);  // left wheel moves forward
    analogWrite(E1,150);   
    analogWrite(E2, 150);
} 
void turnR(void)            // move backward
{
   digitalWrite(M1,HIGH);   // both left and right wheel move backward
   digitalWrite(M2, HIGH);  
   analogWrite(E1,150);   
   analogWrite(E2, 150);  
}
void stopp(void)              // stop
{
    digitalWrite(M1,LOW);    
    digitalWrite(M2, LOW);  
    analogWrite(E1, 0);   
    analogWrite(E2, 0);  // both left and right wheel stop
}
void setup()
{
M_Control_IO_config(); 
irrecv.enableIRIn(); // Start the receiver
}
int on = 0;
unsigned long last = millis();
void loop()
{
// If it's been at least 1/4 second since the last
// IR received, toggle the relay if (millis() - last > 250)
{
on = !on;
digitalWrite(11, on ? HIGH : LOW);
}
if (irrecv.decode(&results))
{
if (results.value == advance1 )
advance();
if (results.value == back1 )
back();
if (results.value == left )
turnL();
if (results.value == right )
turnR();
if (results.value == stop )
stopp();
last = millis();
irrecv.resume(); // Receive the next value
}
} 


Note:do remember to add IRremote folder into installation directory \Arduino\compiler libraries, or else it will fail to compile the code.
For example: C:\Program Files\Arduino\libraries


Test Result
Wire it up as the above diagram, stack the motor shield onto main control board, then upload the test code to the board, press down the POWER button on the motor shield.
Aligned at IR receiver module, press down the button on the infrared remote control, it will control the smart car running randomly.
thumb


12) Bluetooth Control for Smart Car


Overview
This project is a simple infrared remote control system based on Keyestudio Arduino UNO R3 main control board.The smart car with UNO R3 as the controlling core, makes use of Bluetooth module to receive the mobile phone’s Bluetooth signal, and Bluetooth signal will feed back to the main control board.
The main control board then will analyze and judge the collected signals to control the driving motor in time, thus adjust the turning direction of smart car. Finally achieve the function that the smart car can be randomly controlled by mobile phone’s Bluetooth device.


Principle

  • 1.Connect the Bluetooth module to the motor drive shield, enable the Bluetooth module communicate with APP on the mobile phone.
  • 2.Send the characters info like“U”“D”“L”“R”“S”to the Bluetooth module using phone’s APP.
  • 3.When the main board receives the info from Bluetooth module, it will correspondingly control the motor drive shield so as to control the turning direction of four motors. This way can randomly control the moving state of smart car.
  • 4.If main board receives the info“U”, smart car will go forward; if receives“D”, smart car will go backward. If receives the info“L”, smart car will turn left; if receives the info“R”, smart car will turn right. The smart car will stop when the main board receives the info“S”.


Connection Diagram
thumb


Test Code
You can copy and paste the code below on Arduino software.

#define E1  9  
#define E2  5 
#define M1  2          
#define M2  4         
int val;
void M_Control_IO_config(void)// initialization function of motor driver shield IO 
{
  pinMode(M1,OUTPUT); // 
  pinMode(M2,OUTPUT); // 
  pinMode(E1,OUTPUT); // 
  pinMode(E2,OUTPUT); //   
}
void advance(void)        // turn left 
{
   digitalWrite(M1,LOW);  // left wheel moves forward
   digitalWrite(M2,HIGH); // right wheel moves backward
   analogWrite(E1,150);   
   analogWrite(E2, 150);
}   
void back(void)        // turn right
{
   digitalWrite(M1,HIGH);   // left wheel moves backwards
   digitalWrite(M2, LOW);   // right wheel moves forward
   analogWrite(E1,150);   
   analogWrite(E2, 150);
}
void turnL(void)     // move forward
{
    digitalWrite(M1,LOW);    // right wheel moves forward
    digitalWrite(M2, LOW);  // left wheel moves forward
    analogWrite(E1,150);   
    analogWrite(E2, 150);
}
 void turnR(void)            // move backward
{
   digitalWrite(M1,HIGH);   // both left and right wheel move backward
   digitalWrite(M2, HIGH);  
   analogWrite(E1,150);   
   analogWrite(E2, 150);  
}
void stopp(void)              // stop
{
    digitalWrite(M1,LOW);    
    digitalWrite(M2, LOW);  
    analogWrite(E1, 0);   
    analogWrite(E2, 0);  // both left and right wheel stop
}
void setup() 
{ 
   Serial.begin(9600);
   M_Control_IO_config();        // initialization of motor driver shield IO
   stopp();
} 
void loop() 
{ 
val=Serial.read(); 
if(val=='U')advance();
if(val=='D')back();
if(val=='L')turnL();
if(val=='R')turnR();
if(val=='S')stopp();
}


Test Result
Wire it up as the above diagram, stack the motor shield onto main control board, then upload the test code to the board, and press down the POWER button on the motor shield. Powered up, power indicator D1 will light up and led light on the Bluetooth module will flash.
Then open the Bluetooth on your phone, pair the device, enter 1234, you can see the paired device. Open your APP to connect the Bluetooth, if connected, the led on the Bluetooth module is normally on. Finally, aligned at the Bluetooth module of car, press the controlled button on your APP, it can freely control the smart car running.
thumb


Resources

Get One Now