KS0551 Keyestudio 4WD Mecanum Robot For Arduino
KS0551 Keyestudio 4WD Mecanum Robot For Arduino
*About keyestudio
world.
http://www.keyestudio.com
1. Download address:https://fs.keyestudio.com/KS0551
difficulty learning the kit, please feel free to contact us. Welcome
*Warning
www.keyestudio.com
1
keyestudio
keep it out of reach of children under 7 years old.
power.
*Copyright
www.keyestudio.com
2
keyestudio
1. Description
cope with this problem. This product not only allows your child to
www.keyestudio.com
3
keyestudio
kit. In fact, it is also simple to install and connect from complex
ultrasonic module,etc.
you can seek to alter code and add extra modules to enrich the
functions.
2. Features
www.keyestudio.com
4
keyestudio
board, etc.
circuit.
you can extend other sensors and modules. What’s more, the
3. Parameters
Input voltage:7-12V
Working voltage:5V
4. Kit
www.keyestudio.com
5
keyestudio
MD0487
1 Acrylic Board 1
T=3mm
2 Lego Holes 1
T=3mmm
Motor Plate
3 4
4 4.5V Motor 4
5 23*15*5mm Fixing 4
Board
DX-BT24 Bluetooth
6 1
Module
www.keyestudio.com
6
keyestudio
7 Servo 1
8 Mecanum Wheels 4
Keyestudio Sensor
9 1
Shield V6 for
Arduino
Keyestudio UNO
10 1
PLUS
Keyestudio 4WD
Mecanum Robot
11 Lower 1
Board(with motor d
river)
www.keyestudio.com
7
keyestudio
M3*15MM Dual-
12 4
pass Copper Pillar
13 4
4265c Lego Part
14 4
43093 Lego Part
HC-SR04 Ultrasonic
17 1
Sensor
www.keyestudio.com
8
keyestudio
M3*8mm Flat Head
18 10
Screw
M3 Nickle-plated
19 10
Nut
M3*30mm Round
20 9
Head Screw
M2 Nickle-plated
21 3
Nut
M2*8mm Round
22 3
Head Screw
M1.4 Nickle-plated
23 6
Nut
M1.4*10mm Round
24 6
Head Screw
M2.3*16mm Round
25 4
Head Screw
www.keyestudio.com
9
keyestudio
Remote Control
26 1
(without batteries)
Plastic String
27 5
3*100mm
28 USB Cable 1
HX-2.54 2P DuPont
29 1
Wire 100mm
HX-2.54 5P DuPont
30 1
Wire 100mm
31 HX-2.54 4P DuPont 2
Wire 50mm
HX2.54mm-4P to
150mm
33 XH2.54 3P DuPont 3
www.keyestudio.com
10
keyestudio
Wire 50mm
3*40mm
34 1
Screwdriver
35 TT Coupling 4
M1.2*5mm Round
36 Head Self-tapping 6
Screw
www.keyestudio.com
11
keyestudio
a reset button.
Microcontroller ATMEGA328P-AU
Operating Voltage 5V
www.keyestudio.com
12
keyestudio
Digital I/O Pins 14 个 (D0-D13)
6 个
PWM Digital I/O Pins
(D3,D5,D6,D9,D10,D11)
32 KB (ATMEGA328P-PU) of
bootloader
SRAM 2 KB (ATMEGA328P-PU)
EEPROM 1 KB (ATMEGA328P-PU)
www.keyestudio.com
13
keyestudio
www.keyestudio.com
14
keyestudio
www.keyestudio.com
15
keyestudio
click“DOWNLOADS” to enter download page, as shown below:
ZIP file.
There are two versions of IDE for WINDOWS system, you can
suggest you use the first one that installs directly everything you
With the Zip package you need to install the drivers manually.
www.keyestudio.com
16
keyestudio
www.keyestudio.com
17
keyestudio
(3)Installing the driver
computer.
www.keyestudio.com
18
keyestudio
www.keyestudio.com
19
keyestudio
www.keyestudio.com
20
keyestudio
Open the DIRVERS file to see the driver of the CP210X series
chips
www.keyestudio.com
21
keyestudio
www.keyestudio.com
22
keyestudio
www.keyestudio.com
23
keyestudio
(4)Interfaces for the expansion board:
The interface of motors, 7 color LEDs and RGB on the car are
(5)Driver Board
bottom board, and they share a signal pin (the 7-color LED also
www.keyestudio.com
24
keyestudio
share a signal pin), which is convenient to receive the infrared
you need to select the correct Arduino board that matches the
www.keyestudio.com
26
keyestudio
Then select the correct COM port (you can see the corresponding
www.keyestudio.com
27
keyestudio
www.keyestudio.com
28
keyestudio
F- Used to send the serial data received from board to the serial
monitor.
www.keyestudio.com
29
keyestudio
for the development board. Next, we will burn a code to
Test Code
void setup() {
Serial.begin(9600);
void loop() {
Serial.println("Hello world!");
www.keyestudio.com
30
keyestudio
www.keyestudio.com
31
keyestudio
www.keyestudio.com
32
keyestudio
Upload the program successfully, open the serial monitor and set
www.keyestudio.com
33
keyestudio
6. The Installation of Keyestudio
www.keyestudio.com
34
keyestudio
Part 1
Components
Needed
Installation
Diagram
www.keyestudio.com
35
keyestudio
Prototype
Part 2
www.keyestudio.com
36
keyestudio
Components
Needed
www.keyestudio.com
37
keyestudio
Installation
Diagram
www.keyestudio.com
38
keyestudio
Prototype
Part 3
www.keyestudio.com
39
keyestudio
Components
Needed
Installation
Diagram
www.keyestudio.com
40
keyestudio
Prototype
www.keyestudio.com
41
keyestudio
#include <Servo.h>
Servo myservo; //Define a servo
void setup() {
myservo.attach(9); //The pin of the servo is connected
Adjust the to D9
delay(500);
servo to 90
}
void loop() {
degrees
}
according to
the right
picture
www.keyestudio.com
42
keyestudio
Components
Needed
Installation
Diagram
(mind the
installation
direction)
www.keyestudio.com
43
keyestudio
Prototype
Part 5
Components
Needed
www.keyestudio.com
44
keyestudio
Installation
Diagram
Prototype
www.keyestudio.com
45
keyestudio
Part 6
Components
Needed
www.keyestudio.com
46
keyestudio
Installation
Diagram
Prototype
www.keyestudio.com
47
keyestudio
Part 7
Components
Needed
www.keyestudio.com
48
keyestudio
Installation
Diagram
(mind the
direction of
the motor)
Prototype
www.keyestudio.com
49
keyestudio
Part 8
Components
Needed
www.keyestudio.com
50
keyestudio
Installation
Diagram
(Pay
attention to
the
installation
direction of
the
mecanum
wheel)
Prototype
www.keyestudio.com
51
keyestudio
Part 9
Components
Needed
www.keyestudio.com
52
keyestudio
Installation
Diagram
Prototype
www.keyestudio.com
53
keyestudio
Part 10
Components
Needed
Installation
Diagram
www.keyestudio.com
54
keyestudio
Prototype
Inserting a
Bluetooth
module
correctly
www.keyestudio.com
55
keyestudio
Wiring Diagram
The
wiring
of the
ultraso
nic
module
(ECHO pin is
connected to
13,TRIG pin is
connected to
12)
www.keyestudio.com
56
keyestudio
The
wiring
of the
servo
(The servo pin
is connected
to 9)
www.keyestudio.com
57
keyestudio
The
wiring
of the
power
(Power must
be plugged
into the 5V
terminal)
The
wiring
of the
IR
receivin
g
sensor
(The pin of the
IR receiving
sensor is
connected to
A4)
www.keyestudio.com
58
keyestudio
The
wiring
of the
RGB
2812
LED(The
pin of the
2812 LED is
connected to
10)
The
wiring
of the
7-color
LED (The
pin of the 7-
color LED is
connected to
A3)
www.keyestudio.com
59
keyestudio
The
wiring
of the
M3 、 M4
motor
(D6D7D8D11)
The
wiring
of the
M1 、 M2
motor
(D2D3D4D5)
www.keyestudio.com
60
keyestudio
The
wiring
of the
line-
trackin
g
sensor(T
he pin of the
line-tracking
sensor is
connected to
A0A1A2)
Connect
the motor
to the
correspond
ing
interface
as shown
in the
figure
www.keyestudio.com
61
keyestudio
Installat
ion of b
atteries
7.Projects
www.keyestudio.com
62
keyestudio
(1) Description
colors. This module is the same as the normal LED driver, which
the first project, we will use a basic test code to control it to blink
www.keyestudio.com
63
keyestudio
for 3 seconds, off for 1 second to achieve the control effect.
You can also change the time of the LEDs on and off in your code
Driver Control: Each of the two 7-color LEDs uses an NPN triode
www.keyestudio.com
64
keyestudio
(5V) to this pin, both triodes will turn on, and there will be
voltage at both ends of the 7-color LEDs and they will flash. On
this pin, both triodes turn off. At this time, there is no voltage at
(The signal ends of the two 7-colored LEDs are connected to the
void setup() {
pinMode(LED, OUTPUT);// Configure 7-color LED pin mode for output
}
void loop() {
digitalWrite(LED, HIGH);// Turn on the 7-color LED flash
delay(5000); // Delay 5000 ms
www.keyestudio.com
65
keyestudio
delay(1000); // Delay 1000 ms
}
//*******************************************************************
Click upload the program and you will see the 7-color LEDs flash
is INPUT or OUTPUT.
(1) Description
www.keyestudio.com
66
keyestudio
WS2812 LED to display different colors. Its principle is different
There are four LEDs on our motor drive board, namely four
Working voltage:DC 5V
Pin number:3(VCC,GND,S)
Driver Control: The four pixel LEDs are connected in series, and
we can control any one of the LEDs with a pin and make it
www.keyestudio.com
67
keyestudio
When the first 24bit data extracted by the first pixel, which will
www.keyestudio.com
68
keyestudio
R G B R G B R G B (3)
0 0 0 255 255 0 176 224 230
41 36 33 227 207 87 65 105 225
192 192 192 255 153 18 106 90 205
128 138 135 235 124 85 135 206 235
112 128 105 255 227 132
128 128 105 255 215 0 0 255 255
218 165 105 56 94 15
255 255 255 227 168 105 8 46 84
250 235 215 255 97 0 127 255 212
240 255 255 255 97 3 64 224 208
245 245 245 237 145 33 0 255 0
255 235 205 255 128 0 127 255 0
255 248 220 245 222 179 61 145 64
252 230 201 0 201 87
255 250 240 128 24 24 34 139 34
220 220 220 163 148 128 124 252 0
248 248 255 138 54 15 50 205 50
240 255 240 135 51 36 189 252 201
250 255 240 210 105 30 107 124 35
250 240 230 255 125 64 48 128 20
255 222 173 240 230 140 46 139 87
253 245 230 188 143 143 0 255 127
255 245 238 199 97 20
255 250 250 115 74 18 160 32 240
94 38 18 138 43 226
255 0 0 160 82 45 160 102 211
156 102 31 139 69 19 153 51 250
227 23 13 244 164 96 218 112 214
255 127 80 210 180 140 221 160 221
178 34 34
176 23 31 0 0 255
176 48 96 61 89 171
135 38 87 11 23 70
250 128 114 3 168 158
255 99 71 25 25 112
255 69 0 51 161 201
255 0 255 0 199 140
www.keyestudio.com
69
keyestudio
(3) Test Code
/*
Keyestudio 4WD Mecanum Robot for Arduino
lesson 2.1
RGB2812
http://www.keyestudio.com
*/
#include <Adafruit_NeoPixel.h>
//Create a category named rgb_2812 to control 4 LEDs and the pins are connected to D10
Adafruit_NeoPixel rgb_2812 = Adafruit_NeoPixel(4, 10, NEO_GRB + NEO_KHZ800);
void setup() {
rgb_2812.begin(); //Activate rgb2818
rgb_2812.setBrightness(100); //Set the initial brightness to 100,(0~255)
rgb_2812.show(); //Initialize all NeoPixels to the closed state
void loop() {
}
the driver baseplate are respectively bright red, green, blue and
white.
www.keyestudio.com
70
keyestudio
.begin(); - We will use it to initialize RGB2812.
the brightness, 0~255, the larger the value, the brighter the LED
will be.
This function can make any LED display any color. It must be
noted that the first parameter n is the RGB number to be set, the
flowing LED, and make the color random to see their effect.
/*
Keyestudio 4WD Mecanum Robot for Arduino
lesson 2.2
RGB2812
http://www.keyestudio.com
www.keyestudio.com
71
keyestudio
*/
#include <Adafruit_NeoPixel.h>
//Create a category named rgb_2812 to control 4 LEDs and the pins are connected to D10
Adafruit_NeoPixel rgb_2812 = Adafruit_NeoPixel(4, 10, NEO_GRB + NEO_KHZ800);
void setup() {
rgb_2812.begin(); //Activate rgb2818
rgb_2812.setBrightness(150); //Initial brightness(0~255)
rgb_2812.show(); //Initialize all NeoPixels to the closed state
}
void loop() {
uint8_t r = random(0, 255);
uint8_t g = random(0, 255);
uint8_t b = random(0, 255);
for (uint8_t i = 0; i < 4; i++)
{
rgb_2812.setPixelColor(i, r, g, b);//The color of the I +1 LED is random(r,g,b)
rgb_2812.show(); //Refresh display
delay(100); //Wait for a while
}
}
Project 3: Servo
(1) Description
obstacle
www.keyestudio.com
72
keyestudio
need a servo to control the ultrasonic module to rotate left and
right, and then detect the distance between the car and the
the servo angle, we only need to set the control angle in the
you will learn how to control the servo to rotate back and forth
Driver voltage:3.3V or 5V
www.keyestudio.com
73
keyestudio
Control Principle:
180°. But note that for different brand servo, the same signal
may have different rotation angle. (The servo signal end is D9 pin
www.keyestudio.com
74
keyestudio
connected to our expansion board)
servo
//************************************************************************
/*
Keyestudio 4WD Mecanum Robot for Arduino
lesson 3.1
Servo
http://www.keyestudio.com
*/
#define servo_pin 9 //The servo is connected to D9
void setup() {
pinMode(servo_pin, OUTPUT); //Set the servo pin mode to output
}
void loop() {
for (uint8_t angle = 0; angle < 180; angle++)
{
servopulse(servo_pin, angle);
}
for (uint8_t angle = 180; angle > 0; angle--)
{
servopulse(servo_pin, angle);
}
}
www.keyestudio.com
75
keyestudio
(4) Test Code2: Use the servo library to drive servo
//**************************************************************
/*
Keyestudio 4WD Mecanum Robot for Arduino
lesson 3.2
Servo
http://www.keyestudio.com
*/
#include <Servo.h>
Servo myservo; //Define an instance of a servo
void setup() {
myservo.attach(9); //The pin of the servo is connected to D9
}
void loop() {
for (uint8_t angle = 0; angle < 180; angle++)//From 0 to 180 degrees
{
myservo.write(angle); //Rotate to angle
delay(15); //Wait for a while
}
for (uint8_t angle = 180; angle > 0; angle--)//From 180 to 0 degrees
myservo.write(angle); //Rotate to angle
delay(15);
}
}
After uploading the test code, connect the wires of the servo and
power up. The servo will rotate from 0 degrees to 180 degrees
library to drive.
(Note: When we use the servo library file, the PWM output of D9
www.keyestudio.com
76
keyestudio
and D10 will fail, because they share the same timer 1 with the
servo library.)
rotate to angle.
For example, the input variable ranges from 0 to 180 and the
(which can be understood as high level ), and the low level can be
www.keyestudio.com
77
keyestudio
myservo.attach(9); - This function is to set the pin to which our
angle position.
Project 4: Motor
(1) Description
www.keyestudio.com
78
keyestudio
Gear motor is the integration of gearmotor and motor, which is
IO port to force the motor to drive, either the motor will not
driver chip. The DRV8833 motor driver chip has been included on
www.keyestudio.com
79
keyestudio
Motor type:F130SA-11200-38V
No-load current:≤80mA
www.keyestudio.com
80
keyestudio
Direction
Right Front M1 Right Rear M4 Left Front M2 Left Rear M3
Motor Motor Motor Motor
M3 Rotates
M1Rotates M4 Rotates M2 Rotates
Advance clockwise
clockwise (LOW) clockwise (LOW) clockwise (LOW)
(LOW)
M1 Rotates M4 Rotates M2 Rotates M3Rotates
Move Back anticlockwise(HIG anticlockwise(HIG anticlockwise(HIG anticlock
H) H) H) wise(HIGH)
M3Rotates
M1Rotates M4 Rotates M2 Rotates
Rotate Left anticlock
clockwise(LOW) clockwise(LOW) clockwise (HIGH)
wise(HIGH)
M1 Rotates M4 Rotates M3 Rotates
Rotate M2 Rotates
anticlockwise(HIG anticlockwise(HIG clockwise(LOW
Right clockwise (LOW)
H) H) )
M4 Rotates M2 Rotates M3 Rotates
Left M1Rotates
anticlockwise(HIG anticlockwise(HIG clockwise(LOW
Translation clockwise (LOW)
H) H) )
M1 Rotates M3Rotates
Right M4 Rotates M2 Rotates
anticlockwise(HIG anticlock
Translation clockwise (LOW) clockwise (LOW)
H) wise(HIGH)
M3 Rotates
Left Front M1 Rotates
M4 Stop M2 Stop Clockwise(LO
Move clockwise (LOW)
W)
M4 Rotates M2 Rotates
Left Rear
M1 Stop anticlockwise(HIG anticlockwise(HIG M3 Stop
Move
H) H)
Right Front M4 Rotates M2 Rotates
M1 Stop M3 Stop
Move clockwise(LOW) clockwise(LOW)
www.keyestudio.com
81
keyestudio
M1 Rotates M3 Rotates
Right Rear
anticlockwise(HIG M4 Stop M2 Stop anticlock
Move
H) wise(HIGH)
of a motor
//*************************************************************************
/*
Keyestudio 4WD Mecanum Robot for Arduino
lesson 4.1
Motor
http://www.keyestudio.com
*/
void setup() {
/****Set all motor control pins to output mode***/
pinMode(M1, OUTPUT);
pinMode(PWM1, OUTPUT);
}
void loop() {
digitalWrite(M1, LOW); //Motor rotates clockwise
analogWrite(PWM1, 100); //Set the speed
delay(2000); //Wait for 2 seconds
digitalWrite(M1, LOW);
analogWrite(PWM1, 0); //Stop when the speed is 0
delay(1000);
digitalWrite(M1, LOW);
www.keyestudio.com
82
keyestudio
analogWrite(PWM1, 0); //Stop when the speed is 0
delay(1000);
}
void setup() {
/****All motor control pins are set to output mode***/
pinMode(M1, OUTPUT);
pinMode(PWM1, OUTPUT);
pinMode(M2, OUTPUT);
pinMode(PWM2, OUTPUT);
www.keyestudio.com
83
keyestudio
pinMode(M3, OUTPUT);
pinMode(PWM3, OUTPUT);
pinMode(M4, OUTPUT);
pinMode(PWM4, OUTPUT);
void loop() {
Advance(); //The car advance
delay(2000);
Back(); //The car moves back
delay(2000);
Turn_Left(); //The car turns left
delay(2000);
Turn_Right(); //The car turns right
delay(2000);
Stop(); //The car stops
delay(1000);
}
digitalWrite(M2, LOW);
analogWrite(PWM2, speed2);
digitalWrite(M3, LOW);
analogWrite(PWM3, speed3);
}
digitalWrite(M2, HIGH);
analogWrite(PWM2, 255 - speed2);
digitalWrite(M3, HIGH);
www.keyestudio.com
84
keyestudio
analogWrite(PWM3, 255 - speed3);
}
digitalWrite(M2, HIGH);
analogWrite(PWM2, 255 - speed2);
digitalWrite(M3, HIGH);
analogWrite(PWM3, 255 - speed3);
}
digitalWrite(M2, LOW);
analogWrite(PWM2, speed2);
digitalWrite(M3, LOW);
analogWrite(PWM3, speed3);
}
www.keyestudio.com
85
keyestudio
(5) Test Result
Code2 Result : The car goes forward for 2 seconds, goes back
for 2 s, turns left for 2 s, turns right for 2 s and then stops for 1 s.
(Note: Since the speed control pins used by the right motor are
D3 and D11, the frequency of these two PWM outputs are about
possible.)
www.keyestudio.com
86
keyestudio
(1) Description
front of the car driving bottom plate, and the component used
www.keyestudio.com
87
keyestudio
Interface: 5PIN interface (connect to A0,A1,A2)
outputs a high level. When the light intensity and the current
www.keyestudio.com
88
keyestudio
increase, the output voltage will become smaller, when it is small
www.keyestudio.com
89
keyestudio
/*******Define the pin of Line Tracking Sensor**********/
#define SensorLeft A0 //input pin of left sensor
#define SensorMiddle A1 //input pin of middle sensor
#define SensorRight A2 //input pin of right sensor
void setup() {
Serial.begin(9600); //Start the serial monitor and set baud rate to 9600
/****All Line Tracking Sensor interface are set to input mode***/
pinMode(SensorLeft, INPUT);
pinMode(SensorMiddle, INPUT);
pinMode(SensorRight, INPUT);
void loop() {
uint8_t SL = digitalRead(SensorLeft); //Read the value of the left Line Tracking Sensor
uint8_t SM = digitalRead(SensorMiddle); //Read the value of the intermediate Line Tracking Sensor
uint8_t SR = digitalRead(SensorRight); //Read the value of the right Line Tracking Sensor
if (SM == HIGH) {
Serial.print("Left:"); //Serial port prints the information
Serial.print(" Middle:");
Serial.print(SM);
Serial.print(" Right:");
Serial.print(SR);
Serial.println(); //Line feed
line feed
line feed
delay(100);
}
board, open the serial monitor and set baud rate to 9600. If the
www.keyestudio.com
90
keyestudio
line tracking sensor does not detect any object, the output signal
is high level, that is, 1. When we use a white object for occlusion,
input mode.
read signal, and then print it out through the serial port.
printing.
www.keyestudio.com
91
keyestudio
(1) Description
use a line tracking sensor to detect the black track of the road,
and the road surface detection signal is fed back to the control
steering of the car, making the car run automatically along the
www.keyestudio.com
92
keyestudio
car.
www.keyestudio.com
93
keyestudio
#define M4 8
void setup() {
Serial.begin(9600); //Start the serial monitor and set baud rate to 9600
/****All motor control pins are set to output mode***/
pinMode(M1, OUTPUT);
pinMode(PWM1, OUTPUT);
pinMode(M2, OUTPUT);
pinMode(PWM2, OUTPUT);
pinMode(M3, OUTPUT);
pinMode(PWM3, OUTPUT);
pinMode(M4, OUTPUT);
pinMode(PWM4, OUTPUT);
/****All Line Tracking Sensor interface are set to input mode***/
pinMode(SensorLeft, INPUT);
pinMode(SensorMiddle, INPUT);
pinMode(SensorRight, INPUT);
void loop() {
uint8_t SL = digitalRead(SensorLeft); //Read the value of the left Line Tracking Sensor
uint8_t SM = digitalRead(SensorMiddle); //Read the value of the left Line Tracking Sensor
uint8_t SR = digitalRead(SensorRight); //Read the value of the right Line Tracking Sensor
if (SM == HIGH) {
www.keyestudio.com
94
keyestudio
if (SL == LOW && SR == HIGH) { // black on right, white on left, turn right
Turn_Right();
}
else if (SR == LOW && SL == HIGH) { // black on left, white on right, turn left
Turn_Left();
}
else { // white on both sides, going forward
Advance();
}
}
else {
if (SL == LOW && SR == HIGH) { // black on right, white on left, turn right
Turn_Right();
}
else if (SR == LOW && SL == HIGH) { // white on right, black on left, turn left
Turn_Left();
}
else { // all white, stop
Stop();
}
}
}
digitalWrite(M2, LOW);
analogWrite(PWM2, speed2);
digitalWrite(M3, LOW);
analogWrite(PWM3, speed3);
}
www.keyestudio.com
95
keyestudio
digitalWrite(M2, HIGH);
analogWrite(PWM2, 255 - speed2);
digitalWrite(M3, HIGH);
analogWrite(PWM3, 255 - speed3);
}
digitalWrite(M2, HIGH);
analogWrite(PWM2, 255 - speed2);
digitalWrite(M3, HIGH);
analogWrite(PWM3, 255 - speed3);
}
digitalWrite(M2, LOW);
analogWrite(PWM2, speed2);
digitalWrite(M3, LOW);
analogWrite(PWM3, speed3);
}
www.keyestudio.com
96
keyestudio
}
Note: (1) The width of the black line must be greater than or
(2) When testing the car, do not under the sun. If problems
(1)
Description
www.keyestudio.com
97
keyestudio
The ultrasonic sensor uses sonar to determine distance to an
receiver modules.
with arduino and the ultrasonic sensor, and learn how to use the
sensor in arduino.
Working voltage:+5V DC
Precision: 0.3 cm
www.keyestudio.com
98
keyestudio
Principle
direction, and the counter starts timing at the same time of the
(1) Pull down TRIG then trigger high level signals with least 10us;
return;
(3) If there is a signal return, ECHO outputs a high level, the high
www.keyestudio.com
99
keyestudio
(3) Test Code
/*
Keyestudio 4WD Mecanum Robot for Arduino
lesson 7
Ultrasonic sensor
http://www.keyestudio.com
*/
void setup() {
Serial.begin(9600); //Set baud rate to 9600
pinMode(EchoPin, INPUT); //The ECHO pin is set to input mode
pinMode(TrigPin, OUTPUT); //The TRIG pin is set to output mode
}
void loop() {
float distance = Get_Distance(); //Get the distance and save in the distance variable
Serial.print("ditance:");
Serial.print(distance);
Serial.println("cm");
delay(100);
}
www.keyestudio.com
10
0
keyestudio
}
monitor and set the baud rate to 9600, the monitor will
www.keyestudio.com
10
1
keyestudio
The time is t (us), the total distance back and forth is s=v*t. V is
(1) Description
The ultrasonic sensor detects the smart car and the obstacle
www.keyestudio.com
10
2
keyestudio
/*
Keyestudio 4WD Mecanum Robot for Arduino
lesson 8
Ultrasonic follow Robot
http://www.keyestudio.com
*/
#include <Servo.h>
Servo myservo; //Define an instance of a servo
www.keyestudio.com
10
3
keyestudio
/****JM1Motor controls pins***/
#define PWM1 3
#define M1 2
/****JM4Motor controls pins***/
#define PWM4 11
#define M4 8
void setup() {
Serial.begin(9600); //Set baud rate to 9600
myservo.attach(9); //The pins of the servo are connected to D9
/****All motor control pins are set to output mode***/
pinMode(M1, OUTPUT);
pinMode(PWM1, OUTPUT);
pinMode(M2, OUTPUT);
pinMode(PWM2, OUTPUT);
pinMode(M3, OUTPUT);
pinMode(PWM3, OUTPUT);
pinMode(M4, OUTPUT);
pinMode(PWM4, OUTPUT);
pinMode(EchoPin, INPUT); //The ECHO pin is set to input mode
pinMode(TrigPin, OUTPUT); //The TRIG pin is set to output mode
myservo.write(90); //Rotate to 90 degrees
delay(100);
www.keyestudio.com
10
4
keyestudio
}
void loop() {
float distance = Get_Distance(); ///Get the distance and save in the distance variable
if (distance >= 20 && distance <= 40) //The range of advance
{
Advance();
}
else if (distance > 10 && distance < 20) //The range of stop
{
Stop();
}
else if (distance <= 10) //The range of moving back
{
Back();
}
else //Other cases stop
{
Stop();
}
}
digitalWrite(M2, LOW);
analogWrite(PWM2, speed2);
www.keyestudio.com
10
5
keyestudio
digitalWrite(M3, LOW);
analogWrite(PWM3, speed3);
}
digitalWrite(M2, HIGH);
analogWrite(PWM2, 255 - speed2);
digitalWrite(M3, HIGH);
analogWrite(PWM3, 255 - speed3);
}
digitalWrite(M2, HIGH);
analogWrite(PWM2, 255 - speed2);
digitalWrite(M3, HIGH);
analogWrite(PWM3, 255 - speed3);
}
digitalWrite(M2, LOW);
analogWrite(PWM2, speed2);
digitalWrite(M3, LOW);
analogWrite(PWM3, speed3);
}
www.keyestudio.com
10
6
keyestudio
/**********The car stops***********/
void Stop(void) {
digitalWrite(M1, LOW);
analogWrite(PWM1, 0);
digitalWrite(M2, LOW);
analogWrite(PWM2, 0);
digitalWrite(M3, LOW);
analogWrite(PWM3, 0);
digitalWrite(M4, LOW);
analogWrite(PWM4, 0);
}
After uploading the test code successfully, the servo will rotates
Note: The obstacles can solely move in front of the car, which can
not turn.
www.keyestudio.com
10
7
keyestudio
Project 9: Ultrasonic Avoidance Smart Car
(1) Description
www.keyestudio.com
10
8
keyestudio
www.keyestudio.com
10
9
keyestudio
/****JM2Motor controls pins***/
#define PWM2 5
#define M2 4
/****JM3Motor controls pins***/
#define PWM3 6
#define M3 7
void loop() {
distance_M = Get_Distance(); //Get the distance and save in the distance variable
if (distance_M < 20) {//When the distance in front is less than 20cm
Stop(); //Robot stop
delay(500); //Delay 500ms
myservo.write(180); //Ultrasonic cradle head turns left
www.keyestudio.com
11
0
keyestudio
delay(500); //Delay 500ms
distance_L = Get_Distance(); //Assign the left ultrasonic distance to variable a1
delay(100); //Read values when stable
myservo.write(0); //ltrasonic cradle head turns right
delay(500); //Delay 500ms
distance_R = Get_Distance(); //Assign the right ultrasonic distance to variable a2
delay(100); //Read values when stable
digitalWrite(M2, LOW);
www.keyestudio.com
11
1
keyestudio
analogWrite(PWM2, speed2);
digitalWrite(M3, LOW);
analogWrite(PWM3, speed3);
}
digitalWrite(M2, HIGH);
analogWrite(PWM2, 255 - speed2);
digitalWrite(M3, HIGH);
analogWrite(PWM3, 255 - speed3);
}
digitalWrite(M2, HIGH);
analogWrite(PWM2, 255 - speed2);
digitalWrite(M3, HIGH);
analogWrite(PWM3, 255 - speed3);
}
digitalWrite(M2, LOW);
analogWrite(PWM2, speed2);
digitalWrite(M3, LOW);
analogWrite(PWM3, speed3);
}
www.keyestudio.com
11
2
keyestudio
/**********The car stops***********/
void Stop(void) {
digitalWrite(M1, LOW);
analogWrite(PWM1, 0);
digitalWrite(M2, LOW);
analogWrite(PWM2, 0);
digitalWrite(M3, LOW);
analogWrite(PWM3, 0);
digitalWrite(M4, LOW);
analogWrite(PWM4, 0);
}
The car moves forward when the distance from the obstacle in
the left distance is bigger than the right, the car turns left,
(1) Description
www.keyestudio.com
11
3
keyestudio
There is no doubt that the IR remote control can be seen
the work from the infrared receiving output compatible with TTL
www.keyestudio.com
11
4
keyestudio
The 38KHz carrier signal transmitted by the IR remote control is
and 1 signals.
The user code of the same remote control is unchanged, and the
data difference.
When pressing the key , the remote control sends out the
key is pressed.
www.keyestudio.com
11
5
keyestudio
void setup()
{
Serial.begin(9600);
// In case the interrupt driver crashes on setup, give a clue
// to the user what's going on.
Serial.println("Enabling IRin");
irrecv.enableIRIn(); // Start the receiver
Serial.println("Enabled IRin");
}
www.keyestudio.com
11
6
keyestudio
void loop() {
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
irrecv.resume(); // Receive the next value
}
delay(100);
}
monitor and set the baud rate to 9600. Send signals with the
www.keyestudio.com
11
7
keyestudio
decode succeeded.
www.keyestudio.com
11
8
keyestudio
Project 11: IR Remote Control Smart Car
(1) Description
control.
www.keyestudio.com
11
9
keyestudio
Infrared The key The status of the
0xFF02FD Stop
:
www.keyestudio.com
12
0
keyestudio
0xFFB04F Upper right move
:
www.keyestudio.com
12
1
keyestudio
#define speed1 100 //Righr front motor speed
#define speed4 100 //Righr rear motor speed
void setup()
{
Serial.begin(9600);
/****All motor control pins are set to output mode***/
pinMode(M1, OUTPUT);
pinMode(PWM1, OUTPUT);
pinMode(M2, OUTPUT);
pinMode(PWM2, OUTPUT);
pinMode(M3, OUTPUT);
pinMode(PWM3, OUTPUT);
pinMode(M4, OUTPUT);
pinMode(PWM4, OUTPUT);
// In case the interrupt driver crashes on setup, give a clue
// to the user what's going on.
irrecv.enableIRIn(); // Start the receiver
}
void loop() {
if (irrecv.decode(&results)) {
switch (results.value)
{
case 0xFF629D: Advance(); break;//
case 0xFFA857: Back(); break;//
case 0xFF22DD: Turn_Left(); break;//
case 0xFFC23D: Turn_Right(); break;//
case 0xFF02FD: Stop(); break;//OK
case 0xFF30CF: L_Move(); break;//4
case 0xFF7A85: R_Move(); break;//6
case 0xFF6897: LU_Move(); break;//1
case 0xFF10EF: LD_Move(); break;//7
case 0xFFB04F: RU_Move(); break;//3
case 0xFF5AA5: RD_Move(); break;//9
default: break;
}
irrecv.resume(); // Receive the next value
}
www.keyestudio.com
12
2
keyestudio
}
digitalWrite(M2, LOW);
analogWrite(PWM2, speed2);
digitalWrite(M3, LOW);
analogWrite(PWM3, speed3);
}
digitalWrite(M2, HIGH);
analogWrite(PWM2, 255 - speed2);
digitalWrite(M3, HIGH);
analogWrite(PWM3, 255 - speed3);
}
digitalWrite(M2, HIGH);
analogWrite(PWM2, 255 - speed2);
digitalWrite(M3, HIGH);
analogWrite(PWM3, 255 - speed3);
}
www.keyestudio.com
12
3
keyestudio
digitalWrite(M1, HIGH);
analogWrite(PWM1, 255 - speed1);
digitalWrite(M4, HIGH);
analogWrite(PWM4, 255 - speed4);
digitalWrite(M2, LOW);
analogWrite(PWM2, speed2);
digitalWrite(M3, LOW);
analogWrite(PWM3, speed3);
}
digitalWrite(M2, HIGH);
analogWrite(PWM2, 255 - speed2);
digitalWrite(M3, LOW);
analogWrite(PWM3, speed3);
}
www.keyestudio.com
12
4
keyestudio
digitalWrite(M2, LOW);
analogWrite(PWM2, speed2);
digitalWrite(M3, HIGH);
analogWrite(PWM3, 255 - speed3);
}
digitalWrite(M2, LOW);
analogWrite(PWM2, 0);
digitalWrite(M3, LOW);
analogWrite(PWM3, speed3);
}
digitalWrite(M2, HIGH);
analogWrite(PWM2, 255 - speed2);
digitalWrite(M3, LOW);
analogWrite(PWM3, 0);
}
digitalWrite(M2, LOW);
www.keyestudio.com
12
5
keyestudio
analogWrite(PWM2, speed2);
digitalWrite(M3, LOW);
analogWrite(PWM3, 0);
}
digitalWrite(M2, LOW);
analogWrite(PWM2, 0);
digitalWrite(M3, HIGH);
analogWrite(PWM3, 255 - speed3);
}
Uploading the test code and powering up. Align the IR remote
the car.
www.keyestudio.com
12
6
keyestudio
appears.
(1) Description
such as the baud rate and device name of the serial port as
required.
low cost, small size, low power consumption and high sensitivity
all modules of the car in one code, which contains all functions
www.keyestudio.com
12
7
keyestudio
except IR remote control mentioned in the previous projects. We
convenient.
the APP. A dialog box appears on the APP interface, then click
"OK" .
4. First turn on the bluetooth of the phone /iPad, and then click
www.keyestudio.com
12
8
keyestudio
the connect button (control) in the upper right corner of the APP
www.keyestudio.com
12
9
keyestudio
Find "BT24" in the search results and click "Connect" :
displayed as follows:
www.keyestudio.com
13
0
keyestudio
Search for mecanum Robot on Google Play or download:
http://8.210.52.206/mecanum_robot.apk
APP, and a dialog box appears. Click "Allow" in the dialog box to
open the bluetooth of the mobile phone. You can also turn on
www.keyestudio.com
13
1
keyestudio
your phone's bluetooth before opening the APP.
Click the icon above, enter the APP and select TANK ROBOT, then
password.
After the APP is installed, let's test the code together: The
www.keyestudio.com
13
2
keyestudio
code may not be burned, and we need to open the positioning
code)
Upload the code to the development board, and then plug in the
bluetooth module.
/*
Keyestudio 4WD Mecanum Robot for Arduino
lesson 12.1
APP Test
http://www.keyestudio.com
*/
void setup() {
Serial.begin(9600);//Start the serial monitor and set the baud rate to 9600
}
void loop() {
if (Serial.available())//If the receiving area is not empty
{
char ble_val = Serial.read();//Read the data of the bluetooth
Serial.println(ble_val);//The serial monitor prints the read data
}
}
www.keyestudio.com
13
3
keyestudio
plug it upside down. Connect the bluetooth module and turn on
Aim at the bluetooth module and press the APP button of the
www.keyestudio.com
13
4
keyestudio
#include <Adafruit_NeoPixel.h>
#include <Servo.h>
Servo myservo; //Define an instance of a servo
//Create a category named rgb_2812 to control 4 LEDs and the pins are connected to D10
Adafruit_NeoPixel rgb_2812 = Adafruit_NeoPixel(4, 10, NEO_GRB + NEO_KHZ800);
#define LED A3
www.keyestudio.com
13
5
keyestudio
/*******Ultrasonic Sensor interface*****/
#define EchoPin 13 //ECHO to D13
#define TrigPin 12 //TRIG to D12
/********默认速度补偿 30***********/
uint8_t speed1 = 100; //Right front motor speed
uint8_t speed4 = 100; //Right rear motor speed
void setup() {
Serial.begin(9600);//Start the serial monitor and set baud rate to 9600
myservo.attach(9); //The pin of the servo is connected to D9
pinMode(LED, OUTPUT);//Configure 7-color LED pins mode for output
/****All motor control pins are set to output mode***/
pinMode(M1, OUTPUT);
pinMode(PWM1, OUTPUT);
pinMode(M2, OUTPUT);
pinMode(PWM2, OUTPUT);
pinMode(M3, OUTPUT);
pinMode(PWM3, OUTPUT);
www.keyestudio.com
13
6
keyestudio
pinMode(M4, OUTPUT);
pinMode(PWM4, OUTPUT);
/****All interfaces of the Line Tracking Sensor are set to input mode***/
pinMode(SensorLeft, INPUT);
pinMode(SensorMiddle, INPUT);
pinMode(SensorRight, INPUT);
/***********main loop***************/
void loop() {
if (Serial.available())//If the receiving area is not empty
{
ble_val = Serial.read();//Read Bluetooth data
Serial.println(ble_val);//The serial prints the read data
switch (ble_val)
{
/*********************小车行驶************************/
case 's': Stop(); break; //Stop
case 'a': Advance(); break; //Go ahead
case 'c': Back(); break; //retreat
case 'b': Turn_Left(); break; //Turn left
case 'd': Turn_Right(); break; //Turn right
case 'k': L_Move(); break; //Left shift
case 'h': R_Move(); break; //Right shift
case 'l': LU_Move(); break; //Upper left move
case 'j': LD_Move(); break; //lower left move
case 'g': RU_Move(); break; //Upper right move
case 'i': RD_Move(); break; //lower right move
case 'e': drift_left(); break; //Drift
case 'f': drift_right(); break; //Drift
www.keyestudio.com
13
7
keyestudio
case 'r': Avoidance(); break; //Obstacle Avoidance
www.keyestudio.com
13
8
keyestudio
break;
default: break;
}
}
}
/*********************Obstacle avoidance*******************************/
void Avoidance(void)
{
while (1)
{
distance_M = Get_Distance(); //Get the distance and save in the distance variable
if (distance_M < 20) {//When the distance in front is less than 20cm
Stop(); //Robot stop
delay(500); //Delay 500ms
myservo.write(180); //Ultrasonic cradle head turns left
delay(500); //延时 500ms
distance_L = Get_Distance(); //Assign the left ultrasonic distance to variable a1
delay(100); //Read values when stable
myservo.write(0); //Ultrasonic cradle head turns right
delay(500); //Delay 500ms
distance_R = Get_Distance(); //Assign the right ultrasonic distance to variable a2
delay(100); //Read values when stable
www.keyestudio.com
13
9
keyestudio
}
}
}
/*********************Follow*******************************/
void Follow(void)
{
while (1)
{
float distance = Get_Distance(); //Get the distance and save in the distance variable
if (distance >= 20 && distance <= 40) //Range of advance
{
Advance();
}
else if (distance > 10 && distance < 20) //Range of stop
{
Stop();
}
else if (distance <= 10) //Range of fall back
{
Back();
}
else //Other cases stop
{
Stop();
}
ble_val = Serial.read();//Read the data of the bluetooth
if (ble_val == 's') {
Stop();
break;
}
}
}
/*********************Line Tracking*******************************/
void Tracking(void) { //Tracking black line
while (1)
{
uint8_t SL = digitalRead(SensorLeft); //Read the value of the left Line Tracking Sensor
uint8_t SM = digitalRead(SensorMiddle); //Read the value of the intermediate Line Tracking Sensor
uint8_t SR = digitalRead(SensorRight); //Read the value of the right Line Tracking Sensor
if (SM == HIGH) {
if (SL == LOW && SR == HIGH) { // black on right, white on left, turn right
www.keyestudio.com
14
0
keyestudio
Turn_Right();
}
else if (SR == LOW && SL == HIGH) { // black on left, white on right, turn left
Turn_Left();
}
else { // white on both sides, going forward
Advance();
}
}
else {
if (SL == LOW && SR == HIGH) { // black on right, white on left, turn right
Turn_Right();
}
else if (SR == LOW && SL == HIGH) { // white on right, black on left, turn left
Turn_Left();
}
else { // all white, stop
Stop();
}
}
ble_val = Serial.read();//Read the data of the bluetooth
if (ble_val == 's') {
Stop();
break;
}
}
}
/*********************RGB2812 display*******************************/
void showColor() {
www.keyestudio.com
14
1
keyestudio
// Serial.print("color num:"); //Serial debugging
// Serial.println(color_num);
// There are only 7 colors, you can add them yourself
if (color_num > 6)color_num = 0;
if (color_num < 0)color_num = 6;
switch (color_num) {
case 0:
for (int i = 0; i < 4; i++) {
rgb_2812.setPixelColor(i, 255, 0, 0); //The iLED is red
}
rgb_2812.show(); //Display
break;
case 1:
for (int i = 0; i < 4; i++) {
rgb_2812.setPixelColor(i, 255, 80, 0); //The iLED is orange
}
rgb_2812.show(); //Display
break;
case 2:
for (int i = 0; i < 4; i++) {
rgb_2812.setPixelColor(i, 255, 255, 0); //The iLED is yellow
}
rgb_2812.show(); //Display
break;
case 3:
for (int i = 0; i < 4; i++) {
rgb_2812.setPixelColor(i, 0, 255, 0); //The iLED is green
}
rgb_2812.show(); //Display
break;
case 4:
for (int i = 0; i < 4; i++) {
rgb_2812.setPixelColor(i, 0, 0, 255); //The iLED is blue
}
rgb_2812.show(); //Display
break;
case 5:
for (int i = 0; i < 4; i++) {
rgb_2812.setPixelColor(i, 0, 255, 255); //The iLED is indigo
}
rgb_2812.show(); //Display
break;
case 6:
www.keyestudio.com
14
2
keyestudio
for (int i = 0; i < 4; i++) {
rgb_2812.setPixelColor(i, 160, 32, 240);//The iLED is purple
}
rgb_2812.show(); //Display
break;
default : break;
}
}
/*********************Close RGB2812*******************************/
void closeColor() { //Close aii 2818 LEDs
for (int i = 0; i < 4; i++) {
rgb_2812.setPixelColor(i, 0, 0, 0); //The iLED goes off
rgb_2812.show(); //Execute display
}
}
digitalWrite(M2, LOW);
analogWrite(PWM2, speed2);
digitalWrite(M3, LOW);
analogWrite(PWM3, speed3);
}
digitalWrite(M2, HIGH);
analogWrite(PWM2, 255 - speed2);
digitalWrite(M3, HIGH);
analogWrite(PWM3, 255 - speed3);
}
www.keyestudio.com
14
3
keyestudio
/**********The car rotates on the left***********/
void Turn_Left(void) {
digitalWrite(M1, LOW);
analogWrite(PWM1, speed1);
digitalWrite(M4, LOW);
analogWrite(PWM4, speed4);
digitalWrite(M2, HIGH);
analogWrite(PWM2, 255 - speed2);
digitalWrite(M3, HIGH);
analogWrite(PWM3, 255 - speed3);
}
digitalWrite(M2, LOW);
analogWrite(PWM2, speed2);
digitalWrite(M3, LOW);
analogWrite(PWM3, speed3);
}
www.keyestudio.com
14
4
keyestudio
analogWrite(PWM4, 255 - speed4);
digitalWrite(M2, HIGH);
analogWrite(PWM2, 255 - speed2);
digitalWrite(M3, LOW);
analogWrite(PWM3, speed3);
}
digitalWrite(M2, LOW);
analogWrite(PWM2, speed2);
digitalWrite(M3, HIGH);
analogWrite(PWM3, 255 - speed3);
}
digitalWrite(M2, LOW);
analogWrite(PWM2, 0);
digitalWrite(M3, LOW);
analogWrite(PWM3, speed3);
}
www.keyestudio.com
14
5
keyestudio
digitalWrite(M2, HIGH);
analogWrite(PWM2, 255 - speed2);
digitalWrite(M3, LOW);
analogWrite(PWM3, 0);
}
digitalWrite(M2, LOW);
analogWrite(PWM2, speed2);
digitalWrite(M3, LOW);
analogWrite(PWM3, 0);
}
digitalWrite(M2, LOW);
analogWrite(PWM2, 0);
digitalWrite(M3, HIGH);
analogWrite(PWM3, 255 - speed3);
}
digitalWrite(M2, LOW);
analogWrite(PWM2, 0);
www.keyestudio.com
14
6
keyestudio
digitalWrite(M3, HIGH);
analogWrite(PWM3, 255 - speed3);
}
digitalWrite(M2, LOW);
analogWrite(PWM2, 0);
digitalWrite(M3, LOW);
analogWrite(PWM3, speed3);
}
mode.
www.keyestudio.com
14
7
keyestudio
Click to enter follow mode, click it again will exit the mode.
speed of the two motors on the left and the same operation on
the right.
2812 LEDs under the bottom plate, and the middle button is used
The rest of the buttons are all used to drive the car, but unlike
the other buttons, they drive when we press them and stop when
we release them.
www.keyestudio.com
14
8
keyestudio
8. Common Problems
9. Resource
https://fs.keyestudio.com/KS0551
www.keyestudio.com
14
9