Ks0364 keyestudio Smart Little Turtle Robot V2.0: Difference between revisions

From Keyestudio Wiki
Jump to navigation Jump to search
Line 1,445: Line 1,445:
<br>
<br>


===='''3) Turtle Robot Avoiding Obstacles'''====
==== 3) Turtle Robot Avoiding Obstacles ====
'''Description:''' <br>
'''Description:''' <br>
It is rather not suitable for human to work in some relatively harsh environments. At this moment, if we have a robot that can shuttle freely in such environments, then how good should it be! <br>
It is rather not suitable for human to work in some relatively harsh environments. At this moment, if we have a robot that can shuttle freely in such environments, then how good should it be! <br>

Revision as of 11:40, 12 October 2018

Keyestudio Smart Little Turtle Robot V2.0


Intorduction

When you tell your child how beautiful the world is, it is better to take him directly to feel it; when the child asks you why the small alarm clock always sings, it is better to open the alarm clock and explore the secret of the sound with your child. When your child has a desire for a robot gift, you might do it with him as well! Now with ARDUINO, everything is impossible.
Arduino is a convenient, flexible and easy-to-use open source electronic prototyping platform, which is suitable for entry-level developers who are new to hardware. Now our keyestudio team has upgraded the turtle robot based on the turtle generation1, adding some more interesting features. Let you enjoy the DIY fun and programming while learning with your child.

keyestudio Smart Little Turtle V2.0 is an enhanced kit based on easy-to-use and flexible Arduino platform. You are able to learn how to get started with both Arduino platform and Mixly block coding.
We provide you with complete tutorials of Arduino programming language and Mixly Graphical program to control the smart turtle robot, achieving the functions of line tracking, automatic obstacle avoidance, Bluetooth control and infrared remote control.
Furthermore, it adds a 8*8 matrix that can show you the running states of robot. The wiring for the turtle robot is more simple.You can easily build the robot with a little or even no programming experience.


Parameters

  • Motor’svoltage range: 1-6V; motor shaft length: 10mm; speed: 6.0V 100rpm/min.
  • Motor control is driven by L298P.
  • Three groups of line tracking modules, to detect black-white line with higher accuracy and can also be used for anti-fall control.
  • Ultrasonic module is used to detect whether there is obstacles or not.
  • Bluetooth wireless modulecan be paired with Bluetooth device on phone to remotely control the turtle robot.
  • Infrared receiver modulematches with an infrared remote control to control the turtle robot.
  • Add a 8*8 dot matrix module, showing the robot states.
  • Can access to the external voltage 6~ 12V


Component List

When get this turtle robot kit, at first glance, you will see the beautiful big packaging box. And each component is tidily packed inside the small box. What components you should get to build the robot? We have listed all the components as follows:

No. Component Quantity Picture
1 Keyestudio UNO R3 Main Board 1
thumb
2 Keyestudio quick connectors motor driver shield 1
thumb
3 Keyestudio quick connectors IR receiver module 1
thumb
4 Keyestudio quick connectors line tracking sensor 1
thumb
5 Keyestudio quick connectors 12FN20 motor A 1
thumb
6 Keyestudio quick connectors 12FN20 motor B 1
thumb
7 Keyestudio 8x8 Dot Matrix Module 1
thumb
8 Keyestudio quick connectors ultrasonic module 1
thumb
9 keyestudio Bluetooth HC-06 1
thumb
10 Keyestudio JMFP-4 17-button 86*40*6.5MM yellow (eco-friendly) (no battery) 1
thumb
11 Double-Connector JST-PH2.0MM-5P 24AWG blue-green-yellow-red-black wire 15CM (reverse direction) 1
thumb
12 Double-Connector JST-PH2.0MM-4P 24AWG green-yellow-red-black wire 8CM (reverse direction) 1
thumb
13 Double-Connector JST-PH2.0MM-3P 24AWG yellow-red-black wire 8CM (reverse direction) 1
thumb
14 Double-Connector JST-PH2.0MM-2P 24AWG red-black wire 160mm 2
thumb
thumb
15 Battery holder with JST-PH2.0MM-2P socket lead, black-red lead length 150mm 1
thumb
16 4-cell AA battery case 1
thumb
17 Screw M2*12MM round head 4
thumbthumbthumbthumb
18 Nut M2 nickle plating 4
thumbthumbthumbthumb
19 Screw M3*6MM round head 27
thumb thumb thumb thumb thumb thumb thumb thumb thumb
20 Screw M3*6MM flat head 2
thumb
21 Nut M3 nickle plating 5
thumb thumbthumbthumbthumb
22 Dual-pass M3*10MM Copper Pillar 8
thumb thumb thumb thumbthumb thumb thumb thumb
23 Dual-pass M3*40MM Copper Pillar 4
thumb thumb thumb thumb
24 keyestudio 9G black servo 180° with mounts 1
thumb
25 N20 motor wheel 2
thumbthumb
26 White U-type motor holder 2
thumb
27 Black plastic platform 1
thumb
28 3PI miniQ universal caster 304 stainless steel 1
thumb
29 Black-yellow Handle 3*40MM cross screwdriver 1
thumb
30 1m USB cable 1
thumb
31 Black cable ties 3*100MM 5
thumb thumb thumb thumb thumb
32 Robot bottom PCB 1
thumb
33 Robot TOP PCB 1
thumb
34 female-female jumper wire 1
thumb
35 keyestudio White Piranha LED Module 1
thumb
36 3Pin female header jumper wire length 20CM 2.54mm 1
thumb


Assembly Guide

When all the components have been counted well, cannot wait to assemble it? Follow the assembly steps here to build your own robot.
(1) How to get started with? Begin with the bottom parts.
Firstly, you should prepare the components as follows:

  • M3*6MM round-head screw *2
  • Nut M3 nickle plating *2
  • Bottom PCB*1
  • Tracking sensor *1
  • Universal caster *2


thumb
Insert two M3*6MM round-head screw into the tracking sensor, then tighten two M3 Nuts to the screws.
thumb

Back view:
thumb

Then fix the two universal casters to the bottom PCB board.
thumb
Well done as below:
thumb


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

  • U-type holder* 2
  • M2*12MM round-head screws *4
  • M2 Nut *4
  • Motor *2


thumb
Firstly place four M2 Nuts inside the holes of white N20 motor holders. You should get it as below.
thumb

thumb

Then place the white holders onto the motors.
thumb
After that, fix these two motor connectors on the bottom PCB with four M2*12MM round-head screws.
thumb
Back view:
thumb


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

  • wheel *2


thumb
Directly plug the two yellow wheels into the motor shaft. You get it as below.
thumb


(4) Completed the above assembly, let's install the battery case. You should get all the installed parts as below.

  • M3*6MM round-head screws *2
  • M3 Nut *2
  • Battery case *1
  • 18650 Batteries ( not included) *2


thumb

We have provided you with two kinds of battery case. Here we install the 18650 2-cell battery case for the robot. So we will take the turtle robot installed with 18650 battery case as example to start the following project sections.
Firstly you can install the 2-cell AA battery case to the bottom PCB with two M3*6MM round-head screws and M3 nuts as below.
thumb
Then insert well the batteries.
thumb

If you prefer to install another 4-cell AA battery case, please see below.
thumb

thumb


Completed the above steps, you should get prepared for wire connection of motors and tracking sensor below.

  • JST-PH2.0MM-2P 24AWG black-red wire 160mm*2
  • JST-PH2.0MM-5P 24AWG blue-green-yellow-red-black wire 15CM *1


thumb

Separately connect the 2P black-red wire 160mm to the motor A and B below.
thumb
Then connect the 5P blue-green-yellow-red-black wire 15CM to the tracking sensor below.
thumb


(5) Above parts are installed well, start to install the top parts for the robot. you should get these components as follows:

  • Top PCB *1
  • M3 Nut *1
  • M3*6MM round-head screws *9
  • M3*10MM dual-pass copper pillar *8
  • IR receiver sensor *1


thumb
According to the silk mark of bottom PCB, install the IR receiver to the PCB using a M3 nut and a M3*6MM round-head screw. Then screw 8 dual-pass copper pillars to the PCB with 8 M3*6MM round-head screws.
thumb

Followed by assembling the control board on bottom PCB. Prepare well the components below: on bottom PCB. Prepare well the components below:

  • Motor drive shield*1
  • UNO R3 board*1
  • M3*6MM round-head screws *4


thumb

First of all, tighten the UNO board to the PCB using four M3*6MM round-head screws.
thumb
Then simply stack the drive shield onto the UNO R3.
thumb


(6) Time to assemble the motor and plastic platform:

  • black plastic platform *1
  • M1.2*5 tapping screws *4
  • Servo *1
  • cross white mount *1
  • M2*8 screw *1

mount the servo to the black plastic platform with four M1.2*5 tapping screws(included in plastic platform), a cross white mount and a M2*8 screw (included for servo)
thumb

Firstly upload the code to UNO R3 to control the servo rotate to 90 degrees. Detailed method please refer to the project 3 micro servo control mentioned below.
thumb

Then fix the cross white mount to the black plastic platform with four M1.2*5 tapping screws.
thumb

thumb
Then adjust the servo towards front in 90 degrees to install it.
thumb

After that, fix the servo to the plastic platform using a M2*8 screw.
thumb

thumb

Finally, mount well another two plastic platform holders using two M2*8 screws.
thumb

thumb


(7) Until now, let’s install the ultrasonic sensor to Servo platform part.

  • ultrasonic sensor *1
  • JST-PH2.0MM-4P wire 8CM *1
  • Nylon cable ties*2


thumb
Simply connect the wire to ultrasonic sensor, and then tighten the ultrasonic sensor to the black plastic platform using two cable ties through the holes of sensor.
thumb

thumb
Next, mount them onto the top PCB like below. For the top PCB, first connect the tracking sensor to the drive shield using a JST-PH2.0MM-3P yellow-red-black wire 8CM.
thumb

After that, mount the ultrasonic platform part onto the top PCB with four M3*6MM round-head screws. Then connect well one end of the wire connected to ultrasonic sensor to the drive shield.

  • Top PCB part
  • Ultrasonic platform part
  • M3*6MM round-head screw*4


thumb

thumb


(8) Completed the above assembly, let's install the dot matrix display for this small turtle.

  • Dot matrix display *1
  • Jumper wire *4
  • M3*6MM round-head screws *4
  • M3*40MM dual-pass copper pillar* 4

Firstly, connect the jumper wires to the four pins of matrix display.
thumb thumb

Then screw the four M3*40MM copper pillars to the bottom PCB with four M3*6MM round-head screws.
thumb
After that, assemble the bottom PCB parts, 8*8 dot matrix display and top PCB parts together using four M3*6MM round-head screws.
thumb

Plug the matrix display into the bottom PCB.
thumb
Finally screw the top PCB to the bottom PCB with four M3*6MM round-head screws.
thumb

Hookup Guide:
thumb

Congrats! The whole turtle robot is installed well.
thumb


Project Details for Turtle Robot

Project 1: Getting Started with ARDUINO

1)Core Part of Robot

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.

UNO R3--.png


Well, let's at first have a look at this UNO R3 board.
UNO.png

KS0313 5.1-1.png USB Connection

Arduino board can be powered via USB connector. Or you can program the board via the USB port.

KS0313 5.1-2.png DC Power Jack

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

KS0313 5.1-3.png Voltage Regulator

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

KS0313 5.1-4.png Crystal Oscillator

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

KS0313 5.1-5.png Arduino RESET

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

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

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

KS0313 5.1-11.png Analog Pins

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

KS0313 5.1-12.png Microcontroller

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

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

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

KS0313 5.1-14.png Power LED Indicator

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

KS0313 5.1-15.png TX and RX LED

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

KS0313 5.1-16.png Digital I/O

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

KS0313 5.1-17.png AREF

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


Installing Arduino IDE

When you get the UNO development board, first you should install the software and driver of Arduino. You can see all the Arduino software versions from the link below:
https://www.arduino.cc/en/Main/OldSoftwareReleases#1.5.x
Or you can browse the ARDUINO website at this link, https://www.arduino.cc, pop up the following interface.
KS0313-1.png

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

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

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

KS0313-4.png

KS0313-5.png

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


Installing Arduino (Windows)

Install Arduino with the exe. Installation package
thumb

Click“I Agree”to see the following interface.
thumb

Click “Next”. Pop up the interface below.
thumb

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

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

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

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


Installing Driver

Next, we will introduce the driver installation of UNO R3 development board. The driver installation may have slight differences in different computer systems. So in the following let’s move on to the driver installation in the WIN 7 system.
The Arduino folder contains both the Arduino program itself and the drivers that allow the Arduino to be connected to your computer by a USB cable. Before we launch the Arduino software, you are going to install the USB drivers.
Plug one end of your USB cable into the Arduino and the other into a USB socket on your computer.
When you connect UNO board to your computer at the first time, right click the icon of your “Computer” —>for “Properties”—> click the “Device manager”, under “Other Devices”, you should see an icon for“Unknown device” with a little yellow warning triangle next to it. This is your Arduino.

Driver 1.png

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

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

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

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

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

Up to now, the driver is installed well. Then you can right click “Computer” —>“Properties”—>“Device manager”, you should see the device as the figure shown below.
Driver 7.png


2) Example Use of ARDUINO IDE

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


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

  • Build a new project
  • Open an exiting project example

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

Arduino 1-8-5 new.png0313 箭头.pngArduino 1-8-5 new2.png

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

Arduino 1-8-5 example.png 0313 箭头.pngArduino 1-8-5 example2.png


STEP 3: Select Arduino Board
On the Arduino software, you should click Tools→Board , select the correct board. Here in our tutorial we should select Arduino Uno. Shown as below.
Arduino 1-8-5 board.png


STEP 4: Select Serial Port
If you are not sure which port is correct, at first directly open the Control Panel of your computer, then click to open Device Manager, you can check the COM port here. Shown as below.

Driver 7.png

Then you should click Tools→Serial Port. It may be COM3 or higher (COM1 and COM2 are usually reserved as hardware serial port).
Arduino 1-8-5 port.png


STEP 5: Upload the Code to Your Board
Before showing you how to upload the code to your board, first of all let me introduce the function of each icon on the Tool bar of Arduino IDE. Look at the picture showed below.
图片1- arduino toolbar.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



3) Light up an LED

Overview:
In the above section, you have learned how to use the development software. So want to try it out with an example project? Get started with one more basic program, bringing you enter the wonderful programming world of ARDUINO. Great, follow the project sections below to have your first awesome try!
LED experiment is one of the more basic experiments in learning ARDUINO. Here we will use our keyestudio LED module. On the module, you will see a light emitting diode (LED), which has two states of on and off. Since our module itself has done well the circuit, you can use it in a simple way. Just need to connect its pins.
There are three lead-out pins on the module, respectively negative pin(marked -), positive pin(marked +) and signal pin(marked S). Note that the modules from different manufactures may have different pin labels.

Next, connect the three pins of LED module to keyestudio UNO R3 shield using three dupont jumper wires. Connect negative pin to the ground, positive pin to 5V, and signal pin to Digital 11. Shown as below.
Connect It Up:
Ks0364 - connection 1.png


Test Code 1:

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


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


What you should see?
Done wiring, compile the code and then click on the 'Upload' button. The second button from the left on the toolbar of Arduino IDE. When upload well the code to the board, you will see the status at the bottom of window will change to “Done uploading”.
KS0364 - 图片1.png

Eventually, you will see the LED light up for one second, then off one second. Congrats! Your first programming is done successfully.
KS0313(4)-2.png


4) PWM Controlled Brightness

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

KS0313(4).png

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

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

KS0313(4)-1.png

In fact, PWM can be applied to dimming lamps, motor speed, sound production, etc.
In the following, we are going to control the brightness of the LED.

Hookup Guide:
On the aspect of hardware, we still utilize the LED connected to D11 mentioned above. You can refer to the wiring diagram as follows:

Ks0364 - connection 2.png


Sample Code 2:

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


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

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

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


Example Result:
Upload the above code to the board, you could change the LED brightness in the code.
KS0313(4)-2.png

Furthermore, in the motor driving project below, it also involves the concept PWM.



5) Light up LED Matrix

Overview:
In the previous project, we have simply tested the LED. Now we have added a new 8*8 Dot Matrix module to the turtle to show the robot states. Amazing display!
Do you know how is the cool advertising display made? It is exactly composed of these small LED matrix. If you want to make a similar display, this keyestudio 8*8 Dot Matrix module will meet you need.
This tiny display has 64 LEDs packed into a 8*8 dot matrix. It integrated HT16K33as driver chip, so with this LED matrix module, you can control it through connecting the I2C communication interfaces ( A4-SDA ; A5-SCL).
It is great for displaying image/text or creating bizarre patterns, and is highly portable and convenient to use. Of course you can program it via IDE or via Mixly block. With just a few steps, you are ready to impress others!

KS0364 - 图片2.png


Hookup Guide:
Connect the LED matrix module to the pin header on the motor drive shield. Connect the SCL pin to pin A5, SDA pin to pin A4; Connect VCC pin to 5V, GND to ground.
thumb


Sample Code 3:

#include <Wire.h>
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
Adafruit_LEDBackpack matrix = Adafruit_LEDBackpack();
void setup() {
  Serial.begin(9600);
  Serial.println("HT16K33 test");
  
  matrix.begin(0x70);  // pass in the address
}

void loop() 
{
  /////////smile face///////////////
    matrix.displaybuffer[0] = B00000011;
    matrix.displaybuffer[1] = B10000000;
    matrix.displaybuffer[2] = B00010011;
    matrix.displaybuffer[3] = B00100000;
    matrix.displaybuffer[4] = B00100000;
    matrix.displaybuffer[5] = B00010011;
    matrix.displaybuffer[6] = B10000000;
    matrix.displaybuffer[7] = B00000011;
    matrix.writeDisplay();
}


Code To Note:
In the code, it needs to call three libraries, that is Wire.h; Adafruit_LEDBackpack.h and Adafruit_GFX.h
Wire.h is a built-in library of Arduino IDE, so not need to add it, but you should place the libraries folder Adafruit_LEDBackpack.h and Adafruit_GFX.h inside the libraries directory of IDE.
thumb

Note: place well the libraries folder, need to reopen the Arduino IDE again, and the libraries should be effective.
You can download the libraries from the link below:
https://drive.google.com/open?id=16ii-ZQTNK_Fn8KG81rhBw7JX1zQQ6nG6

Place the LED matrix as the right picture shown.

点阵.png

In the experiment, you can control the LED dot matrix display through the code matrix.displaybuffer[0] = B00000011
Note: the number 0 in the matrix.displaybuffer[0] represents the columns of LED. The number 0 is the first column, and the number 1 represents the second column. The rest can be done in the same manner.
B00000011 represents the on and off state of 8 LEDs in the cols. The number 0 represents off, while the number 1 represents on.
So matrix.displaybuffer[0] = B00000011 means that the first column, the LEDs in the row 1, 8, 7, 6, 5, 4 are set to off, the LEDs in the row 3 and 2 are on.


What you should see?
Hookup well and upload the code to the board, you should see the keyestudio 8*8 Dot matrix show a smile face.
thumb


Project 2: Line Tracking Turtle

1) Principle and Application of Line Tracking Sensor

Overview:
The tracking sensor is actually an infrared sensor. The component used here is the TCRT5000 infrared tube.
Its working principle is to use the different reflectivity of infrared light to the color, then convert the strength of the reflected signal into a current signal.
During the process of detection, black is active at HIGH level, but white is active at LOW level. And detection height is 0-3 cm.

The following figure is our keyestudio 3-channel line tracking module. We have integrated 3 sets of TCRT5000 infrared tube on a single board, which is more convenient for wiring and control.
By rotating the adjustable potentiometer on the sensor, it can adjust the detection sensitivity of the sensor.
thumb


TECH SPECS:

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


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

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

Test Code 4:

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

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


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

Test Code 5:

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


Test Code 6:

int sensor3 = 8; //  define the pin of right sensor as pin D8
int ledPin =11; // define LEDpin as Digital 11
void setup() 
{
    pinMode(sensor3, INPUT); // define the sensor as INPUT
    pinMode(ledPin,OUTPUT); // define LED as OUTPUT
}

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


Upload well the code to the board, you should see that if the tracking sensor detects a white object, the LED module will light up.
thumb

In the section below, we are about to match the digital sensors with other modules to make interactive works.


2) Motor Driving and Speed Control


thumb

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


Specifications:

  • 1. Logic part input voltage: 5V
  • 2. Driving part input voltage: DC 7-12V
  • 3. Logic part working current: <36mA
  • 4.Driving part working current: <2A
  • 5. Maximum powerdissipation: 25W (T=75℃)
  • 6. Control signal input level:
High level: 2.3V<Vin<5V    
Low level: -0.3V<Vin<1.5V
  • 7. Working temperature: -25℃~+130℃


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

thumb


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


Well, next let’s create the sketch.
The code logic of the robot is nothing more than 5 kinds of movement modes, namely go forward, go backward, turn left, turn right and stop. So think about it. How could it implement those functions?
Simply, for example, both left and right motor of robot turn forward, so it is able to go forward. If both the left and right motor turn reverse, the robot will go backward. 
Besides, if the left motor turns forward but right motor turns reverse, the robot will turn right. If the right motor turns forward but left motor turns reverse, the robot will turn left.
So how to control the forward and backward of motor? Actually, you can easily achieve that by controlling the microcontroller pin for motor direction to be HIGH or LOW level.

It is much more easier to understand the motor turning, however, it would be a little bit complicated to work out the speed control of motor. As for the speed control of motor, it involves the PWM mode mentioned in the previous section. So what is PWM?
PWM is the short for Pulse Width Modulation. PWM is a technique for getting analog results with digital means. Digital control is used to create a square wave (a signal switched between on and off) to control the analog output. The output voltage of Arduino Digital port only has LOW and HIGH level, corresponding to the output voltage of 0 Volts and 5 Volts.
Like the graphic shown below, the green lines represent a regular time period. This duration or period is the inverse of the PWM frequency. In other words, with Arduino's PWM frequency at about 500Hz, the green lines would measure 2 milliseconds each.
A call to analogWrite() is on a scale of 0-255, such that analogWrite(255) requests a 100% duty cycle (always on), and analogWrite(127) is a 50% duty cycle (on half the time) for example.

KS0313(4)-1.png


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

The function called by the PWM is: analogWrite(pin, value).
Note that the value is between 0 (always off) and 255 (always on). The speed of the motor is controlled actually by this value. The bigger the value is, the faster the speed is. Rather, the smaller the value is, the slower the speed it is until it stops.

In the following figure, look at the language logic for motor states: go forward, backward, turn left, turn right and stop.
M1 and M2 represent the motor’s direction control, that is, forward and backward rotation.
E1 and E2 represent the speed control, and speed is set to 150.

E1 M1 E2 M2
Forward 150 HIGH Motor A goes forward 150 HIGH Motor B goes forward
Backward 150 LOW Motor A goes backward 150 LOW Motor B goes backward
Left 150 LOW Motor A goes forward 150 HIGH Motor B goes backward
Right 150 HIGH Motor A goes backward 150 LOW Motor B goes forward
Stop 0 LOW Motor A stops 0 LOW Motor B stops



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

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

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

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

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

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

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


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


3) Line Tracking Turtle

Ks0364 - line track .jpg

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


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


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


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

2.The middle tracking sensor does not detect a black line, if the sensor on the left side detects a white line, while the sensor on the right side detects a black line, the smart car will turn right.
On the contrary, if the sensor on the right detects a white line, but the left one detects a black line, the smart car will turn left. If three sensors all detect a white line, it will stop.

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

#define INT_A 2 // define the left motor direction pin D2
#define INT_B 4 // define the right motor direction pin D4
#define left_A 9 // define the left motor speed(PWM)pin D9
#define right_B 5 // define the right motor speed(PWM)D5

const int S1 = 8;  // S1 right tracking sensor control pin to D8
const int S2 = 7;  // S2 middle tracking sensor control pin to D7
const int S3 = 6;  // S3 left tracking sensor control pin to D6
int s1,s2,s3;   //define three variables, separately receive the digital value read by 3-channel tracking sensor (0 or 1)


void setup() 
{  
  Serial.begin(9600);    //set the monitor baud rate to 9600
  delay(100);       //delay 100ms
  pinMode(INT_A,OUTPUT);      // set the motor control pin as OUTPUT
  pinMode(INT_B,OUTPUT);
  pinMode(left_A,OUTPUT);
  pinMode(right_B,OUTPUT);
}

void loop() 
{
    s1 = digitalRead(S1); //assign the digital value read from pin S1,S2,S3 to s1,s2,s3
    s2 = digitalRead(S2);
    s3 = digitalRead(S3);
    
    if(s2==1)  //if s2 pin detects a black line
    {
      if(s3==1 && s1==0)  //if s3 pin detects a black line but s1 doesn’t 
      {
        left();     // turn left
      }
      else if(s3==0 && s1==1)  //if s3 does not detect a black line, but s1 detects it.
      {
        right();   //turn right 
      }
      else      //other situations
      {
        front();   // go forward
      }
    }
  
    else  //s2 does not detect a black line 
    {
      if(s3==1&&s1==0)  //if s3 detects a black line 
      {
        left();     //turn left
      }
      else if(s3==0&&s1==1)  //s1 detects a black line
      {
        right();    // turn right
      }
      else  // none detect black line
      {
        Stop();      // stop
      }
    }
    
}
// forward
void front()
{
  digitalWrite(INT_A,LOW);    // control the left motor turn forward
  digitalWrite(INT_B,LOW);   // control the right motor turn forward
  analogWrite(left_A,200);   // set the motor speed(PWM=200)
  analogWrite(right_B,200); 
}
//backward
void back()  
{
  digitalWrite(INT_A,HIGH);   // control the left motor turn backward 
  digitalWrite(INT_B,HIGH);    // control the right motor turn backward
  analogWrite(left_A,200);   
  analogWrite(right_B,200);
}
//turn left
void left()
{
  digitalWrite(INT_A,HIGH);    // control the left motor turn backward
  digitalWrite(INT_B,LOW);    // control the right motor turn forward
  analogWrite(left_A,100);    // motors speed(PWM为100)
  analogWrite(right_B,100);
}
// turn right 
void right()
{
  digitalWrite(INT_A,LOW);     // control the left motor turn forward
  digitalWrite(INT_B,HIGH);     // control the right motor turn backward 
  analogWrite(left_A,100);
  analogWrite(right_B,100);
}
// stop
void Stop()
{
  digitalWrite(INT_A,LOW);  
  digitalWrite(INT_B,LOW);
  analogWrite(left_A,0);    // both side PWM is 0
  analogWrite(right_B,0);
}


Example Picture:

thumb

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


Project 3: Turtle Robot Avoiding Obstacles


1) Principle and Application of Ultrasonic Module

KS0313 3-2-1.png

Description:
There is an animal called bat in nature. The bats can fly at night, not depend on its eyes, but on its ears and vocal organs. When the bat flies, it will emit a scream, an ultrasonic signal that humans cannot hear because of its high audio frequency. If these ultrasonic signals hit other objects on the flight path, they will be reflected back immediately. After receive the returned information, the bats complete the whole process of listening, seeing, calculating and bypassing obstacles during the flutter.

The principle of the ultrasonic rangefinder module is as the same as the above principle.
The ultrasonic module will emit the ultrasonic waves after trigger signal. When the ultrasonic waves encounter the object and are reflected back, the module outputs an echo signal, so it can determine the distance of object from the time difference between trigger signal and echo signal.
Ultrasonic sensor has a wide range of sensitivity, no blind area, and no interference with obstacles.


As the following picture shown, it is our keyestudio ultrasonic module. You can see it has two somethings like eyes. One is transmitting end, the other is receiving end.

thumb thumb


TECH SPECS:

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


Hookup Guide:
Connect the ultrasonic module to the shield. Shown as below.
thumb


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

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


Example Code 9:

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


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


2) Micro Servo Control


thumb
Description:
Servo motor is a position control rotary actuator. It mainly consists of housing, circuit board, core-less motor, gear and position sensor.
  Included with your servo motor you will find a variety of white motor mounts that connect to the shaft of your servo. You may choose to attach any mount you wish for the circuit. It will serve as a visual aid, making it easier to see the servo spin.

Working principle:
The receiver or MCU outputs a signal to the servo motor. The motor has a built-in reference circuit that gives out reference signal, cycle of 20ms and width of 1.5ms. The motor compares the acquired DC bias voltage to the voltage of potentiometer and outputs a voltage difference.
Servo motors come with many specifications. But all of them have three connection wires, distinguished by brown, red, orange color (different brand may have different color). Brown one is for GND, red one for power positive, orange one for signal.

thumb

When you send the right signal through the signal wire, the servo will move to a specific angle and stay there. Common servos rotate over a range of about 0° to 180°. The signal that is sent is a PWM signal.
thumb

The rotation angle of servo motor is controlled by regulating the duty cycle of PWM(Pulse-Width Modulation) signal. The standard cycle of the PWM signal is 20ms (50Hz). Theoretically, the width is distributed between 1ms-2ms, but in fact, it's between 0.5ms-2.5ms. The width corresponds the rotation angle from 0° to 180°.


Parameters:

  • Operating voltage: DC 4.8V〜6V
  • Angle range: about 180°(in 500→2500μsec)
  • Pulsewidth range: 500→2500μsec
  • No-load speed: 0.12±0.01 sec/60(DC 4.8V); 0.1±0.01 sec/60(DC 6V)
  • No-load current: 200±20mA(DC 4.8V); 220±20mA(DC 6V)
  • Stop torque: 1.3±0.01kg/cm(DC 4.8V); 1.5±0.1kg/cm(DC 6V)
  • Stop current: ≦850mA(DC 4.8V); ≦1000mA(DC 6V)
  • Standby current: 3±1mA(DC 4.8V); 4±1mA(DC 6V)
  • Operation temperature: -10℃〜50℃
  • Save temperature: -20℃〜60℃
  • Motor wire length: 250 ± 5 mm
  • Dimensions: 22.9mm*12.2mm*30mm
  • Weight: 9± 1 g (without servo mounts)


Hookup Guide:
Ready to start hooking everything up? Check out the connection diagram below. Connect the black servo to the shield. Brown wire is for GND, red one for 5V pin, orange one for signal pin.
thumb


You can check out the test code for the servo below.

Code 10:

int servopin=3;// define the digital 9 is connected to servo signal line 
int myangle;// define the angle variable
int pulsewidth;// define the pulsewidth variable
int val;
void setup()
{
pinMode(servopin,OUTPUT);// set the servo interface as OUTPUT
Serial.begin(9600);// connect to serial port, baud rate to 9600
Serial.println("servo=o_seral_simple ready" );
servopulse(servopin,90);// call the pulse function, make the servo rotate to 90degree
} 
void loop()
{
  servopulse(servopin,90);// call the pulse function, make the servo rotate to 90degree
}
void servopulse(int servopin,int myangle)// define a pulse function 
{
pulsewidth=(myangle*11)+500;// convert the angle into pulse width of 500-2480 
digitalWrite(servopin,HIGH);// set the servo pin to HIGH 
delayMicroseconds(pulsewidth);// delay the microseconds of pulsewidth
digitalWrite(servopin,LOW);// set the servo pin to LOW
delay(20-pulsewidth/1000);
}


Test Result:
Wire it up and upload well the code, press down the reset button on the shield, micro servo will rotate to the angle of 90 degrees.
thumb


3) Turtle Robot Avoiding Obstacles

Description:
It is rather not suitable for human to work in some relatively harsh environments. At this moment, if we have a robot that can shuttle freely in such environments, then how good should it be!
Based on this original intention, our team develop the robot that be able to automatically avoid an obstacle when running on complicated terrain.

Ks0364 avoid.jpg

This project is a simple and automatic obstacle avoidance system based on Arduino control board.
The smart robot with UNO R3 as the controlling core, makes use of ultrasonic module and micro servo of 180 degrees to detect the obstacles, and the detection signal will feed back to the control board.
Arduino main board will then analyze and judge the collected signals to control the motordriving in time. Finally control the smart car automatically avoid an obstacle ahead to run forward smoothly.


Project Principle:

  • 1. Use the ultrasonic module to detect the distance between the robot and obstacle ahead.
  • 2. When the measured distance between ultrasonic sensor and obstacle ahead is less than 15cm, smart robot will stop for 100ms. The ultrasonic will make use of servo to turn left in 90 degrees, and stop for 100ms to detect the obstacle distance on the left. Then use the servo to turn right in 180 degrees, stop to detect the obstacle distance on the right.
  • 3. If the distance measured at the left side is greater than that of the right side, ultrasonic sensor will first turn to the front, turtle robot turns left in 90 degrees and then goes forward.
  • Otherwise, turtle robot will turn right in 90 degrees and then go forward.
  • 4. Arduino control board will control the motor’s rotating direction and servo angle according to the distance value measured by ultrasonic sensor between robot and obstacle.


Wiring Diagram:
Firstly you can follow the connection diagram below. Connect the ultrasonic module, micro servo and two motors to the drive shield.
thumb


Code 11:
Let’s move on to an example code for the obstacle avoidance robot. You can see the code reference below:

#define INT_A 2    // control the left motor direction pin to D2
#define INT_B 4    // control the right motor direction pin to D4
#define left_A 9    // define the left motor speed as pin D9
#define right_B 5   // define the right motor speed as pin D5
// Ultrasonic 
int servopin=3;// digital 3 is connected to servo signal pin
int myangle;// define the angle
int pulsewidth;// define the pulsewidth 
#include <SR04.h>    // add the ultrasonic libraries 
#define TRIG_PIN 12   // define the pin ting of ultrasonic as D12
#define ECHO_PIN 13   //define the pin echo of ultrasonic as D13
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);    // build the ultrasonic object to control the ultrasonic 
long a,a1,a2;      // used to receive the distance measured by ultrasonic 
void setup() 
{
  Serial.begin(9600);  // set the monitor baud rate to 9600
  delay(100);     // delay 100ms
  pinMode(INT_A,OUTPUT);     // set the motor control pin as OUTPUT 
  pinMode(INT_B,OUTPUT);
  pinMode(left_A,OUTPUT);
  pinMode(right_B,OUTPUT); 
  pinMode(servopin,OUTPUT);// set the servo pin as OUTPUT 
  servopulse(servopin,90);  // call the pulse function, make the ultrasonic keep front.
}

void loop() 
{
   a=sr04.Distance();    // assign the front distance measured by ultrasonic to a
   Serial.print(a);     // print a value on the monitor 
   Serial.println("cm");   // print cm and line wrap 
   delay(100);    // delay
   if(a<15)    // whether the distance a is less than 15cm, if yes, then perform the program in the brace.
   {
    Stop();    // car stops
    delay(100);   // delay 100ms
    servopulse(servopin,160);// call the pulse function to make ultrasonic sensor turn left in 90 degrees
    a1=sr04.Distance();  // assign the left obstacle distance measured by ultrasonic to a1
    Serial.print("a1 = ");  // print out the a1 = on the serial monitor
    Serial.print(a1);      //print a1 value 
    Serial.println("cm");   // print cm and line wrap
    delay(100);    // delay 100ms
    servopulse(servopin,20);// call the pulse function to make ultrasonic sensor turn right in 90 degrees
    a2=sr04.Distance();      // assign the right obstacle distance measured by ultrasonic to a2
    Serial.print("a2 = ");
    Serial.print(a2);
    Serial.println("cm");
    delay(100);
    if(a1>a2)     // whether a1 is greater than a2(whether left distance is greater than that measured on the right side.)
    {
      servopulse(servopin,90);// call the pulse function, make the ultrasonic keep front.
      left();    //turn left
      delay(370);   // delay370ms,the time for car to turn left in 90 degrees as much as possible.
      front();  // go front
    }
    else     // if a1<a2
    {
      servopulse(servopin,90);  // call the pulse function, make the ultrasonic keep front.
      right();  // turn right 
      delay(370);  // delay 370ms,the time for car to turn right in 90 degrees as much as possible. 
      front();   // the car goes forward 
    }
   }
   else   // if a>15cm
   {
    front();   //the car goes forward
   }

}

// forward
void front()
{
  digitalWrite(INT_A,LOW);    // control the left motor turn forward
  digitalWrite(INT_B,LOW);   // control the right motor turn forward
  analogWrite(left_A,200);   // set the motor speed(PWM=200)
  analogWrite(right_B,200); 
}
// backward
void back()  
{
  digitalWrite(INT_A,HIGH);   // control the left motor turn backward 
  digitalWrite(INT_B,HIGH);    //control the right motor turn backward 
  analogWrite(left_A,200);   
  analogWrite(right_B,200);
}
// turn left
void left()
{
  digitalWrite(INT_A,HIGH);    //control the left motor turn backward 
  digitalWrite(INT_B,LOW);    // control the right motor turn forward
  analogWrite(left_A,150);    // two motors’ speed(PWM为150)
  analogWrite(right_B,150);
}
// turn right 
void right()
{
  digitalWrite(INT_A,LOW);     // control the left motor turn forward
  digitalWrite(INT_B,HIGH);     // control the right motor turn backward 
  analogWrite(left_A,150);
  analogWrite(right_B,150);
}
// stop
void Stop()
{
  digitalWrite(INT_A,LOW);   
  digitalWrite(INT_B,LOW);   
  analogWrite(left_A,0);     // PWM of both left and right is 0
  analogWrite(right_B,0);
}
// servo
void servopulse(int servopin,int myangle)// define a pulse function
{
  for(int i=0;i<50;i++)
  {
    pulsewidth=(myangle*11)+500;// convert the angle into the pulsewidth of 500-2480 
    digitalWrite(servopin,HIGH);// servo pin to HIGH 
    delayMicroseconds(pulsewidth);// delay the microseconds of pulsewidth 
    digitalWrite(servopin,LOW);// servo pin to LOW
    delay(20-pulsewidth/1000); // delay the rest circle time to LOW level(20ms circle )
  }
}


Test Result:
Upload the above code to the control board, and stack well the drive shield onto control board, then press lightly down the POWER button on the drive shield.
When detects an obstacle ahead, our smart robot is able to automatically avoid it to run forward freely. You can try it out and see whether it works in that way.

thumb