Make Your Own Arduino Powered 4-in-1 Robot Car | Step by Step instructions

Hello and welcome back! In this project, we will learn how to make a 4-in-1 2WD Robot Car using the Arduino UNO platform. This robot can be controlled in four ways, IR remote control, line tracking, Bluetooth control, and WiFi control. For this, I used the OSOYOO 2WD Robot Car Starter Kit, so we don’t need any additional chassis or extra components. You can get this kit at an affordable price using this link, and there’s also a 15% discount available. Also, you can easily build and program this robot with the provided instructions. Even if you have no experience with robotics, don’t worry! This kit will guide you step by step, making everything simple to understand.
With this kit, you don’t need to write your own programs either. The kit provides pre-written codes, so you can test them and improve your knowledge. Also, this is a great way to learn about Arduino, sensors, motor control, and wireless communication.
- You can find more details about this kit by clicking on this link – Click here.
Ok, let’s do it step by step. You can buy it with a 15% discount. Simply visit the OSOYOO official website and use the following coupon code.
Step 1
Firstly, unbox your robot package and check all the components inside the box.



















Step 2
Second, remove the robot chassis sticker and attach the motor metal brackets to the motors. Then install them on the chassis.








Step 3
Thirdly, install the caster wheel onto the chassis.






Step 4
Now, attach the wheels to the gear motors.




Step 5
After that, install the Arduino UNO board and the motor driver board onto the robot chassis.









Step 6
Next, connect the motors to the motor driver board. After that, install the battery holder onto the chassis.







Step 7
Now, connect the WiFi Shield to the Arduino UNO board. Then, install the IR receiver module at the front of the robot.





Step 8
Afterward, install the tracking sensor modules onto the chassis.





Step 9
Okay, now connect the battery holder to the shield. Then, connect the motor driver board, IR receiver, and line tracking modules to the WiFi shield.
Motor Pins
- Black — D9
- Purple — D12
- Green — D11
- Yellow — D7
- White — D8
- Red — D6
IR Receiver
- GND — GND
- 5V — VCC
- D10 — S
Left Tracking Sensor
- VCC — 5V
- GND — GND
- DO — D2
Right Tracking Sensor
- VCC — 5V
- GND — GND
- DO — D3








Step 10
Afterward, arrange the wires neatly using tape. Then, charge the 18650 Li-ion batteries and place them into the battery holder.





Step 11
Now, let’s upload the programs one by one to the robot car. Follow the instructions below for each step.
IR Control of the Robot Car
- Connect your robot to the computer. Then, copy and paste the following code into the Arduino IDE. Also, make sure to include the IRremote library in the Arduino IDE before uploading the code.
- IRremote library — Download
- Program — Download

/* ___ ___ ___ _ _ ___ ___ ____ ___ ____
* / _ \ /___)/ _ \| | | |/ _ \ / _ \ / ___) _ \| \
*| |_| |___ | |_| | |_| | |_| | |_| ( (__| |_| | | | |
* \___/(___/ \___/ \__ |\___/ \___(_)____)___/|_|_|_|
* (____/
* www.osoyoo.com IR remote control smart car
* program tutorial https://osoyoo.com/2017/09/21/2wd-robot-car-infrared-remote/
* Copyright John Yu
*/
#include "IRremote.hpp"
#define IR_RECEIVE_PIN 10 //IR receiver Signal pin connect to Arduino pin D10
#define speedPinR 9 // RIGHT PWM pin connect MODEL-X ENA
#define RightMotorDirPin1 12 //Right Motor direction pin 1 to MODEL-X IN1
#define RightMotorDirPin2 11 //Right Motor direction pin 2 to MODEL-X IN2
#define speedPinL 6 // Left PWM pin connect MODEL-X ENB
#define LeftMotorDirPin1 7 //Left Motor direction pin 1 to MODEL-X IN3
#define LeftMotorDirPin2 8 //Left Motor direction pin 1 to MODEL-X IN4
#define IR_ADVANCE 24 //code from IR controller "▲" button
#define IR_BACK 82 //code from IR controller "▼" button
#define IR_RIGHT 90 //code from IR controller ">" button
#define IR_LEFT 8 //code from IR controller "<" button
#define IR_STOP 28 //code from IR controller "OK" button
#define IR_turnsmallleft 13 //code from IR controller "#" button
enum DN
{
GO_ADVANCE, //go forward
GO_LEFT, //left turn
GO_RIGHT,//right turn
GO_BACK,//backward
STOP_STOP,
DEF
}Drive_Num=DEF;
bool stopFlag = true;//set stop flag
bool JogFlag = false;
uint16_t JogTimeCnt = 0;
uint32_t JogTime=0;
uint8_t motor_update_flag = 0;
/***************motor control***************/
void go_Advance(void) //Forward
{
digitalWrite(RightMotorDirPin1, HIGH);
digitalWrite(RightMotorDirPin2,LOW);
digitalWrite(LeftMotorDirPin1,HIGH);
digitalWrite(LeftMotorDirPin2,LOW);
analogWrite(speedPinL,150);
analogWrite(speedPinR,150);
}
void go_Left(int t=0) //Turn left
{
digitalWrite(RightMotorDirPin1, HIGH);
digitalWrite(RightMotorDirPin2,LOW);
digitalWrite(LeftMotorDirPin1,LOW);
digitalWrite(LeftMotorDirPin2,HIGH);
analogWrite(speedPinL,0);
analogWrite(speedPinR,150);
delay(t);
}
void go_Right(int t=0) //Turn right
{
digitalWrite(RightMotorDirPin1, LOW);
digitalWrite(RightMotorDirPin2,HIGH);
digitalWrite(LeftMotorDirPin1,HIGH);
digitalWrite(LeftMotorDirPin2,LOW);
analogWrite(speedPinL,150);
analogWrite(speedPinR,0);
delay(t);
}
void go_Back(int t=0) //Reverse
{
digitalWrite(RightMotorDirPin1, LOW);
digitalWrite(RightMotorDirPin2,HIGH);
digitalWrite(LeftMotorDirPin1,LOW);
digitalWrite(LeftMotorDirPin2,HIGH);
analogWrite(speedPinL,150);
analogWrite(speedPinR,100);
delay(t);
}
void stop_Stop() //Stop
{
digitalWrite(RightMotorDirPin1, LOW);
digitalWrite(RightMotorDirPin2,LOW);
digitalWrite(LeftMotorDirPin1,LOW);
digitalWrite(LeftMotorDirPin2,LOW);
}
/**************detect IR code***************/
void do_IR_Tick()
{
if(IrReceiver.decode())
{
uint16_t command = IrReceiver.decodedIRData.command;
if(command==IR_ADVANCE)
{
Drive_Num=GO_ADVANCE;
}
else if(command==IR_RIGHT)
{
Drive_Num=GO_RIGHT;
}
else if(command==IR_LEFT)
{
Drive_Num=GO_LEFT;
}
else if(command==IR_BACK)
{
Drive_Num=GO_BACK;
}
else if(command==IR_STOP)
{
Drive_Num=STOP_STOP;
}
command = 0;
IrReceiver.resume();
}
}
/**************car control**************/
void do_Drive_Tick()
{
switch (Drive_Num)
{
case GO_ADVANCE:go_Advance();JogFlag = true;JogTimeCnt = 1;JogTime=millis();break;//if GO_ADVANCE code is detected, then go advance
case GO_LEFT: go_Left();JogFlag = true;JogTimeCnt = 1;JogTime=millis();break;//if GO_LEFT code is detected, then turn left
case GO_RIGHT: go_Right();JogFlag = true;JogTimeCnt = 1;JogTime=millis();break;//if GO_RIGHT code is detected, then turn right
case GO_BACK: go_Back();JogFlag = true;JogTimeCnt = 1;JogTime=millis();break;//if GO_BACK code is detected, then backward
case STOP_STOP: stop_Stop();JogTime = 0;break;//stop
default:break;
}
Drive_Num=DEF;
//keep current moving mode for 200 millis seconds
if(millis()-JogTime>=200)
{
JogTime=millis();
if(JogFlag == true)
{
stopFlag = false;
if(JogTimeCnt <= 0)
{
JogFlag = false; stopFlag = true;
}
JogTimeCnt--;
}
if(stopFlag == true)
{
JogTimeCnt=0;
stop_Stop();
}
}
}
void setup()
{
pinMode(RightMotorDirPin1, OUTPUT);
pinMode(RightMotorDirPin2, OUTPUT);
pinMode(speedPinL, OUTPUT);
pinMode(LeftMotorDirPin1, OUTPUT);
pinMode(LeftMotorDirPin2, OUTPUT);
pinMode(speedPinR, OUTPUT);
stop_Stop();
IrReceiver.begin(IR_RECEIVE_PIN, DISABLE_LED_FEEDBACK);
}
void loop()
{
do_IR_Tick();
do_Drive_Tick();
}


- Next, select the board and port. After, click the upload button.



- Now, you can test the project using the arrow keys on the IR remote.




Line Tracking of the Robot Car
- Connect the robot to the computer, then copy and paste the following code into the Arduino IDE.
- Program — Download
/* ___ ___ ___ _ _ ___ ___ ____ ___ ____
* / _ \ /___)/ _ \| | | |/ _ \ / _ \ / ___) _ \| \
*| |_| |___ | |_| | |_| | |_| | |_| ( (__| |_| | | | |
* \___/(___/ \___/ \__ |\___/ \___(_)____)___/|_|_|_|
* (____/
* Arduino 2WD robot Car Tutorial Lesson 1
* Tutorial URL https://osoyoo.com/?p=11380
* CopyRight www.osoyoo.com
* This program will make car tracking a black line
*/
#define IN1 12 //K1、K2 motor direction
#define IN2 11 //K1、K2 motor direction
#define IN3 7 //K3、K4 motor direction
#define IN4 8 //K3、K4 motor direction
#define ENA 9 // Needs to be a PWM pin to be able to control motor speed ENA
#define ENB 6 // Needs to be a PWM pin to be able to control motor speed ENA
#define RightSensor 3
#define LeftSensor 2
#define RIGHT_TIME 15
#define LEFT_TIME 15
#define FORWARD_TIME 15
#define FORWARD_SPEED 120
int left_status=0;
int right_status=0;
#define M_SPEED1 150 //motor speed
#define M_SPEED2 170 //motor speed
/*motor control*/
void go_back() //motor rotate clockwise -->robot go ahead
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4,HIGH);
}
void go_ahead(int t=FORWARD_TIME) //motor rotate counterclockwise -->robot go back
{
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4,LOW);
delay(t);
}
void go_stop() //motor brake -->robot stop
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4,LOW);
}
void turn_left(int t) //left motor rotate clockwise and right motor rotate counterclockwise -->robot turn right
{
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
delay(t);
}
void turn_right(int t) //left motor rotate counterclockwise and right motor rotate clockwise -->robot turn left
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
delay(t);
}
/*set motor speed */
void set_motorspeed(int lspeed,int rspeed) //change motor speed
{
analogWrite(ENA,lspeed);//lspeed:0-255
analogWrite(ENB,rspeed);//rspeed:0-255
}
/*read line folloe sensors*/
void read_sensor_values()
{
left_status=digitalRead(LeftSensor);
right_status=digitalRead(RightSensor);
Serial.print("right=");
Serial.print(right_status);
Serial.print(" left_status=");
Serial.println(left_status);
}
void auto_tarcking(){
read_sensor_values();
if((left_status==LOW)&&(right_status==HIGH)){ //The right sensor is on the black line.The left sensor is on the white line
set_motorspeed(M_SPEED1,M_SPEED1);
turn_right(RIGHT_TIME);
}
else if((left_status==HIGH)&&(right_status==LOW)){//The right sensor is on the white line.The left sensor is on the black line
set_motorspeed(M_SPEED1,M_SPEED1);
turn_left(LEFT_TIME);
}
else if((left_status==LOW)&&(right_status==LOW)){//The left an right sensor are on the white line.
set_motorspeed(FORWARD_SPEED,FORWARD_SPEED);
go_ahead();
}
else if((left_status==HIGH)&&(right_status==HIGH)){//The left an right sensor are on the black line.
go_stop();
}
}
void setup() {
/*line follow sensors */
pinMode(LeftSensor,INPUT);
pinMode(RightSensor,INPUT);
/******L298N******/
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
Serial.begin(9600);
}
void loop() {
auto_tarcking();
}
- Next, select the board and port. After, click the upload button.



- Now, you can test the robot on a black track over a white surface.


Bluetooth Control of the Robot Car
- Connect your robot to the computer. Then, copy and paste the following code into the Arduino IDE.
- Program — Download
/* ___ ___ ___ _ _ ___ ___ ____ ___ ____
* / _ \ /___)/ _ \| | | |/ _ \ / _ \ / ___) _ \| \
*| |_| |___ | |_| | |_| | |_| | |_| ( (__| |_| | | | |
* \___/(___/ \___/ \__ |\___/ \___(_)____)___/|_|_|_|
* (____/
* OSOYOO 2WD Smart Car Lesson 4: Mock Driving through Bluetooth APP
* Tutorial URL https://osoyoo.com/?p=11383
* CopyRight www.osoyoo.com
* After running the code, smart car will go forward 5 seconds, then go backward 5
* seconds, then left turn for 5 seconds then right turn for 5 seconds then stop.
*
*/
#include <SoftwareSerial.h>
SoftwareSerial BLTSerial(4, 5); //RX,TX
int buttonState;
#define dir1PinRight 12 // Right Motor direction pin 1 to MODEL-X IN1
#define dir2PinRight 11 // Right Motor direction pin 2 to MODEL-X IN2
#define speedPinRight 9 //RIGHT PWM pin connect MODEL-X ENA
#define speedPinLeft 6 // Left PWM pin connect MODEL-X ENB
#define dir1PinLeft 7 //Left Motor direction pin 1 to MODEL-X IN3
#define dir2PinLeft 8 //Left Motor direction pin 2 to MODEL-X IN4
#define TURNSpeed1 140
#define TURNSpeed2 90
//back speed
#define MAX_PACKETSIZE 32 //Serial receive buffer
#define SPEED 150
struct car_status{
int speed;
int angle;
int direct;
};
char buffUART[MAX_PACKETSIZE];
unsigned int buffUARTIndex = 0;
unsigned long preUARTTick = 0;
int Speed =150; //avoidance motor speed
;
/*motor control*/
void go_Advance(void) //Forward
{
digitalWrite(dir1PinRight, HIGH);
digitalWrite(dir2PinRight,LOW);
digitalWrite(dir1PinLeft,HIGH);
digitalWrite(dir2PinLeft,LOW);
}
void go_Left() //Turn left
{
digitalWrite(dir1PinRight, HIGH);
digitalWrite(dir2PinRight,LOW);
digitalWrite(dir1PinLeft,LOW);
digitalWrite(dir2PinLeft,HIGH);
}
void go_Right() //Turn right
{
digitalWrite(dir1PinRight, LOW);
digitalWrite(dir2PinRight,HIGH);
digitalWrite(dir1PinLeft,HIGH);
digitalWrite(dir2PinLeft,LOW);
}
void go_Back() //Reverse
{
digitalWrite(dir1PinRight, LOW);
digitalWrite(dir2PinRight,HIGH);
digitalWrite(dir1PinLeft,LOW);
digitalWrite(dir2PinLeft,HIGH);
}
void back_Left() //Reverse
{
digitalWrite(dir1PinRight, LOW);
digitalWrite(dir2PinRight,HIGH);
digitalWrite(dir1PinLeft,HIGH);
digitalWrite(dir2PinLeft,LOW);
}
void back_Right() //Reverse
{
digitalWrite(dir1PinRight, HIGH);
digitalWrite(dir2PinRight,LOW);
digitalWrite(dir1PinLeft,LOW);
digitalWrite(dir2PinLeft,HIGH);
}
void stop_Stop(){
digitalWrite(dir1PinRight,LOW);
digitalWrite(dir2PinRight,LOW);
digitalWrite(dir1PinLeft,LOW);
digitalWrite(dir2PinLeft,LOW);
set_Motorspeed(0,0) ;
}
/*set motor speed */
void set_Motorspeed(int speed_L,int speed_R)
{
analogWrite(speedPinRight,speed_R);
analogWrite(speedPinLeft,speed_L);
}
//WiFi / Bluetooth through the serial control
void do_Uart_Tick()
{
char Uart_Date=0;
if(BLTSerial.available())
{
size_t len = BLTSerial.available();
uint8_t sbuf[len + 1];
sbuf[len] = 0x00;
BLTSerial.readBytes(sbuf, len);
//parseUartPackage((char*)sbuf);
memcpy(buffUART + buffUARTIndex, sbuf, len);//ensure that the serial port can read the entire frame of data
buffUARTIndex += len;
preUARTTick = millis();
if(buffUARTIndex >= MAX_PACKETSIZE - 1)
{
buffUARTIndex = MAX_PACKETSIZE - 2;
preUARTTick = preUARTTick - 200;
}
}
car_status cs;
if(buffUARTIndex > 0 && (millis() - preUARTTick >= 100))//APP send flag to modify the obstacle avoidance parameters
{ //data ready
buffUART[buffUARTIndex] = 0x00;
Uart_Date=buffUART[0];
cs=get_status(buffUART);
buffUARTIndex = 0;
}
Speed=cs.speed+50;
if (Speed>250) Speed=250;
Serial.println(Uart_Date);
switch (Uart_Date) //serial control instructions
{
case 'M':
go_Advance();
set_Motorspeed(Speed,Speed) ;
break;
case 'L':
go_Left();
if(cs.angle=1){
set_Motorspeed(0,TURNSpeed2) ;
}
if(cs.angle=2){
set_Motorspeed(50,TURNSpeed1) ;
}
break;
case 'R':
go_Right();
if(cs.angle=-1){
set_Motorspeed(TURNSpeed2,0) ;
}
if(cs.angle=-2){
set_Motorspeed(TURNSpeed1,50) ;
}
break;
case 'B':
go_Back();
set_Motorspeed(Speed,Speed) ;
break;
case 'X':
back_Left();
if(cs.angle=1){
set_Motorspeed(0,TURNSpeed2) ;
}
if(cs.angle=2){
set_Motorspeed(50,TURNSpeed1) ;
}
break;
case 'Y':
back_Right();
if(cs.angle=-1){
set_Motorspeed(TURNSpeed2,0) ;
}
if(cs.angle=-2){
set_Motorspeed(TURNSpeed1,50) ;
}
break;
case 'E': stop_Stop() ;break;
case 'J': stop_Stop() ;break;
default:break;
}
}
//car motor control
void setup()
{
pinMode(dir1PinRight, OUTPUT);
pinMode(dir2PinRight, OUTPUT);
pinMode(speedPinRight, OUTPUT);
pinMode(dir1PinLeft, OUTPUT);
pinMode(dir2PinLeft, OUTPUT);
pinMode(speedPinLeft, OUTPUT);
stop_Stop();
Serial.begin(9600);//In order to fit the Bluetooth module's default baud rate, only 9600
BLTSerial.begin(9600);
}
int lastButtonState=0;
void loop()
{
do_Uart_Tick();
}
car_status get_status( char buffUART[])
{
car_status cstatus;
int index=2;
if (buffUART[index]=='-'){
cstatus.angle=-buffUART[index+1]+'0';
index=index+3;
} else {
cstatus.angle=buffUART[index]-'0';
index=index+2;
}
int currentvalue;
int spd=0;
while (buffUART[index]!=',')
{
currentvalue=buffUART[index]-'0';
spd=spd*10+currentvalue;
index++;
}
cstatus.speed=spd;
index++;
cstatus.direct=buffUART[index]-'0';
return cstatus;
}
- Now, select the board and port. After, click the upload button.



- Afterward, remove the USB cable and connect the Bluetooth module to the shield. Then, power on your robot car and download and install the OSOYOO Imitation Driving app from the Play Store or App Store.
- Then, open the app and connect it to the robot car. If you need more details, please visit the OSOYOO Robot Car Kit guide.







Remote Control with Wi-Fi APP
- Remove the Bluetooth module. Then remove the jumper caps and change to connect ESP URAT E-TX to D4 and E-RX to D5.


- Now, connect the robot car to the computer. Then, copy and paste the following code into the Arduino IDE. Also, make sure to include the WIFIESP Master library before uploading the code.
- WIFIESP Master library — Download
- Program — Download
/* ___ ___ ___ _ _ ___ ___ ____ ___ ____
* / _ \ /___)/ _ \| | | |/ _ \ / _ \ / ___) _ \| \
*| |_| |___ | |_| | |_| | |_| | |_| ( (__| |_| | | | |
* \___/(___/ \___/ \__ |\___/ \___(_)____)___/|_|_|_|
* (____/
* Osoyoo Wifi Arduino 2WD Robot Car project
* USe WI-FI UDP protocol to control robot car
* tutorial url: https://osoyoo.com/?p=57088
*/
/*Declare L298N Dual H-Bridge Motor Controller directly since there is not a library to load.*/
#include <WiFiEspUdp.h>
WiFiEspUDP Udp;
unsigned int localPort = 8888; // local port to listen on
//Define L298N Dual H-Bridge Motor Controller Pins
#define RightDirectPin1 12 //Right Motor direction pin 1 to MODEL-X IN1 grey
#define RightDirectPin2 11 //Right Motor direction pin 1 to MODEL-X IN2 yellow
#define speedPinL 6 //Left PWM pin connect MODEL-X ENB brown
#define LeftDirectPin1 7 //Left Motor direction pin 1 to MODEL-X IN3 green
#define LeftDirectPin2 8 //Left Motor direction pin 1 to MODEL-X IN4 white
#define speedPinR 9 // RIGHT PWM pin connect MODEL-X ENA blue
#define SOFT_RX 4 // Softserial RX port
#define SOFT_TX 5 //Softserial TX port
#define FAST_SPEED 180
#define MID_SPEED 130
#define SPEED 120 //avoidance motor speed
#define SPEED_LEFT 125
#define SPEED_RIGHT 125
#define BACK_SPEED 120 //back speed
const int turntime = 300; //Time the robot spends turning (miliseconds)
const int backtime = 300; //Time the robot spends turning (miliseconds)
#define MAX_PACKETSIZE 32 //Serial receive buffer
char buffUART[MAX_PACKETSIZE];
unsigned int buffUARTIndex = 0;
unsigned long preUARTTick = 0;
enum DS
{
MANUAL_DRIVE,
AUTO_DRIVE_LF, //line follow
AUTO_DRIVE_UO //ultrasonic obstruction
}Drive_Status=MANUAL_DRIVE;
enum DN
{
GO_ADVANCE,
GO_LEFT,
GO_RIGHT,
GO_BACK,
STOP_STOP,
DEF
}Drive_Num=DEF;
String WorkMode="?";
#define DEBUG true
#include "WiFiEsp.h"
// Emulate Serial1 on pins 9/10 by default
// If you want to use Hard Serial1 in Mega2560 , please remove the wifi shield jumper cap on ESP8266 RX/TX PIN , CONNECT TX->D18 RX->D19
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(SOFT_RX, SOFT_TX); // RX, TX
#endif
char ssid[] = "**********"; // replace *** with your router wifi SSID (name)
char pass[] = "**********"; // replace *** with your router wifi password
char packetBuffer[5];
int status = WL_IDLE_STATUS; // the Wifi radio's status
int connectionId;
// use a ring buffer to increase speed and reduce memory allocation
RingBuffer buf(8);
void go_Advance(void) //Forward
{
digitalWrite(RightDirectPin1, HIGH);
digitalWrite(RightDirectPin2,LOW);
digitalWrite(LeftDirectPin1,HIGH);
digitalWrite(LeftDirectPin2,LOW);
set_Motorspeed(SPEED,SPEED);
}
void go_Left() //Turn left
{
digitalWrite(RightDirectPin1, HIGH);
digitalWrite(RightDirectPin2,LOW);
digitalWrite(LeftDirectPin1,LOW);
digitalWrite(LeftDirectPin2,HIGH);
set_Motorspeed(0,SPEED_RIGHT);
}
void go_Right() //Turn right
{
digitalWrite(RightDirectPin1, LOW);
digitalWrite(RightDirectPin2,HIGH);
digitalWrite(LeftDirectPin1,HIGH);
digitalWrite(LeftDirectPin2,LOW);
set_Motorspeed(SPEED_LEFT,0);
}
void go_Back() //Reverse
{
digitalWrite(RightDirectPin1, LOW);
digitalWrite(RightDirectPin2,HIGH);
digitalWrite(LeftDirectPin1,LOW);
digitalWrite(LeftDirectPin2,HIGH);
set_Motorspeed(BACK_SPEED,BACK_SPEED);
}
void stop_Stop() //Stop
{
digitalWrite(RightDirectPin1, LOW);
digitalWrite(RightDirectPin2,LOW);
digitalWrite(LeftDirectPin1,LOW);
digitalWrite(LeftDirectPin2,LOW);
set_Motorspeed(0,0);
}
void set_Motorspeed(int SPEED_L,int SPEED_R)
{
analogWrite(speedPinL,SPEED_L);
analogWrite(speedPinR,SPEED_R);
}
//car motor control
void do_Drive_Tick()
{
if(Drive_Status == MANUAL_DRIVE)
{
switch (Drive_Num)
{
case GO_ADVANCE:
go_Advance();
Serial.println("go ahead");
// delay(AHEAD_TIME);
break;
case GO_LEFT:
go_Left();
// delay(LEFT_TURN_TIME);
Serial.println("TURN left");
break;
case GO_RIGHT:
go_Right();
// delay(LEFT_TURN_TIME);
Serial.println("TURN right");
break;
case GO_BACK:
go_Back();
// delay(BACK_TIME);
Serial.println("GO back");
break;
case STOP_STOP:
stop_Stop();
// JogTime = 0;
Serial.println("STOP");
break;
default:
break;
}
}
else if(Drive_Status==AUTO_DRIVE_LF)
{
//Serial.println("auto track");
// auto_tracking();
}
}
void setup()
{
pinMode(RightDirectPin1, OUTPUT);
pinMode(RightDirectPin2, OUTPUT);
pinMode(speedPinL, OUTPUT);
pinMode(LeftDirectPin1, OUTPUT);
pinMode(LeftDirectPin2, OUTPUT);
pinMode(speedPinR, OUTPUT);
stop_Stop();//stop move
Serial.begin(9600); // initialize serial for debugging
Serial1.begin(115200); // initialize serial for ESP module
Serial1.print("AT+CIOBAUD=9600\r\n");
Serial1.write("AT+RST\r\n");
Serial1.begin(9600); // initialize serial for ESP module
WiFi.init(&Serial1); // initialize ESP module
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
while (true); // don't continue
}
// Serial.print("Attempting to start AP ");
// Serial.println(ssid);
//AP mode
//status = WiFi.beginAP(ssid, 10, "", 0);
//STA mode
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network
status = WiFi.begin(ssid, pass);
}
Serial.println("You're connected to the network");
Serial.println();
printWifiStatus();
Udp.begin(localPort);
Serial.print("Listening on port ");
Serial.println(localPort);
}
boolean flag=false;
void loop()
{
int packetSize = Udp.parsePacket();
if (packetSize) { // if you get a client,
Serial.print("Received packet of size ");
Serial.println(packetSize);
int len = Udp.read(packetBuffer, 255);
if (len > 0) {
packetBuffer[len] = 0;
}
char c=packetBuffer[0];
switch (c) //serial control instructions
{
case 'A':Drive_Status=MANUAL_DRIVE; Drive_Num=GO_ADVANCE; WorkMode="GO_ADVANCE";break;
case 'L':Drive_Status=MANUAL_DRIVE; Drive_Num=GO_LEFT; WorkMode="GO_LEFT";break;
case 'R':Drive_Status=MANUAL_DRIVE; Drive_Num=GO_RIGHT;WorkMode="GO_RIGHT";break;
case 'B':Drive_Status=MANUAL_DRIVE; Drive_Num=GO_BACK;WorkMode="GO_BACK";break;
case 'E':Drive_Status=MANUAL_DRIVE; Drive_Num=STOP_STOP;WorkMode="STOP_STOP";break;
case 'T':Drive_Status=AUTO_DRIVE_LF;WorkMode="line follow";break;
default:break;
} //END OF ACTION SWITCH
}
do_Drive_Tick();
} //end of loop
void printWifiStatus()
{
// print your WiFi shield's IP address
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print where to go in the browser
Serial.println();
Serial.print("To see this page in action, connect to ");
Serial.print(ssid);
Serial.print(" and open a browser to http://");
Serial.println(ip);
Serial.println();
}


- Now, enter your WiFi SSID and password in the code.

- Finally, select the correct board and port, then click the upload button to upload the code. Afterward, open the Serial Monitor. Now, you will see the Local IP address. Please write it down for the next step.





- Now, download and install the OSOYOO IoT UDP Robot Car app from the Play Store or App Store. Then, open the app and click the settings button. After that, enter your local IP address.
- Okay! Now you can control the robot car using the WiFi remote.





OK, enjoy your robot! The full video guide is below. We hope to see you in the next project. Have a great day!
Make Your Own Arduino Powered 4-in-1 Robot Car | Step by Step instructions