Previous                                                                    Tutorial                                                                   Tutorial

NOTE: ALL OSOYOO PRODUCTS FOR ARDUINO ARE THIRD PARTY BOARD WHICH IS FULLY COMPATIBLE WITH ARDUINO

Contents

Authorized Online Retailers

Shopify                                      Amazon

Objective

In this lesson, we will use 2WD Robot Car Starter Kit to make a simple remote controlled car. Once the car installation is completed, we will use a Infrared Remote to control the car movements including go forward, go backward, stop, turn left and turn right.

top

Hardware Installation

1) Install the 2WD Robot Car

Install the robot car basic framework as per Lesson 1. If you have already completed the installation in Lesson 1, just keep it, no need to remove any module installed.

More information about this module please refer to:

Lesson – IR Remote Receiver Module and Controller

Connect OSOYOO UART Wi-Fi Shield with IR Receiver:

Software Installation

Notice: Shut off your battery or unplug your power adapter when upload sketch code to Arduino.

Step 1: Install latest IDE (If you have IDE version after 1.1.16, please skip this step)
Download IDE from https://www.arduino.cc/en/Main/Software?setlang=en, then install the software.

Step 2: Install IRremote library into IDE (If you have already installed IRremote library, please skip this step)
Download IRremote library from https://downloads.arduino.cc/libraries/github.com/z3t0/IRremote-4.3.1.zip, then import the library into IDE(Open IDE-> click Sketch→Include Library→Add .Zip Library)

In the pop-up window, go to your downloads’ folder, select the IRremote.zip file you downloaded just now.

NOTE!!

  1. If you have a late version of Arduino IDE with a library IRRobotRemote, it may conflict and you may have to remove that library.Make sure to delete Arduino(root)/libraries/RobotIRremote. Where Arduino(root) refers to the install directory of Arduino. The library RobotIRremote has similar definitions to IRremote and causes errors.
  2. Learn how to install an Arduino library on Arduino IDE, please visit https://osoyoo.com/2017/05/08/how-to-install-additional-arduino-libraries/

Step 3: Download https://osoyoo.com/driver/2wd/lesson2.zip and unzip it, you will see a folder called lesson2.

Step 4: Connect UNO R3 board to PC with USB cable, open Arduino IDE, choose corresponding board and port for your project

Step 5: Arduino IDE:  Click file → click Open → choose code “lesson2.ino” in lesson2 folder, load the code into Arduino, upload the sketch to the board.

top

Code Explanation

The following   lines of code expresses the IR code of ∧∨ in the IR controller, each IR code of the button can get from the IR sample code:

#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

Decodes the infrared signal and compares the received infrared signal with a predefined value, if it is equal, assigns the corresponding value to Drive_Num

void do_IR_Tick()
{
  if(IR.decode(&IRresults))
  {
    if(IRresults.value==IR_ADVANCE)
    {
      Drive_Num=GO_ADVANCE;
    }
    else if(IRresults.value==IR_RIGHT)
    {
       Drive_Num=GO_RIGHT;
    }
    else if(IRresults.value==IR_LEFT)
    {
       Drive_Num=GO_LEFT;
    }
    else if(IRresults.value==IR_BACK)
    {
        Drive_Num=GO_BACK;
    }
    IRresults.value = 0;
    IR.resume();
  }
}

This piece of code is the core of the infrared control code, press the corresponding key, the car perform the corresponding action, if the pressed key  is not defined in the program, the car will not respond.

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();
      }
    }

top

Testing

Disconnect OSOYOO UNO R3 board from PC, put 2 fully-charged 18650 battery into battery box (check the box instruction and make sure polar direction is correct). Open the power switch in the battery box.

Press IR controller keys to control the car movements:

▲: Forward

▼: Backward

◄: Turn left

►: Turn right

Troubleshooting:

If the car can’t move, please check the following:

  1. If the battery can work;
  2. If the IR controller is too far away from the receiver;
  3. If the connection is right

If above 3 points are all checked and the IR control still doesn’t work. The reason might be the IR remote sends a different button code which does not match our sample code. In order to solve this problem. Please take following steps:
Step A) Get the IR code of each button in your IR remote from following link.
https://osoyoo.com/driver/2wd/IR_print_commands_v4.zip

unzip the file upload above sketch into your board and open the serial monitor in your upper-right corner.

Press the ◄ ► ▲ ▼ “OK” button in your remote, you will see their IR code as following picture:

Write down the IR code of your control buttons ◄ ► ▲ ▼ “OK” button,

STEP B)replace the IR code in lesson 2 sketch file:
Open your Lesson 2 code again, then you will see following lines define the IR CODE of each button:
#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
Please change the value of each button in above lines to match the code from Step A). If you don’t know how to change, just Send Email to us and give us the code of each button from Step A), I can help you to change the code and email new sketch file to you.
Above method can also allow you to use other IR sending device (i.e TV remote, DVD remote, air conditioner remote etc) to control the car. Just use Step A) to get the key code of your remote and change the sketch file in Step B), it will work.
Note:
1) Please try to align the transmitter port of the IR remote control with the IR receiver head as the picture in the attachment.
2) Please press remote control gently and at regular intervals

top

Parts and Devices:

No. Picture Device Qty. Data sheet Buy in Link
1 OSOYOO basic board 1 click here Click here to buy
2 OSOYOO UART Wi-Fi shield V1.3 1 click here Click here to buy
3 OSOYOO model X motor driver module 1 click here Click here to buy
4 IR receiver 1 click here Click here to buy
5 IR remote controller 1 click here Click here to buy
6 Tracking sensor module 2 click here Click here to buy
7 HC module 1 click here Click here to buy
8 Gear motor 2 Click here to buy
9 Metal Motor Holders with screws 2 Click here to buy
10 Wheel 2 Click here to buy
11 Universal Wheel 1
12 6pin male to 6pin female jumper wire 1 Click here to buy
13 OSOYOO 2pin PnP cable 20 cm 1 Click here to buy
14 20Pin jumper wire Male to female 15 cm 1 Click here to buy
15 Philips screwdriver 1 Click here to buy
16 Hex Screwdriver 1 Click here to buy
17 18650 battery box 1 Click here to buy
18 Battery charger for 18650 battery 1 Click here to buy
19 1Pair 18650 batteries 1 Click here to buy
20 M3*5 Hex Screws 10
21 M3*10 screw 4
22 M3 nut 4
23 Copper pillar 4
24 Screw for wheel 2
25 M3 plastic screw 15
26 M3 plastic pillar 15
27 M3 plastic nut 15

top

Previous                                                                    Tutorial                                                                   Tutorial