Ks0441 Keyestudio Desktop Mini Bluetooth Smart Car V3.0

From Keyestudio Wiki
Jump to: navigation, search
Keyestudio Desktop Mini Bluetooth Smart Car V3.0


Description

We can often see others on the internet making use of control boards and electrical components to build their own creative robots. Wanna DIY your own robot?
Here comes keyestudio desktop mini Bluetooth smart car V3.0, which is an upgraded version of keyestudio desktop mini Bluetooth smart car V2.0.
The smart car still keeps the functions like line tracking, obstacle avoidance, IR and Bluetooth control and more.
Furthermore, we make a great improvement for the smart car as follows:

  • 1) The Acrylic plates are more bright and colorful;
  • 2) Adding a microphone sound module to make a fantastic sound when driving the car run;
  • 3) Using Bluetooth HM-10 module,which can support Bluetooth 4.0; supporting both Android and iOS system; also can actuate the smart car with our own designed Bluetooth APP.
  • 4) Can freely choose the battery case 18650 or 4-cell AA battery case to supply power for the robot car. Note that batteries are Not Included. Users can freely choose two 18650 batteries or four AA batteries (1.5V) to supply power for the robot car.
  • 5) Making improvements on the motor drive board; one is coming with a slide switch for controlling the power switch;the other is adding 8 jumper caps to control the DC motor direction,easy for code debugging.
  • 6) Coding the robot car with Mixly blocks software, simple and ready to play.

From the basics up to complex projects, through this kit you can learn to control the robot car with Mixly blocks coding. Easy to code and learn coding and computational thinking.
If you are looking for inspiration, you can find a great variety of tutorials here. Take your brain on a fun and inspiring journey through the world of programming and electronics.


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

When get this smart car kit, at first glance, you will see the beautiful packaging box. And each component is safely packed inside the small bag in order.
You will get such a bulk of components and screws to make your own smart car. So we have listed all the components as follows:

441图1.png

441图2.png

441图3.png

441图4.png

441图5.png


Software Introduction

1)Installing Arduino IDE

When program the UNO development board, you can download the Arduino integrated development environment from the link:


See more contents at:


Ks0436-9.png

The functions of each button on the Toolbar are listed below:
IDE.png

IDE 1.png Verify/Compile Check the code for errors
IDE 2.png Upload Upload the current Sketch to the Arduino
IDE 3.png New Create a new blank Sketch
IDE 4.png Open Show a list of Sketches
IDE 5.png Save Save the current Sketch
IDE 6.png Serial Monitor Display the serial data being sent from the Arduino


Or you can browse the KEYESTUDIO website at this link, https://www.keyestudio.com/ and then click on the WIKI Tutorial.
图片wiki.png



2)Introduction for Mixly Blocks

Mixly is a free open-source graphical Arduino programming software, based on Google’s Blockly graphical programming framework, and developed by Mixly [email protected] BNU.
It is a free open-source graphical programming tool for creative electronic development; a complete support ecosystem for creative e-education; a stage for maker educators to realize their dreams.
More info please check the link to download the Mixly blocks software.

Before starting the robot projects, please click the link to get the basic understanding of Mixly software.
Ks0446图片8.png



3)Import Robot Library

For the robot kit, we have developed keyestudio robot car library.
Don’t forget to import the keyestudio desktop car library to Mixly software before coding the robot projects.
Must import the robot car library first, or else you CANN'T check all the test code.


441图6.png

Unzip the desktop_car library package, you can see the desktop_car XML.document.
441图7.png

Then import this document into Mixly library. Import custom library successfully!
441图8.png

You are able to click “Manager” to manage all imported libraries.
Note: sometimes it may exists a conflict between libraries, so should keep only correct car library when using and delete other library.

441图9.png



Projects Guide


Project 1: Built-in LED

Keyestudio UNO R3

When it comes to using the UNO R3 as core of our robot, the UNO is the best board to get started with electronics and coding.
If this is your first experience tinkering with the platform, the UNO is the most robust board you can start playing with.
Well, let's at first have a look at this UNO R3 board.

Here is an explanation chart of what every element and interface of the board does:
Ks0001-pinout.png

KS0001 5-1.png ICSP (In-Circuit Serial Programming) Header

In most case, ICSP is the AVR,an Arduino micro-program 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, slave the output devices under the SPI bus host.
When connecting to PC, program the firmware to ATMEGA328P-PU.

KS0001 5-2.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.

KS0001 5-3.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.

KS0001 5-4.png GND ( Ground pin headers)

Used for circuit ground

KS0001 5-5.png AREF

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

KS0001 5-6.png SDA

IIC communication pin

KS0001 5-7.png SCL

IIC communication pin

KS0001 5-8.png ICSP (In-Circuit Serial Programming) Header

In most case, ICSP is the AVR,an Arduino micro-program header consisting of MOSI, MISO, SCK, RESET, VCC, and GND. Connected to ATMEGA 16U2-MU. When connecting to PC, program the firmware to ATMEGA 16U2-MU.

KS0001 5-9.png RESET Button

You can reset your Arduino board, for example, start the program from the initial status. You can use the RESET button.

KS0001 5-10.png D13 LED

There is a built-in LED driven by digital pin 13. When the pin is HIGH value, the LED is on, when the pin is LOW, it's off.

KS0001 5-11.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.

KS0001 5-12.png ATMEGA 16U2-MU

USB to serial chip, can convert the USB signal into serial port signal.

KS0001 5-13.png TX LED

Onboard you can find the label: TX (transmit) When Arduino board communicates via serial port, send the message, TX led flashes.

KS0001 5-14.png RX LED

Onboard you can find the label: RX(receive ) When Arduino board communicates via serial port, receive the message, RX led flashes.

KS0001 5-15.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.

KS0001 5-16.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.
Convert an external input DC7-12V voltage into DC 5V, then switch DC 5V to the processor and other components.

KS0001 5-17.png DC Power Jack

Arduino board can be supplied with an external power DC7-12V from the DC power jack.

KS0001 5-18.png IOREF

Used to configure the operating voltage of microcontrollers. Use it less.

KS0001 5-19.png RESET Header

Connect an external button to reset the board. The function is the same as reset button (labeled 9)

KS0001 5-20.png Power Pin 3V3

A 3.3 volt supply generated by the on-board regulator. Maximum current draw is 50 mA.

KS0001 5-21.png Power Pin 5V

Provides 5V output voltage

KS0001 5-22.png Vin

You can supply an external power input DC7-12V through this pin to Arduino board.

KS0001 5-23.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) Can also used as digital pins, A0=D14, A1=D15, A2=D16, A3=D17, A4=D18, A5=D19.

KS0001 5-24.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 on the Arduino IDE, you must know what IC is on your board. This information can be checked at the top of IC.



Let’s make a simple test for the UNO built-in LED (D13).
We will work on blinking an LED. That’s right - it’s as simple as turning a light on and off!
Now enough talking - let’s get started with the LED project.

Blinking an LED
It’s pretty simple to turn a built-in led on and off. We only require UNO R3 control board and a USB cable to enter the wonderful programming world.
Connect your UNO R3 board to the computer’s USB port using a USB cable for communication.

0428图片15.png


Test Code:
Open Mixly blocks platform to get started with coding.
First, click IN/OUT, drag the “DigitalWrite PIN# (0)Stat(HIGH)” block.

0428图片16.png

This block is used to set the level HIGH or LOW of Digital pin.

  • Select HIGH is to set the HIGH level.
  • Select LOW is to set the LOW level.
  • The HIGH level is the state of high voltage, generally recorded as 1.
  • High voltage, high current, the LED lights.
  • The LOW level is the state of low voltage, generally recorded as 0.
  • Low voltage, low current, the LED Not lights.

To observe the LED blink obviously, we need to add a Delay block.
Check the test code below and upload it to your UNO R3 board.
0428图片17.png

What you should see:
Drag the test code to Mixly window; remember to select the proper board and COM port.
Then compile and upload the code to your control board. Upload success message will appear on the bottom bar.
The UNO built-in LED (label “L”) will turn on for 1 second, and then turn off for 1 second, alternately and circularly.

0428图片18.png

0428图片19.png



Project 2: LED Blink

keyestudio LED module


Overview:
LED blink is one of the most basic experiments in learning programming.
This project we use keyestudio white LED module. You will learn first how to blink an LED.

There are three lead-out pins on the module, respectively negative pin(marked -), positive pin(marked +) and signal pin(marked S).
Separately connect the white LED module to the pin G, 5V and D3 of keyestudio motor drive shield V2 using three F-F jumper wires. Then stack the motor drive shield V2 onto the UNO control board.


441图11.png

Note: stack the motor drive shield on the UNO board; connect white LED module to motor drive shield (pin G for GND, V for 5V, S for digital pin3 (S)). Connect the power to BAT connector.
Done wiring, upload the test code to the board, so as to turn on an LED light.


Test Code:
Now write the program to make the white LED flash.

Go to click library “Desktop_Car_V3”, drag out the blockframless, this block is used to set the HIGH/LOW for digital port;
Click the drop-drown triangle icon to select HIGH for digital pin, with voltage; select LOW for digital pin, with no voltage.
So what should we set the white LED pin output HIGH or LOW to turn on the LED? Through testing, set to HIGH, white LED turns on; set to LOW, white LED turns off.
And go to “Control”, drag out the blockframless to add a delay time.
Duplicate this code stringframlessonce and change to LOW.
We turn on the white LED for one second then off for one second.
framless

Note: uploading the test code, DO NOT connect the Bluetooth module to motor drive shield. Otherwise, code upload fails.


Result:
Done uploading the code, turn the slide switch ON.
You will see the LED module turn on for one second, then off for one second.
441图14.png



  • Little Knowledge:

1.In the code, we’ve set the LED signal pin to D3 in the library; we can set the led signal pin without using library. The block 441图15.png from library “Desktop_Car_V3” is used to set the HIGH/LOW for digital port; Click the drop-drown triangle icon to select HIGH for digital pin, with voltage; select LOW for digital pin, with no voltage.
Besides, To make the same effect, you can use the block 0441图1.png from “In/Out”. change the pin0 to pin3.
So you can see the same final effect using the block 441图15.png or 441图16.png.


2.What happens when you change the number in one or both of the delay(1000)
441图17.png
This delay period is in milliseconds, so if you want the LED to blink as low or fast, change the value, try 500 or 2000.



  • Extension Practice:

Try making the LED blink without using library. Set the LED signal to D11, and turn on for 0.5 second; off for 0.2 second, alternately and circularly.
0441图2.png



Project 3: Obstacles Detection


Overview:
The robot car kit is packed with 2 infrared obstacle detector sensors.
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.


441图18.png

TECH SPECS:

  • Operating Voltage: DC 3.3-5V
  • Detection Distance: 2-40cm
  • Interface: 3PIN
  • Output Signal: Digital signal


In this project, we read the signal level of obstacle detector sensor to judge whether detect obstacles or not.
When detects an obstacle, sensor’s signal pin outputs LOW (display 0); otherwise, output HIGH (display 1).
Show the result on the serial monitor, and control the external LED module turn ON/OFF.


Wiring Diagram:
Connect two infrared obstacle detector modules and an LED module to keyestudio motor drive shield V2. Then stack the motor drive shield onto UNO control board.
441图19.png

Note: stack the motor drive shield onto UNO control board. connect the left obstacle detector sensor to P3(G、V、A0) connector on the motor drive shield; the right obstacle detector sensor to P5(G、V、A2) connector. If the digital ports are not enough, analog port can be used as digital port. Analog port A0 corresponds to digital port14; A1 corresponds to digital port15.
The white LED module is connected to motor drive shield; pin G for GND, V for 5V, S for digital pin3 (S). Connect the power to BAT connector.


Test Code:

We have connected well the both obstacle detector sensors, white LED module and power supply. Now write the program to test the left and the right obstacle detector sensor.
Go to “Control”, drag out the “setup” block; and drag the “Serial baud rate(9600)” block from “Serial Port” into the “setup” block.

To read the measured signal info by both obstacle detector sensors, we click the “Serial Port” , drag out the blockPro4-图片2.png; drag out the blockP3-2.png
from “Text” into the blockPro4-图片2.png, and then duplicate the complete block three times. Change the firstP3-2.png to “left_sensor”; drag out the blockP3-5.png
from library “Desktop_Car_V3”to replace the secondP3-2.png; delete the third hello box, forming a blank box; Change the fourthP3-2.png
to “right_sensor”.

And again go to “Serial Port” , drag out the blockPro4-图片2.png; duplicate the blockP3-5.pngonce and drag it intoPro4-图片2.png, click the drop-down triangle icon to select the “right_infrared_avoid” And go to “Control”, drag the delay blockPro4-图片3.png; set the delay time in 500ms.
P3-12.png

Upload the above code to see the effect. Powered on, the Pled LED on the obstacle detector sensor turns on. Through testing, if detected obstacle, obstacle detector sensor outputs LOW 0 and the built-in Sled LED turns on red; no obstacle, the sensor outputs HIGH 1 and the built-in Sled LED is off.
We’ve measured what signal the left and the right obstacle detector sensor send. Next the white LED module is turned on when any obstacle detector sensor detects an obstacle.
Next write the program that can turn on or off white LED module using the left and the right obstacle detector sensor.

Here we can use the condition statementP3-13.pngorP3-14.png.But the blockP3-14.png is more efficient than P3-13.png.
Go to “Control” , drag out the blockP3-13.png ,then click the blue gear icon, appear the edit box, drag theP3-17.pngblockP3-14.pnginto block. So you can get the blockP3-14.png
.

Next, go to the “Logic”, drag out the blockP3-20.png ,and drag out the blockP3-5.png.from the “Desktop_Car_V3”into the first input box at the left side of “=”; drag thePro8-图片20.pngfrom the “Math”into the second input box at the right side of “=” ; like this:P3-23.png.
We duplicate the block once and click the drop-down triangle icon to select the “right_infrared_avoid”.
P3-24.png

And again go to the “Logic”, drag out the blockP3-24.png behind if statement; click the drop-down triangle icon to select “or”. then drag the block P3-24.png andP3-24.png into the input box of blockP3-25.png.

Click the “Desktop_Car_V3”, drag out the blockP3-29.pnginto do statement, keep HIGH; duplicate the block once and set to LOW and drag it into else statement.

P3-30.png

Now we have written the code and upload it to see the final result!
P3-31.png



Special note:
You can turn the tirmpot on the obstacle detector sensor to adjust the inductive sensitivity. Rotate the potentiometer near the infrared emitter to the end clockwise, then adjust the potentiometer near the infrared receiver to observe the Sled light, turn the Sled light off, and keep the critical point to be lit. The sensitivity is the best.


Result:
Done wiring, connect the UNO control board to computer’s USB port with USB cable to upload the code.
Code upload success, open the serial monitor, and set the baud rate to 9600. We can see the HIGH or LOW level of signal pin of left and right sensors. As shown below.
When any sensor detects obstacle (output 0), external LED module will turn on; otherwise, LED turns off.
441图21.png




  • Little Knowledge:

1.In the code,we use the library 441图22.png to read the HIGH/LOW of the left infrared avoiding sensor (A0); using the block 441图23.png also makes sense.
The signal pin of the right infrared avoiding sensor is A2.


2.Ks0446图片30.png means the baud rate is set to 9600;
0428图片43.png Print the specified number, text or other value on serial monitor.
0428图片44.pngPrint the specified number, text or other value on newline of monitor.

3.0428图片45.pngmeans that if condition 1 is satisfied, it's going to be A, otherwise it's going to be B.
When using, you can find the if...do... statement block in the Mixly Control Block. Then click the gear icon on the block to drag out the else or else if block you need to use.
0428图片46.png

4.0441图3.pngThis is a logical statement. It’s available as long as can satisfy any one of the two conditions.



  • Extension Practice:

1.Change the test code without using the library, making the same function.
0441图4.png



Project 4: Playing Melody

441图31.png


Overview:
The keyestudio power amplifier module integrates an adjustable potentiometer, a passive buzzer speaker, an audio amplifier 8002B chip and 3pin header interface.
When testing, we can input square waves of different frequency at the signal pin to make passive buzzer speaker produce a sound.
We can turn the potentiometer to adjust the sound amplification, that is, to adjust the sound volume.
In this project, we will code the buzzer in power amplifier module to produce a tone. And if string a bunch of tones together, you’ve got music!

Note: stack the motor drive shield onto UNO control board; connect the pin (G、V、S) of power amplifier module to the pin G, 5V, D11 of motor drive shield V2 with 3P female-to-female jumper wire. Connect the power supply to BAT connector.
Wiring Diagram:

441图32.png


Coding:
Write the program that can make the power amplifier module play a tone.

Click the “Desktop_Car_V3”, drag out the blockP4-1.png,click the drop-down triangle icon behind frequency; you’ll see a series of pitch name in English letters and Numbers. You can choose different pitch name to set different frequency.

1(Do)、2(Re)、3(Mi)、4(Fa)、5(Sol)、6(La)、7(Si) are the roll-call in music. They correspond to NOTE C、NOTE D、NOTE E、NOTE F、NOTE G、NOTE A、NOTE B in the frequency drop-down list.
From 1(Do)to 7(Si), that is from C to B. As the below table shown. The pitch/tone is getting higher and higher.

1(Do) 2(Re) 3(Mi) 4(Fa) 5(Sol) 6(La) 7(Si)
NOTE_C NOTE_D NOTE_E NOTE_F NOTE_G NOTE_A NOTE_B


P4-2.png

Music requires tones as well as beats. The duration of each note, is the beat. We can use Delay block to set the beats. The larger the value, the longer the delay time is.

Click the drop-down triangle icon on the blockP4-3.png to select the frequency NOTE_A4.

And go to “Control”, drag the delay blockPro4-图片3.png; set the delay time 200ms.

Click the imported library“Desktop_Car_V3”, drag out the blockP4-5.png, and delay 200ms.
Copy the above string and change the last delay from 200 milliseconds to 1000 milliseconds.

Upload the complete code to see what will happen.


Code 1: play a tone
P4-6.png


Code 2: do re mi fa so la si do
We have introduced the knowledge of power amplifier module and tone play. Now write the program for the buzzer playing tune “do re mi fa so la si do”.
Click the “Desktop_Car_V3”, drag out the blockP4-3.png,click the drop-down triangle icon to select the frequency NOTE_C4.
And go to “Control”, drag the delay blockPro4-图片3.png; set the delay time 300ms.
Duplicate the above code string seven times and click the drop-down triangle icon to separately select the frequency NOTE_D4、NOTE_E4、NOTE_F4、NOTE_G4、NOTE_A4、NOTE_B4、NOTE_C5. Keep the delay time 300ms.

Click the imported library“Desktop_Car_V3”, drag out the blockP4-5.png, and duplicate a delay block once and set to 2000ms.

441图34.png


Code 3: Ode to Joy

How to use the power amplifier module to play a song of Ode to Joy? Next we write the program to make the buzzer play the song of Ode to Joy.
Click the“Desktop_Car_V3”, drag out the blockP4-11.png,click the drop-down triangle icon to select the song Ode_to_Joy. Then drag out the block P4-12.pngto switch off the sound.
And go to “Control”, drag the delay blockPro4-图片3.png; set the delay time 3000ms.


P4-14.png



Result:
Done wiring, connect the UNO control board to computer’s USB port with USB cable to upload the code. Then turn the slide switch ON.
Upload code 1, buzzer will produce a tone of 440Hz for 0.2 second then off for 0.2 second, circularly.
Upload code 2, buzzer will play a tune “do re mi fa so la si do” circularly.
Upload code 3, buzzer will play a song Ode To Joy circularly.
441图36.png

Remember that you can turn the potentiometer to adjust the sound volume if can’t hear the tone.
441图37.png



  • Little Knowledge:

1.In the code 1, we use the library441图38.png, the signal pin of passive buzzer module is connected to D11, with a frequency of 440Hz tone. Note that you can click the Code on the Mixly window to check out the Arduino code.
441图39.png

2.In the code 1441图40.png means the buzzer will make no tone.
3.In the code 2441图41.png, we set the buzzer can play different tones of different frequencies.
4.In the code 3441图42.png, means the buzzer will play a specific song. You can choose the tune Ode to Joy, or Birthday.



  • Extension Practice:

1.For code 3, you can click to check out the corresponding language C; find out the tone of corresponding frequency and duration time, then refer to the code 2, try write into your own code.
441图43.png


  • Tips:

441图44.png set the frequency; 441图45.png set a period of time;

The corresponding frequency of the first M3 is 658Hz; and the duration time is 300*1=300ms. The rest is in the same manner.
441图46.png


Project 5: Obstacles Alarm


Overview:
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 an echo signal, so it can determine the distance of object from the time difference between trigger signal and echo signal.
The ultrasonic module is commonly used in robot car DIY process. It can detect whether an obstacle ahead and we can measure the distance between ultrasonic sensor and obstacles by calculation.
When DIY smart car, we can use the measured distance data to program the robot car avoiding or following obstacles.

In this project, we are going to measure the distance between ultrasonic module and obstacles ahead, triggering the power amplifier module to make a sound.
When the measured distance between ultrasonic and obstacles ahead is less than 10cm, the speaker will produce a tone of 440Hz; otherwise, not sound.
441图47.png


TECH SPECS:

  • Operating Voltage: DC 5V
  • Operating Current: 15mA
  • Operating Frequency: 40khz
  • Maximum Range: 2-3m
  • Minimum Range: 2m
  • Sensing Angle: 15 degrees
  • Trigger Input Signal: 10µS TTL pulse


Wiring Diagram:
Note: connect the ultrasonic sensor to motor drive shield’s P2 connector, VCC pin to V, Trig pin to digital 13 (S), Echo pin to digital 12 (S), G pin to GND(G);
Connect the pin (G、V、S) of power amplifier module to the pin G, 5V, D11(S) of motor drive shield with 3P female-to-female jumper wire; Then stack the motor drive shield onto UNO control board. Connect the power supply to BAT connector.


441图48.png


Test Code:

The ultrasonic sensor is connected to P2 connector of motor drive shield , VCC pin to V, Trig pin to digital 13 (S), Echo pin to digital 12 (S), G pin to GND(G); Trig pin is to trigger signal and Echo pin is to receive echo signal.
Next need to write the program to get the specific distance measured by ultrasonic sensor.

Go to “Control”, drag out the “setup” block P5-1.png;
Drag out the blockP5-2.png from “SerialPort” into the “setup” block.

Go to the “SerialPort” again, drag out the blockP5-3.png and P5-4.png
Go to “Text”, drag out the blockP5-5.png into the block P5-3.png , and change the word “hello” to “distance=”.

Then go to “Desktop_Car_V3”, drag and drop the ultrasonic blockP5-7.png into “Serial printIn” block. To make the value print slowly, we add a delay block.
And again go to “Control”, drag the delay blockPro4-图片3.png; set the delay time in 500ms.
Upload the code success, open the serial monitor to check the distance between ultrasonic sensor and an obstacle.

P5-9.png

In the following, try to realize another two distance situations:
When the measured distance between the ultrasonic sensor and front obstacles is smaller than 10cm, power amplifier module plays sound.
To judge whether the distance is smaller than 10cm or greater than 10cm, here we can use the condition statementP3-13.pngorP3-14.png . But the blockP3-14.png is more efficient thanP5-13.png.

Go to “Control”, drag out the blockP5-13.png, then click the blue gear icon, appear the edit box, drag theP3-17.png block intoPro8-图片15.pngblock. So you can get the blockP3-14.png.

Next, go to “Logic”, drag the blockP3-20.pnginto the if statement, and drag out the blockP5-7.pngfrom the “Desktop_Car_V3”into the first input box at the left side of “=”; drag the Pro8-图片20.pngfrom the “Math” into the second input box at the right side of “=” ; change the“=” to“<” , change the value 0 to 10; like this:P5-21.png.

Click the“Desktop_Car_V3”, drag out the blockP4-3.pnginto the do statement,click the drop-down triangle icon to select the frequency NOTE_A4.
Then drag out the blockP4-12.pnginto the else statement to switch off the sound.

P5-24.png

Okay. Now we have written the complete code for ultrasonic sensor and power amplifier module. Upload the code.

P5-25.png



Result:
Done wiring, connect the UNO control board to computer’s USB port with USB cable to upload the code.
Code upload success, open the serial monitor, and set the baud rate to 9600. You can see the distance between ultrasonic and obstacle ahead, with a unit of cm.
When the measured distance between ultrasonic and obstacles ahead is less than 10cm, the speaker will produce a tone of 440Hz; otherwise, not sound.
441图50.png



  • Little Knowledge:

1.In the code, we use the 441图51.png to measure the distance between ultrasonic sensor and obstacle ahead, with a unit of cm.
2.Ks0446图片30.png means the baud rate is set to 9600;
0441图5.png: print the distance value on the newline of monitor.

But if you use the block 0464-pro5图23.png, it will not print the value on the newline; just print on the monitor. The difference between them is whether need to make line wrap.

3.In the code also call the if...do... statement 0428图片45.png
Refer to the detailed use in the project 3 please.



  • Extension Practice:

1.You can reset the distance measured by ultrasonic sensor. Change the different distance value to make the buzzer play a tone of different frequency.
0441图7.png



Project 6: Motor Driving and Speed Control

Overview:

KS0435-3.png

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

Direct stack the motor driver shield onto UNO R3 board, after the BAT is powered on, turn the Slide button ON, to supply the power for both keyestudio motor driver shield V2 and UNO R3 board.
For simple wiring, the motor driver shield comes with anti-reverse interfaces. When connecting the motor, power supply and sensor modules, you just need to plug in directly.

The Bluetooth interface on the motor driver shield is fully compatible with keyestudio HM-10 Bluetooth module. When connecting, just plug HM-10 Bluetooth module into the corresponding interface.

At the same time, the motor drive shield has brought out extra digital and analog ports in 2.54mm pin headers, so that you can continue to add other sensors for experiments extension.
The motor drive shield can access to 4 DC motors, defaulted by jumper connection. The motor connector A and A1, connector B and B1 are separately in parallel.

The 8 jumpers can be applied to control the turning direction of 4 motors.
For instance, if change the 2 jumpers near the motor A connector from horizontal connection to vertical connection, the turning direction of motor A is opposite to the original rotation direction.

thumb


Specifications:

  • 1)Logic part input voltage: DC5V
  • 2)Driving part input voltage (limit): DC 6-18V
  • 3)Driving part input voltage (recommended): DC 7-12V
  • 4)Logic part working current: <36mA
  • 5)Driving part working current: <2A
  • 6)Maximum power dissipation: 25W (T=75℃)
  • 7)Working temperature: -25℃~+130℃


PINOUT Instructions:

thumb


Wiring Diagram:
Connect two motors to keyestudio motor drive shield V2; stack the motor drive shield onto UNO control board.
441图57.png


Driving Motor
According to the wiring diagram, default the jumper connection method.
Follow the table below to drive the 2 motors rotate by Digital, PWM pins, so as to control the robot car run.
The PWM value is in the range of 0-255. The greater the value set, the faster the motors rotate.

0441图8.png


Test Code: (without library)

Navigate the desktop Bluetooth car to turn forward for 2 seconds, backward for 2 seconds, and then rotate to left for 2 seconds, rotate to right for 2 seconds, stop for 2 seconds.

We go to write the code for motor A, B to turn front. Go to “In/Out”, drag out the blockP6-1.png and P6-2.png; separately duplicate these two blocks, set to digital pin 4 and pin 2, change the analog pin 3 to digital pin 5 and 9. Pin 4 and pin 2 set to HIGH; pin 5 and pin 9 are PWM pin. The greater the PWM value, the faster the speed is. So here we assign both value to 100.
Next go to drag out the blockP6-3.png from “Control”, and set to 2000ms.
P6-4.png

So now we complete the code for robot moving front for 2 seconds.
Let’s move on to write the code for robot turning back, rotating to left, rotating to right and stop.
Duplicate the finished code string three times; Set the pin 4 and pin 2 to LOW, assign both pin 5 and pin 9 to 100, so that the motor A, B will turn backward, thus the robot will turn back.
P6-5.png

Set the pin 4 to LOW and pin 2 to HIGH, assign both pin 5 and pin 9 to 100, so that the motor A turns back and motor B turns front, thus the robot will rotate to left.
P6-6.png

Set the pin 4 to HIGH and pin 2 to LOW, assign both pin 5 and pin 9 to 100, so that the motor A turns front and motor B turns back, thus the robot will rotate to right.
P6-7.png

Go to “In/Out” again, drag out the blockP6-2.png and duplicate once, change the pin3 to pin 5 and pin 9, assign the value 0; then add a delay block 2000ms. Upload the complete code to see the desktop robot move.
Code:
441图59.png


Result:
Done wiring, connect the UNO control board to computer’s USB port with USB cable to upload the code.
441图60.png

Upload success, turn the Slide switch ON. The 2 motors act like the smart car to turn forward for 2 seconds, backward for 2 seconds, rotate to left for 2 seconds, rotate to right for 2 seconds, stop for 2 seconds, alternately and circularly.
441图61.png



  • Little Knowledge:

1.The code logic is completely based on the motor driving reference table. Check it out.
2.The PWM value is in the range of 0-255. The greater the value set, the faster the motors rotate. Base on that, you can set the speed as you like.


  • Extension Practice:

1.Based on the logic table, try to reset a new moving track for your smart car. Reference code:
0441图9.png


Project 7: Library Driving Motor

Overview:
There are many ways to drive the motor. We have learned how to control the 2 motors in the previous section, so as to drive the smart car run. It is troublesome to control the smart car via control port. For this, we specially create the library to drive the robot car more simple and easier. When setting, the PWM value is in the range of 0-255. The greater the value set, the faster the motors rotate.


Wiring Diagram:
441图57.png


Test Code: (with library)

Click the“Desktop_Car_V3”, drag out the blockP7-1.png and set to PWM100; so the robot will move front at a speed of PWM100.
This blockP7-2.pnghas the same function as the string blockP7-3.png.
Next go to drag out the blockP6-3.pngfrom “Control”, and set to 2000ms.

How to write the code for robot back and stop?
Click the “Desktop_Car_V3”, drag out the blockP7-5.png and P7-6.png , set to PWM100; respectively add a delay block in 2000ms.

Continue to write the program for robot, rotate to left for 2 seconds, rotate to right for 2 seconds, turn left for 2 seconds, turn right for 2 seconds, stop for 2 seconds.

Drag out the blockP7-7.png,P7-8.png ,P7-9.png ,P7-10.png andP7-6.png ; set to PWM100 and respectively add a delay block, set to delay 2000ms.

441图62.png


Result:
Done wiring, connect the UNO control board to computer’s USB port with USB cable to upload the code.
Upload success, turn the Slide switch ON. The 2 motors act like smart car to turn forward for 2 seconds, backward for 2 seconds, stop for 2 seconds, rotate to left for 2 seconds, rotate to right for 2 seconds, turn left for 2 seconds, turn right for 2 seconds, stop for 2 seconds, circularly.
441图63.png



  • Little Knowledge:

1.The code using library to set the car’s motion state, easy and simple, shortening the code length.
2.The control logic is the same as project 6-motor driving. We can click to check out the corresponding language C of motor mixly code.
441图64.png


  • Extension Practice:

1.Based on the logic table, try to reset a new moving track for your smart car. (Reference program)
0441图10.png



Project 8: 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. The detection height is 0-3 cm.
For keyestudio 3-channel line tracking module, we have integrated 3 sets of TCRT5000 infrared tube on a single board. It is more convenient for wiring and control.
By rotating the adjustable potentiometer on the sensor, it can adjust the detection sensitivity of the sensor.

Special note: before testing, turn the potentiometer on the sensor to adjust the detection sensitivity.
When adjust the LED front the trimpot at the threshold between ON and OFF, the sensitivity is the best.


TECH SPECS:

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


441图66.png


Wiring Diagram:
Next let’s do a simple test for this tracking module. The connection diagram is shown as below.
441图67.png

Note: stack the motor drive shield onto UNO control board. connect the line tracking sensor to motor drive shield’s P1 connector (G, V, D6, D7, D8); Connect the pin (G、V、S) of white LED module to the pin G, 5V, D3(S) of motor drive shield with 3P female-to-female jumper wire. Connect the power supply to BAT connector.


Coding:
Now write the program to test the line tracking sensor.
Go to “Control”, drag out the “setup” block; and drag the “Serial baud rate(9600)”block from “SerialPort” into the “setup” block.

To respectively read the left, the center and the right tracking sensor on the line tracking module, we click the “SerialPort” , drag out the blockP8-1.png; drag out the blockP3-2.png from “Text” into the block P8-1.png, and then duplicate the complete block six times.

Change the firstP3-2.png to “left_tracking=”; drag out the block P8-5.png from library “Desktop_Car_V3” to replace the second P3-2.png; delete the third hello box, forming a blank box; change the fourth P3-2.png to “center_tracking=”.

Duplicate the blockP8-5.png once to replace the fifthP3-2.png and click the drop-down triangle icon to select the “center_tracking”; delete the sixth hello box, forming a blank box; change the seventh P3-2.png to “right_tracking=”.

And again go to “SerialPort”, drag out the block P8-1.png; duplicate the block P8-5.png once and drag it into P8-1.png, click the drop-down triangle icon to select the “right_tracking” And go to “Control”, drag the delay blockPro4-图片3.png; set to delay 1000ms.

P8-15.png

Complete and upload the above code to see the result. It can tell black and white.
Through testing, if line tracking sensor detects white, output LOW 0 and the built-in LED turns on red; detecting black, output HIGH 1 and the built-in LED is off.

We’ve measured what signal the line tracking sensor sends. Next the white LED is turned on when any tracking sensor detects white.
Next write the program that can turn on or off white LED module using line tracking sensor.

To judge whether the left, the center and the right tracking sensor detect black or white, here we can use the condition statement P3-13.png or P3-14.png.
But the blockP3-14.png is more efficient thanP3-13.png.

Go to “Control”, drag out the blockP3-13.png, then click the blue gear icon, appear the edit box, drag the block P3-17.png into Pro8-图片15.png block. So you can get the blockP3-14.png .

Next, go to the “Logic”, drag out the blockP3-20.png, and drag out the blockP8-5.png from the “Desktop_Car_V3” into the first input box at the left side of “=”;
drag the Pro8-图片20.png from the “Math” into the second input box at the right side of “=” ; like this:P8-26.png.
We duplicate the block twice, and respectively click the drop-down triangle icon to select “center_tracking” and “right_tracking”.P8-27.pngP8-28.png.

And again go to the “Logic”, drag out the block P3-25.png and click the drop-down triangle to select “or”; duplicate the block P8-30.pngonce and make as P8-31.png; drag this block behind into the if statement.

Now respectively drag the blockP8-26.png,P8-27.pngandP8-28.png into the input box of blockP8-31.png.

Click the “Desktop_Car_V3”, drag out the blockP3-29.png into do statement, keep HIGH; duplicate the block once and set to LOW and drag it into else statement.

P8-37.png

Now we have written the code of tracking sensor controlling white LED module. Upload the complete to see the final result!
P8-38.png



Result:
Done wiring, connect the UNO control board to computer’s USB port with USB cable to upload the code.
Upload success, turn the Slide switch ON.
When the left TCRT5000 infrared tube detects a white line, LED module lights; detecting a black line, LED turns off.
In a similar way, we use other 2-way TCRT5000 infrared tubes to detect the black-white line.
441图70.png



  • Little Knowledge:

1.In the code, we use the library 441图71.png to read the HIGH/LOW of the left sensor (D6); using the block 441图72.png also makes sense.
The signal pin of the middle sensor is D7; the signal pin of the right sensor is D8.

2.0428图片45.pngmeans that if condition 1 is satisfied, it's going to be A, otherwise it's going to be B.
When using, you can find the if...do...statement block in the Mixly Control Block. Then click the gear icon on the block to drag out the else or else if block you need to use.
0428图片46.png



  • Extension Practice:

1.Change the test code without using the library, making the same result.(reference program)
0441图11.png



Project 9: Infrared Receiver


  • IR Remote Control:
remote control

There is no doubt that infrared remote control is commonly seen in our daily life. It's hard to imagine our world without it.
An infrared remote control can be used to control a wide range of home appliances such as television, audio, video recorders and satellite signal receivers.
Well, in the following let’s get a better understanding of the infrared remote control.

Infrared remote control is composed of infrared transmitting and infrared receiving systems. That is, consist of an infrared remote control, an infrared receiver module and a microcontroller that can decode. You can refer to the figure below.

Ks0436-201.png

The 38K infrared carrier signal transmitted by an infrared remote controller is encoded by an encoding chip inside the remote controller. It is composed of a pilot code, user code, data code, and data inversion code.
The time interval between pulses is used to distinguish whether it is a signal 0 or 1. (when the ratio of high level to low level is about 1:1, considered as signal 0.) And the encoding is just well composed of signal 0 and 1.

The user code of the same button on remote controller is unchanged. Using difference data distinguish the key pressed on the remote control.
When press down a button on the remote control, it will send out an infrared carrier signal. And when infrared receiver receives that signal, its program will decode the carrier signal, and through different data codes, thus can judge which key is pressed.
The microcontroller is decoded by an received signal 0 or 1 to determine which key is pressed by the remote control.


  • IR Receiver Module:

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, outputting Digital signal. Suitable for IR remote control and infrared data transmission.
The infrared receiver module has only three pins (Signal, VCC, GND), very convenient to communicate with Arduino and other microcontrollers.

IR Receiver Module


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


Hookup Guide:
Note: connect the infrared receiver sensor to P4(G、V、A1) connector on the motor drive shield. If the digital ports are not enough, analog port can be used as digital port. Analog port A0 corresponds to digital port14; A1 corresponds to digital port15.
The white LED module is connected to motor drive shield; pin G for GND, V for 5V, S for digital pin3 (S). Connect the power to BAT connector.

0441图12.png



Test Code:
Now write the program. When aligning at the IR receiver, press the key on the IR remote control, available to check the input signal change of IR receiver on the serial monitor.
Go to “Control”, drag out theP9-1.pngblock; and drag theP9-2.pngblock from “SerialPort” into the “setup” block.
Next, IR receiver will receive the infrared signal when press different keys on the IR remote control.

We first click the imported library “Desktop_Car_V3”, drag out the blockP9-3.png; drag the blockP9-4.png from “SerialPort” into the infrared receiver block just made.
Then go to “Variables”, drag out the block P9-5.pnginto the blockP9-4.png.

So the infrared receiver can receive the infrared signal.
P9-7.png

Upload this code, open the serial monitor; aimed at the infrared receiver sensor, press the key on the IR remote control, IR receiver will receive the infrared signal, and indicator turns on red. And you can see the key encoding on the serial monitor.

P9-8.png

Next move on to realize the IR receiver controlling white LED with IR remote control.
Press the front key P9-9.png on the IR remote control, white LED turns on; press the key P9-10.png , white LED turns off. So here we call the if statement P9-11.png from the “Control”. According to the measured result, we know the infrared encoding(string value) of front key P9-9.png is FF629D; the infrared encoding(string value) of key P9-10.png is FF02FD.
As the command key of IR remote control is hexadecimal code, the front must add 0x.

If ir_rec=0xFF629D, press the front key P9-9.png on the IR remote control, white LED turns on.

Go to the “Logic”, drag out the block P3-20.png into the if statement, and drag out the block P9-16.pngfrom the “Variables” into the first input box at the left side of “=”; drag the Pro8-图片20.pngfrom the “Math” into the second input box at the right side of “=” and type “0xFF629D” , like this:P9-18.png.

Click the imported library “Desktop_Car_V3”, drag out the blockP3-29.png into do statement, keep HIGH.

P9-20.png

Duplicate the above code string once, change “0xFF629D” to “0xFF02FD” and set to LOW.

P9-21.png
Okay, the complete program has been written well. Upload the code to see the infrared remote control effect!

P9-22.png



Test Result:
Code upload success, open the serial monitor, and set the baud rate to 9600.
Press your remote control, aimed at the infrared receiver, to send the signal, and you will see the encoding of each button on the remote control.
Note if press the control button too long, easily appear unreadable code. Shown as below figure.
441图77.png


Below we have listed out each button value of keyestudio remote control. So you can keep it for reference.
thumb



Little Knowledge:
1.In the code, we direct use the library441图78.png; the signal pin of IR receiver module is A1; the IR receiver receives an infrared signal and outputs 16-bit encoding, printing out on serial monitor (baud rate 9600).
2.We can test out the 16-bit encoding of each button on the infrared remote control by source code. Or you can see the button encoding chart shown above.


Extension Practice:
1.Driving the 2 motors’ turning direction and speed by infrared remote control.
(refer to project 6- motor driving)
Combine infrared receiver and motors driving knowledge to build an infrared remote control car. (reference program)
thumb



Assembly Steps for Desktop Car

441图79.png


Follow the assembly steps below to build your own robot.

1) Bottom motor parts

You should mount two motors on the Acrylic bottom board.
Prepare the components as follows:

  • M2 Nut *4
  • White N20 motor holder *2
  • 12FN20 motor connector *2
  • M2*10MM round-head screw *4
  • Acrylic bottom board *1


441图80.png

First place four M2 Nuts inside the holes of white N20 motor holders.
441图81.png

Note: the Acrylic plate is marked with A, B for the two motors. Mount the motor A to label A on the Acrylic plate; motor B to Acrylic position B.

Then mount the white N20 holders onto the motors.
441图82.png

Fix these two 12FN20 motor connectors on the Acrylic bottom plate with four M2*10MM round-head screws.Tighten them with screwdriver.
441图83.png

441图84.png


2) Battery case

You can choose the 18650 2-cell battery case or 4-cell AA battery case.Mount the battery case on the acrylic bottom board.
Here we install the 4-cell AA battery case for the smart car. You should first get some parts below:

  • 4-cell Battery case*1
  • M3*8MM flat-head screws *2
  • M3 Nut *2

Fix the battery case on the top of Acrylic board using two M3*8MM flat-head screws and two M3 Nuts. Tighten the screws with screwdriver and self-prepared wrench.
441图85.png

441图86.png


3) Tracking sensor and wheels

Assemble the line tracking sensor and connect the wire:

  • JST-PH2.0MM-2P 24AWG red-black wire 160mm *2
  • JST-PH2.0MM-5P blue-green-yellow-red-black connector wire 15CM *1
  • M3*5+6MM single-pass copper pillars *2
  • M3*6MM round-head screws *2
  • M3 Nut *2
  • Wheel *2
  • Line tracking sensor *1


441图87.png

Connect 2 pieces of JST-PH2.0MM-2P red-black wire 160mm to the 12FN20 motor connectors. Connect two wheels to the motor spin.
441图88.png

Insert two M3*5+6MM single-pass copper pillars into the holes on the line tracking sensor, and tighten two M3 Nuts on the copper pillars. Shown below. Tighten the nuts and screws with a self-prepared wrench
441图89.png441图90.png

After that, mount the line tracking sensor on the Acrylic board with two M3*6MM round-head screws. Tighten the nuts and screws with a screwdriver and self-prepared wrench.
441图91.png

Connect a JST-PH2.0MM-5P 24AWG blue-green-yellow-red-black connector wire 15CM to the connector of tracking sensor. Shown below.
441图92.png


Completed the above assembly, let's install the caster for this small car.

  • W420 ball caster wheel *1
  • M3*6MM round-head screws *6
  • M3 Nut *2
  • M3*8+6MM single-pass copper pillar *2
  • M3*40MM dual-pass copper pillar *4


441图93.png

441图94.png441图95.png

Screw the W420 ball caster wheel on the Acrylic bottom board with two M3*6MM round-head screws, two M3 Nuts, two M3*8+6MM single-pass copper pillars. Tighten the screws with a screwdriver.
441图96.png

Screw four M3*40MM dual-pass copper pillars on the 4 corner holes on acrylic bottom board with four M3*6MM round-head screws. Tighten the screws with a screwdriver.
441图97.png

441图98.png441图99.png


4) Ultrasonic module

Now 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
  • Ultrasonic acrylic board *1
  • JST-PH2.0MM-4P connector wire 8CM *1


441图100.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.
Then connect the JST-PH2.0MM-4P connector wire to ultrasonic module.
441图101.png



5) Acrylic top board

Fix other sensors on the Acrylic top board. Prepare parts as follows:

  • Acrylic top board *1
  • Obstacle detector sensor *2
  • IR receiver sensor *1
  • Keyestudio power amplifier module *1
  • M3*10MM round-head screw *4
  • M3 Nut *8
  • M3*8+6MM single-pass copper pillar*4
  • JST-PH2.0MM-3P yellow-red-black wire 8CM *3
  • 3pin F-F jumper wire *1


441图102.png

Tighten four M3*8+6MM single-pass copper pillars on the acrylic top board with four M3 Nuts using a wrench.
441图103.png

Separately mount two obstacle detector sensors and an IR receiver sensor on Acrylic top plate with three M3*10MM screws and three M3 Nuts. Tighten them with a screwdriver and a self-prepared wrench.
441图104.png

Mount keyestudio power amplifier module on Acrylic top plate with a M3*10MM screw and a M3 Nut. Tighten them with a screwdriver and a self-prepared wrench.
441图105.png

441图106.png

Connect the wire to the keyestudio power amplifier module, obstacle detector sensors and an IR receiver sensor.
441图107.png



6) Complete Car

Till now, the smart car is almost installed well.
Assemble all the finished parts and install the control board as follows:

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


441图108.png

441图109.png

Firstly, insert the ultrasonic module into the two holes of Acrylic bottom board.
Then, screw the Acrylic top board to the copper pillars mounted on Acrylic bottom plate with four M3*6MM round-head screws.
441图110.png

After that, mount the UNO R3 main board onto the Acrylic top board with four M3*6MM round-head screws using a screwdriver.
And stack the motor drive shield onto UNO R3 board.
441图111.png

441图112.png

Finally insert the 8 jumpers and HM-10 Bluetooth module into the motor drive shield.
(8 jumpers direction are by default; changing the jumper direction will change the motor turning direction)
441图113.png

441图114.png


Up to now, you have finished the hardware installation of the smart car. Congrats!
For wiring, you can connect all the wires according to the corresponding silk-screen on the board.
Connection diagram:
441图115.png

Keyestudio Desktop Mini Bluetooth Smart Car V3.0


The Desktop car is now completing installation.
Follow the detailed project instructions to build your own robot with various functions.



Project 10: Following Robot


Circuit Design:
In the above sections we already introduced the motor drive shield, sensor, module, motors and other elements.
According to the project 3/5/7 -- obstacle detection, obstacle alarm, and library driving motor,
we’re now ready to give the robot capability - Object Following!

In the project, we make the robot measure whether exist obstacles at both sides with obstacle detector sensors. Measure the distance between obstacle and robot, and then use the measured data to rotate the two motors, so as to control the robot car run.


Below is a specific logic table of following robot for reference:
0441图14.png


Build Following Robot:
Based on the designed circuit, we are going to build a following robot car.
Check the circuit diagram and test code below.

441图118.png

Note: stack the motor drive shield onto UNO control board. connect the ultrasonic sensor to motor drive shield’s P2 connector with 4P jumper wire, VCC pin to V, Trig pin to digital 13 (S), Echo pin to digital 12 (S), G pin to GND(G);
Connect the left obstacle detector sensor to the P3(G、V、A0) connector on the motor drive shield; the right obstacle detector sensor to P5(G、V、A2) connector with 3P jumper wire;
Connect the motor A and motor B to connector A and B separately. Connect the power supply to BAT connector.


Test Code:

Now write the program to achieve the function of following robot.

First we set up three variables, “distance”, “val_L”and “val_R”.

The variable “distance” means save the distance value measured by ultrasonic sensor; “val_L”and “val_R” respectively save the signal of obstacle detected by the left and the right obstacle detector sensor.

Click “Variables”, drag out the blockP10-1.png; and drag the blockP10-2.pngfrom “Math”into the blockP10-1.png. Then duplicate the blockP10-4.pngtwice; respectively change “item”into“distance”、“val_L”and“val_R”; set the value to 0.

P10-5.png

A variable is like a box, and a new variable is like making a box; we can give the box a name, like we just called it “distance”. The things placed inside the box can be changed, like we can place oranges, apples, pears, etc.
The function of the variable box in this program is to load the distance digit. With this box called “distance”, we can store the measured distance digit between ultrasonic sensor and front obstacle. So every time I mention distance, it refers to the distance value measured by the ultrasonic sensor at that time.

Click “Variables”, drag out the blockP10-6.pngandP10-7.png; and drag the blockP10-8.pngfrom “Desktop_Car_V3” into the blockP10-6.png.

And again go to drag the blockP10-10.pnginto the blockP10-7.png; then duplicate this code string once, change“val_L”to“val_R”, click the drop-down triangle to select “right_infrared_avoid”.

P10-12.png

Next judge whether the ultrasonic sensor detects front obstacle or the left and the right obstacle detector module detects obstacle.

Here we can use the judgement statement “if...do...else if...do...” . First write the program when the centre obstacle distance detected by ultrasonic sensor is smaller than or equal to 5cm, or both the left and the right obstacle detector module detect obstacle, the robot will turn back at a speed of PWM200.

Go to “Control”, drag out the blockPro8-图片14.png, then click the blue gear icon, appear the edit box, drag the P10-14.png block intoPro8-图片15.pngblock five times. So you can get the block:
P10-16.png

Next, go to “Logic”, drag the blockP3-20.png and select“≤”; go to “Variables”, drag out the blockPro8-图片13.png into the first input box at the left side of “≤”; drag thePro8-图片20.pngfrom the “Math” into the second input box at the right side of “≤” ; change the value 0 to 5; like this:P10-20.png(note you can type the value flexibly.)

We duplicate this block twice, respectively change the variable“distance”to“val_L”and“val_R”;“≤”to“=”
We have mentioned before that the obstacle detector module detects obstacle, output LOW digital signal 0; detects no obstacle, output HIGH digital signal 1. So here change the value 5 to 0.
P10-21.pngP10-22.png

Think back, we use the two words “or”, “and ” when describe the judgement statement. There are two blocks in “Logic” to represent either one of two conditions happens or both of them happen at the same time, that is P10-23.png,P10-24.png .

Drag out the block P10-24.png and click the drop-down triangle to select “or” and can get the blockP10-23.png.

Drag the block P10-24.png into block P10-23.png to make as P10-29.png.

Then drag the block P10-20.png into the first input box of or blockP10-29.png; drag the block P10-21.pngand P10-22.png respectively into the input box of and block P10-29.png. So can get the block P10-33.pngand drag it behind to the if statement. This means the judgment condition is when the centre obstacle distance detected by ultrasonic sensor is smaller than or equal to 5cm, or both the left and the right obstacle detector module detect obstacle.

Followed by drag out the blockP10-34.png from “Desktop_Car_V3” into the do statement; change the PWM0 into PWM200.
Thus, we now have written well the program when the front obstacle distance detected by ultrasonic sensor is smaller than or equal to 5cm, or both the left and the right obstacle detector module detect obstacle, the robot will turn back at a speed of PWM200.
P10-35.png

We now move on to write the program. When the front obstacle distance detected by ultrasonic sensor is greater than 5cm, and the left obstacle detector module detects obstacle and the right one didn’t detect obstacle, the robot will rotate to left at a speed of PWM200.

Duplicate the block P10-33.png once and drag into the first else if statement; set to distance>5 and val_L=0 and val_R=1P10-37.png

Drag out the blockP10-38.pngfrom“Desktop_Car_V3” into the do statement; change the PWM0 into PWM200.

Next write the program that the front obstacle distance detected by ultrasonic sensor is greater than 5cm, and the left obstacle detector module didn’t detect obstacle and the right one detects obstacle, the robot will rotate to right at a speed of PWM200.

Duplicate the blockP10-37.pngonce and drag into the second else if statement; set to distance>5 and val_L=1 and val_R=0 P10-41.png

Drag out the block P10-42.png fromDesktop_Car_V3”into the do statement; change the PWM0 into PWM200.
P10-43.png

Move on to write the program. When the front obstacle distance detected by ultrasonic sensor is greater than 5cm, and smaller than or equal to 15cm, and both obstacle detector modules didn’t detect obstacle, the robot will stop running.

Go to “Logic”, drag out the blockP10-44.png; duplicate the block P10-45.pngonce and set to “distance≤15”; then separately drag the block P10-45.pngand P10-47.pnginto the input box of block P10-44.png, like this:P10-49.png.

And again drag out the blockP10-44.png, then drag the block P10-49.png into the first input box of blockP10-44.png; duplicate the block P10-53.png once and drag it into the second input box of block P10-44.png and change “val_R=0” into “val_R=1”.
P10-55.png

Now drag the block P10-55.png into the third else if statement. Drag out the block P10-61.pngfrom“Desktop_Car_V3” into the do statement.

Next it’s easy to write the program. When the front obstacle distance detected by ultrasonic sensor is greater than 15cm, and smaller than or equal to 35cm, and both obstacle detector modules didn’t detect obstacle, the robot will go front at a speed of PWM255.

Direct duplicate the block P10-55.png once and change to distance>15 and distance≤35, and drag it into the fourth else if statement.
Drag out the block P10-63.pngfrom “Desktop_Car_V3”into the do statement, and set the value to 255.

Finally write the program. When the front obstacle distance detected by ultrasonic sensor is greater than 35cm, and both obstacle detector modules didn’t detect obstacle, the robot will stop running.

Duplicate the code block P10-41.pngonce and drag it into the fifth else if statement. Change to distance>35 and val_L=1 and val_R=1
Drag out the block P10-61.png from “Desktop_Car_V3” into the do statement.
P10-66.png

Now the code for following robot is finished. Upload the complete code to operate your desktop robot!

Test Code:
Can’t connect the Bluetooth module when upload the code, otherwise, code upload fails.
You should upload the code success, then plug in the Bluetooth module.

441图119.png



Result:
Stack the motor drive shield onto UNO board. Connect the UNO control board to computer’s USB port with USB cable to upload the code.
Upload success and turn the slide switch to ON position. The robot will follow the front object to move.
441图116.png


Project 11: Obstacle Avoiding Robot


Circuit Design:
We’re now ready to give the robot another capability - Obstacle Avoiding!
It is pretty simple. Just keep the same components and connection method as following robot, but need to change the code.


Below is a specific logic table of Obstacle Avoiding robot for reference:
0441图15.png


Build Obstacle Avoiding Robot:
Based on the designed circuit, we are going to build an obstacle avoiding robot car.
Check the circuit diagram and test code below.

441图121.png


Test Code:

In the above section we have introduced how to use the ultrasonic sensor and obstacle detector module to make a following robot. Now let’s write the program to make the robot automatically avoid obstacles.

First we set up three variables, “distance”, “val_L”and “val_R”. The variable “distance” means save the distance value measured by ultrasonic sensor; “val_L”and “val_R” respectively save the signal of obstacle detected by the left and the right obstacle detector sensor.

Click “Variables”, drag out the blockP10-1.png; and drag the block P10-2.pngfrom “Math” into the blockP10-1.png. Then duplicate the blockP10-4.pngtwice; respectively change “item”into“distance”、“val_L”and“val_R”; set the value to 0.

P11-5.png

Click “Variables”, drag out the blockP10-6.pngand P10-7.png; and drag the block P10-8.pngfrom “Desktop_Car_V3” into the block P10-6.png. And again go to drag the block P10-10.pnginto the blockP10-7.png; then duplicate this code string once, change“val_L”to“val_R”, click the drop-down triangle to select “right_infrared_avoid”.

P11-12.png

Next judge whether the ultrasonic sensor detects front obstacle or the left and the right obstacle detector module detects obstacle. Here we can use the judgement statement “if...do...else if...do...”

First write the program. No matter how far the front obstacle detected by ultrasonic sensor, as long as both the left and the right obstacle detector module detect obstacle, the robot will turn back for one second at a speed of PWM150, and then turn left for 0.5 second at a speed of PWM200.

Go to “Control”, drag out the blockPro8-图片14.png, then click the blue gear icon, appear the edit box, drag theP10-14.png block into Pro8-图片15.pngblock four times. So you can get the block:
P11-16.png

Next, go to “Logic”, drag the blockP3-20.png; go to “Variables”, drag out the block P11-22.png into the first input box at the left side of “=”; drag the Pro8-图片20.pngfrom the “Math” into the second input box at the right side of “=” ; keep the value 0; like this:P11-24.png.

We duplicate this block once, and change the variableP11-22.png to P11-26.png; like this:P10-22.png
And again go to “Logic”, drag the block P11-28.pnginto the if statement, then drag the block P11-24.png and P10-22.png into the input box of blockP11-28.png.
P11-32.png

Drag out the block P11-32.pngfrom “Desktop_Car_V3”into the do statement, and set the value to PWM150. And add a delay block in 1000ms.
Then drag out the block P11-32.pnginto the do statement, and set the value to PWM200. And add a delay block in 500ms.
Till now we have made a piece of code like below:
P11-32.png

We now move on to write the program. No matter how far the front obstacle detected by ultrasonic sensor, the left obstacle detector module didn’t detect obstacle and the right one detects obstacle, the robot will rotate to left at a speed of PWM200; the left obstacle detector module detects obstacle and the right one didn’t detect obstacle, the robot will rotate to right at a speed of PWM200.

Duplicate the blockP11-32.png once and drag into the first else if statement; set to val_L=1 and val_R=0
Drag out the blockP10-38.png from “Desktop_Car_V3” into the do statement; change the PWM0 into PWM200.
Duplicate the blockP11-32.png once again and drag into the second else if statement; set to val_L=0 and val_R=1
Drag out the blockP11-36.pngfrom“Desktop_Car_V3” into the do statement; change the PWM0 into PWM200.
P11-37.png

We continue to write the program. When the front obstacle distance detected by ultrasonic sensor is smaller than or equal to 10cm, and both obstacle detector modules didn’t detect obstacle, the robot will rotate to right at a speed of PWM200. When the front obstacle distance detected by ultrasonic sensor is greater than 10cm, and the left obstacle detector module detects obstacle and the right one didn’t detect obstacle, the robot will go front at a speed of PWM200.

Go to “Logic”, drag the blockP3-20.png and select“≤”; go to “Variables”, drag out the blockPro8-图片13.png into the first input box at the left side of “≤”; drag the Pro8-图片20.pngfrom the “Math” into the second input box at the right side of “≤” ; change the value 0 to 10; like this:P11-43.png

Drag out the block P10-24.png into the third else if statement; drag the block P11-43.pnginto the first input box of blockP10-24.png; duplicate the block P11-47.pngonce and drag it into the second input box of block P10-24.png; set to val_L=1 and val_R=1
P11-49.png

Drag out the block P11-36.pngfrom“Desktop_Car_V3” into the do statement; change the PWM0 into PWM200.

Then duplicate the block P11-49.pngonce and drag it into the fourth else if statement; change to distance>10, and drag out the block P11-52.png into the do statement; change the PWM0 into PWM200.

P11-53.png


Source Code:
Now the code for obstacle avoiding robot is finished. Upload the complete code to see the final effect!
Can’t connect the Bluetooth module when upload the code, otherwise, code upload fails. You should upload the code success, then plug in the Bluetooth module.
441图122.png



Result:
Upload success and turn the slide switch to ON position.
The robot can automatically avoid the front obstacle to run.
441图123.png



Project 12: Line Tracking Robot


Circuit Design:
In the above sections we already introduced the motor drive shield, sensor, module, motors and other elements.
According to the project 7/8 -- library driving motor, line tracking sensor,
we’re now ready to give the robot capability - Line Tracking!
In the project, we make the robot detect black line at the car bottom with line tracking sensor. Then control the 2 motors rotate by measured result, so as to drive the robot track black line.


Below is a specific logic table of line tracking robot for reference:
0441图16.png


Build Line Tracking Robot:
Based on the designed circuit, we are going to build a line tracking robot car.
Check the circuit diagram and test code below.

441图125.png

Note: stack the motor drive shield onto UNO control board. connect the line tracking sensor to motor drive shield’s P1 connector (G, V, D6, D7, D8); respectively connect the motor A and B to connector A and B on the motor drive shield. Connect the power supply to BAT connector.


Test Code:
Now write the program to build a line tracking robot.
The line tracking sensor detects white, output LOW 0; detecting black, output HIGH 1.
To judge whether the left, the center and the right tracking sensor detect black line, if the center tracking sensor detects black line, the robot will go front at a speed of PWM200.

Here we can use the condition statementP3-13.pngorP3-14.png . But the blockP3-14.pngis more efficient thanPro8-图片14.png .
Go to “Control”, drag out the blockPro8-图片14.png, then click the blue gear icon, appear the edit box, drag theP3-17.png block intoPro8-图片15.png block. So you can get the block P3-14.png.


Next, go to the “Logic”, drag out the block P3-20.png into the if statement, and drag out the block P8-5.png from the “Desktop_Car_V3” into the first input box at the left side of “=” and click drop-down triangle to select “center tracking”; drag the Pro8-图片20.png from the “Math” into the second input box at the right side of “=” and change the value to 1 ; like this: P12-4.png.

Drag out the block P12-5.png from “Desktop_Car_V3” into the do statement, and set the value to PWM200.

P12-7.png

Or else, in the case that the center tracking sensor detects white line, if the left tracking sensor detects black line and the right tracking sensor detects white line, the robot will rotate to left at a speed of PWM200; if the left tracking sensor detects white line and the right tracking sensor detects black line, the robot will rotate to right at a speed of PWM200.

Here we can use the condition statement “if...do...else if...do...”

Go to “Control”, drag out the blockPro8-图片14.png, then click the blue gear icon, appear the edit box, drag the P12-9.png block into Pro8-图片15.pngblock. So you can get the block P12-11.pngand then drag this block into the else statement of block .

P12-7.png

Next, go to the “Logic”, drag out the blockP3-20.png, and drag out the block P8-5.pngfrom the “Desktop_Car_V3” into the first input box at the left side of “=”; drag thePro8-图片20.pngfrom the “Math” into the second input box at the right side of “=” and change the value to 1 ; like this:P12-16.png.

We duplicate the block once, and respectively click the drop-down triangle icon to select “right_tracking” and change the value to 0; P8-28.png.

And again go to the Logic”, drag out the block P3-25.png into the if statement; respectively drag the block P12-16.png and P8-28.pnginto the input box of block P3-25.png. Get the block: P12-22.png

Drag out the blockP12-23.png from “Desktop_Car_V3” into the do statement, and set the value to PWM200.
P12-24.png

Next duplicate the block P12-22.png once and drag it into else if statement; change to “left_tracking=0 and right_tracking=1” Drag out the block P12-26.png from “Desktop_Car_V3” into the do statement, and set the value to PWM200.

Till now we have made a piece of code like below:
P12-27.png


In the case that the center tracking sensor detects white line, if the left tracking sensor detects white line and the right tracking sensor detects white line, the robot will stop running; if the left tracking sensor detects black line and the right tracking sensor detects black line, the robot will stop running.

Click the blue gear icon on the left upper corner of if statement, appear the edit box, drag the P3-17.png block beneath else if block P12-31.png. So that the block P12-11.pngwill turn into P12-33.png.
Drag out the block P12-34.pngfrom “Desktop_Car_V3” into the else statement.

Now we’ve written well the program code for line tracking function. Upload the code to see the final effect!
Note: Can’t connect the Bluetooth module when upload the code, otherwise, code upload fails. You should upload the code success, then plug in the Bluetooth module.
441图126.png


Result:
Upload success and turn the slide switch to ON position.
The robot can automatically track black line.
441图127.png


Project 13: IR Remote Control Robot

remote control

Circuit Design:
In the above sections we’ve already introduced the motor drive shield, sensor, module, motors and other elements.
According to the project 7/9 -- library driving motor, infrared receiver,
we’re now ready to give the robot capability - IR Remote Control!
In the infrared receiver section, we have listed out each button encoding of remote control.
In this project code, we can set the button value to control the robot status.


Below is a specific logic table of IR Remote Control robot for reference:
0441图17.png


Build IR Remote Control Robot:
Based on the designed circuit, we are going to build an IR remote control car.
Check the circuit diagram and test code below.

441图129.png

Note: Stack the motor drive shield onto UNO board. Due to IR receiver sensor inputting the digital signal, connect the infrared receiver sensor to P4(G、V、A1) connector on the motor drive shield. Respectively connect the motor A and B to the connector A and B on the motor drive shield. Connect the power to BAT connector.



Coding:
Write the program to make an infrared remote control robot.
Go to “Control”, drag out the P9-1.pngblock; and drag the P9-2.pngblock from “SerialPort” into the “setup” block.

Next, to control the robot by infrared remote control, we first click the imported library “Desktop_Car_V3”, drag out the blockP9-3.png; drag the blockP9-4.png from “SerialPort” into the infrared receiver block just made.

Then go to “Variables”, drag out the block P9-5.png into the block P9-4.png.
P13-7.png

Press the keys to navigate the robot how to run. Here we use the condition statement P13-8.png.
Go to “Control”, drag out the P13-8.pngblock. According to the project 9, we have listed out the string value of each key on infrared remote control.

The front key P13-10.pngis FF629D; back key P13-11.png is FFA857; left key P13-12.png is FF22DD; right key P13-13.png is FFC23D; P13-14.pngis FF02FD; P13-15.pngis FF30CF; P13-16.pngis FF7A85;P13-17.png is FF9867; P13-18.png is FF38C7.

As the command key of IR remote control is hexadecimal code, the front must add 0x.

If ir_rec=0xFF629D, press the keyP9-9.png on the IR remote control, the robot will go front at a speed of PWM150.

Go to the “Logic”, drag out the block P3-20.png into the if statement, and drag out the block P9-16.pngfrom the “Variables” into the first input box at the left side of “=”; drag the Pro8-图片20.pngfrom the “Math” into the second input box at the right side of “=” and type “0xFF629D” , like this:P9-18.png.

Click the imported library “Desktop_Car_V3”, drag out the block P13-25.png into do statement and change to PWM150.
P13-26.png

If ir_rec=0xFFA857, press the key P13-11.pngon the IR remote control, the robot will go back at a speed of PWM150.

Duplicate the block P9-18.pngonce and change “0xFF629D” to “0xFFA857” and drag into the if statement. Click the imported library “Desktop_Car_V3”, drag out the block P13-32.pnginto do statement and change to PWM150.
P13-33.png

At the same way, if ir_rec=0xFF22DD, press the key P13-12.pngon the IR remote control, the robot will turn left.
If ir_rec=0xFFC23D, press the key P13-13.pngon the IR remote control, the robot will turn right.

If ir_rec=0xFF02FD, press the key P13-14.pngon the IR remote control, the robot will stop.
If ir_rec=0xFF30CF, press the key P13-15.pngon the IR remote control, the robot will rotate to left at a speed of PWM100.
If ir_rec=0xFF7A85, press the key P13-16.pngon the IR remote control, the robot will rotate to right at a speed of PWM100.

Duplicate the block P9-18.png five times and respectively change “0xFF629D” to “0xFF22DD”, “0xFFC23D”, “0xFF02FD”, “0xFF30CF”, “0xFF7A85”. Click the '“Desktop_Car_V3”, respectively drag out the block P13-44.png,P13-45.png ,P13-46.png ,P13-47.png and P13-48.pnginto do statement and change to PWM100.


P13-51.png

If ir_rec=0xFF9867, press the key P13-17.png on the IR remote control, the robot will go front at a speed of PWM255.
If ir_rec=0xFF38C7, press the key P13-18.pngon the IR remote control, the robot will turn back at a speed of PWM255.

Duplicate the block P9-18.pngtwice and respectively change “0xFF629D” to “0xFF9867”, “0xFF38C7”.
Click the “Desktop_Car_V3”, respectively drag out the block P13-25.png,P13-32.png and change to PWM250.
P13-57.png


Test Code:
Can’t connect the Bluetooth module when upload the code, otherwise, code upload fails.
You should upload the code successfully, then plug in the Bluetooth module.

Upload the code success, aimed at IR receiver, press the key on the remote control to control the robot.
P13-58.png


Result:
Stack the motor drive shield onto UNO board.Connect the UNO control board to computer’s USB port with USB cable to upload the code.
Upload success and turn the slide switch to ON position.
We can use infrared remote control to randomly give commands to robot car.
441图131.png


Project 14: Bluetooth Controlled Robot

We have built an infrared control smart car. In this project we are going to make a Bluetooth control smart car. Since it is a control smart car, there should be a control terminal and a controlled terminal.
In the course, we use the mobile phone as the console (host), and the HM-10 Bluetooth module (slave) connected to smart car as the controlled terminal.
When using, we need to install an APP on the phone, and connect the HM-10 Bluetooth module, then we use the buttons on the Bluetooth APP to control the smart car to achieve various motion states.

Bluetooth Remote Control

Bluetooth technology is a wireless standard technology that enables short-distance data exchange between fixed devices, mobile devices, and building personal area networks (using UHF radio waves in the ISM band of 2.4 to 2.485 GHz).
The robot kit is equipped with the HM-10 Bluetooth module, which is a master-slave machine. When use as the Host, it can send commands to the slave actively; when use as the Slave, it can only receive commands from the host.
The HM-10 Bluetooth module supports the Bluetooth 4.0 protocol, which not only supports Android mobile, but also supports iOS system.

In the experiment, we default to use the HM-10 Bluetooth module as a Slave and the cellphone as a Host.
We install the Bluetooth APP on the mobile phone, connecting the Bluetooth module; finally use the Bluetooth APP to control the robot car move, or to control the working status of other sensor modules on the robot car.
We provide you with 2 types of mobile APP, for Android and iOS system.

In this project, tap the forward button of the Bluetooth APP to control the buzzer sound. When the Bluetooth APP is successfully connected to the Bluetooth module, press the forward button of the Bluetooth APP, and the buzzer makes a small "click,click,click"; release the button to turn off the buzzer.


Parameters of HM-10 Bluetooth Module:

thumb

  • Bluetooth protocol: Bluetooth Specification V4.0 BLE
  • No byte limit in serial port Transceiving
  • In open environment, realize 100m ultra-distance communication with iphone4s
  • USB protocol: USB V2.0
  • Working frequency: 2.4GHz ISM band
  • Modulation method: GFSK(Gaussian Frequency Shift Keying)
  • Transmission power: -23dbm, -6dbm, 0dbm, 6dbm, can be modified by AT command.
  • Sensitivity: ≤-84dBm at 0.1% BER
  • Transmission rate: Asynchronous: 6K bytes ; Synchronous: 6k Bytes
  • Security feature: Authentication and encryption
  • Supporting service: Central & Peripheral UUID FFE0, FFE1
  • Power consumption: Auto sleep mode, stand by current 400uA~800uA, 8.5mA during transmission.
  • Power supply: 5V DC
  • Working temperature: -5 to +65 Centigrade


Using Bluetooth APP


  • For Android system:
441图132.png

1.Click the Desktop_Car compression package to direct install the Desktop_Car APP; installed well, appear the icon below on your mobile phone:
thumb

Download the Desktop_Car package from the link below:
https://drive.google.com/open?id=1mP3dCQu76xa-C3BbaECfQH0c9AaUFJek

Or you can download the keyestudio Desktop_Car APP direct from the Google Play:
thumb


2.Tap the Desktop_Car icon to enter the Bluetooth APP. As shown below.
thumb
  3.Done uploading the code to UNO R3 board, connect the Bluetooth module, the LED on the Bluetooth module will flash.
Then tap the option CONNECT on the APP, searching the Bluetooth.
thumb

4.Click to connect the Bluetooth. HMSoft connected, Bluetooth LED will turn on normally.

5.First read the character of each key on mobile APP via serial port and know the key function. Click the up button 441图141.pngon the APP, buzzer will make a sound “click, click”; release the button, buzzer will turn off.



For iOS system:
1.Open the APP store
thumb

2.Click to search keyestudio, and you will see the keyes BT car.
thumb

3.Tap to open the keyes BT car
4.To open Bluetooth, click the “Connect” on the upper left corner, searching and connecting Bluetooth.
thumb

5.Tap the Desktop_Car icon to enter the control interface.
thumb

thumb

6.Click the music icon 441图138.png to open the music control interface.
thumb



Wiring Diagram:

thumb
Note: Stack the motor drive shield onto UNO control board. Connect the pin (G、V、S) of power amplifier module to the pin G, 5V, D11(S) of motor drive shield with 3P jumper wire. Plug firmly the Bluetooth module (RXD、TXD、GND、VCC) into the motor drive shield (TX、RX、-(GND)、+(VCC)). Connect the power supply to BAT connector.


Coding:
Write the program to know what signal the Bluetooth module sends.
Go to “Control”, drag out the thumbblock; and drag the thumb block from “SerialPort” into the “setup” block.

We first click the imported library “Desktop_Car_V3”, drag out the block thumb; drag the block Pro9-图片2.png from “SerialPort” into the BLE receiver block just made.
.
Then go to “Variables”, drag out the block thumb into the block thumb.

thumb

Upload the code success, connect Bluetooth module, open serial monitor and set the baud rate to 9600. Aimed at Bluetooth module, press the key on the mobile APP, and you can see the corresponding control character of key.


thumb


Below we have listed each button on the Bluetooth APP and what each button features.
thumb

thumb


We have read the character of each key on mobile APP via serial port and know the key function.

Click the button thumbon the APP, buzzer will make a sound “click, click”; release the button, buzzer will turn off.
Here we call the condition statement block thumb.

Go to “Control”, drag out the blockthumb, then click the blue gear icon, appear the edit box, drag the blockthumb into thumbblock. So you can get the block thumb.

Next, go to the “Logic”, drag out the block thumbinto the if statement, and drag out the block thumbfrom the “Variables” into the first input box at the left side of “=” ; drag the thumbfrom the “Text” into the second input box at the right side of “=”. when press the buttonthumb, mobile Bluetooth will send a character “F” to Bluetooth module, Bluetooth module will receive the character “F”, so we replace the “a” with “F”thumb.

Click the “Desktop_Car_V3”, drag out the block thumb,click the drop-down triangle icon to select the frequency NOTE_A4.
And go to “Control”, drag the delay block thumb; set the delay time 200ms.
Click the “Desktop_Car_V3” again, drag out the block thumb, and add a delay 200ms.
Duplicate this piece of code once and change the final delay time to 1000ms.

thumb


Code:
We’ve written the complete code. Next upload the code success, press and release the button thumbon Bluetooth APP to see the final effect.
Pay close attention that can’t connect the Bluetooth module when upload the code, otherwise, code upload fails.
You should upload the code successfully and then plug in the Bluetooth module.
thumb



Result:
Stack the motor drive shield onto UNO board. Done wiring, connect the UNO control board to computer’s USB port with USB cable to upload the code.
Make sure you have installed the Bluetooth APP on mobile phone. Power on the motor drive shield, Bluetooth indicator flashes and then open mobile APP to connect the Bluetooth module.
Bluetooth connected, press the Up button on the Bluetooth APP, buzzer will make a sound “click, click” ; release the button, buzzer will switch off.



Build Bluetooth Control Robot

We’ve already learned to drive the buzzer speaker sound using the up button 441图141.png on Bluetooth APP.
Based on that, we can extend several buttons to control the motor drive shield and other sensor modules on the robot car.
Now get ready to give the robot car an extra function --Bluetooth Remote Control!

Based on the designed circuit, we are going to build a Bluetooth remote control desktop car.
Follow the wiring diagram and test code below.
thumb

Note: Stack the motor drive shield onto UNO control board. Plug firmly the Bluetooth module (RXD、TXD、GND、VCC) into the motor drive shield (TX、RX、-(GND)、+(VCC)). Connect the motor A and motor B to connector A and B separately. Connect the power supply to BAT connector.


Coding:
Write the program to realize the functions we want. Use the button441图141.png441图148.png441图149.png441图150.png441图151.png441图152.png441图153.png441图154.png and 441图155.png on the Bluetooth APP to navigate the robot go front, back, turn left, turn right, stop, rotate to left, rotate to right, go front at the fastest speed, go back at the fastest speed.

Go to “Control”, drag out the thumbblock; and drag the thumb block from “SerialPort” into the “setup” block.
We first click the imported library “Desktop_Car_V3”, drag out the block thumb; drag the block thumbfrom “SerialPort” into the BLE receiver block just made.

Then go to “Variables”, drag out the block Pro9-图片3.png into the block thumb.
thumb

Press any direction button and stop button, the Bluetooth module will receive corresponding signal, and robot car will move and stop in the corresponding direction.

So here require to make “option” judgement; we will use the block Pro9-图片5.png from “Control”.
Drag out the block Pro9-图片5.png, then click the blue gear icon, appear the edit boxPro9-图片6.png, drag the Pro9-图片7.pngblock into Pro9-图片8.pngblock, turning into Pro9-图片9.png.

When the value of the "expression" behind the switch block is equal to the value of the "constant expression" behind a case block, the statement following this case is executed.
After executing the statement following a case, the process control is transferred to the next case to continue execution.

Since the Bluetooth APP has several direction buttons, press the different direction buttons to make the robot move in the corresponding direction; press stop button, robot car will stop.

So here we set 9 case statements; click the blue gear icon, appear the edit box P15-7.png, drag the Pro9-图片7.pngblock into Pro9-图片8.png block eight times again.

Press the arrow buttons on the APP, robot car will move in the corresponding direction.

Go to the “Variables” , drag out the block P14-17.pnginto the switch statement. Press the button 441图141.png , robot car will go front at a speed of PWM150. Go to “Text” , drag the block Pro9-图片12.png into case statement. Because press the button441图141.png and mobile Bluetooth will send a character “F” to Bluetooth module, Bluetooth module will receive the character “F”, so we replace the “a” with “F”.

Drag the block P15-13.pngfrom the “Desktop_Car_V3” beneath the case statement and change the PWM0 to PWM150.

thumb

Press the button 441图148.png, mobile Bluetooth will send a character “B” to Bluetooth module, Bluetooth module receives the character “B”, robot car will go back at a speed of PWM150.

Go to “Text” , drag the block Pro9-图片12.png into case statement, replacing the “a” with “B”. Drag the block P15-15.png from the “Desktop_Car_V3 beneath the case statement and change the PWM0 to PWM150.

Press the button441图149.png , mobile Bluetooth will send a character “L” to Bluetooth module, Bluetooth module receives the character “L”, robot car will turn left.

Go to “Text” , drag the block Pro9-图片12.pnginto case statement, replacing the “a” with “L”. Drag the block P15-18.png from the “Desktop_Car_V3” beneath the case statement.

Press the button 441图150.png , mobile Bluetooth will send a character “R” to Bluetooth module, Bluetooth module receives the character “R”, robot car will turn right.

Go to “Text” , drag the block Pro9-图片12.pnginto case statement, replacing the “a” with “R”. Drag the block P15-19.png from the “Desktop_Car_V3” beneath the case statement.

Press the button 441图151.png, mobile Bluetooth will send a character “S” to Bluetooth module, Bluetooth module receives the character “S”, robot car will STOP.

Go to “Text” , drag the block Pro9-图片12.png into case statement, replacing the “a” with “S”. Drag the block P15-20.png from the “Desktop_Car_V3”beneath the case statement.

Press the button P15-25.png, mobile Bluetooth will send a character “Q” to Bluetooth module, Bluetooth module receives the character “Q”, robot car will rotate to left.

Go to “Text” , drag the block Pro9-图片12.png into case statement, replacing the “a” with “Q”. Drag the block P15-26.png from the “Desktop_Car_V3”beneath the case statement and change the PWM0 to PWM100.

Press the button P15-32.png, mobile Bluetooth will send a character “E” to Bluetooth module, Bluetooth module receives the character “E”, robot car will rotate to right.

Go to “Text” , drag the block Pro9-图片12.png into case statement, replacing the “a” with “E”. Drag the block P15-27.pngfrom the“Desktop_Car_V3”beneath the case statement and change the PWM0 to PWM100.

Press the button P15-28.png, mobile Bluetooth will send a character “W” to Bluetooth module, Bluetooth module receives the character “W”, robot car will go front at the fastest speed of PWM255.

Go to “Text” , drag the block Pro9-图片12.png into case statement, replacing the “a” with “W”. Drag the block P15-13.png from the“Desktop_Car_V3” beneath the case statement and change the PWM0 to PWM255.

Press the button P15-30.png, mobile Bluetooth will send a character “Z” to Bluetooth module, Bluetooth module receives the character “Z”, robot car will go back at the fastest speed of PWM255.
Go to “Text” , drag the block Pro9-图片12.pnginto case statement, replacing the “a” with “Z”.
0441图21.png


We have completed the program to realize the functions we want. Use the button441图141.png441图148.png441图149.png441图150.png441图151.png441图152.png441图153.png441图154.png and 441图155.png on the Bluetooth APP to navigate the robot go front, back, turn left, turn right, stop, rotate to left, rotate to right, go front at the fastest speed, go back at the fastest speed.


Code:
Note: should upload the code success then plug in Bluetooth module on the tank robot. Otherwise, code upload fails.

thumb



Result:
Stack the motor drive shield onto UNO board. Connect the UNO control board to computer’s USB port with USB cable to upload the code. Turn the slide switch ON.
Make sure you have installed the Bluetooth APP on mobile phone. Power on the motor drive shield, Bluetooth indicator flashes and then open mobile APP to connect the Bluetooth module.
Bluetooth connected, we can use Bluetooth APP to randomly navigate the desktop car.
Press441图141.png , go forward; press441图148.png, go backward; press441图149.png, turn left; press441图150.png, turn right; press441图151.png, stop; press441图152.png, rotate to the left; press441图153.png , rotate to the right; press441图154.png , move forward at the fastest speed; press441图155.png , turn around; press441图156.png, start mobile direction sensing control, and tap again this button to exit the direction sensing control.



Project 15: Bluetooth Multi-function Robot


How to build a multi-function robot combined with all the functions we've learned? In this circuit, we use a complete code to program the smart car to navigate the world on its own. Pretty simple and easy to switch different functions.


Hookup Guide:
Pay attention that can’t connect the Bluetooth module when upload the code, otherwise, code upload fails.
You should upload the code successfully, then plug in the Bluetooth module.


441图157.png

Note: stack the motor drive shield onto UNO control board; connect the line tracking sensor to motor drive shield’s P1 connector (G, V, D6, D7, D8); connect the ultrasonic sensor to motor drive shield’s P2 connector, VCC pin to V, Trig pin to digital 13 (S), Echo pin to digital 12 (S), G pin to GND(G); Connect the pin (G、V、S) of power amplifier module to the pin G, 5V, D11(S) of motor drive shield with 3P female-to-female jumper wire;
Connect the left obstacle detector sensor to P3(G、V、A0) connector on the motor drive shield; the right obstacle detector sensor to P5(G、V、A2) connector; Connect the infrared receiver sensor to P4(G、V、A1) connector on the motor drive shield;
Plug firmly the Bluetooth module (RXD、TXD、GND、VCC) into the motor drive shield (TX、RX、-(GND)、+(VCC)). Connect the motor A and motor B to connector A and B separately. Connect the power supply to BAT connector.


Coding:

Write the program to know what signal the Bluetooth module sends.
Go to “Control”, drag out the thumbblock; and drag the thumbblock from “SerialPort” into the “setup” block.
We first click the imported library “Desktop_Car_V3”, drag out the block thumb; drag the block thumbfrom “SerialPort” into the BLE receiver block just made.
Then go to “Variables”, drag out the block thumb into the block thumb.

thumb

Press any direction button and stop button, the Bluetooth module will receive corresponding signal, and robot car will move and stop in the corresponding direction.

So here require to make “option” judgement; we will use the block Pro9-图片5.png from “Control”.
Drag out the block Pro9-图片5.png, then click the blue gear icon, appear the edit boxPro9-图片6.png, drag the Pro9-图片7.pngblock into Pro9-图片8.pngblock, turning into Pro9-图片9.png.

When the value of the "expression" behind the switch block is equal to the value of the "constant expression" behind a case block, the statement following this case is executed.
After executing the statement following a case, the process control is transferred to the next case to continue execution.

Since the Bluetooth APP has several direction buttons, stop button, and button for obstacle avoiding, line tracking, ultrasonic follow function, press the different direction buttons to make the robot move in the corresponding direction and start different functions.

So here we set 12 case statements; click the blue gear icon, appear the edit box P15-7.png, drag the Pro9-图片7.png block into Pro9-图片8.png block eleven times again.

Press the arrow buttons on the APP, robot car will move in the corresponding direction.

Go to the “Variables” , drag out the block P14-17.pnginto the switch statement.
Press the button 441图141.png , robot car will go front at a speed of PWM200. Go to “Text” , drag the block Pro9-图片12.pnginto case statement. Because press the button441图141.png and mobile Bluetooth will send a character “F” to Bluetooth module, Bluetooth module will receive the character “F”, so we replace the “a” with “F”.

Drag the block P15-13.pngfrom the “Desktop_Car_V3” beneath the case statement and change the PWM0 to PWM200.

Press the button 441图148.png, mobile Bluetooth will send a character “B” to Bluetooth module, Bluetooth module receives the character “B”, robot car will go back at a speed of PWM200.

Go to “Text” , drag the block Pro9-图片12.png into case statement, replacing the “a” with “B”. Drag the block P15-15.png from the “Desktop_Car_V3” beneath the case statement and change the PWM0 to PWM200.

Press the button441图149.png , mobile Bluetooth will send a character “L” to Bluetooth module, Bluetooth module receives the character “L”, robot car will turn left.

Go to “Text” , drag the block Pro9-图片12.pnginto case statement, replacing the “a” with “L”. Drag the block P15-18.png from the “Desktop_Car_V3” beneath the case statement.

Press the button 441图150.png , mobile Bluetooth will send a character “R” to Bluetooth module, Bluetooth module receives the character “R”, robot car will turn right.

Go to “Text” , drag the block Pro9-图片12.png into case statement, replacing the “a” with “R”. Drag the block P15-19.png from the “Desktop_Car_V3” beneath the case statement.

Press the button 441图151.png, mobile Bluetooth will send a character “S” to Bluetooth module, Bluetooth module receives the character “S”, robot car will STOP.

Go to “Text” , drag the block Pro9-图片12.pnginto case statement, replacing the “a” with “S”. Drag the block P15-20.png from the “Desktop_Car_V3” beneath the case statement.
Because Bluetooth module receives a character “S”, buzzer in the power amplifier module will make a sound, so here we need to drag the block P15-23.png from “Desktop_Car_V3” to turn off buzzer.

P15-24.png

Press the button P15-25.png, mobile Bluetooth will send a character “Q” to Bluetooth module, Bluetooth module receives the character “Q”, robot car will rotate to left.

Go to “Text” , drag the block Pro9-图片12.png into case statement, replacing the “a” with “Q”. Drag the block P15-26.png from the “Desktop_Car_V3”beneath the case statement and change the PWM0 to PWM200.

Press the button P15-32.png, mobile Bluetooth will send a character “E” to Bluetooth module, Bluetooth module receives the character “E”, robot car will rotate to right.

Go to “Text” , drag the block Pro9-图片12.png into case statement, replacing the “a” with “E”. Drag the block P15-27.pngfrom the “Desktop_Car_V3” beneath the case statement and change the PWM0 to PWM200.

Press the button P15-28.png, mobile Bluetooth will send a character “W” to Bluetooth module, Bluetooth module receives the character “W”, robot car will go front at the fastest speed of PWM255.

Go to “Text” , drag the block Pro9-图片12.pnginto case statement, replacing the “a” with “W”. Drag the block P15-13.png from the “Desktop_Car_V3” beneath the case statement and change the PWM0 to PWM255.

Press the button P15-30.png, mobile Bluetooth will send a character “Z” to Bluetooth module, Bluetooth module receives the character “Z”, robot car will go back at the fastest speed of PWM255.
Go to “Text” , drag the block Pro9-图片12.pnginto case statement, replacing the “a” with “Z”.
P15-31.png

Then write the program for robot backward. Go to “Functions”, we drag out the block Pro10-图片26.png and name it as P15-34.png. Go to “Desktop_Car_V3”, drag out the block P15-13.pnginto the block P15-34.pngand delay 200ms; followed by drag out the P15-26.png and add a delay 700ms; then duplicate the block P15-13.png once and change the PWM0 to PWM255.

And go to “Variables” , we drag out the block P15-39.png, and drag out the block P15-40.pngfrom “Math” into the block P15-39.pngand assign the value 0.

In such way, the robot can move back once.

P15-42.png
Again go to “Functions”, we drag out the block P15-43.pnginto the main program case ‘Z

We have completed the program to realize the functions that use the button 441图141.png441图148.png441图149.png441图150.png441图151.pngP15-65.pngP15-68.pngP15-66.png and P15-67.pngon the Bluetooth APP to navigate the robot go front, back, turn left, turn right, stop, rotate to left, rotate to right, go front at the fastest speed, go back at the fastest speed.

P15-44.png

Next write the program for ultrasonic follow, line tracking, obstacle avoiding function.
Click the button P15-45.png, mobile Bluetooth will send a character “U” to Bluetooth module, Bluetooth module receives the character “U”, the robot car will start ultrasonic following function.

Go to “Text” , drag the block Pro9-图片12.png into case statement, replacing the “a” with “U”.

Move on to write the code string for ultrasonic following.

Go to “Functions”, we drag out the block Pro10-图片26.png and name it as P15-47.png.

Here need to set up a variable.

First go to “Variables”, drag out the block Pro10-图片28.png; then drag the block Pro10-图片29.pngfrom “Math” into value behind; replace “item” with “flag”, and default as an integer, assign the variable “flag” to 0.
Then drag this block Pro10-图片30.pnginto Pro10-图片31.pngblock, so that set up a variable block Pro10-图片32.png.
Now go to “Variables, drag out the block Pro10-图片33.pnginto the block P15-47.pngand assign a value 0.

Go to “Control”, drag out the block Pro10-图片35.pnginto the block Pro10-图片76.png. Go to “Logic”, drag the block Pro10-图片37.png into the repeat while block Pro10-图片35.png. And drag the variable block Pro10-图片32.pnginto the first input box of block Pro10-图片37.png; drag a block Pro10-图片29.pnginto the second input box of block Pro10-图片37.pngand keep the digit 0.

P15-78.png

Followed by set up three variables, “distance”, “sensor_L”and “sensor_R”.

The variable “distance” means save the distance value measured by ultrasonic sensor; “sensor_L”and “sensor_R” respectively save the distance value detected by the left and the right obstacle detector sensor.

Click “Variables”, drag out the block P10-1.png; and drag the block P10-2.pngfrom “Math” into the block P10-1.png. Then duplicate the block P10-4.png twice; respectively change “item”into“distance”、“sensor_L”and“sensor_R”; set the value to 0.
P15-84.png

Click “Variables”, drag out the block P10-6.pnginto the do block P15-86.png; and drag the block P10-8.png from “Desktop_Car_V3” into the block P10-6.png.

And click “Variables” again, drag out the block P15-89.png, and go to drag the block P10-10.pngfrom “Desktop_Car_V3”into the block P15-89.png; then duplicate this code string once, change“sensor_L”to“sensor_R”, click the drop-down triangle to select“right_infrared_avoid”.
P15-100.png

Next judge whether the ultrasonic sensor detects front obstacle or the left and the right obstacle detector module detects obstacle. Here we can use the judgement statement “if...do...else if...do...” .

When the front obstacle distance detected by ultrasonic sensor is smaller than or equal to 5cm, and both obstacle detector modules detect obstacle, the robot will go back at a speed of PWM200.

Go to “Control”, drag out the block Pro8-图片14.png, then click the blue gear icon, appear the edit box, drag the P10-14.png block into Pro8-图片15.png block five times. So you can get the block:
P10-16.png

Next, go to “Logic”, drag out the block P3-20.pngand change“=”into“≤”; go to “Variables”, drag out the block Pro8-图片13.png into the first input box at the left side of “≤”; drag the Pro8-图片20.pngfrom the “Math” into the second input box at the right side of “≤” ; change the value 0 to 5; like this: P10-20.png(note the value 5 can be set flexibly).

We duplicate the distance block twice, and change the variable Pro8-图片13.png to P15-003.png, P15-004.png, and change the“≤”into“=”;

We have mentioned before that the obstacle detector module detects obstacle, output LOW digital signal 0; detects no obstacle, output HIGH digital signal 1. So here change the value 5 to 0. like this: P15-005.png,P15-006.png

Think back, we use the two words “or”, “and ” when describe the judgement statement. There are two blocks in “Logic” to represent either one of two conditions happens or both of them happen at the same time, that is P10-23.png,P10-24.png.

Drag out the block P10-24.png and click the drop-down triangle to select “or” and can get the block P10-23.png .
Drag the block P10-24.pnginto block P10-23.png to make as P10-29.png.

Then drag the block P10-20.pnginto the first input box of or block P10-29.png; drag the block P15-005.png and P15-006.pngrespectively into the input box of and block P10-29.png. So can get the block P15-94.png and drag it behind to the if statement.

This means the judgment condition is when the centre obstacle distance detected by ultrasonic sensor is smaller than or equal to 5cm, or both the left and the right obstacle detector module detect obstacle.

Followed by drag out the block P10-34.pngfrom “Desktop_Car_V3” into the do statement; change the PWM0 into PWM200.

Now we have finished the program. When the front obstacle distance detected by ultrasonic sensor is smaller than or equal to 5cm, and both obstacle detector modules detect obstacle, the robot will go back at a speed of PWM200.
P15-92.png

We now move on to write the program. When the front obstacle distance detected by ultrasonic sensor is greater than 5cm, and the left obstacle detector module detects obstacle and the right one didn’t detect obstacle, the robot will rotate to left at a speed of PWM200.

Duplicate the block P15-94.pngonce and drag into the first else if statement; set to distance>5 and val_L=0 and val_R=1
P15-90.png

Drag out the block P10-38.pngfrom “Desktop_Car_V3” into the do statement; change the PWM0 into PWM200.

Next write the program that the front obstacle distance detected by ultrasonic sensor is greater than 5cm, and the left obstacle detector module didn’t detect obstacle and the right one detects obstacle, the robot will rotate to right at a speed of PWM200.

Duplicate the block P15-90.pngonce and drag into the second else if statement; set to distance>5 and val_L=1 and val_R=0
P15-61.png

Drag out the block P10-42.png from“Desktop_Car_V3”into the do statement; change the PWM0 into PWM200.
P15-63.png

Move on to write the program. When the front obstacle distance detected by ultrasonic sensor is greater than 5cm, and smaller than or equal to 15cm, and both obstacle detector modules didn’t detect obstacle, the robot will stop running.

Go to “Logic”, drag out the block P10-44.png; duplicate the block P10-45.pngonce and set to “distance≤15”; then separately drag the block P10-45.pngand P10-47.pnginto the input box of block P10-44.png, like this: P10-49.png.

And again drag out the block P10-44.png, then drag the block P10-49.pnginto the first input box of block P10-44.png; duplicate the block P10-53.png once and drag it into the second input box of block P10-44.pngand change “val_R=0” into “val_R=1”.

P10-55.png

Now drag the block P10-55.pnginto the third else if statement. Drag out the block P10-61.png from “Desktop_Car_V3” into the do statement.

Next it’s easy to write the program. When the front obstacle distance detected by ultrasonic sensor is greater than 15cm, and smaller than or equal to 35cm, and both obstacle detector modules didn’t detect obstacle, the robot will go front at a speed of PWM255.

Direct duplicate the block P10-55.png once and change to distance>15 and distance≤35, and drag it into the fourth else if statement.
Drag out the block P10-63.png from“Desktop_Car_V3” into the do statement, and set the value to 255.


Finally write the program. When the front obstacle distance detected by ultrasonic sensor is greater than 35cm, and both obstacle detector modules didn’t detect obstacle, the robot will stop running.

Duplicate the code block P15-61.png once and drag it into the fifth else if statement. Change to distance>35 and val_L=1 and val_R=1
Drag out the block P10-61.pngfrom“Desktop_Car_V3” into the do statement.
P15-113.png

Click the button 441图151.png, the robot car will exit the ultrasonic follow function.
We drag out the block Pro9-图片1.pnginto the repeat while block P15-117.png .
Then go to “Control”, drag out the block Pro8-图片14.pnginto the block Pro9-图片1.png. And drag out the block Pro10-图片37.pngfrom “Math” into the if statement. br>

Then drag the block Pro10-图片022.pngfrom the “Variables” into the first input box of Pro10-图片37.png; drag the block Pro10-图片023.png from the “Text” into the second input box of Pro10-图片37.pngand change the “a” into “S”.

Go to “Variables”,drag out the block Pro10-图片33.png into the do block Pro10-图片026.png; and drag out the block Pro10-图片29.pngfrom “Math” into the block Pro10-图片33.pngand change value 0 into 1.
P15-129.png


Finally, go to “Functions”, we drag out the block P15-1-1.pnginto the main program case ‘U’
P15-1-2.png

Now the program for following robot is finished!

Move on to write the code string for line tracking function.
Press the button P15-1-3.png, mobile Bluetooth will send a character “X” to Bluetooth module, Bluetooth module receives the character “X”, robot car will go front at the fastest speed of PWM255.

Go to “Text” , drag the block Pro9-图片12.pnginto case statement, replacing the “a” with “X”.
Then write the program for robot line tracking. Go to “Functions”, we drag out the block Pro10-图片26.pngand name it asP15-1-6.png.

Go to “Variables” , drag out the block Pro10-图片33.pnginto the block P15-1-6.png and assign a value 0.
Go to “Control”, drag out the block Pro10-图片35.pnginto the block P15-1-6.png.
Go to “Logic”, drag the block Pro10-图片37.png into the repeat while block Pro10-图片35.png.
And drag the variable block Pro10-图片33.pnginto the first input box of block Pro10-图片37.png; drag a block Pro10-图片29.pnginto the second input box of block Pro10-图片37.pngand keep the digit 0.
P15-1-11.png

Followed by set up three variables, “val_L”, “val_C” and “val_R” ;

These three variables respectively save the obstacle signal measured by the left, middle and the right line tracking sensor.

Click “Variables”, drag out the block P10-1.png; and drag the block Pro10-图片29.pngfrom “Math” into the block P10-1.png. Then duplicate the block P10-4.pngtwice; respectively change “item”into“val_L”, “val_C” and “val_R”.
P15-1-16.png

Click “Variables”, drag out the block P15-1-17.pnginto the do block P15-86.png; and drag the block P15-1-19.png from “Desktop_Car_V3”into the block P15-1-17.png, like this P15-1-21.png;
then duplicate this code string twice, change“val_L”to“val_C” and “val_R”, click the drop-down triangle to select “center_tracking” and “right_tracking”.

P15-1-22.png

The line tracking sensor detects white, output LOW 0; detecting black, output HIGH 1.

To judge whether the left, the center and the right tracking sensor detect black line, if the center tracking sensor detects black line, the robot will go front at a speed of PWM200.

Here we can use the condition statement P3-13.png or P3-14.png.
But the block P3-14.pngis more efficient than P5-13.png.

Go to “Control”, drag out the block Pro8-图片14.png into the block P15-1-35.png, then click the blue gear icon, appear the edit box, drag the blockP3-17.png into Pro8-图片15.pngblock. So you can get the blockP3-14.png .

Next, go to the “Logic”, drag out the block P3-20.pnginto the if statement, and drag out the block P15-1-40.pngfrom the “Variables” into the first input box at the left side of “=” ; drag thePro8-图片20.png from the “Math” into the second input box at the right side of “=” and change the value to 1 ;

Drag out the block P12-5.pngfrom “Desktop_Car_V3”into the do statement, and set the value to PWM200.
P15-1-43.png

Or else, in the case that the center tracking sensor detects white line, if the left tracking sensor detects black line and the right tracking sensor detects white line, the robot will rotate to left at a speed of PWM200; if the left tracking sensor detects white line and the right tracking sensor detects black line, the robot will rotate to right at a speed of PWM200.

Here we can use the condition statement “if...do...else if...do...else”

Go to “Control”, drag out the block Pro8-图片14.png, then click the blue gear icon, appear the edit box, drag the P12-9.png block into Pro8-图片15.png block then drag the P3-17.png beneath the block P12-31.png. So you can get the block P12-33.png; then drag this block beneath the else block P15-1-50.png.

Next, go to the “Logic”, drag out the block P3-20.png, and drag out the block P15-1-52.png from the “Variables” into the first input box at the left side of “=” ; drag the Pro8-图片20.png from the “Math” into the second input box at the right side of “=” and change the value to 1 ; like this: P15-1-54.png

We duplicate this block P15-1-54.png once, and change to P15-1-56.pngand change the value to 0.P15-1-57.png

And again go to the “Logic”, drag out the block P3-25.png into the if statement; respectively drag the block P15-1-54.png and P15-1-57.png into the input box of block P3-25.png.
P15-1-62.png

Drag out the block P12-23.png from“Desktop_Car_V3” into the do statement, and set the value to PWM200.
Next duplicate the block P15-1-62.pngonce and drag it into else if statement; change to “val_L=0 and val_R=1”
P15-207.png

Drag out the block P12-26.pngfrom“Desktop_Car_V3”into the do statement, and set the value to PWM200.
P15-209.png

In the case that the center tracking sensor detects white line, if the left tracking sensor detects white line and the right tracking sensor detects white line, the robot will stop running; if the left tracking sensor detects black line and the right tracking sensor detects black line, the robot will stop running.

Drag out the block P12-34.pngfrom“Desktop_Car_V3”into the else statement.

Click the button 441图151.png , the robot car will exit the line tracking function.
We drag out the block Pro9-图片1.pnginto the repeat while block P15-117.png .

Then go to “Control”, drag out the block Pro8-图片14.pnginto the block Pro9-图片1.png. And drag out the block Pro10-图片37.pngfrom “Math” into the if statement. Then drag the block Pro10-图片022.pngfrom the “Variables” into the first input box of Pro10-图片37.png; drag the block Pro10-图片023.png from the “Text” into the second input box of Pro10-图片37.pngand change the “a” into “S”.

Go to “Variables”,drag out the block Pro10-图片33.png into the do block ; and drag out the block Pro10-图片29.png from “Math” into the block Pro10-图片33.pngand change value 0 into 1.

P15-220.png

Finally,go to “Functions”, we drag out the block P15-221.png into the main program case ‘X’ P15-222.png
Now the program for line tracking robot is finished!

Move on to write the code string for obstacle avoiding function.

Press the button P15-1-3.png, mobile Bluetooth will send a character “Y” to Bluetooth module, Bluetooth module receives the character “Y”, robot car will go front at the fastest speed of PWM255.

Go to “Text” , drag the block Pro9-图片12.pnginto case statement, replacing the “a” with “Y”.
Then write the program for robot avoiding obstacle. Go to “Functions”, we drag out the block Pro10-图片26.pngand name it as P15-226.png .
Go to “Variables”, drag out the block Pro10-图片33.pnginto the block P15-226.pngand assign a value 0.
Go to “Control”, drag out the block Pro10-图片35.pnginto the block P15-226.png.

Go to “Logic”, drag the block Pro10-图片37.png into the repeat while block Pro10-图片35.png.
And drag the variable block Pro10-图片32.png into the first input box of block Pro10-图片37.png; drag a block Pro10-图片29.pnginto the second input box of block Pro10-图片37.pngand keep the digit 0.
P15-337.png

Click “Variables”, drag out the block P10-6.png into the block P15-86.png; and drag the block P10-8.png from“Desktop_Car_V3” into the block P10-6.png.
And again click “Variables”, drag out the block P15-89.png; go to drag the block P10-10.pngfrom “Desktop_Car_V3”into the block P15-89.png; then duplicate this code string once, change“sensor_L”to“sensor_R”, click the drop-down triangle to select“right_infrared_avoid”.
P15-345.png

Next judge whether the ultrasonic sensor detects front obstacle or the left and the right obstacle detector module detects obstacle. Here we can use the judgement statement “if...do...else if...do...”

First write the program. No matter how far the front obstacle detected by ultrasonic sensor, as long as both the left and the right obstacle detector module detect obstacle, the robot will turn back for one second at a speed of PWM150, and then turn left for 0.5 second at a speed of PWM200.

Go to “Control”, drag and drop the block Pro8-图片14.pnginto the do statement of block P15-86.png, then click the blue gear icon, appear the edit box, drag the P10-14.png block into Pro8-图片15.pngblock four times. So you can get the block:
P11-16.png

Next, go to “Logic”, drag the block P3-20.png; go to “Variables”, drag out the block P11-22.png into the first input box at the left side of “=”; drag the Pro8-图片20.pngfrom the “Math” into the second input box at the right side of “=” ; change the value 0 to 5; like this:P11-24.png.
We duplicate this block once, and change the variable P11-22.png to P11-26.png; like this:P10-22.png
And again go to “Logic”, drag the block P11-28.pnginto the if statement, then drag the block P11-24.pngand P10-22.png into the input box of block P11-28.png.
P11-32.png

Drag out the block P15-362.png from “Desktop_Car_V3”into the do statement, and set the value to PWM150. And add a delay block in 1000ms.
Then drag out the block P15-363.pnginto the do statement, and set the value to PWM200. And add a delay block in 500ms.
Till now we have made a piece of code like below:
P15-364.png

We now move on to write the program. No matter how far the front obstacle detected by ultrasonic sensor, the left obstacle detector module didn’t detect obstacle and the right one detects obstacle, the robot will rotate to left at a speed of PWM200; the left obstacle detector module detects obstacle and the right one didn’t detect obstacle, the robot will rotate to right at a speed of PWM200.

Duplicate the block P11-32.png once and drag into the first else if statement; set to val_L=1 and val_R=0
Drag out the block P10-38.pngfrom “Desktop_Car_V3” into the do statement; change the PWM0 into PWM200.
Duplicate the block P11-32.pngonce again and drag into the second else if statement; set to val_L=0 and val_R=1

Drag out the block P11-36.png from“Desktop_Car_V3” into the do statement; change the PWM0 into PWM200.
P15-369.png

We continue to write the program. When the front obstacle distance detected by ultrasonic sensor is smaller than or equal to 10cm, and both obstacle detector modules didn’t detect obstacle, the robot will rotate to right at a speed of PWM200. When the front obstacle distance detected by ultrasonic sensor is greater than 10cm, and the left obstacle detector module detects obstacle and the right one didn’t detect obstacle, the robot will go front at a speed of PWM200.

go to “Logic”, drag the block P3-20.pngand select“≤”; go to “Variables”, drag out the block Pro8-图片13.pnginto the first input box at the left side of “≤”; drag the Pro8-图片20.pngfrom the “Math” into the second input box at the right side of “≤” ; change the value 0 to 10; like this:P11-43.png

Drag out the block P10-24.png into the third else if statement; drag the block P11-43.pnginto the first input box of block P10-24.png; duplicate the block P11-47.pngonce and drag it into the second input box of block P10-24.png; set to val_L=1 and val_R=1
P11-49.png
Drag out the block P11-36.png from “Desktop_Car_V3”into the do statement; change the PWM0 into PWM200.

Then duplicate the block P11-49.pngonce and drag it into the fourth else if statement; change to distance>10, and drag out the block P11-52.pnginto the do statement; change the PWM0 into PWM200.
P15-383.png

Click the button 441图151.png, the robot car will exit the line tracking function.
We drag out the block Pro9-图片1.pnginto the repeat while block P15-117.png.
Then go to “Control”, drag out the block Pro8-图片14.pnginto the block Pro9-图片1.png. And drag out the block Pro10-图片37.pngfrom “Math” into the if statement. Then drag the block Pro10-图片022.pngfrom the “Variables” into the first input box of Pro10-图片37.png ; drag the block Pro10-图片023.png from the “Text” into the second input box of Pro10-图片37.pngand change the “a” into “S”.

Go to “Variables”,drag out the block Pro10-图片33.png into the do block Pro10-图片026.png; and drag out the block Pro10-图片29.pngfrom “Math” into the block Pro10-图片33.pngand change value 0 into 1.
P15-399.png

Finally, go to “Functions”, we drag out the block P15-400.pnginto the main program case ‘Y
Now the program for obstacle avoiding robot is finished!
P15-401.png


Move on to make the buzzer in power amplifier module play a tune “do re mi fa so la si do” and then play a specific song.

Press the button 441图138.pngon the APP to enter the music interface; press the button P15-403.png, mobile Bluetooth will send a character “1” to Bluetooth module, Bluetooth module receives the character “1”, buzzer will play a tone of NOTE_C4.

For the main program, go to “Text” , drag the block Pro9-图片12.pnginto case statement, replacing the “a” with “1”.
Click the“Desktop_Car_V3”, drag out the block P4-3.png,click the drop-down triangle icon to select the frequency NOTE_C4.

Duplicate this piece of code seven times and click the drop-down triangle icon to separately select the frequency NOTE_D4、NOTE_E4、NOTE_F4、NOTE_G4、NOTE_A4、NOTE_B4、NOTE_C5.
P15-406.png

Press the button P15-407.png, Bluetooth module sends the character “2”, buzzer will play a tone of NOTE_D4.
Press the buttonP15-408.png, Bluetooth module sends the character “3”, buzzer will play a tone of NOTE_E4.
Press the buttonP15-409.png Bluetooth module sends the character “4”, buzzer will play a tone of NOTE_F4.
Press the buttonP15-410.png, Bluetooth module sends the character “5”, buzzer will play a tone of NOTE_G4.
Press the buttonP15-411.png, Bluetooth module sends the character “6”, buzzer will play a tone of NOTE_A4.
Press the buttonP15-412.png, Bluetooth module sends the character “8”, buzzer will play a tone of NOTE_C5.
Press the buttonP15-413.png, Bluetooth module sends the character “P”, buzzer will play a song HAPPY BIRTHDAY.
Press the buttonP15-620.png Bluetooth module sends the character “P”, buzzer will play a song HAPPY BIRTHDAY.

Here we need to set up the variable“mus”to represent the number of button P15-620.pngpressed.

Click “Variables”, drag out the block P10-1.png; and drag the blockPro10-图片29.pngfrom “Math”into the blockP10-1.png.
And change“item”into“mus”; set the value to 0.

Go to “Control”, we drag out the block Pro10-图片35.pnginto the main program case ‘P’
And drag out the block Pro10-图片37.pngfrom “Math” into the while statement. Then drag the block P15-604.pngfrom the “Variables” into the first input box of block Pro10-图片37.png; drag the block Pro10-图片29.png from the “Math” into the second input box of block Pro10-图片37.png; change the “0” into “1” and change the“=”into “<” .
P15-609.png

Go to “Desktop_Car_V3”,drag out the block P15-610.png into the block P15-611.png; click the drop-down triangle to select the birthday song; and drag out the block P15-612.png beneath the birthday block.
Then drag out the block P15-613.pngfrom the “Variables” beneath the block P15-612.png; drag out the block P15-615.pngfrom the “Math” into the blockP15-613.png.
And again drag and drop the block P15-604.png into the input box at the left side of“+”, replacing 1; the right side value 1 remains the same.

So can get this block 618.png and drag this block beneath the No Tone block.
P15-619.png

To make the number of buttonP15-620.png pressed return 0, we go to drag out the block P15-613.png into the main program case ‘P’ and assign value 0.
P15-622.png

Now we’ve written well the program code for multiple functions. Upload the code to see the final effect!

Note: Can’t connect the Bluetooth module when upload the code, otherwise, code upload fails. You should upload the code success, then plug in the Bluetooth module.
P15-625.png



Result:
Stack the motor drive shield onto UNO board. Connect the UNO control board to computer’s USB port with USB cable to upload the code.
Turn the slide switch ON.
Connected Bluetooth module, we can use Bluetooth APP to navigate the desktop car by clicking the different buttons on the APP.
Tap Stop button to pause the function.
Note: the Android Bluetooth APP CANNOT realize the voice control441图160.png.

441图159.png



Resources

  • Get all the resources here:

https://1drv.ms/u/s!ArhgRvK6-RyJcxRRe3SxqYE_X3I?e=fJHrQl

  • Video:

http://video.keyestudio.com/ks0441/



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 and development, production and marketing.

Keyestudio is a best-selling brand owned by KEYES Corporation, our product lines range 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.
Welcome check more contents from our official website:
http://www.keyestudio.com

Logo图片1.png


For more details of our products, you can check it from the links below.


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: [email protected]
Thank you in advance.



Buy From